forked from recursivefunk/good-env
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
271 lines (242 loc) · 7.25 KB
/
index.js
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
const is = require('is_js')
const ok = is.existy
module.exports = Object
.create({
/**
* @description Fetches the env var with the given key. If no env var
* with the specified key exists, the default value is returned if it is
* provided else it returns undefined
*
* @param {(string|string[])} keyObj - A unique key for an item or a list of possible keys
* @param {(string|number)} defaultVal - The default value of an item if it doesn't
* already exist
*
*/
get (keyObj, defaultVal) {
let keys
let value
if (is.string(keyObj)) {
keys = [keyObj]
} else if (is.array(keyObj)) {
keys = keyObj.map(k => k.trim())
} else {
throw Error(`Invalid key(s) ${keyObj}`)
}
keys.some(key => {
if (ok(process.env[key])) {
value = process.env[key]
return true
}
})
if (!ok(value) && typeof ok(defaultVal)) {
value = defaultVal
}
value = (is.string(value)) ? value.trim() : value
return value
},
/**
* @description Gets all items specified in the object. If the item is an
* array, the function will perform a standard get with no defaults. If the
* item is an object {}, the function will use the values as defaults -
* null values will be treated as no default specified
*
* @param {string[]} items - An array of keys
*
*/
getAll (items) {
const objReducer = (obj, getter) => (
Object.keys(obj).reduce((prev, next, index) => {
prev[next] = getter(next, obj[next])
return prev
}, {})
)
const arrReducer = (keys, getter) => {
const arr = items.map(key => getter(key))
return arr.reduce((prev, next, index) => {
prev[keys[index]] = arr[index]
return prev
}, {})
}
if (is.array(items)) {
return arrReducer(items, this.get)
} else if (is.json(items)) {
return objReducer(items, this.get)
} else {
throw Error(`Invalid arg ${items}`)
}
},
/**
* @description Determines whether or not all of the values given key is
* truthy
*
* @param {(string|string[])} keys - A unique key or array of keys
*
*/
ok: (...keys) => keys.every(key => ok(process.env[key])),
/**
* @description This method ensures 1 to many environment variables either
* exist, or exist and are of a designated type
*
* @example
* ensure(
* // Will ensure 'HOSTNAME' exists
* 'HOSTNAME',
* // Will ensure 'PORT' both exists and is a number
* { 'PORT': { type: 'number' }},
* // Will ensure 'INTERVAL' exists, it's a number and its value is greater
* // than or equal to 1000
* { 'INTERVAL': { type: 'number', ok: s => s >= 1000 }}
* // ...
* )
*
*/
ensure (...items) {
const self = this
const getKit = item => {
switch (item) {
case 'string':
console.log('triggered string')
return { validator: is.string, getter: self.get.bind(self) }
case 'number':
console.log('triggered number')
return { validator: is.number, getter: self.getNumber.bind(self) }
case 'boolean':
console.log('triggered boolean')
return { validator: is.boolean, getter: self.getBool.bind(self) }
default:
console.log(`i thould throw\n`)
throw Error(`Invalid type "${item}"`)
}
}
return items.every(item => {
if (is.string(item)) {
if (this.ok(item)) {
return true
} else {
throw Error(`No environment configuration for var "${item}"`)
}
} else if (is.json(item)) {
const key = Object.keys(item)[0]
const type = item[key].type
const validator = item[key].ok
if (type && !validType(type)) {
throw Error(`Invalid expected type "${type}"`)
} else {
const kit = getKit(type)
const val = kit.getter(key)
const result = kit.validator(val)
if (!result) {
throw Error(`Unexpected result for key="${key}". It may not exist or may not be a valid "${type}"`)
}
if (validator && is.function(validator)) {
const valid = validator(val)
if (!valid) {
throw Error(`Value ${val} did not pass validator function for key "${key}"`)
}
}
return true
}
} else {
throw Error(`Invalid key ${item}`)
}
})
},
/**
* @description Fetches the value at the given key and attempts to coerce
* it into a boolean
*
* @param {string} key - A unique key
* @param {boolean} defaultVal - The default value
*
*/
getBool (key, defaultVal) {
let value
value = process.env[key]
if (ok(value)) {
let ret
value = value.toLowerCase().trim()
if (value === 'true') {
ret = true
} else if (value === 'false') {
ret = false
}
return ret
} else if (defaultVal === true || defaultVal === false) {
return defaultVal
}
return false
},
/**
* @description An alias function for getBool()
*
* @param {string} key - A unique key
* @param {boolean} defaultVal - The default value if none exists
*
*/
bool (key, defaultVal) {
return this.getBool(key, defaultVal)
},
/**
* @description Fetches the value at the given key and attempts to
* coherse it into an integer
*
* @param {string} key - A unique key
* @param {number} defaultVal - The default value
*
*/
getNumber (key, defaultVal) {
let value
let intVal
let valIsInt
value = this.get(key, defaultVal)
intVal = parseInt(value, 10)
valIsInt = is.integer(intVal)
if (value === defaultVal) {
return value
} else if (valIsInt) {
return intVal
}
},
/**
* @description An alias function for getNumber()
*
*/
num (key, defaultVal) {
return this.getNumber(key, defaultVal)
},
/**
* @description Fetches the value at the given key and attempts to
* coherse it into a list of literal values
*
* @param {string} key - A unique key
* @param {object} options
*
*/
getList (key, opts = { dilim: ',', cast: null }) {
const { dilim, cast } = opts
let value
value = this.get(key, [])
if (!is.array(value)) {
let ret = value.split(dilim).map(i => i.trim())
if (cast && cast === 'number') {
ret = mapNums(ret)
}
return ret
} else {
return value
}
},
/**
* @description An alias function for getList()
*
* @param {string} key - A unique key
* @param {object} options
*
*/
list (key, opts) {
return this.getList(key, opts)
}
})
const parse = (items, converter) => items.map(t => converter(t, 10))
const mapNums = items => parse(items, parseInt)
const validType = item => ['number', 'boolean', 'string'].includes(item)