1 | 'use strict'
|
2 |
|
3 | class Config {
|
4 | constructor (values) {
|
5 | this.keyDefined = (key) => key in values
|
6 | this.getValue = (key) => values[key]
|
7 | }
|
8 |
|
9 | get (key) {
|
10 | if (this.keyDefined(key)) {
|
11 | return this.getValue(key)
|
12 | } else {
|
13 | throw new Error(`Unknown configuration: ${key}`)
|
14 | }
|
15 | }
|
16 | }
|
17 |
|
18 | class ConfigBuilder {
|
19 | constructor () {
|
20 | this.variables = {}
|
21 | }
|
22 |
|
23 | build () {
|
24 | return new Config(this.variables)
|
25 | }
|
26 |
|
27 | addValue (key, value) {
|
28 | if (value === undefined) {
|
29 | throw new Error(`Failed to add "${key}" property. The value cannot be undefined`)
|
30 | }
|
31 | if (key in this.variables) {
|
32 | throw new Error(`"${key}" already has a value defined.`)
|
33 | }
|
34 | this.variables[key] = value
|
35 | return this
|
36 | }
|
37 |
|
38 | addValueWithDefault (key, value, defaultValue) {
|
39 | if (defaultValue === undefined) {
|
40 | throw new Error(`Failed to add "${key}" property. Default value cannot be undefined`)
|
41 | }
|
42 | return this.addValue(key, value === undefined ? defaultValue : value)
|
43 | }
|
44 | }
|
45 |
|
46 | const config = new ConfigBuilder()
|
47 | .addValue('NETWORK', process.env.NETWORK || 'mainnet')
|
48 | .build()
|
49 |
|
50 | export { config }
|