envapt 4.1.0 → 5.0.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/CHANGELOG.md +328 -0
- package/README.md +58 -946
- package/dist/Envapter-CBSM3v-5.cjs +3 -0
- package/dist/Envapter-CBSM3v-5.cjs.map +1 -0
- package/dist/Envapter-D8FEdzBR.mjs +3 -0
- package/dist/Envapter-D8FEdzBR.mjs.map +1 -0
- package/dist/config.cjs +2 -0
- package/dist/config.cjs.map +1 -0
- package/dist/config.d.cts +1 -0
- package/dist/config.d.mts +1 -0
- package/dist/config.mjs +2 -0
- package/dist/config.mjs.map +1 -0
- package/dist/index.cjs +1 -927
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +847 -401
- package/dist/index.d.cts.map +1 -0
- package/dist/index.d.mts +1127 -0
- package/dist/index.d.mts.map +1 -0
- package/dist/index.mjs +1 -915
- package/dist/index.mjs.map +1 -1
- package/package.json +52 -45
- package/dist/index.d.ts +0 -681
package/dist/index.d.ts
DELETED
|
@@ -1,681 +0,0 @@
|
|
|
1
|
-
import { DotenvConfigOptions } from 'dotenv';
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Enum for built-in converters
|
|
5
|
-
* @public
|
|
6
|
-
*/
|
|
7
|
-
declare enum Converters {
|
|
8
|
-
String = "string",
|
|
9
|
-
Number = "number",
|
|
10
|
-
Boolean = "boolean",
|
|
11
|
-
Bigint = "bigint",
|
|
12
|
-
Symbol = "symbol",
|
|
13
|
-
Integer = "integer",
|
|
14
|
-
Float = "float",
|
|
15
|
-
Json = "json",
|
|
16
|
-
Array = "array",
|
|
17
|
-
Url = "url",
|
|
18
|
-
Regexp = "regexp",
|
|
19
|
-
Date = "date",
|
|
20
|
-
Time = "time"
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Type alias for converter values to maintain compatibility with existing string-based API
|
|
24
|
-
* @internal
|
|
25
|
-
*/
|
|
26
|
-
type ConverterValue = `${Converters}`;
|
|
27
|
-
/**
|
|
28
|
-
* Valid array element converter types (excludes array, json, regexp)
|
|
29
|
-
* @internal
|
|
30
|
-
*/
|
|
31
|
-
type ArrayElementConverter = Exclude<Converters, Converters.Array | Converters.Json | Converters.Regexp>;
|
|
32
|
-
/**
|
|
33
|
-
* Type alias for array element converter values
|
|
34
|
-
* @internal
|
|
35
|
-
*/
|
|
36
|
-
type ArrayElementConverterValue = `${ArrayElementConverter}`;
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* User defined options for dotenv configuration
|
|
40
|
-
*
|
|
41
|
-
* "processEnv" and "path" are managed by Envapter and should not be included in user-defined config.
|
|
42
|
-
* @public
|
|
43
|
-
*/
|
|
44
|
-
type PermittedDotenvConfig = Omit<DotenvConfigOptions, 'processEnv' | 'path'>;
|
|
45
|
-
/**
|
|
46
|
-
* Built-in converter types for common environment variable patterns
|
|
47
|
-
* @public
|
|
48
|
-
*/
|
|
49
|
-
type BuiltInConverter = ConverterValue | Converters;
|
|
50
|
-
/**
|
|
51
|
-
* Primitive types supported by Envapter
|
|
52
|
-
* @public
|
|
53
|
-
*/
|
|
54
|
-
type PrimitiveConstructor = typeof String | typeof Number | typeof Boolean | typeof BigInt | typeof Symbol;
|
|
55
|
-
/**
|
|
56
|
-
* Valid array converter element types (excludes array, json, regexp)
|
|
57
|
-
* @public
|
|
58
|
-
*/
|
|
59
|
-
type ValidArrayConverterBuiltInType = ArrayElementConverterValue | ArrayElementConverter;
|
|
60
|
-
/**
|
|
61
|
-
* Array converter configuration for custom delimiters and element types
|
|
62
|
-
* @public
|
|
63
|
-
*/
|
|
64
|
-
interface ArrayConverter {
|
|
65
|
-
/**
|
|
66
|
-
* Delimiter to split the string by
|
|
67
|
-
*/
|
|
68
|
-
delimiter: string;
|
|
69
|
-
/**
|
|
70
|
-
* Type to convert each array element to (excludes array, json, and regexp types)
|
|
71
|
-
*/
|
|
72
|
-
type?: ArrayElementConverter | ArrayElementConverterValue;
|
|
73
|
-
}
|
|
74
|
-
/**
|
|
75
|
-
* String value from a .env file or environment variable
|
|
76
|
-
* @public
|
|
77
|
-
*/
|
|
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[]];
|
|
84
|
-
/**
|
|
85
|
-
* Custom parser function type for environment variables
|
|
86
|
-
* @param raw - Raw string value from environment
|
|
87
|
-
* @param fallback - Fallback value if parsing fails
|
|
88
|
-
* @returns Parsed value of type T
|
|
89
|
-
* @public
|
|
90
|
-
*/
|
|
91
|
-
type ConverterFunction<TFallback = unknown> = (raw: BaseInput, fallback?: TFallback) => TFallback;
|
|
92
|
-
/**
|
|
93
|
-
* Environment variable converter - can be a primitive constructor, built-in converter string, array converter object, or custom parser function
|
|
94
|
-
* @see {@link PrimitiveConstructor} for primitive types
|
|
95
|
-
* @see {@link Converters} for built-in types
|
|
96
|
-
* @see {@link ArrayConverter} for array converter configuration
|
|
97
|
-
* @see {@link ConverterFunction} for custom parser functions
|
|
98
|
-
* @public
|
|
99
|
-
*/
|
|
100
|
-
type EnvaptConverter<TFallback> = PrimitiveConstructor | Converters | ConverterValue | ArrayConverter | ConverterFunction<TFallback>;
|
|
101
|
-
/**
|
|
102
|
-
* Options for the \@Envapt decorator (modern API)
|
|
103
|
-
* @public
|
|
104
|
-
*/
|
|
105
|
-
interface EnvaptOptions<TFallback = string> {
|
|
106
|
-
/**
|
|
107
|
-
* Default value to use if environment variable is not found
|
|
108
|
-
*/
|
|
109
|
-
fallback?: TFallback;
|
|
110
|
-
/**
|
|
111
|
-
* Built-in converter, custom converter function, or boxed-primitives (String, Number, Boolean, Symbol, BigInt)
|
|
112
|
-
* @see {@link EnvaptConverter} for details
|
|
113
|
-
*/
|
|
114
|
-
converter?: EnvaptConverter<TFallback>;
|
|
115
|
-
}
|
|
116
|
-
type JsonPrimitive = string | number | boolean | null;
|
|
117
|
-
type JsonArray = JsonValue[];
|
|
118
|
-
interface JsonObject {
|
|
119
|
-
[key: string]: JsonValue;
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* JSON value types for custom converters
|
|
123
|
-
* @internal
|
|
124
|
-
*/
|
|
125
|
-
type JsonValue = JsonPrimitive | JsonArray | JsonObject;
|
|
126
|
-
interface ConverterMap {
|
|
127
|
-
string: string;
|
|
128
|
-
number: number;
|
|
129
|
-
boolean: boolean;
|
|
130
|
-
bigint: bigint;
|
|
131
|
-
symbol: symbol;
|
|
132
|
-
integer: number;
|
|
133
|
-
float: number;
|
|
134
|
-
json: JsonValue;
|
|
135
|
-
array: string[];
|
|
136
|
-
url: URL;
|
|
137
|
-
regexp: RegExp;
|
|
138
|
-
date: Date;
|
|
139
|
-
time: number;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* Type mapping for built-in converters to their return types
|
|
143
|
-
* @internal
|
|
144
|
-
*/
|
|
145
|
-
type BuiltInConverterReturnType<ConverterKey extends BuiltInConverter> = ConverterKey extends Converters ? ConverterMap[`${ConverterKey}`] : ConverterKey extends keyof ConverterMap ? ConverterMap[ConverterKey] : never;
|
|
146
|
-
/**
|
|
147
|
-
* Return type for built-in converter functions
|
|
148
|
-
* @internal
|
|
149
|
-
*/
|
|
150
|
-
type ReturnValuesOfConverterFunctions = ConverterMap[BuiltInConverter];
|
|
151
|
-
/**
|
|
152
|
-
* Function type for built-in converter functions
|
|
153
|
-
* @internal
|
|
154
|
-
*/
|
|
155
|
-
type BuiltInConverterFunction = (...args: Parameters<(...args: any[]) => ReturnValuesOfConverterFunctions>) => ReturnValuesOfConverterFunctions | undefined;
|
|
156
|
-
/**
|
|
157
|
-
* Map of built-in converter functions
|
|
158
|
-
* @internal
|
|
159
|
-
*/
|
|
160
|
-
type MapOfConverterFunctions = Record<BuiltInConverter, BuiltInConverterFunction>;
|
|
161
|
-
/**
|
|
162
|
-
* Time unit types for duration conversions
|
|
163
|
-
* @internal
|
|
164
|
-
*/
|
|
165
|
-
type TimeUnit = 'ms' | 's' | 'm' | 'h';
|
|
166
|
-
/**
|
|
167
|
-
* Helper type for getter methods that conditionally return undefined based on whether a fallback is provided
|
|
168
|
-
* If fallback is provided, return ReturnType. If no fallback (undefined), return ReturnType | undefined.
|
|
169
|
-
* @internal
|
|
170
|
-
*/
|
|
171
|
-
type ConditionalReturn<ReturnType, TFallback> = TFallback extends undefined ? ReturnType | undefined : ReturnType;
|
|
172
|
-
/**
|
|
173
|
-
* Advanced type inference for built-in and array converters
|
|
174
|
-
* Maps converter types to their expected return types
|
|
175
|
-
* @internal
|
|
176
|
-
*/
|
|
177
|
-
type InferConverterReturnType<TConverter extends BuiltInConverter | ArrayConverter> = TConverter extends BuiltInConverter ? BuiltInConverterReturnType<TConverter> : TConverter extends ArrayConverter ? TConverter['type'] extends BuiltInConverter ? BuiltInConverterReturnType<TConverter['type']>[] : string[] : unknown[];
|
|
178
|
-
/**
|
|
179
|
-
* Complete type inference for advanced converter methods
|
|
180
|
-
* @internal
|
|
181
|
-
*/
|
|
182
|
-
type AdvancedConverterReturn<TConverter extends BuiltInConverter | ArrayConverter, TFallback = undefined> = ConditionalReturn<InferConverterReturnType<TConverter>, TFallback>;
|
|
183
|
-
/**
|
|
184
|
-
* Type inference for primitive constructor return types
|
|
185
|
-
* @internal
|
|
186
|
-
*/
|
|
187
|
-
type InferPrimitiveReturnType<TConstructor extends PrimitiveConstructor> = TConstructor extends typeof String ? string : TConstructor extends typeof Number ? number : TConstructor extends typeof Boolean ? boolean : TConstructor extends typeof BigInt ? bigint : TConstructor extends typeof Symbol ? symbol : never;
|
|
188
|
-
/**
|
|
189
|
-
* Type inference for primitive fallback values
|
|
190
|
-
* @internal
|
|
191
|
-
*/
|
|
192
|
-
type InferPrimitiveFallbackType<TFallback extends string | number | boolean | bigint | symbol | undefined> = TFallback extends string ? string : TFallback extends number ? number : TFallback extends boolean ? boolean : TFallback extends bigint ? bigint : TFallback extends symbol ? symbol : undefined;
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Usage 1: Custom converter function with fallback provided
|
|
196
|
-
*
|
|
197
|
-
* @param key - Environment variable name to load (string or ordered array of strings)
|
|
198
|
-
* @param options - Configuration options with custom converter and required fallback
|
|
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
|
-
* ```
|
|
214
|
-
*/
|
|
215
|
-
declare function Envapt<TFallback>(key: EnvKeyInput, options: {
|
|
216
|
-
converter: (raw: string | undefined, fallback: TFallback) => TFallback;
|
|
217
|
-
fallback: TFallback;
|
|
218
|
-
}): PropertyDecorator;
|
|
219
|
-
/**
|
|
220
|
-
* Usage 2: Custom converter function without fallback
|
|
221
|
-
*
|
|
222
|
-
* @param key - Environment variable name(s) to load
|
|
223
|
-
* @param options - Configuration options with custom converter only
|
|
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
|
-
* ```
|
|
236
|
-
*/
|
|
237
|
-
declare function Envapt<TReturnType>(key: EnvKeyInput, options: {
|
|
238
|
-
converter: ConverterFunction<TReturnType>;
|
|
239
|
-
}): PropertyDecorator;
|
|
240
|
-
/**
|
|
241
|
-
* Usage 3: Built-in converter with optional fallback
|
|
242
|
-
*
|
|
243
|
-
* @param key - Environment variable name(s) to load
|
|
244
|
-
* @param options - Configuration options with built-in converter
|
|
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
|
-
* ```
|
|
264
|
-
*/
|
|
265
|
-
declare function Envapt<TConverter extends BuiltInConverter>(key: EnvKeyInput, options: {
|
|
266
|
-
converter: TConverter;
|
|
267
|
-
fallback?: InferConverterReturnType<TConverter> | undefined;
|
|
268
|
-
}): PropertyDecorator;
|
|
269
|
-
/**
|
|
270
|
-
* Usage 4: Array converter with optional fallback
|
|
271
|
-
*
|
|
272
|
-
* @param key - Environment variable name(s) to load
|
|
273
|
-
* @param options - Configuration options with array converter
|
|
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
|
-
* ```
|
|
295
|
-
*/
|
|
296
|
-
declare function Envapt<TConverter extends ArrayConverter>(key: EnvKeyInput, options: {
|
|
297
|
-
converter: TConverter;
|
|
298
|
-
fallback?: InferConverterReturnType<TConverter> | undefined;
|
|
299
|
-
}): PropertyDecorator;
|
|
300
|
-
/**
|
|
301
|
-
* Usage 5: Primitive constructor with optional fallback
|
|
302
|
-
*
|
|
303
|
-
* @param key - Environment variable name(s) to load
|
|
304
|
-
* @param options - Configuration options with primitive constructor
|
|
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
|
-
* ```
|
|
317
|
-
*/
|
|
318
|
-
declare function Envapt<TConstructor extends PrimitiveConstructor>(key: EnvKeyInput, options: {
|
|
319
|
-
converter: TConstructor;
|
|
320
|
-
fallback?: InferPrimitiveReturnType<TConstructor>;
|
|
321
|
-
}): PropertyDecorator;
|
|
322
|
-
/**
|
|
323
|
-
* Usage 6: Fallback only (no converter)
|
|
324
|
-
*
|
|
325
|
-
* @param key - Environment variable name(s) to load
|
|
326
|
-
* @param options - Configuration options with fallback only
|
|
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
|
-
* ```
|
|
339
|
-
*/
|
|
340
|
-
declare function Envapt<TFallback>(key: EnvKeyInput, options: {
|
|
341
|
-
fallback: TFallback;
|
|
342
|
-
converter?: undefined;
|
|
343
|
-
}): PropertyDecorator;
|
|
344
|
-
/**
|
|
345
|
-
* Classic API: No fallback
|
|
346
|
-
*
|
|
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
|
-
* ```
|
|
356
|
-
*/
|
|
357
|
-
declare function Envapt<_TReturnType = string | null>(key: EnvKeyInput): PropertyDecorator;
|
|
358
|
-
/**
|
|
359
|
-
* Classic API: Primitive fallback only
|
|
360
|
-
*
|
|
361
|
-
* @param key - Environment variable name(s) to load
|
|
362
|
-
* @param fallback - Default primitive value
|
|
363
|
-
* @param converter - Optional primitive constructor (String, Number, etc.)
|
|
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
|
-
* ```
|
|
378
|
-
*/
|
|
379
|
-
declare function Envapt<TFallback extends string | number | boolean | bigint | symbol | undefined>(key: EnvKeyInput, fallback: InferPrimitiveFallbackType<TFallback>, converter?: PrimitiveConstructor): PropertyDecorator;
|
|
380
|
-
|
|
381
|
-
/**
|
|
382
|
-
* @internal
|
|
383
|
-
*/
|
|
384
|
-
interface EnvapterService {
|
|
385
|
-
getRaw(key: EnvKeyInput): string | undefined;
|
|
386
|
-
get(key: EnvKeyInput, def?: string): string | undefined;
|
|
387
|
-
}
|
|
388
|
-
/**
|
|
389
|
-
* Parser class for handling environment variable template resolution and type conversion
|
|
390
|
-
* @internal
|
|
391
|
-
*/
|
|
392
|
-
declare class Parser {
|
|
393
|
-
private readonly envService;
|
|
394
|
-
private readonly TEMPLATE_REGEX;
|
|
395
|
-
constructor(envService: EnvapterService);
|
|
396
|
-
/**
|
|
397
|
-
* Resolve template variables in a string while handling circular references and missing variables
|
|
398
|
-
* @internal
|
|
399
|
-
*/
|
|
400
|
-
resolveTemplate(key: string, value: string, stack?: Set<string>): string;
|
|
401
|
-
convertValue<TFallback>(key: EnvKeyInput, fallback: TFallback | undefined, converter: EnvaptConverter<TFallback> | undefined, hasFallback: boolean): TFallback | null | undefined;
|
|
402
|
-
private processFallbackForConverter;
|
|
403
|
-
private convertPrimitiveToString;
|
|
404
|
-
private processBuiltInConverter;
|
|
405
|
-
private processArrayConverter;
|
|
406
|
-
private processCustomConverter;
|
|
407
|
-
private resolveConverter;
|
|
408
|
-
}
|
|
409
|
-
|
|
410
|
-
/**
|
|
411
|
-
* Base class for environment variable management
|
|
412
|
-
* Handles configuration, caching, and basic environment loading
|
|
413
|
-
* @internal
|
|
414
|
-
*/
|
|
415
|
-
declare abstract class EnvapterBase {
|
|
416
|
-
protected static _envPaths: string[];
|
|
417
|
-
protected static _userDefinedDotenvConfig: PermittedDotenvConfig;
|
|
418
|
-
/**
|
|
419
|
-
* Set custom .env file paths. Accepts either a single path or array of paths.
|
|
420
|
-
* Setting new paths clears the cache and reloads environment variables.
|
|
421
|
-
*/
|
|
422
|
-
static set envPaths(paths: string[] | string);
|
|
423
|
-
/**
|
|
424
|
-
* Get currently configured .env file paths
|
|
425
|
-
*/
|
|
426
|
-
static get envPaths(): string[];
|
|
427
|
-
/**
|
|
428
|
-
* Set custom dotenv configuration options.
|
|
429
|
-
*/
|
|
430
|
-
static set dotenvConfig(config: PermittedDotenvConfig);
|
|
431
|
-
/**
|
|
432
|
-
* Get current dotenv configuration options
|
|
433
|
-
*/
|
|
434
|
-
static get dotenvConfig(): PermittedDotenvConfig;
|
|
435
|
-
protected static refreshCache(): void;
|
|
436
|
-
protected static resolveKeyInput(keyInput: EnvKeyInput): {
|
|
437
|
-
key: string;
|
|
438
|
-
value: string | undefined;
|
|
439
|
-
};
|
|
440
|
-
protected static get config(): Map<string, unknown>;
|
|
441
|
-
/**
|
|
442
|
-
* Get raw environment variable value without parsing or conversion.
|
|
443
|
-
*/
|
|
444
|
-
getRaw(key: EnvKeyInput): string | undefined;
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
/**
|
|
448
|
-
* Environment types supported by Envapter
|
|
449
|
-
* @public
|
|
450
|
-
*/
|
|
451
|
-
declare enum Environment {
|
|
452
|
-
Development = 0,
|
|
453
|
-
Staging = 1,
|
|
454
|
-
Production = 2
|
|
455
|
-
}
|
|
456
|
-
/**
|
|
457
|
-
* Mixin for environment detection and checking methods
|
|
458
|
-
* @internal
|
|
459
|
-
*/
|
|
460
|
-
declare class EnvironmentMethods extends EnvapterBase {
|
|
461
|
-
protected static _environment: Environment | undefined;
|
|
462
|
-
protected static determineEnvironment(env?: string | Environment): void;
|
|
463
|
-
private static getRawValue;
|
|
464
|
-
/**
|
|
465
|
-
* Get the current application environment
|
|
466
|
-
*/
|
|
467
|
-
static get environment(): Environment;
|
|
468
|
-
/**
|
|
469
|
-
* Set the application environment. Accepts either Environment enum or string value.
|
|
470
|
-
*/
|
|
471
|
-
static set environment(env: string | Environment);
|
|
472
|
-
/**
|
|
473
|
-
* @see {@link EnvironmentMethods.environment}
|
|
474
|
-
*/
|
|
475
|
-
get environment(): Environment;
|
|
476
|
-
/**
|
|
477
|
-
* @see {@link EnvironmentMethods.environment}
|
|
478
|
-
*/
|
|
479
|
-
set environment(env: string | Environment);
|
|
480
|
-
/**
|
|
481
|
-
* Check if the current environment is production
|
|
482
|
-
*/
|
|
483
|
-
static get isProduction(): boolean;
|
|
484
|
-
/**
|
|
485
|
-
* @see {@link EnvironmentMethods.isProduction}
|
|
486
|
-
*/
|
|
487
|
-
get isProduction(): boolean;
|
|
488
|
-
/**
|
|
489
|
-
* Check if the current environment is staging
|
|
490
|
-
*/
|
|
491
|
-
static get isStaging(): boolean;
|
|
492
|
-
/**
|
|
493
|
-
* @see {@link EnvironmentMethods.isStaging}
|
|
494
|
-
*/
|
|
495
|
-
get isStaging(): boolean;
|
|
496
|
-
/**
|
|
497
|
-
* Check if the current environment is development
|
|
498
|
-
*/
|
|
499
|
-
static get isDevelopment(): boolean;
|
|
500
|
-
/**
|
|
501
|
-
* @see {@link EnvironmentMethods.isDevelopment}
|
|
502
|
-
*/
|
|
503
|
-
get isDevelopment(): boolean;
|
|
504
|
-
protected static refreshCache(): void;
|
|
505
|
-
}
|
|
506
|
-
|
|
507
|
-
/**
|
|
508
|
-
* Mixin for primitive environment variable getter methods
|
|
509
|
-
* @internal
|
|
510
|
-
*/
|
|
511
|
-
declare class PrimitiveMethods extends EnvironmentMethods implements EnvapterService {
|
|
512
|
-
protected static readonly parser: Parser;
|
|
513
|
-
private static _get;
|
|
514
|
-
/**
|
|
515
|
-
* Get a string environment variable with optional fallback.
|
|
516
|
-
* Supports template variable resolution using `${VAR}` syntax.
|
|
517
|
-
* Accepts a single key or an ordered array of keys (first match wins).
|
|
518
|
-
*/
|
|
519
|
-
static get<Default extends string | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<string, Default>;
|
|
520
|
-
/**
|
|
521
|
-
* @see {@link PrimitiveMethods.get}
|
|
522
|
-
*/
|
|
523
|
-
get(key: EnvKeyInput, def?: string): string | undefined;
|
|
524
|
-
/**
|
|
525
|
-
* Get a number environment variable with optional fallback.
|
|
526
|
-
* Automatically converts string values to numbers.
|
|
527
|
-
* Accepts a single key or an ordered array of keys (first match wins).
|
|
528
|
-
*/
|
|
529
|
-
static getNumber<Default extends number | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<number, Default>;
|
|
530
|
-
/**
|
|
531
|
-
* @see {@link PrimitiveMethods.getNumber}
|
|
532
|
-
*/
|
|
533
|
-
getNumber<Default extends number | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<number, Default>;
|
|
534
|
-
/**
|
|
535
|
-
* Get a boolean environment variable with optional fallback.
|
|
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).
|
|
538
|
-
*/
|
|
539
|
-
static getBoolean<Default extends boolean | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<boolean, Default>;
|
|
540
|
-
/**
|
|
541
|
-
* @see {@link PrimitiveMethods.getBoolean}
|
|
542
|
-
*/
|
|
543
|
-
getBoolean<Default extends boolean | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<boolean, Default>;
|
|
544
|
-
/**
|
|
545
|
-
* Get a bigint environment variable with optional fallback.
|
|
546
|
-
* Automatically converts string values to bigint.
|
|
547
|
-
* Accepts a single key or an ordered array of keys (first match wins).
|
|
548
|
-
*/
|
|
549
|
-
static getBigInt<Default extends bigint | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<bigint, Default>;
|
|
550
|
-
/**
|
|
551
|
-
* @see {@link PrimitiveMethods.getBigInt}
|
|
552
|
-
*/
|
|
553
|
-
getBigInt<Default extends bigint | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<bigint, Default>;
|
|
554
|
-
/**
|
|
555
|
-
* Get a symbol environment variable with optional fallback.
|
|
556
|
-
* Creates a symbol from the string value.
|
|
557
|
-
* Accepts a single key or an ordered array of keys (first match wins).
|
|
558
|
-
*/
|
|
559
|
-
static getSymbol<Default extends symbol | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<symbol, Default>;
|
|
560
|
-
/**
|
|
561
|
-
* @see {@link PrimitiveMethods.getSymbol}
|
|
562
|
-
*/
|
|
563
|
-
getSymbol<Default extends symbol | undefined = undefined>(key: EnvKeyInput, def?: Default): ConditionalReturn<symbol, Default>;
|
|
564
|
-
}
|
|
565
|
-
|
|
566
|
-
/**
|
|
567
|
-
* Mixin for advanced methods for environment variable conversion using built-in and custom converters
|
|
568
|
-
* @internal
|
|
569
|
-
*/
|
|
570
|
-
declare class AdvancedMethods extends PrimitiveMethods {
|
|
571
|
-
/**
|
|
572
|
-
* Get an environment variable using a built-in converter.
|
|
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.
|
|
575
|
-
*/
|
|
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;
|
|
578
|
-
/**
|
|
579
|
-
* @see {@link AdvancedMethods.getUsing}
|
|
580
|
-
*/
|
|
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;
|
|
583
|
-
/**
|
|
584
|
-
* Get an environment variable using a custom converter function.
|
|
585
|
-
* Accepts a single key or an ordered list for automatic fallback.
|
|
586
|
-
*/
|
|
587
|
-
static getWith<TReturnType, TFallback extends TReturnType | undefined = undefined>(key: EnvKeyInput, converter: ConverterFunction<TReturnType>, fallback?: TFallback): ConditionalReturn<TReturnType, TFallback>;
|
|
588
|
-
/**
|
|
589
|
-
* @see {@link AdvancedMethods.getWith}
|
|
590
|
-
*/
|
|
591
|
-
getWith<TReturnType, TFallback extends TReturnType | undefined = undefined>(key: EnvKeyInput, converter: ConverterFunction<TReturnType>, fallback?: TFallback): ConditionalReturn<TReturnType, TFallback>;
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
/**
|
|
595
|
-
* Main configuration class for environment variable management.
|
|
596
|
-
*
|
|
597
|
-
* Provides both static and instance methods for retrieving typed environment variables
|
|
598
|
-
* with support for template resolution, multiple .env files, and environment detection.
|
|
599
|
-
*
|
|
600
|
-
* Extend your own classes from this to define properties with \@Envapt decorators and provide access to environment variables methods.
|
|
601
|
-
*
|
|
602
|
-
* @example
|
|
603
|
-
* ```ts
|
|
604
|
-
* // Static usage
|
|
605
|
-
* const port = Envapter.getNumber('PORT', 3000);
|
|
606
|
-
* const url = Envapter.get('API_URL', 'http://localhost');
|
|
607
|
-
* const replica = Envapter.get(['READONLY_URL', 'DATABASE_URL'], 'sqlite://memory');
|
|
608
|
-
*
|
|
609
|
-
* // Instance usage
|
|
610
|
-
* const env = new Envapter();
|
|
611
|
-
* const dbUrl = env.get('DATABASE_URL', 'sqlite://memory');
|
|
612
|
-
* const primaryHost = env.get(['PRIMARY_HOST', 'SECONDARY_HOST']);
|
|
613
|
-
* ```
|
|
614
|
-
*
|
|
615
|
-
* @public
|
|
616
|
-
*/
|
|
617
|
-
declare class Envapter extends AdvancedMethods {
|
|
618
|
-
/**
|
|
619
|
-
* Tagged template literal for resolving environment variables in template strings.
|
|
620
|
-
*
|
|
621
|
-
* @example
|
|
622
|
-
* ```ts
|
|
623
|
-
* // Given API_HOST=api.example.com and API_PORT=8080 in environment
|
|
624
|
-
* const endpoint = Envapter.resolve`Connecting to ${'API_HOST'}:${'API_PORT'}`;
|
|
625
|
-
* // Returns: "Connecting to api.example.com:8080"
|
|
626
|
-
*
|
|
627
|
-
* // Works with template variables in .env too:
|
|
628
|
-
* // API_URL=https://${API_HOST}:${API_PORT}
|
|
629
|
-
* const message = Envapter.resolve`Service endpoint: ${'API_URL'}`;
|
|
630
|
-
* // Returns: "Service endpoint: https://api.example.com:8080"
|
|
631
|
-
* ```
|
|
632
|
-
*/
|
|
633
|
-
static resolve(strings: TemplateStringsArray, ...keys: string[]): string;
|
|
634
|
-
/**
|
|
635
|
-
* @see {@link Envapter.resolve}
|
|
636
|
-
*/
|
|
637
|
-
resolve(strings: TemplateStringsArray, ...keys: string[]): string;
|
|
638
|
-
}
|
|
639
|
-
|
|
640
|
-
declare enum EnvaptErrorCodes {
|
|
641
|
-
/** Thrown when an invalid fallback value is provided */
|
|
642
|
-
InvalidFallback = 101,
|
|
643
|
-
/** Thrown when fallback value type doesn't match expected converter type */
|
|
644
|
-
InvalidFallbackType = 102,
|
|
645
|
-
/** Thrown when array fallback contains elements of wrong type */
|
|
646
|
-
ArrayFallbackElementTypeMismatch = 103,
|
|
647
|
-
/** Thrown when fallback type doesn't match the specified converter */
|
|
648
|
-
FallbackConverterTypeMismatch = 104,
|
|
649
|
-
/** Thrown when invalid array converter configuration is provided */
|
|
650
|
-
InvalidArrayConverterType = 201,
|
|
651
|
-
/** Thrown when an invalid built-in converter is specified */
|
|
652
|
-
InvalidBuiltInConverter = 202,
|
|
653
|
-
/** Thrown when a custom converter is not a function */
|
|
654
|
-
InvalidCustomConverter = 203,
|
|
655
|
-
/** Thrown when converter type is not recognized */
|
|
656
|
-
InvalidConverterType = 204,
|
|
657
|
-
/** Thrown when primitive type coercion on fallback value fails */
|
|
658
|
-
PrimitiveCoercionFailed = 205,
|
|
659
|
-
/** Thrown when delimiter is missing in array converter configuration */
|
|
660
|
-
MissingDelimiter = 301,
|
|
661
|
-
/** Thrown when invalid user-defined configuration is provided */
|
|
662
|
-
InvalidUserDefinedConfig = 302,
|
|
663
|
-
/** Thrown when specified environment files don't exist */
|
|
664
|
-
EnvFilesNotFound = 303,
|
|
665
|
-
/** Thrown when no valid environment key is provided */
|
|
666
|
-
InvalidKeyInput = 304
|
|
667
|
-
}
|
|
668
|
-
/**
|
|
669
|
-
* Custom error for better DX and debugging when using Envapt.
|
|
670
|
-
*
|
|
671
|
-
* @example
|
|
672
|
-
* ```ts
|
|
673
|
-
* throw new EnvaptError(EnvaptErrorCode.InvalidFallback, "Invalid fallback value provided for environment variable.");
|
|
674
|
-
* ```
|
|
675
|
-
*/
|
|
676
|
-
declare class EnvaptError extends Error {
|
|
677
|
-
readonly code: EnvaptErrorCodes;
|
|
678
|
-
constructor(code: EnvaptErrorCodes, message: string);
|
|
679
|
-
}
|
|
680
|
-
|
|
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 };
|