summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJoe Carstairs <65492573+Sycamost@users.noreply.github.com>2023-08-12 13:07:26 +0100
committerJoe Carstairs <65492573+Sycamost@users.noreply.github.com>2023-08-12 13:47:45 +0100
commit4c05aafd2de0bf6c4ae5e331149f8d88348d68bc (patch)
treef39a6093f58841329115a3be25c249270226db6e /src
parent841e02c14234430e70a967e042ed23a5f00ca04e (diff)
Adds client ID and environment env vars to frontend
Diffstat (limited to 'src')
-rw-r--r--src/lib/env.ts43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/lib/env.ts b/src/lib/env.ts
new file mode 100644
index 0000000..20f18f5
--- /dev/null
+++ b/src/lib/env.ts
@@ -0,0 +1,43 @@
+import dotenv from 'dotenv';
+
+// Parsing the env file.
+dotenv.config({ path: '../../.env' });
+
+type Env = {
+ PAYPAL_LIVE_CLIENT_ID: string,
+ PAYPAL_SANDBOX_CLIENT_ID: string,
+ ENVIRONMENT: 'dev' | 'prod',
+};
+
+type DirtyEnv = { [key in keyof Env]?: string };
+
+const ENV: { [key in keyof Env]: number } = {
+ PAYPAL_LIVE_CLIENT_ID: 1,
+ PAYPAL_SANDBOX_CLIENT_ID: 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