envapt 4.0.2 → 4.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +206 -0
- package/README.md +29 -3
- package/dist/index.cjs +134 -115
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +63 -39
- package/dist/index.d.ts +63 -39
- package/dist/index.mjs +134 -115
- package/dist/index.mjs.map +1 -1
- package/package.json +3 -30
package/dist/index.mjs
CHANGED
|
@@ -6,7 +6,7 @@ var __defProp = Object.defineProperty;
|
|
|
6
6
|
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
7
7
|
|
|
8
8
|
// src/Error.ts
|
|
9
|
-
var EnvaptErrorCodes = /* @__PURE__ */ ((EnvaptErrorCodes2)
|
|
9
|
+
var EnvaptErrorCodes = /* @__PURE__ */ (function(EnvaptErrorCodes2) {
|
|
10
10
|
EnvaptErrorCodes2[EnvaptErrorCodes2["InvalidFallback"] = 101] = "InvalidFallback";
|
|
11
11
|
EnvaptErrorCodes2[EnvaptErrorCodes2["InvalidFallbackType"] = 102] = "InvalidFallbackType";
|
|
12
12
|
EnvaptErrorCodes2[EnvaptErrorCodes2["ArrayFallbackElementTypeMismatch"] = 103] = "ArrayFallbackElementTypeMismatch";
|
|
@@ -19,8 +19,9 @@ var EnvaptErrorCodes = /* @__PURE__ */ ((EnvaptErrorCodes2) => {
|
|
|
19
19
|
EnvaptErrorCodes2[EnvaptErrorCodes2["MissingDelimiter"] = 301] = "MissingDelimiter";
|
|
20
20
|
EnvaptErrorCodes2[EnvaptErrorCodes2["InvalidUserDefinedConfig"] = 302] = "InvalidUserDefinedConfig";
|
|
21
21
|
EnvaptErrorCodes2[EnvaptErrorCodes2["EnvFilesNotFound"] = 303] = "EnvFilesNotFound";
|
|
22
|
+
EnvaptErrorCodes2[EnvaptErrorCodes2["InvalidKeyInput"] = 304] = "InvalidKeyInput";
|
|
22
23
|
return EnvaptErrorCodes2;
|
|
23
|
-
})(
|
|
24
|
+
})({});
|
|
24
25
|
var EnvaptError = class extends Error {
|
|
25
26
|
static {
|
|
26
27
|
__name(this, "EnvaptError");
|
|
@@ -95,19 +96,18 @@ var Validator = class {
|
|
|
95
96
|
*/
|
|
96
97
|
static isValidArrayConverterType(value) {
|
|
97
98
|
if (typeof value !== "string") return false;
|
|
98
|
-
const invalidTypes = [
|
|
99
|
+
const invalidTypes = [
|
|
100
|
+
"array",
|
|
101
|
+
"json",
|
|
102
|
+
"regexp"
|
|
103
|
+
];
|
|
99
104
|
if (invalidTypes.includes(value)) return false;
|
|
100
|
-
const validTypes = ListOfBuiltInConverters.filter(
|
|
101
|
-
(type) => !invalidTypes.includes(type)
|
|
102
|
-
);
|
|
105
|
+
const validTypes = ListOfBuiltInConverters.filter((type) => !invalidTypes.includes(type));
|
|
103
106
|
return validTypes.includes(value);
|
|
104
107
|
}
|
|
105
108
|
static customConvertor(converter) {
|
|
106
109
|
if (typeof converter !== "function") {
|
|
107
|
-
throw new EnvaptError(
|
|
108
|
-
203 /* InvalidCustomConverter */,
|
|
109
|
-
`Custom converter must be a function, got ${typeof converter}.`
|
|
110
|
-
);
|
|
110
|
+
throw new EnvaptError(EnvaptErrorCodes.InvalidCustomConverter, `Custom converter must be a function, got ${typeof converter}.`);
|
|
111
111
|
}
|
|
112
112
|
}
|
|
113
113
|
/**
|
|
@@ -115,13 +115,10 @@ var Validator = class {
|
|
|
115
115
|
*/
|
|
116
116
|
static arrayConverter(value) {
|
|
117
117
|
if (!this.isArrayConverter(value)) {
|
|
118
|
-
throw new EnvaptError(
|
|
118
|
+
throw new EnvaptError(EnvaptErrorCodes.MissingDelimiter, "Must have delimiter property");
|
|
119
119
|
}
|
|
120
120
|
if (value.type !== void 0 && !this.isValidArrayConverterType(value.type)) {
|
|
121
|
-
throw new EnvaptError(
|
|
122
|
-
201 /* InvalidArrayConverterType */,
|
|
123
|
-
`"${value.type}" is not a valid converter type`
|
|
124
|
-
);
|
|
121
|
+
throw new EnvaptError(EnvaptErrorCodes.InvalidArrayConverterType, `"${value.type}" is not a valid converter type`);
|
|
125
122
|
}
|
|
126
123
|
}
|
|
127
124
|
/**
|
|
@@ -129,13 +126,10 @@ var Validator = class {
|
|
|
129
126
|
*/
|
|
130
127
|
static builtInConverter(value) {
|
|
131
128
|
if (typeof value !== "string") {
|
|
132
|
-
throw new EnvaptError(
|
|
129
|
+
throw new EnvaptError(EnvaptErrorCodes.InvalidConverterType, `Expected string, got ${typeof value}`);
|
|
133
130
|
}
|
|
134
131
|
if (!ListOfBuiltInConverters.includes(value)) {
|
|
135
|
-
throw new EnvaptError(
|
|
136
|
-
202 /* InvalidBuiltInConverter */,
|
|
137
|
-
`"${value}" is not a valid converter type. Valid types are: ${ListOfBuiltInConverters.join(",")}`
|
|
138
|
-
);
|
|
132
|
+
throw new EnvaptError(EnvaptErrorCodes.InvalidBuiltInConverter, `"${value}" is not a valid converter type. Valid types are: ${ListOfBuiltInConverters.join(",")}`);
|
|
139
133
|
}
|
|
140
134
|
}
|
|
141
135
|
/**
|
|
@@ -144,10 +138,7 @@ var Validator = class {
|
|
|
144
138
|
static validateBuiltInConverterFallback(converter, fallback) {
|
|
145
139
|
const typeChecker = BuiltInConverterTypeCheckers[converter];
|
|
146
140
|
if (!typeChecker(fallback)) {
|
|
147
|
-
throw new EnvaptError(
|
|
148
|
-
104 /* FallbackConverterTypeMismatch */,
|
|
149
|
-
`Fallback type does not match converter "${converter}". Expected ${converter} compatible type.`
|
|
150
|
-
);
|
|
141
|
+
throw new EnvaptError(EnvaptErrorCodes.FallbackConverterTypeMismatch, `Fallback type does not match converter "${converter}". Expected ${converter} compatible type.`);
|
|
151
142
|
}
|
|
152
143
|
}
|
|
153
144
|
/**
|
|
@@ -161,10 +152,7 @@ var Validator = class {
|
|
|
161
152
|
return typeof element !== firstElementType;
|
|
162
153
|
});
|
|
163
154
|
if (hasInconsistentTypes) {
|
|
164
|
-
throw new EnvaptError(
|
|
165
|
-
103 /* ArrayFallbackElementTypeMismatch */,
|
|
166
|
-
`All elements in array fallback must have the same type. Found mixed types.`
|
|
167
|
-
);
|
|
155
|
+
throw new EnvaptError(EnvaptErrorCodes.ArrayFallbackElementTypeMismatch, `All elements in array fallback must have the same type. Found mixed types.`);
|
|
168
156
|
}
|
|
169
157
|
}
|
|
170
158
|
/**
|
|
@@ -175,10 +163,7 @@ var Validator = class {
|
|
|
175
163
|
const firstElement = fallback[0];
|
|
176
164
|
const typeChecker = BuiltInConverterTypeCheckers[converterType];
|
|
177
165
|
if (!typeChecker(firstElement)) {
|
|
178
|
-
throw new EnvaptError(
|
|
179
|
-
103 /* ArrayFallbackElementTypeMismatch */,
|
|
180
|
-
`Array converter type "${converterType}" does not match fallback element type. Expected ${converterType} compatible elements.`
|
|
181
|
-
);
|
|
166
|
+
throw new EnvaptError(EnvaptErrorCodes.ArrayFallbackElementTypeMismatch, `Array converter type "${converterType}" does not match fallback element type. Expected ${converterType} compatible elements.`);
|
|
182
167
|
}
|
|
183
168
|
}
|
|
184
169
|
/**
|
|
@@ -216,33 +201,27 @@ var Validator = class {
|
|
|
216
201
|
if (converter === BigInt) return BigInt(fallback);
|
|
217
202
|
if (converter === Symbol) return Symbol.for(String(fallback));
|
|
218
203
|
} catch (error) {
|
|
219
|
-
throw new EnvaptError(
|
|
220
|
-
205 /* PrimitiveCoercionFailed */,
|
|
221
|
-
`Failed to coerce fallback value using ${converter.name}: ${error.message}`
|
|
222
|
-
);
|
|
204
|
+
throw new EnvaptError(EnvaptErrorCodes.PrimitiveCoercionFailed, `Failed to coerce fallback value using ${converter.name}: ${error.message}`);
|
|
223
205
|
}
|
|
224
|
-
throw new EnvaptError(
|
|
225
|
-
205 /* PrimitiveCoercionFailed */,
|
|
226
|
-
`Unknown primitive converter: ${converter.name}`
|
|
227
|
-
);
|
|
206
|
+
throw new EnvaptError(EnvaptErrorCodes.PrimitiveCoercionFailed, `Unknown primitive converter: ${converter.name}`);
|
|
228
207
|
}
|
|
229
208
|
/**
|
|
230
209
|
* Make sure the user hasn't provided prohibited options in their dotenv config
|
|
231
210
|
*/
|
|
232
211
|
static validateDotenvConfig(config2) {
|
|
233
212
|
if ("path" in config2 || "processEnv" in config2) {
|
|
234
|
-
throw new EnvaptError(
|
|
235
|
-
302 /* InvalidUserDefinedConfig */,
|
|
236
|
-
'Custom dotenvConfig should not include "path" or "processEnv" options. Those are managed by Envapter.'
|
|
237
|
-
);
|
|
213
|
+
throw new EnvaptError(EnvaptErrorCodes.InvalidUserDefinedConfig, 'Custom dotenvConfig should not include "path" or "processEnv" options. Those are managed by Envapter.');
|
|
238
214
|
}
|
|
239
|
-
const validKeys = /* @__PURE__ */ new Set([
|
|
215
|
+
const validKeys = /* @__PURE__ */ new Set([
|
|
216
|
+
"encoding",
|
|
217
|
+
"quiet",
|
|
218
|
+
"debug",
|
|
219
|
+
"override",
|
|
220
|
+
"DOTENV_KEY"
|
|
221
|
+
]);
|
|
240
222
|
const invalidKeys = Object.keys(config2).filter((key) => !validKeys.has(key));
|
|
241
223
|
if (invalidKeys.length > 0) {
|
|
242
|
-
throw new EnvaptError(
|
|
243
|
-
302 /* InvalidUserDefinedConfig */,
|
|
244
|
-
`Invalid dotenvConfig options: ${invalidKeys.join(", ")}. Allowed options: ${Array.from(validKeys).join(", ")}`
|
|
245
|
-
);
|
|
224
|
+
throw new EnvaptError(EnvaptErrorCodes.InvalidUserDefinedConfig, `Invalid dotenvConfig options: ${invalidKeys.join(", ")}. Allowed options: ${Array.from(validKeys).join(", ")}`);
|
|
246
225
|
}
|
|
247
226
|
return true;
|
|
248
227
|
}
|
|
@@ -259,10 +238,7 @@ var Validator = class {
|
|
|
259
238
|
}
|
|
260
239
|
});
|
|
261
240
|
if (missing.length > 0) {
|
|
262
|
-
throw new EnvaptError(
|
|
263
|
-
303 /* EnvFilesNotFound */,
|
|
264
|
-
`Environment file not found at path: ${missing.join(", ")}`
|
|
265
|
-
);
|
|
241
|
+
throw new EnvaptError(EnvaptErrorCodes.EnvFilesNotFound, `Environment file not found at path: ${missing.join(", ")}`);
|
|
266
242
|
}
|
|
267
243
|
}
|
|
268
244
|
};
|
|
@@ -273,15 +249,20 @@ var EnvapterBase = class _EnvapterBase {
|
|
|
273
249
|
static {
|
|
274
250
|
__name(this, "EnvapterBase");
|
|
275
251
|
}
|
|
276
|
-
static _envPaths = [
|
|
277
|
-
|
|
278
|
-
|
|
252
|
+
static _envPaths = [
|
|
253
|
+
".env"
|
|
254
|
+
];
|
|
255
|
+
static _userDefinedDotenvConfig = {
|
|
256
|
+
quiet: true
|
|
257
|
+
};
|
|
279
258
|
/**
|
|
280
259
|
* Set custom .env file paths. Accepts either a single path or array of paths.
|
|
281
260
|
* Setting new paths clears the cache and reloads environment variables.
|
|
282
261
|
*/
|
|
283
262
|
static set envPaths(paths) {
|
|
284
|
-
const newPaths = Array.isArray(paths) ? paths : [
|
|
263
|
+
const newPaths = Array.isArray(paths) ? paths : [
|
|
264
|
+
paths
|
|
265
|
+
];
|
|
285
266
|
Validator.validateEnvFilesExist(newPaths);
|
|
286
267
|
this._envPaths = newPaths;
|
|
287
268
|
this.refreshCache();
|
|
@@ -310,11 +291,45 @@ var EnvapterBase = class _EnvapterBase {
|
|
|
310
291
|
EnvaptCache.clear();
|
|
311
292
|
void this.config;
|
|
312
293
|
}
|
|
294
|
+
static resolveKeyInput(keyInput) {
|
|
295
|
+
const keys = Array.isArray(keyInput) ? keyInput : [
|
|
296
|
+
keyInput
|
|
297
|
+
];
|
|
298
|
+
const normalizedKeys = keys;
|
|
299
|
+
if (normalizedKeys.length === 0) {
|
|
300
|
+
throw new EnvaptError(EnvaptErrorCodes.InvalidKeyInput, "At least one environment key must be provided.");
|
|
301
|
+
}
|
|
302
|
+
if (normalizedKeys.some((k) => typeof k !== "string")) {
|
|
303
|
+
throw new EnvaptError(EnvaptErrorCodes.InvalidKeyInput, "Environment keys must be strings.");
|
|
304
|
+
}
|
|
305
|
+
if (normalizedKeys.some((k) => k.trim() === "")) {
|
|
306
|
+
throw new EnvaptError(EnvaptErrorCodes.InvalidKeyInput, "Environment keys cannot be empty strings.");
|
|
307
|
+
}
|
|
308
|
+
for (const candidate of normalizedKeys) {
|
|
309
|
+
const value = this.config.get(candidate);
|
|
310
|
+
if (value !== void 0) {
|
|
311
|
+
return {
|
|
312
|
+
key: candidate,
|
|
313
|
+
value
|
|
314
|
+
};
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return {
|
|
318
|
+
key: normalizedKeys[0],
|
|
319
|
+
value: void 0
|
|
320
|
+
};
|
|
321
|
+
}
|
|
313
322
|
static get config() {
|
|
314
323
|
if (EnvaptCache.size === 0) {
|
|
315
|
-
const isolatedEnv = {
|
|
324
|
+
const isolatedEnv = {
|
|
325
|
+
...process.env
|
|
326
|
+
};
|
|
316
327
|
try {
|
|
317
|
-
config({
|
|
328
|
+
config({
|
|
329
|
+
path: this._envPaths,
|
|
330
|
+
processEnv: isolatedEnv,
|
|
331
|
+
...this._userDefinedDotenvConfig
|
|
332
|
+
});
|
|
318
333
|
} catch {
|
|
319
334
|
}
|
|
320
335
|
for (const [key, value] of Object.entries(isolatedEnv)) EnvaptCache.set(key, value);
|
|
@@ -325,7 +340,7 @@ var EnvapterBase = class _EnvapterBase {
|
|
|
325
340
|
* Get raw environment variable value without parsing or conversion.
|
|
326
341
|
*/
|
|
327
342
|
getRaw(key) {
|
|
328
|
-
return _EnvapterBase.
|
|
343
|
+
return _EnvapterBase.resolveKeyInput(key).value;
|
|
329
344
|
}
|
|
330
345
|
};
|
|
331
346
|
|
|
@@ -343,8 +358,18 @@ var BuiltInConverters = class _BuiltInConverters {
|
|
|
343
358
|
}
|
|
344
359
|
static boolean(raw, fallback) {
|
|
345
360
|
const lower = raw.toLowerCase().trim();
|
|
346
|
-
const truthyValues = [
|
|
347
|
-
|
|
361
|
+
const truthyValues = [
|
|
362
|
+
"1",
|
|
363
|
+
"yes",
|
|
364
|
+
"true",
|
|
365
|
+
"on"
|
|
366
|
+
];
|
|
367
|
+
const falsyValues = [
|
|
368
|
+
"0",
|
|
369
|
+
"no",
|
|
370
|
+
"false",
|
|
371
|
+
"off"
|
|
372
|
+
];
|
|
348
373
|
if (truthyValues.includes(lower)) return true;
|
|
349
374
|
if (falsyValues.includes(lower)) return false;
|
|
350
375
|
return fallback;
|
|
@@ -468,13 +493,14 @@ var BuiltInConverters = class _BuiltInConverters {
|
|
|
468
493
|
|
|
469
494
|
// src/Parser.ts
|
|
470
495
|
var Parser = class {
|
|
471
|
-
constructor(envService) {
|
|
472
|
-
this.envService = envService;
|
|
473
|
-
}
|
|
474
496
|
static {
|
|
475
497
|
__name(this, "Parser");
|
|
476
498
|
}
|
|
499
|
+
envService;
|
|
477
500
|
TEMPLATE_REGEX = /\${\w*}/g;
|
|
501
|
+
constructor(envService) {
|
|
502
|
+
this.envService = envService;
|
|
503
|
+
}
|
|
478
504
|
/**
|
|
479
505
|
* Resolve template variables in a string while handling circular references and missing variables
|
|
480
506
|
* @internal
|
|
@@ -521,7 +547,7 @@ var Parser = class {
|
|
|
521
547
|
if (primitiveConstructor === Boolean) return "boolean";
|
|
522
548
|
if (primitiveConstructor === BigInt) return "bigint";
|
|
523
549
|
if (primitiveConstructor === Symbol) return "symbol";
|
|
524
|
-
throw new EnvaptError(
|
|
550
|
+
throw new EnvaptError(EnvaptErrorCodes.InvalidConverterType, `Unknown primitive constructor`);
|
|
525
551
|
}
|
|
526
552
|
processBuiltInConverter(key, fallback, resolvedConverter, hasFallback, wasOriginallyConstructor) {
|
|
527
553
|
Validator.builtInConverter(resolvedConverter);
|
|
@@ -541,10 +567,7 @@ var Parser = class {
|
|
|
541
567
|
processArrayConverter(key, fallback, resolvedConverter, hasFallback) {
|
|
542
568
|
Validator.arrayConverter(resolvedConverter);
|
|
543
569
|
if (hasFallback && fallback !== void 0 && !Array.isArray(fallback)) {
|
|
544
|
-
throw new EnvaptError(
|
|
545
|
-
101 /* InvalidFallback */,
|
|
546
|
-
`ArrayConverter requires that the fallback be an array, got ${typeof fallback}`
|
|
547
|
-
);
|
|
570
|
+
throw new EnvaptError(EnvaptErrorCodes.InvalidFallback, `ArrayConverter requires that the fallback be an array, got ${typeof fallback}`);
|
|
548
571
|
}
|
|
549
572
|
if (hasFallback && Array.isArray(fallback)) {
|
|
550
573
|
Validator.validateArrayFallbackElementTypes(fallback);
|
|
@@ -575,12 +598,12 @@ var Parser = class {
|
|
|
575
598
|
};
|
|
576
599
|
|
|
577
600
|
// src/core/EnvironmentMethods.ts
|
|
578
|
-
var Environment = /* @__PURE__ */ ((Environment2)
|
|
601
|
+
var Environment = /* @__PURE__ */ (function(Environment2) {
|
|
579
602
|
Environment2[Environment2["Development"] = 0] = "Development";
|
|
580
603
|
Environment2[Environment2["Staging"] = 1] = "Staging";
|
|
581
604
|
Environment2[Environment2["Production"] = 2] = "Production";
|
|
582
605
|
return Environment2;
|
|
583
|
-
})(
|
|
606
|
+
})({});
|
|
584
607
|
var EnvironmentMethods = class _EnvironmentMethods extends EnvapterBase {
|
|
585
608
|
static {
|
|
586
609
|
__name(this, "EnvironmentMethods");
|
|
@@ -589,7 +612,7 @@ var EnvironmentMethods = class _EnvironmentMethods extends EnvapterBase {
|
|
|
589
612
|
static determineEnvironment(env) {
|
|
590
613
|
const environment = env ?? this.getRawValue("ENVIRONMENT", this.getRawValue("ENV", this.getRawValue("NODE_ENV", "development")));
|
|
591
614
|
if (typeof environment === "string") {
|
|
592
|
-
this._environment = environment.toLowerCase() === "production" ? 2
|
|
615
|
+
this._environment = environment.toLowerCase() === "production" ? 2 : environment === "staging" ? 1 : 0;
|
|
593
616
|
} else {
|
|
594
617
|
this._environment = environment;
|
|
595
618
|
}
|
|
@@ -628,37 +651,37 @@ var EnvironmentMethods = class _EnvironmentMethods extends EnvapterBase {
|
|
|
628
651
|
* Check if the current environment is production
|
|
629
652
|
*/
|
|
630
653
|
static get isProduction() {
|
|
631
|
-
return this.environment === 2
|
|
654
|
+
return this.environment === 2;
|
|
632
655
|
}
|
|
633
656
|
/**
|
|
634
657
|
* @see {@link EnvironmentMethods.isProduction}
|
|
635
658
|
*/
|
|
636
659
|
get isProduction() {
|
|
637
|
-
return _EnvironmentMethods.environment === 2
|
|
660
|
+
return _EnvironmentMethods.environment === 2;
|
|
638
661
|
}
|
|
639
662
|
/**
|
|
640
663
|
* Check if the current environment is staging
|
|
641
664
|
*/
|
|
642
665
|
static get isStaging() {
|
|
643
|
-
return this.environment === 1
|
|
666
|
+
return this.environment === 1;
|
|
644
667
|
}
|
|
645
668
|
/**
|
|
646
669
|
* @see {@link EnvironmentMethods.isStaging}
|
|
647
670
|
*/
|
|
648
671
|
get isStaging() {
|
|
649
|
-
return _EnvironmentMethods.environment === 1
|
|
672
|
+
return _EnvironmentMethods.environment === 1;
|
|
650
673
|
}
|
|
651
674
|
/**
|
|
652
675
|
* Check if the current environment is development
|
|
653
676
|
*/
|
|
654
677
|
static get isDevelopment() {
|
|
655
|
-
return this.environment === 0
|
|
678
|
+
return this.environment === 0;
|
|
656
679
|
}
|
|
657
680
|
/**
|
|
658
681
|
* @see {@link EnvironmentMethods.isDevelopment}
|
|
659
682
|
*/
|
|
660
683
|
get isDevelopment() {
|
|
661
|
-
return _EnvironmentMethods.environment === 0
|
|
684
|
+
return _EnvironmentMethods.environment === 0;
|
|
662
685
|
}
|
|
663
686
|
static refreshCache() {
|
|
664
687
|
super.refreshCache();
|
|
@@ -673,84 +696,84 @@ var PrimitiveMethods = class _PrimitiveMethods extends EnvironmentMethods {
|
|
|
673
696
|
}
|
|
674
697
|
static parser = new Parser(new _PrimitiveMethods());
|
|
675
698
|
static _get(key, type, def) {
|
|
676
|
-
const
|
|
699
|
+
const { key: resolvedKey, value } = this.resolveKeyInput(key);
|
|
700
|
+
const rawVal = value;
|
|
677
701
|
if (!rawVal) return def;
|
|
678
|
-
const parsed = this.parser.resolveTemplate(
|
|
702
|
+
const parsed = this.parser.resolveTemplate(resolvedKey, String(rawVal));
|
|
679
703
|
let result;
|
|
680
|
-
if (type === 1
|
|
681
|
-
else if (type === 2
|
|
682
|
-
|
|
683
|
-
else if (type ===
|
|
684
|
-
result = BuiltInConverters.bigint(parsed, def);
|
|
685
|
-
else if (type === 4 /* Symbol */)
|
|
686
|
-
result = BuiltInConverters.symbol(parsed, def);
|
|
704
|
+
if (type === 1) result = BuiltInConverters.number(parsed, def);
|
|
705
|
+
else if (type === 2) result = BuiltInConverters.boolean(parsed, def);
|
|
706
|
+
else if (type === 3) result = BuiltInConverters.bigint(parsed, def);
|
|
707
|
+
else if (type === 4) result = BuiltInConverters.symbol(parsed, def);
|
|
687
708
|
else result = BuiltInConverters.string(parsed, def);
|
|
688
709
|
return result;
|
|
689
710
|
}
|
|
690
711
|
/**
|
|
691
712
|
* Get a string environment variable with optional fallback.
|
|
692
|
-
* Supports template variable resolution using
|
|
713
|
+
* Supports template variable resolution using `${VAR}` syntax.
|
|
714
|
+
* Accepts a single key or an ordered array of keys (first match wins).
|
|
693
715
|
*/
|
|
694
716
|
static get(key, def) {
|
|
695
|
-
return this._get(key, 0
|
|
717
|
+
return this._get(key, 0, def);
|
|
696
718
|
}
|
|
697
|
-
/**
|
|
698
|
-
* @see {@link PrimitiveMethods.get}
|
|
699
|
-
*/
|
|
700
719
|
get(key, def) {
|
|
701
|
-
return _PrimitiveMethods._get(key, 0
|
|
720
|
+
return _PrimitiveMethods._get(key, 0, def);
|
|
702
721
|
}
|
|
703
722
|
/**
|
|
704
723
|
* Get a number environment variable with optional fallback.
|
|
705
724
|
* Automatically converts string values to numbers.
|
|
725
|
+
* Accepts a single key or an ordered array of keys (first match wins).
|
|
706
726
|
*/
|
|
707
727
|
static getNumber(key, def) {
|
|
708
|
-
return this._get(key, 1
|
|
728
|
+
return this._get(key, 1, def);
|
|
709
729
|
}
|
|
710
730
|
/**
|
|
711
731
|
* @see {@link PrimitiveMethods.getNumber}
|
|
712
732
|
*/
|
|
713
733
|
getNumber(key, def) {
|
|
714
|
-
return _PrimitiveMethods._get(key, 1
|
|
734
|
+
return _PrimitiveMethods._get(key, 1, def);
|
|
715
735
|
}
|
|
716
736
|
/**
|
|
717
737
|
* Get a boolean environment variable with optional fallback.
|
|
718
738
|
* Recognizes: `1`, `yes`, `true`, 'on' as **true**; `0`, `no`, `false`, 'off' as **false** (case-insensitive).
|
|
739
|
+
* Accepts a single key or an ordered array of keys (first match wins).
|
|
719
740
|
*/
|
|
720
741
|
static getBoolean(key, def) {
|
|
721
|
-
return this._get(key, 2
|
|
742
|
+
return this._get(key, 2, def);
|
|
722
743
|
}
|
|
723
744
|
/**
|
|
724
745
|
* @see {@link PrimitiveMethods.getBoolean}
|
|
725
746
|
*/
|
|
726
747
|
getBoolean(key, def) {
|
|
727
|
-
return _PrimitiveMethods._get(key, 2
|
|
748
|
+
return _PrimitiveMethods._get(key, 2, def);
|
|
728
749
|
}
|
|
729
750
|
/**
|
|
730
751
|
* Get a bigint environment variable with optional fallback.
|
|
731
752
|
* Automatically converts string values to bigint.
|
|
753
|
+
* Accepts a single key or an ordered array of keys (first match wins).
|
|
732
754
|
*/
|
|
733
755
|
static getBigInt(key, def) {
|
|
734
|
-
return this._get(key, 3
|
|
756
|
+
return this._get(key, 3, def);
|
|
735
757
|
}
|
|
736
758
|
/**
|
|
737
759
|
* @see {@link PrimitiveMethods.getBigInt}
|
|
738
760
|
*/
|
|
739
761
|
getBigInt(key, def) {
|
|
740
|
-
return _PrimitiveMethods._get(key, 3
|
|
762
|
+
return _PrimitiveMethods._get(key, 3, def);
|
|
741
763
|
}
|
|
742
764
|
/**
|
|
743
765
|
* Get a symbol environment variable with optional fallback.
|
|
744
766
|
* Creates a symbol from the string value.
|
|
767
|
+
* Accepts a single key or an ordered array of keys (first match wins).
|
|
745
768
|
*/
|
|
746
769
|
static getSymbol(key, def) {
|
|
747
|
-
return this._get(key, 4
|
|
770
|
+
return this._get(key, 4, def);
|
|
748
771
|
}
|
|
749
772
|
/**
|
|
750
773
|
* @see {@link PrimitiveMethods.getSymbol}
|
|
751
774
|
*/
|
|
752
775
|
getSymbol(key, def) {
|
|
753
|
-
return _PrimitiveMethods._get(key, 4
|
|
776
|
+
return _PrimitiveMethods._get(key, 4, def);
|
|
754
777
|
}
|
|
755
778
|
};
|
|
756
779
|
|
|
@@ -760,10 +783,10 @@ var AdvancedMethods = class _AdvancedMethods extends PrimitiveMethods {
|
|
|
760
783
|
__name(this, "AdvancedMethods");
|
|
761
784
|
}
|
|
762
785
|
static getUsing(key, converter, fallback) {
|
|
763
|
-
const
|
|
764
|
-
if (!
|
|
786
|
+
const { key: resolvedKey, value } = this.resolveKeyInput(key);
|
|
787
|
+
if (!value) return fallback;
|
|
765
788
|
const hasFallback = fallback !== void 0;
|
|
766
|
-
const result = this.parser.convertValue(
|
|
789
|
+
const result = this.parser.convertValue(resolvedKey, fallback, converter, hasFallback);
|
|
767
790
|
return result;
|
|
768
791
|
}
|
|
769
792
|
getUsing(key, converter, fallback) {
|
|
@@ -771,17 +794,13 @@ var AdvancedMethods = class _AdvancedMethods extends PrimitiveMethods {
|
|
|
771
794
|
}
|
|
772
795
|
/**
|
|
773
796
|
* Get an environment variable using a custom converter function.
|
|
797
|
+
* Accepts a single key or an ordered list for automatic fallback.
|
|
774
798
|
*/
|
|
775
799
|
static getWith(key, converter, fallback) {
|
|
776
|
-
const
|
|
777
|
-
if (!
|
|
800
|
+
const { key: resolvedKey, value } = this.resolveKeyInput(key);
|
|
801
|
+
if (!value) return fallback;
|
|
778
802
|
const hasFallback = fallback !== void 0;
|
|
779
|
-
const result = this.parser.convertValue(
|
|
780
|
-
key,
|
|
781
|
-
fallback,
|
|
782
|
-
converter,
|
|
783
|
-
hasFallback
|
|
784
|
-
);
|
|
803
|
+
const result = this.parser.convertValue(resolvedKey, fallback, converter, hasFallback);
|
|
785
804
|
return result;
|
|
786
805
|
}
|
|
787
806
|
/**
|
|
@@ -868,7 +887,7 @@ function Envapt(key, fallbackOrOptions, converter) {
|
|
|
868
887
|
__name(Envapt, "Envapt");
|
|
869
888
|
|
|
870
889
|
// src/Converters.ts
|
|
871
|
-
var Converters = /* @__PURE__ */ ((Converters2)
|
|
890
|
+
var Converters = /* @__PURE__ */ (function(Converters2) {
|
|
872
891
|
Converters2["String"] = "string";
|
|
873
892
|
Converters2["Number"] = "number";
|
|
874
893
|
Converters2["Boolean"] = "boolean";
|
|
@@ -883,7 +902,7 @@ var Converters = /* @__PURE__ */ ((Converters2) => {
|
|
|
883
902
|
Converters2["Date"] = "date";
|
|
884
903
|
Converters2["Time"] = "time";
|
|
885
904
|
return Converters2;
|
|
886
|
-
})(
|
|
905
|
+
})({});
|
|
887
906
|
/* v8 ignore next -- @preserve */
|
|
888
907
|
|
|
889
908
|
export { Converters, Envapt, EnvaptError, EnvaptErrorCodes, Envapter, Environment };
|