1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | exports.getSanitizedEnv = exports.testOnlySymbol = void 0;
|
4 | var errors_1 = require("./errors");
|
5 | var reporter_1 = require("./reporter");
|
6 | exports.testOnlySymbol = Symbol('envalid - test only');
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | function validateVar(_a) {
|
14 | var spec = _a.spec, name = _a.name, rawValue = _a.rawValue;
|
15 | if (typeof spec._parse !== 'function') {
|
16 | throw new errors_1.EnvError("Invalid spec for \"".concat(name, "\""));
|
17 | }
|
18 | var value = spec._parse(rawValue);
|
19 | if (spec.choices) {
|
20 | if (!Array.isArray(spec.choices)) {
|
21 | throw new TypeError("\"choices\" must be an array (in spec for \"".concat(name, "\")"));
|
22 | }
|
23 | else if (!spec.choices.includes(value)) {
|
24 | throw new errors_1.EnvError("Value \"".concat(value, "\" not in choices [").concat(spec.choices, "]"));
|
25 | }
|
26 | }
|
27 | if (value == null)
|
28 | throw new errors_1.EnvError("Invalid value for env var \"".concat(name, "\""));
|
29 | return value;
|
30 | }
|
31 |
|
32 | function formatSpecDescription(spec) {
|
33 | var egText = spec.example ? " (eg. \"".concat(spec.example, "\")") : '';
|
34 | var docsText = spec.docs ? ". See ".concat(spec.docs) : '';
|
35 | return "".concat(spec.desc).concat(egText).concat(docsText);
|
36 | }
|
37 | var readRawEnvValue = function (env, k) {
|
38 | return env[k];
|
39 | };
|
40 | var isTestOnlySymbol = function (value) { return value === exports.testOnlySymbol; };
|
41 |
|
42 |
|
43 |
|
44 | function getSanitizedEnv(environment, specs, options) {
|
45 | if (options === void 0) { options = {}; }
|
46 | var cleanedEnv = {};
|
47 | var castedSpecs = specs;
|
48 | var errors = {};
|
49 | var varKeys = Object.keys(castedSpecs);
|
50 | var rawNodeEnv = readRawEnvValue(environment, 'NODE_ENV');
|
51 | for (var _i = 0, varKeys_1 = varKeys; _i < varKeys_1.length; _i++) {
|
52 | var k = varKeys_1[_i];
|
53 | var spec = castedSpecs[k];
|
54 | var rawValue = readRawEnvValue(environment, k);
|
55 |
|
56 |
|
57 | if (rawValue === undefined) {
|
58 |
|
59 | var usingDevDefault = rawNodeEnv && rawNodeEnv !== 'production' && spec.hasOwnProperty('devDefault');
|
60 | if (usingDevDefault) {
|
61 | cleanedEnv[k] = spec.devDefault;
|
62 | if (isTestOnlySymbol(spec.devDefault) && rawNodeEnv != 'test') {
|
63 | throw new errors_1.EnvMissingError(formatSpecDescription(spec));
|
64 | }
|
65 | continue;
|
66 | }
|
67 | if ('default' in spec) {
|
68 | cleanedEnv[k] = spec.default;
|
69 | continue;
|
70 | }
|
71 | }
|
72 | try {
|
73 | if (rawValue === undefined) {
|
74 | cleanedEnv[k] = undefined;
|
75 | throw new errors_1.EnvMissingError(formatSpecDescription(spec));
|
76 | }
|
77 | else {
|
78 | cleanedEnv[k] = validateVar({ name: k, spec: spec, rawValue: rawValue });
|
79 | }
|
80 | }
|
81 | catch (err) {
|
82 | if ((options === null || options === void 0 ? void 0 : options.reporter) === null)
|
83 | throw err;
|
84 | if (err instanceof Error)
|
85 | errors[k] = err;
|
86 | }
|
87 | }
|
88 | var reporter = (options === null || options === void 0 ? void 0 : options.reporter) || reporter_1.defaultReporter;
|
89 | reporter({ errors: errors, env: cleanedEnv });
|
90 | return cleanedEnv;
|
91 | }
|
92 | exports.getSanitizedEnv = getSanitizedEnv;
|
93 |
|
\ | No newline at end of file |