-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathenv.ts
More file actions
81 lines (77 loc) · 2.58 KB
/
Copy pathenv.ts
File metadata and controls
81 lines (77 loc) · 2.58 KB
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import { getSingleton } from './singleton';
/** Keep in sync with the test env vars in `jest_setup.ts` */
export type EnvVars = {
baseUrl: string;
pgHost: string;
pgPort: number;
pgDatabase: string;
pgUser: string;
pgPassword: string;
sentryDsn: string;
sentryEnvironment: string;
publicS3BaseUrl: string;
s3Endpoint: string;
s3Region: string;
s3AccessKeyId: string;
s3AccessKeySecret: string;
s3MapsBucket: string;
flagsImplementation: string;
flagsEdgeConfig: string;
flagsEdgeConfigKey: string;
supabaseUrl: string;
supabasePublishableKey: string;
supabaseSecretKey: string;
axiomApiToken: string;
axiomDataset: string;
axiomPublicApiToken: string;
axiomPublicDataset: string;
};
/**
* Retrieves and validates the environment variables.
*/
export function getEnvVars() {
return getSingleton('_envVars', createEnvVars);
}
function createEnvVars(): EnvVars {
const envVars: { [K in keyof EnvVars]: EnvVars[K] | undefined } = {
baseUrl: process.env.NEXT_PUBLIC_BASE_URL,
pgHost: process.env.PGHOST,
pgPort: Number(process.env.PGPORT || undefined),
pgDatabase: process.env.PGDATABASE,
pgUser: process.env.PGUSER,
pgPassword: process.env.PGPASSWORD,
sentryDsn: process.env.SENTRY_DSN,
sentryEnvironment: process.env.SENTRY_ENV,
publicS3BaseUrl: process.env.PUBLIC_S3_BASE_URL,
s3Endpoint: process.env.S3_ENDPOINT,
s3Region: process.env.S3_REGION,
s3AccessKeyId: process.env.S3_ACCESS_KEY_ID,
s3AccessKeySecret: process.env.S3_ACCESS_KEY_SECRET,
s3MapsBucket: process.env.S3_MAPS_BUCKET,
flagsImplementation: process.env.FLAGS_IMPLEMENTATION,
flagsEdgeConfig: process.env.FLAGS_EDGE_CONFIG,
flagsEdgeConfigKey: process.env.FLAGS_EDGE_CONFIG_KEY,
supabaseUrl: process.env.NEXT_PUBLIC_SUPABASE_URL,
supabasePublishableKey: process.env.NEXT_PUBLIC_SUPABASE_PUBLISHABLE_KEY,
supabaseSecretKey: process.env.SUPABASE_SECRET_KEY,
axiomApiToken: process.env.AXIOM_API_TOKEN,
axiomDataset: process.env.AXIOM_DATASET,
axiomPublicApiToken: process.env.NEXT_PUBLIC_AXIOM_API_TOKEN,
axiomPublicDataset: process.env.NEXT_PUBLIC_AXIOM_DATASET,
};
let fail = false;
for (const [key, value] of Object.entries(envVars)) {
if (
value == null ||
(typeof value === 'string' && value.trim() === '') ||
(typeof value === 'number' && isNaN(value))
) {
console.error(`${key} has been left blank in .env -- intentional?`);
fail = true;
}
}
if (fail) {
throw new Error('One or more environment variables were missing, see above.');
}
return envVars as EnvVars;
}