-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: replace all direct env-var reads with ENV object
- Loading branch information
1 parent
8ff0608
commit aacefe0
Showing
11 changed files
with
209 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
import { HttpLink } from "@apollo/client/link/http"; | ||
import { ENV } from "@app/env"; | ||
|
||
export const apiHttpLink = new HttpLink({ | ||
uri: `${process.env.REACT_APP_API_PROTOCOL}://${process.env.REACT_APP_API_BASE_URI}/api`, | ||
}); | ||
export const apiHttpLink = new HttpLink({ uri: ENV.API_URI }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/** | ||
* Fixit environment variables | ||
*/ | ||
export const ENV: FixitEnvVars = { | ||
IS_DEV: import.meta.env.DEV, | ||
IS_PROD: import.meta.env.PROD, | ||
SENTRY_DSN: import.meta.env.VITE_SENTRY_DSN, | ||
API_PROTOCOL: import.meta.env.VITE_API_PROTOCOL, | ||
API_HOST: import.meta.env.VITE_API_HOST, | ||
API_ORIGIN: `${import.meta.env.VITE_API_PROTOCOL}://${import.meta.env.VITE_API_HOST}`, | ||
API_URI: `${import.meta.env.VITE_API_PROTOCOL}://${import.meta.env.VITE_API_HOST}/api`, | ||
STRIPE_PUBLISHABLE_KEY: import.meta.env.VITE_STRIPE_PUBLISHABLE_KEY, | ||
PROMO_CODES: JSON.parse(import.meta.env.VITE_FIXIT_SUB_PROMO_CODES_JSON), | ||
}; | ||
|
||
/** | ||
* Fixit environment variables | ||
* | ||
* Related types: `src/types/Process.env.d.ts` | ||
*/ | ||
export type FixitEnvVars = Readonly<{ | ||
IS_DEV: boolean; | ||
IS_PROD: boolean; | ||
SENTRY_DSN: string; | ||
API_PROTOCOL: string; | ||
API_HOST: string; | ||
/** `"[API_PROTOCOL]://[API_HOST]"` */ | ||
API_ORIGIN: string; | ||
/** `"[API_PROTOCOL]://[API_HOST]/api"` */ | ||
API_URI: string; | ||
STRIPE_PUBLISHABLE_KEY: string; | ||
PROMO_CODES: Record<string, number>; | ||
}>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/* eslint-disable no-console */ | ||
import * as Sentry from "@sentry/react"; | ||
import dayjs from "dayjs"; | ||
import { ENV } from "@app/env"; | ||
import { safeJsonStringify } from "@utils/typeSafety"; | ||
|
||
/** | ||
* Returns a log-message string. | ||
* - Format: `"[<timestamp>][<label>] <messagePrefix?> <message>"` | ||
* - Timestamp format: `"YYYY:MMM:D k:mm:ss.SSS"` | ||
*/ | ||
const getLogMessage = ({ | ||
label, | ||
input, | ||
messagePrefix = "", | ||
}: GetLogMessageArgsProvidedByLoggerUtil & GetLogMessageArgsProvidedByHandler): string => { | ||
let message = `[${dayjs().format("YYYY:MMM:D @k:mm:ss.SSS")}][${label}]`; | ||
|
||
if (messagePrefix) message += ` ${messagePrefix}`; | ||
|
||
message += | ||
input instanceof Error | ||
? input.message | ||
: typeof input === "string" | ||
? input | ||
: safeJsonStringify(input); | ||
|
||
return message; | ||
}; | ||
|
||
/** | ||
* This function returns a logging function suited for the operating environment: | ||
* | ||
* - IN PRODUCTION: | ||
* - Error logs are always sent to Sentry | ||
* - Non-error logs: | ||
* - Sent to Sentry if `isEnabledInProduction` is `true` | ||
* - Ignored if `isEnabledInProduction` is `false` | ||
* | ||
* - IN NON-PRODUCTION ENVS: | ||
* - Error logs are always logged using `console.error` | ||
* | ||
* > Errors are always logged in all environments regardless of | ||
* `isEnabledInProduction` which only applies to non-error logs. | ||
*/ | ||
const getLoggerUtil = ({ | ||
label, | ||
isEnabledInProduction = false, | ||
nonProdConsoleMethod = console.log, | ||
}: GetLogMessageArgsProvidedByLoggerUtil & { | ||
/** Bool flag to enable logging non-errors in prod. */ | ||
isEnabledInProduction?: boolean; | ||
/** The `console` method to use (default: `console.log`). */ | ||
nonProdConsoleMethod?: | ||
| typeof console.log | ||
| typeof console.info | ||
| typeof console.debug | ||
| typeof console.warn | ||
| typeof console.error; | ||
}): LoggerFn => { | ||
// `handleLogMessage` and `handleLogError` are env-dependent and govern where/how logs are sent | ||
const { | ||
handleLogMessage, | ||
handleLogError, | ||
}: Record<"handleLogMessage" | "handleLogError", LoggerFn> = | ||
ENV.IS_PROD === true | ||
? { | ||
handleLogError: (input, messagePrefix) => { | ||
Sentry.captureException(input); | ||
Sentry.captureMessage(getLogMessage({ label, input, messagePrefix })); | ||
}, | ||
handleLogMessage: | ||
isEnabledInProduction === true | ||
? (input, messagePrefix) => { | ||
Sentry.captureMessage(getLogMessage({ label, input, messagePrefix })); | ||
} | ||
: () => {}, | ||
} | ||
: { | ||
handleLogError: (input, messagePrefix) => { | ||
console.error(getLogMessage({ label, input, messagePrefix }), input); | ||
}, | ||
handleLogMessage: (input, messagePrefix) => { | ||
nonProdConsoleMethod(getLogMessage({ label, input, messagePrefix })); | ||
}, | ||
}; | ||
|
||
// The returned fn simply checks if input is an Error, and calls handleLogMessage/handleLogError accordingly | ||
return (input, messagePrefix) => { | ||
if (input instanceof Error) handleLogError(input, messagePrefix); | ||
else handleLogMessage(input, messagePrefix); | ||
}; | ||
}; | ||
|
||
export const logger = { | ||
auth: getLoggerUtil({ | ||
label: "AUTH", | ||
}), | ||
stripe: getLoggerUtil({ | ||
label: "STRIPE", | ||
}), | ||
debug: getLoggerUtil({ | ||
label: "DEBUG", | ||
nonProdConsoleMethod: console.debug, | ||
}), | ||
info: getLoggerUtil({ | ||
label: "INFO", | ||
nonProdConsoleMethod: console.info, | ||
}), | ||
error: getLoggerUtil({ | ||
label: "ERROR", | ||
nonProdConsoleMethod: console.error, | ||
isEnabledInProduction: true, | ||
}), | ||
gql: getLoggerUtil({ | ||
label: "GQL", | ||
}), | ||
gqlError: getLoggerUtil({ | ||
label: "GQL-ERROR", | ||
nonProdConsoleMethod: console.error, | ||
isEnabledInProduction: true, | ||
}), | ||
}; | ||
|
||
/** Args provided to `getLogMessage` by `getLoggerUtil`. */ | ||
type GetLogMessageArgsProvidedByLoggerUtil = { | ||
/** A purpose-related label used to differentiate log sources. */ | ||
label: string; | ||
}; | ||
|
||
/** Args provided to `getLogMessage` by `LoggerFn` invocations. */ | ||
type GetLogMessageArgsProvidedByHandler = { | ||
/** The raw input provided to a logger function. */ | ||
input: unknown; | ||
/** An optional string to prefix the stringified log `input`. */ | ||
messagePrefix?: string | undefined; | ||
}; | ||
|
||
/** This type reflects the structure of the function returned by `getLoggerUtil`. */ | ||
type LoggerFn = ( | ||
input: GetLogMessageArgsProvidedByHandler["input"], | ||
messagePrefix?: GetLogMessageArgsProvidedByHandler["messagePrefix"] | ||
) => void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters