-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.ts
46 lines (35 loc) · 1.13 KB
/
config.ts
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
import path from "path";
import dotenv from "dotenv";
// Parsing the env file.
dotenv.config({ path: path.resolve(__dirname, "./.env") });
// Interface to load env variables
// Note these variables can possibly be undefined
// as someone could skip these varibales or not setup a .env file at all
interface ENV {
ALCHEMY_API_KEY: string | undefined;
}
interface Config {
ALCHEMY_API_KEY: string;
}
// Loading process.env as ENV interface
const getConfig = (): ENV => {
return {
ALCHEMY_API_KEY: process.env.ALCHEMY_API_KEY,
};
};
// Throwing an Error if any field was undefined we don't
// want our app to run if it can't connect to DB and ensure
// that these fields are accessible. If all is good return
// it as Config which just removes the undefined from our type
// definition.
const getSanitzedConfig = (config: ENV): Config => {
for (const [key, value] of Object.entries(config)) {
if (value === undefined) {
throw new Error(`Missing key ${key} in config.env`);
}
}
return config as Config;
};
const config = getConfig();
const sanitizedConfig = getSanitzedConfig(config);
export default sanitizedConfig;