diff options
| author | Joe Carstairs <65492573+Sycamost@users.noreply.github.com> | 2023-08-07 19:41:20 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-08-07 19:41:20 +0100 |
| commit | 500997042e98784c9d3b7a5ad684e36bf67cd0fa (patch) | |
| tree | bfa129cae6a2b981d5b6dc5c3cc16cfb2bc5c95a /api/_env.ts | |
| parent | 2fcbccfbd49c6eaf433272749e32589a6f490200 (diff) | |
| parent | e08094210c15ee0114ae4f11142aa5f89d81c7aa (diff) | |
Merge pull request #1 from Sycamost/master
Update
Diffstat (limited to 'api/_env.ts')
| -rw-r--r-- | api/_env.ts | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/api/_env.ts b/api/_env.ts new file mode 100644 index 0000000..69ed902 --- /dev/null +++ b/api/_env.ts @@ -0,0 +1,48 @@ +import path from 'path'; +import dotenv from 'dotenv'; + +// Parsing the env file. +dotenv.config({ path: path.resolve(__dirname, '../../.env') }); + +type Env = { + PAYPAL_LIVE_CLIENT_ID: string, + PAYPAL_LIVE_SECRET: string, + PAYPAL_SANDBOX_CLIENT_ID: string, + PAYPAL_SANDBOX_SECRET: string, + ENVIRONMENT: 'dev' | 'prod', +}; + +type DirtyEnv = { [key in keyof Env]?: string }; + +const ENV: { [key in keyof Env]: number } = { + PAYPAL_LIVE_CLIENT_ID: 1, + PAYPAL_LIVE_SECRET: 1, + PAYPAL_SANDBOX_CLIENT_ID: 1, + PAYPAL_SANDBOX_SECRET: 1, + ENVIRONMENT: 1, +}; + +const { env }: { env: DirtyEnv } = process; + +const sanitisedEnvEntries = + (Object.keys(ENV) as (keyof Env)[]) + .map<[keyof Env, string]>(key => { + const value = env[key]; + if (!value) { + throw new Error(`Environment not configured correctly. ${key} was not defined.`); + } + + if (key === 'ENVIRONMENT') { + if (!['dev', 'prod'].includes(value)) { + throw new Error(` + Environment not configured correctly. ${key} must be + either 'dev' or 'prod', but '${value}' was provided. + `); + } + } + return [key, value]; + }); + +const sanitisedEnv = Object.fromEntries(sanitisedEnvEntries) as Env; + +export default sanitisedEnv;
\ No newline at end of file |
