-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
245 lines (238 loc) · 10.4 KB
/
index.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
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
/**
* A namespace containing a set of convenience functions that can be used for retrieving
* environment variables and casting them into specific types.
* @example
* ```ts
* import { EnvVar } from "@bb301/env-var-parser";
* import express from "express";
* import morgan from "morgan";
*
* const PORT: number = EnvVar.asInteger("SERVER_PORT");
* const HOSTNAME: string = EnvVar.asOptionalString("SERVER_HOSTNAME") ?? "127.0.0.1";
* const LOGGING_ENABLED: boolean = EnvVar.asBoolean("SERVER_LOGGING_ENABLED");
*
* const app = express();
*
* if (LOGGING_ENABLED) {
* app.use(morgan("common"));
* }
*
* app.get("/", (_, res) => {
* res.send("Hello, Simple Node.js Environment Variable Parser!");
* });
*
* app.listen(PORT, HOSTNAME, () => {
* console.log(`Express server listening at 'http://${HOSTNAME}:${PORT}'...`);
* });
* ```
*/
export namespace EnvVar {
/**
* A function that tries to retrieve an environment variable from the {@link process.env}
* object and, if it succeeds, returns it as a `string` value.
*
* @throws An {@link Error} instance explaining that the variable with given name could
* not be found, or that it was found but it was empty.
*
* @param {string} name The name of the environment variable to look for in {@link process.env}.
*
* @returns {string} A `string` corresponding to the environment variable's value.
*
* @see {@link asOptionalString}
*/
export const asString = (name: string): string => {
if (process.env[name] === undefined) {
throw new Error(`missing environment variable '${name}'`);
}
if (process.env[name] === "") {
throw new Error(`empty environment variable '${name}'`);
}
return process.env[name]!;
}
/**
* A function that tries to retrieve an environment variable from the {@link process.env}
* object and, if it succeeds, returns it as a `string` value, else returns `undefined`.
*
* @param {string} name The name of the environment variable to look for in {@link process.env}.
*
* @returns {string|undefined} A `string` corresponding to the environment variable's value, if any,
* else `undefined`.
*
* @see {@link asString}
*/
export const asOptionalString = (name: string): string | undefined => {
try {
return asString(name);
} catch (_) {
return undefined;
}
}
/**
* A function that tries to retrieve an environment variable from the {@link process.env}
* object and, if it succeeds, validates that it is either "true" or "false", and, if so,
* returns it as a `boolean` value.
*
* @throws An {@link Error} instance explaining that the variable was either not found or that
* it could not be parsed into a valid boolean.
*
* @param {string} name The name of the environment variable to look for in {@link process.env}.
*
* @returns {boolean} A `boolean` corresponding to the environment variable's value.
*
* @see {@link asOptionalBoolean}
*
* @note This function's input is case insensitive, which means that variations of `true` such
* as `True` and `TRUE`, to quote two examples, will also be treated as `true`. Moreover, `T` or
* `t` will be treated as `true`, and, similarly, `F` and `f` will be treated as `false`. Finally,
* `0` will be treated as `false` and `1` as `true`.
*/
export const asBoolean = (name: string): boolean => {
const envVar = asString(name);
const envVarLowercased = envVar.toLowerCase();
if (["true", "t", "1"].indexOf(envVarLowercased) !== -1) {
return true;
}
if (["false", "f", "0"].indexOf(envVarLowercased) !== -1) {
return false;
}
throw new Error(`expecting environment variable '${name}' to be either 'true' (or 't' or '1') or 'false' (or 'f' or '0'), but got '${envVar}' (note: input is not case sensitive)`);
}
/**
* A function that tries to retrieve an environment variable from the {@link process.env}
* object and, if it succeeds, validates that it is either "true" or "false", and, if so,
* returns it as a `boolean` value. If no variable is found, this function returns `undefined`.
*
* @throws An {@link Error} instance explaining that the variable was found but that it could
* not be parsed into a valid boolean.
*
* @param {string} name The name of the environment variable to look for in {@link process.env}.
*
* @returns {boolean|undefined} A `boolean` corresponding to the environment variable's value, if any, else
* `undefined`.
*
* @see {@link asBoolean}
*
* @note This function's input is case insensitive, which means that variations of `true` such
* as `True` and `TRUE`, to quote two examples, will also be treated as `true`. Moreover, `T` or
* `t` will be treated as `true`, and, similarly, `F` and `f` will be treated as `false`. Finally,
* `0` will be treated as `false` and `1` as `true`.
*/
export const asOptionalBoolean = (name: string): boolean | undefined => {
try {
asString(name);
} catch (_) {
return undefined;
}
return asBoolean(name);
}
/**
* A function that tries to retrieve an environment variable from the {@link process.env}
* object and, if it succeeds, validates that it can be parsed as an `integer`, and, if
* so, returns it as a `number` value.
*
* @throws An {@link Error} instance explaining that the variable was either not found or that
* it could not be parsed into a valid integer.
*
* @param {string} name The name of the environment variable to look for in {@link process.env}.
*
* @returns {number} An `integer` corresponding to the parsed variable's value.
*
* @see {@link asOptionalInteger}
*
* @note Internally, this function uses {@link Number.parseInt} to try to convert the string
* into an integer. That means, for example, that a value like "123_test" will yield "123"
* instead of failing. In other words, if {@link Number.parseInt} returns something other than
* `NaN`, this function won't throw.
*/
export const asInteger = (name: string): number => {
const envVar = asString(name);
const value = Number.parseInt(envVar);
if (Number.isNaN(value)) {
throw new Error(`expecting environment variable '${name} = ${envVar}' to be parsable as an integer`);
}
return value;
}
/**
* A function that tries to retrieve an environment variable from the {@link process.env}
* object and, if it succeeds, validates that it can be parsed as an `integer`, and, if
* so, returns it as a `number` value. If no variable is found, this function returns `undefined`.
*
* @throws An {@link Error} instance explaining that the variable was present, but that it could
* not be parsed into a valid integer.
*
* @param {string} name The name of the environment variable to look for in {@link process.env}.
*
* @returns {number|undefined} An `integer` corresponding to the parsed variable's value, if any,
* else `undefined`
*
* @see {@link asInteger}
*
* @note Internally, this function uses {@link Number.parseInt} to try to convert the string
* into an integer. That means, for example, that a value like "123_test" will yield "123"
* instead of failing. In other words, if {@link Number.parseInt} returns something other than
* `NaN`, this function won't throw.
*/
export const asOptionalInteger = (name: string): number | undefined => {
try {
asString(name);
} catch (_) {
return undefined;
}
return asInteger(name);
}
/**
* A function that tries to retrieve an environment variable from the {@link process.env}
* object and, if it succeeds, validates that it can be parsed as a `float` value, and, if
* so, returns it as a `number` value.
*
* @throws An {@link Error} instance explaining that the variable was either not found or that
* it could not be parsed into a valid float.
*
* @param {string} name The name of the environment variable to look for in {@link process.env}.
*
* @returns {number} A `float` corresponding to the parsed variable's value.
*
* @see {@link asOptionalFloat}
*
* @note Internally, this function uses {@link Number.parseFloat} to try to convert the string
* into a number. That means, for example, that a value like "123.5_test" will yield "123.5"
* instead of failing. In other words, if {@link Number.parseFloat} returns something other than
* `NaN`, this function won't throw.
*/
export const asFloat = (name: string): number => {
const envVar = asString(name);
const value = Number.parseFloat(envVar);
if (Number.isNaN(value)) {
throw new Error(`expecting environment variable '${name} = ${envVar}' to be parsable as a float`);
}
return value;
}
/**
* A function that tries to retrieve an environment variable from the {@link process.env}
* object and, if it succeeds, validates that it can be parsed as a `float` value, and, if
* so, returns it as a `number` value. If no variable is found, this function returns `undefined`.
*
* @throws An {@link Error} instance explaining that the variable was present, but that it could
* not be parsed into a valid float.
*
* @param {string} name The name of the environment variable to look for in {@link process.env}.
*
* @returns {number|undefined} A `float` corresponding to the parsed variable's value, if any, else
* `undefined`.
*
* @see {@link asFloat}
*
* @note Internally, this function uses {@link Number.parseFloat} to try to convert the string
* into a number. That means, for example, that a value like "123.5_test" will yield "123.5"
* instead of failing. In other words, if {@link Number.parseFloat} returns something other than
* `NaN`, this function won't throw.
*/
export const asOptionalFloat = (name: string): number | undefined => {
try {
asString(name);
} catch (_) {
return undefined;
}
return asFloat(name);
}
}