summaryrefslogtreecommitdiff
path: root/src/lib/env.ts
blob: 8e996f2f10770f17177ecce9b71132ae21b212b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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;