envapt 4.0.1 → 4.1.0
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/README.md +257 -231
- package/dist/index.cjs +51 -19
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +165 -39
- package/dist/index.d.ts +165 -39
- package/dist/index.mjs +51 -19
- package/dist/index.mjs.map +1 -1
- package/package.json +18 -16
package/dist/index.d.ts
CHANGED
|
@@ -76,6 +76,11 @@ interface ArrayConverter {
|
|
|
76
76
|
* @public
|
|
77
77
|
*/
|
|
78
78
|
type BaseInput = string | undefined;
|
|
79
|
+
/**
|
|
80
|
+
* Accepted shape for environment variable lookups. Either a single key or an ordered list of keys.
|
|
81
|
+
* @public
|
|
82
|
+
*/
|
|
83
|
+
type EnvKeyInput = string | readonly [string, ...string[]];
|
|
79
84
|
/**
|
|
80
85
|
* Custom parser function type for environment variables
|
|
81
86
|
* @param raw - Raw string value from environment
|
|
@@ -189,90 +194,196 @@ type InferPrimitiveFallbackType<TFallback extends string | number | boolean | bi
|
|
|
189
194
|
/**
|
|
190
195
|
* Usage 1: Custom converter function with fallback provided
|
|
191
196
|
*
|
|
192
|
-
* @param key - Environment variable name to load
|
|
197
|
+
* @param key - Environment variable name to load (string or ordered array of strings)
|
|
193
198
|
* @param options - Configuration options with custom converter and required fallback
|
|
194
199
|
* @public
|
|
200
|
+
* @example
|
|
201
|
+
* ```ts
|
|
202
|
+
* // Custom converter that validates a non-empty API key
|
|
203
|
+
* class Config extends Envapter {
|
|
204
|
+
* \@Envapt('API_KEY', {
|
|
205
|
+
* fallback: 'default-key',
|
|
206
|
+
* converter(raw, _fallback) {
|
|
207
|
+
* if (!raw || raw.trim() === '') throw new Error('API_KEY required');
|
|
208
|
+
* return raw.trim();
|
|
209
|
+
* }
|
|
210
|
+
* })
|
|
211
|
+
* static readonly apiKey: string;
|
|
212
|
+
* }
|
|
213
|
+
* ```
|
|
195
214
|
*/
|
|
196
|
-
declare function Envapt<TFallback>(key:
|
|
215
|
+
declare function Envapt<TFallback>(key: EnvKeyInput, options: {
|
|
197
216
|
converter: (raw: string | undefined, fallback: TFallback) => TFallback;
|
|
198
217
|
fallback: TFallback;
|
|
199
218
|
}): PropertyDecorator;
|
|
200
219
|
/**
|
|
201
220
|
* Usage 2: Custom converter function without fallback
|
|
202
221
|
*
|
|
203
|
-
* @param key - Environment variable name to load
|
|
222
|
+
* @param key - Environment variable name(s) to load
|
|
204
223
|
* @param options - Configuration options with custom converter only
|
|
205
224
|
* @public
|
|
225
|
+
* @example
|
|
226
|
+
* ```ts
|
|
227
|
+
* // Custom converter without providing a fallback
|
|
228
|
+
* class Config extends Envapter {
|
|
229
|
+
* \@Envapt('FEATURE_FLAGS', { converter(raw) {
|
|
230
|
+
* // raw may be undefined — handle that here
|
|
231
|
+
* return raw ? raw.split('|').map(s => s.trim()) : [];
|
|
232
|
+
* } })
|
|
233
|
+
* static readonly featureFlags: string[];
|
|
234
|
+
* }
|
|
235
|
+
* ```
|
|
206
236
|
*/
|
|
207
|
-
declare function Envapt<TReturnType>(key:
|
|
237
|
+
declare function Envapt<TReturnType>(key: EnvKeyInput, options: {
|
|
208
238
|
converter: ConverterFunction<TReturnType>;
|
|
209
239
|
}): PropertyDecorator;
|
|
210
240
|
/**
|
|
211
241
|
* Usage 3: Built-in converter with optional fallback
|
|
212
242
|
*
|
|
213
|
-
* @param key - Environment variable name to load
|
|
243
|
+
* @param key - Environment variable name(s) to load
|
|
214
244
|
* @param options - Configuration options with built-in converter
|
|
215
245
|
* @public
|
|
246
|
+
* @example
|
|
247
|
+
* ```ts
|
|
248
|
+
* import { Converters } from 'envapt';
|
|
249
|
+
*
|
|
250
|
+
* class Config extends Envapter {
|
|
251
|
+
* // Use built-in Number converter with a numeric fallback
|
|
252
|
+
* \@Envapt('APP_PORT', { converter: Converters.Number, fallback: 3000 })
|
|
253
|
+
* static readonly port: number;
|
|
254
|
+
*
|
|
255
|
+
* // Use Url converter with a string fallback (must match converter type)
|
|
256
|
+
* \@Envapt('APP_URL', { converter: Converters.Url, fallback: 'http://localhost:3000' })
|
|
257
|
+
* static readonly url: URL;
|
|
258
|
+
*
|
|
259
|
+
* // Prefer CANARY_URL when present, otherwise fall back to APP_URL
|
|
260
|
+
* \@Envapt(['CANARY_URL', 'APP_URL'], { converter: Converters.Url })
|
|
261
|
+
* static readonly canaryUrl: URL | null;
|
|
262
|
+
* }
|
|
263
|
+
* ```
|
|
216
264
|
*/
|
|
217
|
-
declare function Envapt<TConverter extends BuiltInConverter>(key:
|
|
265
|
+
declare function Envapt<TConverter extends BuiltInConverter>(key: EnvKeyInput, options: {
|
|
218
266
|
converter: TConverter;
|
|
219
267
|
fallback?: InferConverterReturnType<TConverter> | undefined;
|
|
220
268
|
}): PropertyDecorator;
|
|
221
269
|
/**
|
|
222
270
|
* Usage 4: Array converter with optional fallback
|
|
223
271
|
*
|
|
224
|
-
* @param key - Environment variable name to load
|
|
272
|
+
* @param key - Environment variable name(s) to load
|
|
225
273
|
* @param options - Configuration options with array converter
|
|
226
274
|
* @public
|
|
275
|
+
* @example
|
|
276
|
+
* ```ts
|
|
277
|
+
* import { Converters } from 'envapt';
|
|
278
|
+
*
|
|
279
|
+
* class Config extends Envapter {
|
|
280
|
+
* // Comma-separated list of origins -> string[]
|
|
281
|
+
* \@Envapt('ALLOWED_ORIGINS', {
|
|
282
|
+
* converter: { delimiter: ',', type: Converters.String },
|
|
283
|
+
* fallback: ['https://example.com']
|
|
284
|
+
* })
|
|
285
|
+
* static readonly allowedOrigins: string[];
|
|
286
|
+
*
|
|
287
|
+
* // Pipe-separated ports -> number[]
|
|
288
|
+
* \@Envapt('PORTS', {
|
|
289
|
+
* converter: { delimiter: '|', type: Converters.Number },
|
|
290
|
+
* fallback: [3000]
|
|
291
|
+
* })
|
|
292
|
+
* static readonly ports: number[];
|
|
293
|
+
* }
|
|
294
|
+
* ```
|
|
227
295
|
*/
|
|
228
|
-
declare function Envapt<TConverter extends ArrayConverter>(key:
|
|
296
|
+
declare function Envapt<TConverter extends ArrayConverter>(key: EnvKeyInput, options: {
|
|
229
297
|
converter: TConverter;
|
|
230
298
|
fallback?: InferConverterReturnType<TConverter> | undefined;
|
|
231
299
|
}): PropertyDecorator;
|
|
232
300
|
/**
|
|
233
301
|
* Usage 5: Primitive constructor with optional fallback
|
|
234
302
|
*
|
|
235
|
-
* @param key - Environment variable name to load
|
|
303
|
+
* @param key - Environment variable name(s) to load
|
|
236
304
|
* @param options - Configuration options with primitive constructor
|
|
237
305
|
* @public
|
|
306
|
+
* @example
|
|
307
|
+
* ```ts
|
|
308
|
+
* // Use primitive constructors to coerce values
|
|
309
|
+
* class Config extends Envapter {
|
|
310
|
+
* \@Envapt('MAX_CONNECTIONS', { converter: Number, fallback: 100 })
|
|
311
|
+
* static readonly maxConnections: number;
|
|
312
|
+
*
|
|
313
|
+
* \@Envapt('FEATURE_ENABLED', { converter: Boolean, fallback: false })
|
|
314
|
+
* static readonly featureEnabled: boolean;
|
|
315
|
+
* }
|
|
316
|
+
* ```
|
|
238
317
|
*/
|
|
239
|
-
declare function Envapt<TConstructor extends PrimitiveConstructor>(key:
|
|
318
|
+
declare function Envapt<TConstructor extends PrimitiveConstructor>(key: EnvKeyInput, options: {
|
|
240
319
|
converter: TConstructor;
|
|
241
320
|
fallback?: InferPrimitiveReturnType<TConstructor>;
|
|
242
321
|
}): PropertyDecorator;
|
|
243
322
|
/**
|
|
244
323
|
* Usage 6: Fallback only (no converter)
|
|
245
324
|
*
|
|
246
|
-
* @param key - Environment variable name to load
|
|
325
|
+
* @param key - Environment variable name(s) to load
|
|
247
326
|
* @param options - Configuration options with fallback only
|
|
248
327
|
* @public
|
|
328
|
+
* @example
|
|
329
|
+
* ```ts
|
|
330
|
+
* // Fallback-only usage (no converter)
|
|
331
|
+
* class Config extends Envapter {
|
|
332
|
+
* \@Envapt('LOG_FILE', { fallback: '/var/log/app.log' })
|
|
333
|
+
* static readonly logFile: string;
|
|
334
|
+
*
|
|
335
|
+
* \@Envapt('RETRY_POLICY', { fallback: { retries: 3, backoff: 'exponential' } })
|
|
336
|
+
* static readonly retryPolicy: unknown;
|
|
337
|
+
* }
|
|
338
|
+
* ```
|
|
249
339
|
*/
|
|
250
|
-
declare function Envapt<TFallback>(key:
|
|
340
|
+
declare function Envapt<TFallback>(key: EnvKeyInput, options: {
|
|
251
341
|
fallback: TFallback;
|
|
252
342
|
converter?: undefined;
|
|
253
343
|
}): PropertyDecorator;
|
|
254
344
|
/**
|
|
255
345
|
* Classic API: No fallback
|
|
256
346
|
*
|
|
257
|
-
* @param key - Environment variable name to load
|
|
347
|
+
* @param key - Environment variable name(s) to load
|
|
348
|
+
* @example
|
|
349
|
+
* ```ts
|
|
350
|
+
* // Classic API: no fallback — property will resolve from env or be null
|
|
351
|
+
* class Config extends Envapter {
|
|
352
|
+
* \@Envapt('SIMPLE_VALUE')
|
|
353
|
+
* static readonly simple?: string | null;
|
|
354
|
+
* }
|
|
355
|
+
* ```
|
|
258
356
|
*/
|
|
259
|
-
declare function Envapt<_TReturnType = string | null>(key:
|
|
357
|
+
declare function Envapt<_TReturnType = string | null>(key: EnvKeyInput): PropertyDecorator;
|
|
260
358
|
/**
|
|
261
359
|
* Classic API: Primitive fallback only
|
|
262
360
|
*
|
|
263
|
-
* @param key - Environment variable name to load
|
|
361
|
+
* @param key - Environment variable name(s) to load
|
|
264
362
|
* @param fallback - Default primitive value
|
|
265
363
|
* @param converter - Optional primitive constructor (String, Number, etc.)
|
|
266
364
|
* @public
|
|
365
|
+
* @example
|
|
366
|
+
* ```ts
|
|
367
|
+
* // Classic API with primitive fallback and optional primitive converter
|
|
368
|
+
* class Config extends Envapter {
|
|
369
|
+
* // Provide fallback only
|
|
370
|
+
* \@Envapt('HOST', 'localhost')
|
|
371
|
+
* static readonly host: string;
|
|
372
|
+
*
|
|
373
|
+
* // Provide fallback and converter
|
|
374
|
+
* \@Envapt('PORT', 8080, Number)
|
|
375
|
+
* static readonly port: number;
|
|
376
|
+
* }
|
|
377
|
+
* ```
|
|
267
378
|
*/
|
|
268
|
-
declare function Envapt<TFallback extends string | number | boolean | bigint | symbol | undefined>(key:
|
|
379
|
+
declare function Envapt<TFallback extends string | number | boolean | bigint | symbol | undefined>(key: EnvKeyInput, fallback: InferPrimitiveFallbackType<TFallback>, converter?: PrimitiveConstructor): PropertyDecorator;
|
|
269
380
|
|
|
270
381
|
/**
|
|
271
382
|
* @internal
|
|
272
383
|
*/
|
|
273
384
|
interface EnvapterService {
|
|
274
|
-
getRaw(key:
|
|
275
|
-
get(key:
|
|
385
|
+
getRaw(key: EnvKeyInput): string | undefined;
|
|
386
|
+
get(key: EnvKeyInput, def?: string): string | undefined;
|
|
276
387
|
}
|
|
277
388
|
/**
|
|
278
389
|
* Parser class for handling environment variable template resolution and type conversion
|
|
@@ -287,7 +398,7 @@ declare class Parser {
|
|
|
287
398
|
* @internal
|
|
288
399
|
*/
|
|
289
400
|
resolveTemplate(key: string, value: string, stack?: Set<string>): string;
|
|
290
|
-
convertValue<TFallback>(key:
|
|
401
|
+
convertValue<TFallback>(key: EnvKeyInput, fallback: TFallback | undefined, converter: EnvaptConverter<TFallback> | undefined, hasFallback: boolean): TFallback | null | undefined;
|
|
291
402
|
private processFallbackForConverter;
|
|
292
403
|
private convertPrimitiveToString;
|
|
293
404
|
private processBuiltInConverter;
|
|
@@ -322,11 +433,15 @@ declare abstract class EnvapterBase {
|
|
|
322
433
|
*/
|
|
323
434
|
static get dotenvConfig(): PermittedDotenvConfig;
|
|
324
435
|
protected static refreshCache(): void;
|
|
436
|
+
protected static resolveKeyInput(keyInput: EnvKeyInput): {
|
|
437
|
+
key: string;
|
|
438
|
+
value: string | undefined;
|
|
439
|
+
};
|
|
325
440
|
protected static get config(): Map<string, unknown>;
|
|
326
441
|
/**
|
|
327
442
|
* Get raw environment variable value without parsing or conversion.
|
|
328
443
|
*/
|
|
329
|
-
getRaw(key:
|
|
444
|
+
getRaw(key: EnvKeyInput): string | undefined;
|
|
330
445
|
}
|
|
331
446
|
|
|
332
447
|
/**
|
|
@@ -398,49 +513,54 @@ declare class PrimitiveMethods extends EnvironmentMethods implements EnvapterSer
|
|
|
398
513
|
private static _get;
|
|
399
514
|
/**
|
|
400
515
|
* Get a string environment variable with optional fallback.
|
|
401
|
-
* Supports template variable resolution using
|
|
516
|
+
* Supports template variable resolution using `${VAR}` syntax.
|
|
517
|
+
* Accepts a single key or an ordered array of keys (first match wins).
|
|
402
518
|
*/
|
|
403
|
-
static get<Default extends string | undefined = undefined>(key:
|
|
519
|
+
static get<Default extends string | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<string, Default>;
|
|
404
520
|
/**
|
|
405
521
|
* @see {@link PrimitiveMethods.get}
|
|
406
522
|
*/
|
|
407
|
-
get
|
|
523
|
+
get(key: EnvKeyInput, def?: string): string | undefined;
|
|
408
524
|
/**
|
|
409
525
|
* Get a number environment variable with optional fallback.
|
|
410
526
|
* Automatically converts string values to numbers.
|
|
527
|
+
* Accepts a single key or an ordered array of keys (first match wins).
|
|
411
528
|
*/
|
|
412
|
-
static getNumber<Default extends number | undefined = undefined>(key:
|
|
529
|
+
static getNumber<Default extends number | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<number, Default>;
|
|
413
530
|
/**
|
|
414
531
|
* @see {@link PrimitiveMethods.getNumber}
|
|
415
532
|
*/
|
|
416
|
-
getNumber<Default extends number | undefined = undefined>(key:
|
|
533
|
+
getNumber<Default extends number | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<number, Default>;
|
|
417
534
|
/**
|
|
418
535
|
* Get a boolean environment variable with optional fallback.
|
|
419
536
|
* Recognizes: `1`, `yes`, `true`, 'on' as **true**; `0`, `no`, `false`, 'off' as **false** (case-insensitive).
|
|
537
|
+
* Accepts a single key or an ordered array of keys (first match wins).
|
|
420
538
|
*/
|
|
421
|
-
static getBoolean<Default extends boolean | undefined = undefined>(key:
|
|
539
|
+
static getBoolean<Default extends boolean | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<boolean, Default>;
|
|
422
540
|
/**
|
|
423
541
|
* @see {@link PrimitiveMethods.getBoolean}
|
|
424
542
|
*/
|
|
425
|
-
getBoolean<Default extends boolean | undefined = undefined>(key:
|
|
543
|
+
getBoolean<Default extends boolean | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<boolean, Default>;
|
|
426
544
|
/**
|
|
427
545
|
* Get a bigint environment variable with optional fallback.
|
|
428
546
|
* Automatically converts string values to bigint.
|
|
547
|
+
* Accepts a single key or an ordered array of keys (first match wins).
|
|
429
548
|
*/
|
|
430
|
-
static getBigInt<Default extends bigint | undefined = undefined>(key:
|
|
549
|
+
static getBigInt<Default extends bigint | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<bigint, Default>;
|
|
431
550
|
/**
|
|
432
551
|
* @see {@link PrimitiveMethods.getBigInt}
|
|
433
552
|
*/
|
|
434
|
-
getBigInt<Default extends bigint | undefined = undefined>(key:
|
|
553
|
+
getBigInt<Default extends bigint | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<bigint, Default>;
|
|
435
554
|
/**
|
|
436
555
|
* Get a symbol environment variable with optional fallback.
|
|
437
556
|
* Creates a symbol from the string value.
|
|
557
|
+
* Accepts a single key or an ordered array of keys (first match wins).
|
|
438
558
|
*/
|
|
439
|
-
static getSymbol<Default extends symbol | undefined = undefined>(key:
|
|
559
|
+
static getSymbol<Default extends symbol | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<symbol, Default>;
|
|
440
560
|
/**
|
|
441
561
|
* @see {@link PrimitiveMethods.getSymbol}
|
|
442
562
|
*/
|
|
443
|
-
getSymbol<Default extends symbol | undefined = undefined>(key:
|
|
563
|
+
getSymbol<Default extends symbol | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<symbol, Default>;
|
|
444
564
|
}
|
|
445
565
|
|
|
446
566
|
/**
|
|
@@ -451,22 +571,24 @@ declare class AdvancedMethods extends PrimitiveMethods {
|
|
|
451
571
|
/**
|
|
452
572
|
* Get an environment variable using a built-in converter.
|
|
453
573
|
* Supports both Converter enum values and array converter configurations.
|
|
574
|
+
* The key can be a single name or an ordered list; the first defined value wins.
|
|
454
575
|
*/
|
|
455
|
-
static getUsing<TConverter extends BuiltInConverter | ArrayConverter, TFallback = undefined>(key:
|
|
456
|
-
static getUsing<TReturn>(key:
|
|
576
|
+
static getUsing<TConverter extends BuiltInConverter | ArrayConverter, TFallback = undefined>(key: EnvKeyInput, converter: TConverter, fallback?: TFallback): AdvancedConverterReturn<TConverter, TFallback>;
|
|
577
|
+
static getUsing<TReturn>(key: EnvKeyInput, converter: BuiltInConverter | ArrayConverter, fallback?: TReturn): TReturn;
|
|
457
578
|
/**
|
|
458
579
|
* @see {@link AdvancedMethods.getUsing}
|
|
459
580
|
*/
|
|
460
|
-
getUsing<TConverter extends BuiltInConverter | ArrayConverter, TFallback = undefined>(key:
|
|
461
|
-
getUsing<TReturn>(key:
|
|
581
|
+
getUsing<TConverter extends BuiltInConverter | ArrayConverter, TFallback = undefined>(key: EnvKeyInput, converter: TConverter, fallback?: TFallback): AdvancedConverterReturn<TConverter, TFallback>;
|
|
582
|
+
getUsing<TReturn>(key: EnvKeyInput, converter: BuiltInConverter | ArrayConverter, fallback?: TReturn): TReturn;
|
|
462
583
|
/**
|
|
463
584
|
* Get an environment variable using a custom converter function.
|
|
585
|
+
* Accepts a single key or an ordered list for automatic fallback.
|
|
464
586
|
*/
|
|
465
|
-
static getWith<TReturnType, TFallback extends TReturnType | undefined = undefined>(key:
|
|
587
|
+
static getWith<TReturnType, TFallback extends TReturnType | undefined = undefined>(key: EnvKeyInput, converter: ConverterFunction<TReturnType>, fallback?: TFallback): ConditionalReturn<TReturnType, TFallback>;
|
|
466
588
|
/**
|
|
467
589
|
* @see {@link AdvancedMethods.getWith}
|
|
468
590
|
*/
|
|
469
|
-
getWith<TReturnType, TFallback extends TReturnType | undefined = undefined>(key:
|
|
591
|
+
getWith<TReturnType, TFallback extends TReturnType | undefined = undefined>(key: EnvKeyInput, converter: ConverterFunction<TReturnType>, fallback?: TFallback): ConditionalReturn<TReturnType, TFallback>;
|
|
470
592
|
}
|
|
471
593
|
|
|
472
594
|
/**
|
|
@@ -482,10 +604,12 @@ declare class AdvancedMethods extends PrimitiveMethods {
|
|
|
482
604
|
* // Static usage
|
|
483
605
|
* const port = Envapter.getNumber('PORT', 3000);
|
|
484
606
|
* const url = Envapter.get('API_URL', 'http://localhost');
|
|
607
|
+
* const replica = Envapter.get(['READONLY_URL', 'DATABASE_URL'], 'sqlite://memory');
|
|
485
608
|
*
|
|
486
609
|
* // Instance usage
|
|
487
610
|
* const env = new Envapter();
|
|
488
611
|
* const dbUrl = env.get('DATABASE_URL', 'sqlite://memory');
|
|
612
|
+
* const primaryHost = env.get(['PRIMARY_HOST', 'SECONDARY_HOST']);
|
|
489
613
|
* ```
|
|
490
614
|
*
|
|
491
615
|
* @public
|
|
@@ -537,7 +661,9 @@ declare enum EnvaptErrorCodes {
|
|
|
537
661
|
/** Thrown when invalid user-defined configuration is provided */
|
|
538
662
|
InvalidUserDefinedConfig = 302,
|
|
539
663
|
/** Thrown when specified environment files don't exist */
|
|
540
|
-
EnvFilesNotFound = 303
|
|
664
|
+
EnvFilesNotFound = 303,
|
|
665
|
+
/** Thrown when no valid environment key is provided */
|
|
666
|
+
InvalidKeyInput = 304
|
|
541
667
|
}
|
|
542
668
|
/**
|
|
543
669
|
* Custom error for better DX and debugging when using Envapt.
|
|
@@ -552,4 +678,4 @@ declare class EnvaptError extends Error {
|
|
|
552
678
|
constructor(code: EnvaptErrorCodes, message: string);
|
|
553
679
|
}
|
|
554
680
|
|
|
555
|
-
export { type AdvancedConverterReturn, type ArrayConverter, type BuiltInConverter, type BuiltInConverterFunction, type ConditionalReturn, type ConverterFunction, Converters, Envapt, type EnvaptConverter, EnvaptError, EnvaptErrorCodes, type EnvaptOptions, Envapter, Environment, type InferConverterReturnType, type InferPrimitiveFallbackType, type InferPrimitiveReturnType, type JsonValue, type MapOfConverterFunctions, type PermittedDotenvConfig, type PrimitiveConstructor, type TimeUnit, type ValidArrayConverterBuiltInType };
|
|
681
|
+
export { type AdvancedConverterReturn, type ArrayConverter, type BuiltInConverter, type BuiltInConverterFunction, type ConditionalReturn, type ConverterFunction, Converters, type EnvKeyInput, Envapt, type EnvaptConverter, EnvaptError, EnvaptErrorCodes, type EnvaptOptions, Envapter, Environment, type InferConverterReturnType, type InferPrimitiveFallbackType, type InferPrimitiveReturnType, type JsonValue, type MapOfConverterFunctions, type PermittedDotenvConfig, type PrimitiveConstructor, type TimeUnit, type ValidArrayConverterBuiltInType };
|
package/dist/index.mjs
CHANGED
|
@@ -19,6 +19,7 @@ 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
|
})(EnvaptErrorCodes || {});
|
|
24
25
|
var EnvaptError = class extends Error {
|
|
@@ -221,7 +222,10 @@ var Validator = class {
|
|
|
221
222
|
`Failed to coerce fallback value using ${converter.name}: ${error.message}`
|
|
222
223
|
);
|
|
223
224
|
}
|
|
224
|
-
throw new EnvaptError(
|
|
225
|
+
throw new EnvaptError(
|
|
226
|
+
205 /* PrimitiveCoercionFailed */,
|
|
227
|
+
`Unknown primitive converter: ${converter.name}`
|
|
228
|
+
);
|
|
225
229
|
}
|
|
226
230
|
/**
|
|
227
231
|
* Make sure the user hasn't provided prohibited options in their dotenv config
|
|
@@ -307,6 +311,26 @@ var EnvapterBase = class _EnvapterBase {
|
|
|
307
311
|
EnvaptCache.clear();
|
|
308
312
|
void this.config;
|
|
309
313
|
}
|
|
314
|
+
static resolveKeyInput(keyInput) {
|
|
315
|
+
const keys = Array.isArray(keyInput) ? keyInput : [keyInput];
|
|
316
|
+
const normalizedKeys = keys;
|
|
317
|
+
if (normalizedKeys.length === 0) {
|
|
318
|
+
throw new EnvaptError(304 /* InvalidKeyInput */, "At least one environment key must be provided.");
|
|
319
|
+
}
|
|
320
|
+
if (normalizedKeys.some((k) => typeof k !== "string")) {
|
|
321
|
+
throw new EnvaptError(304 /* InvalidKeyInput */, "Environment keys must be strings.");
|
|
322
|
+
}
|
|
323
|
+
if (normalizedKeys.some((k) => k.trim() === "")) {
|
|
324
|
+
throw new EnvaptError(304 /* InvalidKeyInput */, "Environment keys cannot be empty strings.");
|
|
325
|
+
}
|
|
326
|
+
for (const candidate of normalizedKeys) {
|
|
327
|
+
const value = this.config.get(candidate);
|
|
328
|
+
if (value !== void 0) {
|
|
329
|
+
return { key: candidate, value };
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return { key: normalizedKeys[0], value: void 0 };
|
|
333
|
+
}
|
|
310
334
|
static get config() {
|
|
311
335
|
if (EnvaptCache.size === 0) {
|
|
312
336
|
const isolatedEnv = { ...process.env };
|
|
@@ -322,7 +346,7 @@ var EnvapterBase = class _EnvapterBase {
|
|
|
322
346
|
* Get raw environment variable value without parsing or conversion.
|
|
323
347
|
*/
|
|
324
348
|
getRaw(key) {
|
|
325
|
-
return _EnvapterBase.
|
|
349
|
+
return _EnvapterBase.resolveKeyInput(key).value;
|
|
326
350
|
}
|
|
327
351
|
};
|
|
328
352
|
|
|
@@ -331,8 +355,8 @@ var BuiltInConverters = class _BuiltInConverters {
|
|
|
331
355
|
static {
|
|
332
356
|
__name(this, "BuiltInConverters");
|
|
333
357
|
}
|
|
334
|
-
static string(raw,
|
|
335
|
-
return String(raw)
|
|
358
|
+
static string(raw, _fallback) {
|
|
359
|
+
return String(raw);
|
|
336
360
|
}
|
|
337
361
|
static number(raw, fallback) {
|
|
338
362
|
const parsed = Number(raw);
|
|
@@ -670,33 +694,36 @@ var PrimitiveMethods = class _PrimitiveMethods extends EnvironmentMethods {
|
|
|
670
694
|
}
|
|
671
695
|
static parser = new Parser(new _PrimitiveMethods());
|
|
672
696
|
static _get(key, type, def) {
|
|
673
|
-
const
|
|
697
|
+
const { key: resolvedKey, value } = this.resolveKeyInput(key);
|
|
698
|
+
const rawVal = value;
|
|
674
699
|
if (!rawVal) return def;
|
|
675
|
-
const parsed = this.parser.resolveTemplate(
|
|
700
|
+
const parsed = this.parser.resolveTemplate(resolvedKey, String(rawVal));
|
|
676
701
|
let result;
|
|
677
702
|
if (type === 1 /* Number */) result = BuiltInConverters.number(parsed, def);
|
|
678
|
-
else if (type === 2 /* Boolean */)
|
|
679
|
-
|
|
680
|
-
else if (type ===
|
|
703
|
+
else if (type === 2 /* Boolean */)
|
|
704
|
+
result = BuiltInConverters.boolean(parsed, def);
|
|
705
|
+
else if (type === 3 /* BigInt */)
|
|
706
|
+
result = BuiltInConverters.bigint(parsed, def);
|
|
707
|
+
else if (type === 4 /* Symbol */)
|
|
708
|
+
result = BuiltInConverters.symbol(parsed, def);
|
|
681
709
|
else result = BuiltInConverters.string(parsed, def);
|
|
682
710
|
return result;
|
|
683
711
|
}
|
|
684
712
|
/**
|
|
685
713
|
* Get a string environment variable with optional fallback.
|
|
686
|
-
* Supports template variable resolution using
|
|
714
|
+
* Supports template variable resolution using `${VAR}` syntax.
|
|
715
|
+
* Accepts a single key or an ordered array of keys (first match wins).
|
|
687
716
|
*/
|
|
688
717
|
static get(key, def) {
|
|
689
718
|
return this._get(key, 0 /* String */, def);
|
|
690
719
|
}
|
|
691
|
-
/**
|
|
692
|
-
* @see {@link PrimitiveMethods.get}
|
|
693
|
-
*/
|
|
694
720
|
get(key, def) {
|
|
695
721
|
return _PrimitiveMethods._get(key, 0 /* String */, def);
|
|
696
722
|
}
|
|
697
723
|
/**
|
|
698
724
|
* Get a number environment variable with optional fallback.
|
|
699
725
|
* Automatically converts string values to numbers.
|
|
726
|
+
* Accepts a single key or an ordered array of keys (first match wins).
|
|
700
727
|
*/
|
|
701
728
|
static getNumber(key, def) {
|
|
702
729
|
return this._get(key, 1 /* Number */, def);
|
|
@@ -710,6 +737,7 @@ var PrimitiveMethods = class _PrimitiveMethods extends EnvironmentMethods {
|
|
|
710
737
|
/**
|
|
711
738
|
* Get a boolean environment variable with optional fallback.
|
|
712
739
|
* Recognizes: `1`, `yes`, `true`, 'on' as **true**; `0`, `no`, `false`, 'off' as **false** (case-insensitive).
|
|
740
|
+
* Accepts a single key or an ordered array of keys (first match wins).
|
|
713
741
|
*/
|
|
714
742
|
static getBoolean(key, def) {
|
|
715
743
|
return this._get(key, 2 /* Boolean */, def);
|
|
@@ -723,6 +751,7 @@ var PrimitiveMethods = class _PrimitiveMethods extends EnvironmentMethods {
|
|
|
723
751
|
/**
|
|
724
752
|
* Get a bigint environment variable with optional fallback.
|
|
725
753
|
* Automatically converts string values to bigint.
|
|
754
|
+
* Accepts a single key or an ordered array of keys (first match wins).
|
|
726
755
|
*/
|
|
727
756
|
static getBigInt(key, def) {
|
|
728
757
|
return this._get(key, 3 /* BigInt */, def);
|
|
@@ -736,6 +765,7 @@ var PrimitiveMethods = class _PrimitiveMethods extends EnvironmentMethods {
|
|
|
736
765
|
/**
|
|
737
766
|
* Get a symbol environment variable with optional fallback.
|
|
738
767
|
* Creates a symbol from the string value.
|
|
768
|
+
* Accepts a single key or an ordered array of keys (first match wins).
|
|
739
769
|
*/
|
|
740
770
|
static getSymbol(key, def) {
|
|
741
771
|
return this._get(key, 4 /* Symbol */, def);
|
|
@@ -754,10 +784,10 @@ var AdvancedMethods = class _AdvancedMethods extends PrimitiveMethods {
|
|
|
754
784
|
__name(this, "AdvancedMethods");
|
|
755
785
|
}
|
|
756
786
|
static getUsing(key, converter, fallback) {
|
|
757
|
-
const
|
|
758
|
-
if (!
|
|
787
|
+
const { key: resolvedKey, value } = this.resolveKeyInput(key);
|
|
788
|
+
if (!value) return fallback;
|
|
759
789
|
const hasFallback = fallback !== void 0;
|
|
760
|
-
const result = this.parser.convertValue(
|
|
790
|
+
const result = this.parser.convertValue(resolvedKey, fallback, converter, hasFallback);
|
|
761
791
|
return result;
|
|
762
792
|
}
|
|
763
793
|
getUsing(key, converter, fallback) {
|
|
@@ -765,13 +795,14 @@ var AdvancedMethods = class _AdvancedMethods extends PrimitiveMethods {
|
|
|
765
795
|
}
|
|
766
796
|
/**
|
|
767
797
|
* Get an environment variable using a custom converter function.
|
|
798
|
+
* Accepts a single key or an ordered list for automatic fallback.
|
|
768
799
|
*/
|
|
769
800
|
static getWith(key, converter, fallback) {
|
|
770
|
-
const
|
|
771
|
-
if (!
|
|
801
|
+
const { key: resolvedKey, value } = this.resolveKeyInput(key);
|
|
802
|
+
if (!value) return fallback;
|
|
772
803
|
const hasFallback = fallback !== void 0;
|
|
773
804
|
const result = this.parser.convertValue(
|
|
774
|
-
|
|
805
|
+
resolvedKey,
|
|
775
806
|
fallback,
|
|
776
807
|
converter,
|
|
777
808
|
hasFallback
|
|
@@ -878,6 +909,7 @@ var Converters = /* @__PURE__ */ ((Converters2) => {
|
|
|
878
909
|
Converters2["Time"] = "time";
|
|
879
910
|
return Converters2;
|
|
880
911
|
})(Converters || {});
|
|
912
|
+
/* v8 ignore next -- @preserve */
|
|
881
913
|
|
|
882
914
|
export { Converters, Envapt, EnvaptError, EnvaptErrorCodes, Envapter, Environment };
|
|
883
915
|
//# sourceMappingURL=index.mjs.map
|