-
Notifications
You must be signed in to change notification settings - Fork 4
/
config.ts
52 lines (42 loc) · 1.04 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
47
48
49
50
51
52
import * as dotenv from 'dotenv'
dotenv.config()
const envVars: string[] = [
'ETHEREUM_NODE_ID',
'XDAI_NODE_HTTP_URL',
'MAINNET_NODE_HTTP_URL',
'XDAI_NODE_WS_URL',
'MAINNET_NODE_WS_URL'
]
// type requiredOptions = {
// [key: string]: string
// }
interface requiredEnv {
//SOCKET_PORT: string
[key: string]: string | undefined
}
class Config {
env: requiredEnv
constructor (envFile: requiredEnv) {
this.env = envFile
this.validateEnv(envFile)
}
//Have this - replace it!
validateEnv (envFile: requiredEnv) {
envVars.forEach((envVar: any) => {
if (envFile[envVar]) {
this.env[envVar] = envFile[envVar]
// console.log(`envVar ---> : ${this[envVar]}`)
} else {
throw new Error(`Need to provide a ${envVar} in the .env`)
}
})
}
get (envVar: string): string | number | undefined {
if (!this.env[envVar]) {
throw new Error(`${envVar} is an invalid env variable`)
}
return this.env[envVar]
}
}
const config = new Config(process.env)
export default config