envapt 1.1.0 → 2.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/dist/index.d.mts CHANGED
@@ -1,20 +1,62 @@
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>;
1
32
  /**
2
- * List of built-in converters for Envapt.
33
+ * Type alias for array element converter values
3
34
  * @internal
4
35
  */
5
- declare const ListOfBuiltInConverters: readonly ["string", "number", "boolean", "bigint", "symbol", "integer", "float", "json", "array", "url", "regexp", "date", "time"];
36
+ type ArrayElementConverterValue = `${ArrayElementConverter}`;
6
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'>;
7
45
  /**
8
46
  * Built-in converter types for common environment variable patterns
9
47
  * @public
10
48
  */
11
- type BuiltInConverter = (typeof ListOfBuiltInConverters)[number];
49
+ type BuiltInConverter = ConverterValue | Converters;
12
50
  /**
13
51
  * Primitive types supported by Envapter
14
52
  * @public
15
53
  */
16
54
  type PrimitiveConstructor = typeof String | typeof Number | typeof Boolean | typeof BigInt | typeof Symbol;
17
- type ValidArrayConverterBuiltInType = Exclude<BuiltInConverter, 'array' | 'json' | 'regexp'>;
55
+ /**
56
+ * Valid array converter element types (excludes array, json, regexp)
57
+ * @public
58
+ */
59
+ type ValidArrayConverterBuiltInType = ArrayElementConverterValue | ArrayElementConverter;
18
60
  /**
19
61
  * Array converter configuration for custom delimiters and element types
20
62
  * @public
@@ -27,7 +69,7 @@ interface ArrayConverter {
27
69
  /**
28
70
  * Type to convert each array element to (excludes array, json, and regexp types)
29
71
  */
30
- type?: ValidArrayConverterBuiltInType;
72
+ type?: ArrayElementConverter | ArrayElementConverterValue;
31
73
  }
32
74
  /**
33
75
  * String value from a .env file or environment variable
@@ -41,30 +83,41 @@ type BaseInput = string | undefined;
41
83
  * @returns Parsed value of type T
42
84
  * @public
43
85
  */
44
- type ConverterFunction<FallbackType = unknown> = (raw: BaseInput, fallback?: FallbackType) => FallbackType;
86
+ type ConverterFunction<TFallback = unknown> = (raw: BaseInput, fallback?: TFallback) => TFallback;
45
87
  /**
46
88
  * Environment variable converter - can be a primitive constructor, built-in converter string, array converter object, or custom parser function
47
89
  * @see {@link PrimitiveConstructor} for primitive types
48
- * @see {@link BuiltInConverter} for built-in types
90
+ * @see {@link Converters} for built-in types
49
91
  * @see {@link ArrayConverter} for array converter configuration
50
92
  * @see {@link ConverterFunction} for custom parser functions
51
93
  * @public
52
94
  */
53
- type EnvaptConverter<FallbackType> = PrimitiveConstructor | BuiltInConverter | ArrayConverter | ConverterFunction<FallbackType>;
95
+ type EnvaptConverter<TFallback> = PrimitiveConstructor | Converters | ConverterValue | ArrayConverter | ConverterFunction<TFallback>;
54
96
  /**
55
97
  * Options for the \@Envapt decorator (modern API)
56
98
  * @public
57
99
  */
58
- interface EnvaptOptions<FallbackType = string> {
100
+ interface EnvaptOptions<TFallback = string> {
59
101
  /**
60
102
  * Default value to use if environment variable is not found
61
103
  */
62
- fallback?: FallbackType;
104
+ fallback?: TFallback;
63
105
  /**
64
- * Built-in converter, custom converter function, or boxed-primitives (String, Number, Boolean)
106
+ * Built-in converter, custom converter function, or boxed-primitives (String, Number, Boolean, Symbol, BigInt)
107
+ * @see {@link EnvaptConverter} for details
65
108
  */
66
- converter?: EnvaptConverter<FallbackType>;
109
+ converter?: EnvaptConverter<TFallback>;
67
110
  }
111
+ type JsonPrimitive = string | number | boolean | null;
112
+ type JsonArray = JsonValue[];
113
+ interface JsonObject {
114
+ [key: string]: JsonValue;
115
+ }
116
+ /**
117
+ * JSON value types for custom converters
118
+ * @internal
119
+ */
120
+ type JsonValue = JsonPrimitive | JsonArray | JsonObject;
68
121
  interface ConverterMap {
69
122
  string: string;
70
123
  number: number;
@@ -73,7 +126,7 @@ interface ConverterMap {
73
126
  symbol: symbol;
74
127
  integer: number;
75
128
  float: number;
76
- json: unknown;
129
+ json: JsonValue;
77
130
  array: string[];
78
131
  url: URL;
79
132
  regexp: RegExp;
@@ -82,249 +135,391 @@ interface ConverterMap {
82
135
  }
83
136
  /**
84
137
  * Type mapping for built-in converters to their return types
138
+ * @internal
139
+ */
140
+ type BuiltInConverterReturnType<ConverterKey extends BuiltInConverter> = ConverterKey extends Converters ? ConverterMap[`${ConverterKey}`] : ConverterKey extends keyof ConverterMap ? ConverterMap[ConverterKey] : never;
141
+ /**
142
+ * Return type for built-in converter functions
143
+ * @internal
85
144
  */
86
- type BuiltInConverterReturnType<ConverterKey extends BuiltInConverter = BuiltInConverter> = ConverterMap[ConverterKey];
145
+ type ReturnValuesOfConverterFunctions = ConverterMap[BuiltInConverter];
146
+ /**
147
+ * Function type for built-in converter functions
148
+ * @internal
149
+ */
150
+ type BuiltInConverterFunction = (...args: Parameters<(...args: any[]) => ReturnValuesOfConverterFunctions>) => ReturnValuesOfConverterFunctions | undefined;
151
+ /**
152
+ * Map of built-in converter functions
153
+ * @internal
154
+ */
155
+ type MapOfConverterFunctions = Record<BuiltInConverter, BuiltInConverterFunction>;
156
+ /**
157
+ * Time unit types for duration conversions
158
+ * @internal
159
+ */
160
+ type TimeUnit = 'ms' | 's' | 'm' | 'h';
161
+ /**
162
+ * Helper type for getter methods that conditionally return undefined based on whether a fallback is provided
163
+ * If fallback is provided, return ReturnType. If no fallback (undefined), return ReturnType | undefined.
164
+ * @internal
165
+ */
166
+ type ConditionalReturn<ReturnType, TFallback> = TFallback extends undefined ? ReturnType | undefined : ReturnType;
167
+ /**
168
+ * Advanced type inference for built-in and array converters
169
+ * Maps converter types to their expected return types
170
+ * @internal
171
+ */
172
+ type InferConverterReturnType<TConverter extends BuiltInConverter | ArrayConverter> = TConverter extends BuiltInConverter ? BuiltInConverterReturnType<TConverter> : TConverter extends ArrayConverter ? TConverter['type'] extends BuiltInConverter ? BuiltInConverterReturnType<TConverter['type']>[] : string[] : unknown[];
173
+ /**
174
+ * Complete type inference for advanced converter methods
175
+ * @internal
176
+ */
177
+ type AdvancedConverterReturn<TConverter extends BuiltInConverter | ArrayConverter, TFallback = undefined> = ConditionalReturn<InferConverterReturnType<TConverter>, TFallback>;
178
+ /**
179
+ * Type inference for primitive constructor return types
180
+ * @internal
181
+ */
182
+ 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;
183
+ /**
184
+ * Type inference for primitive fallback values
185
+ * @internal
186
+ */
187
+ 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;
87
188
 
88
189
  /**
89
- * Instance/Static Property decorator that automatically loads and converts environment variables.
90
- *
91
- * Supports both modern options-based API and classic parameter-based API.
92
- * Automatically detects types from fallback values and provides caching for performance.
93
- *
94
- * Note: Using \@Envapt on a variable for an env that doesn't exist will set the value to `null` if no fallback is provided, regardless of a converter being used.
190
+ * Usage 1: Custom converter function with fallback provided
95
191
  *
96
192
  * @param key - Environment variable name to load
97
- * @param options - Configuration options (modern API)
98
- *
99
- * @example Modern API (recommended):
100
- * ```ts
101
- * class Config extends Envapter {
102
- * ‎ ‎ \@Envapt('PORT', { fallback: 3000 })
103
- * ‎ ‎ static readonly port: number;
104
- *
105
- * ‎ \@Envapt('FEATURES', { fallback: ['auth'], converter: 'array' })
106
- * ‎ ‎ declare private readonly features: string[];
193
+ * @param options - Configuration options with custom converter and required fallback
194
+ * @public
195
+ */
196
+ declare function Envapt<TFallback>(key: string, options: {
197
+ converter: (raw: string | undefined, fallback: TFallback) => TFallback;
198
+ fallback: TFallback;
199
+ }): PropertyDecorator;
200
+ /**
201
+ * Usage 2: Custom converter function without fallback
107
202
  *
108
- * \@Envapt('CONFIG', { converter: 'json' })
109
- * static readonly config: object;
203
+ * @param key - Environment variable name to load
204
+ * @param options - Configuration options with custom converter only
205
+ * @public
206
+ */
207
+ declare function Envapt<TReturnType>(key: string, options: {
208
+ converter: ConverterFunction<TReturnType>;
209
+ }): PropertyDecorator;
210
+ /**
211
+ * Usage 3: Built-in converter with optional fallback
110
212
  *
111
- * \@Envapt('API_URL', { converter: 'url' })
112
- * declare public readonly apiUrl: URL;
213
+ * @param key - Environment variable name to load
214
+ * @param options - Configuration options with built-in converter
215
+ * @public
216
+ */
217
+ declare function Envapt<TConverter extends BuiltInConverter>(key: string, options: {
218
+ converter: TConverter;
219
+ fallback?: InferConverterReturnType<TConverter>;
220
+ }): PropertyDecorator;
221
+ /**
222
+ * Usage 4: Array converter with optional fallback
113
223
  *
114
- * \@Envapt('CUSTOM_LIST', {
115
- * fallback: [1, 2, 3],
116
- * ‎ ‎ ‎ ‎ converter: {
117
- * ‎ ‎ ‎ ‎ delimiter: ',',
118
- * ‎ ‎ type: 'number' // Convert each item to number
119
- * ‎ ‎ ‎ ‎ }
120
- * ‎ ‎ })
121
- * ‎ ‎ static readonly customList: string[];
122
- * }
123
- * ```
224
+ * @param key - Environment variable name to load
225
+ * @param options - Configuration options with array converter
226
+ * @public
227
+ */
228
+ declare function Envapt<TConverter extends ArrayConverter>(key: string, options: {
229
+ converter: TConverter;
230
+ fallback?: InferConverterReturnType<TConverter>;
231
+ }): PropertyDecorator;
232
+ /**
233
+ * Usage 5: Primitive constructor with optional fallback
124
234
  *
125
235
  * @param key - Environment variable name to load
126
- * @param fallback - Default value if variable is not found. Only suppoerts primitive types (string, number, boolean, bigint, symbol).
127
- * @param converter - Custom converter function or built-in converter
236
+ * @param options - Configuration options with primitive constructor
237
+ * @public
238
+ */
239
+ declare function Envapt<TConstructor extends PrimitiveConstructor>(key: string, options: {
240
+ converter: TConstructor;
241
+ fallback?: InferPrimitiveReturnType<TConstructor>;
242
+ }): PropertyDecorator;
243
+ /**
244
+ * Usage 6: Fallback only (no converter)
128
245
  *
129
- * @example Classic API
130
- * ```ts
131
- * class HealthCheck {
132
- * ‎ ‎ \@Envapt('PORT', 3000) declare readonly port: number;
246
+ * @param key - Environment variable name to load
247
+ * @param options - Configuration options with fallback only
248
+ * @public
249
+ */
250
+ declare function Envapt<TFallback>(key: string, options: {
251
+ fallback: TFallback;
252
+ converter?: undefined;
253
+ }): PropertyDecorator;
254
+ /**
255
+ * Classic API: No fallback
133
256
  *
134
- * // code to start server
135
- * ‎ ‎ public start() {
136
- * // works on instance properties
137
- * ‎ ‎ ‎ ‎ console.log(`Server running on port ${this.port}`);
138
- * }
139
- * }
140
- * ```
257
+ * @param key - Environment variable name to load
258
+ */
259
+ declare function Envapt<_TReturnType = string | null>(key: string): PropertyDecorator;
260
+ /**
261
+ * Classic API: Primitive fallback only
141
262
  *
263
+ * @param key - Environment variable name to load
264
+ * @param fallback - Default primitive value
265
+ * @param converter - Optional primitive constructor (String, Number, etc.)
142
266
  * @public
143
267
  */
144
- declare function Envapt<FallbackType = unknown>(key: string, options?: EnvaptOptions<FallbackType>): PropertyDecorator;
145
- declare function Envapt<FallbackType = string | number | boolean | bigint | symbol>(key: string, fallback?: FallbackType, converter?: EnvaptConverter<FallbackType>): PropertyDecorator;
268
+ declare function Envapt<TFallback extends string | number | boolean | bigint | symbol | undefined>(key: string, fallback: InferPrimitiveFallbackType<TFallback>, converter?: PrimitiveConstructor): PropertyDecorator;
146
269
 
147
270
  /**
148
271
  * @internal
149
272
  */
150
273
  interface EnvapterService {
151
274
  getRaw(key: string): string | undefined;
152
- get(key: string, def?: string): string;
153
- getNumber(key: string, def?: number): number;
154
- getBoolean(key: string, def?: boolean): boolean;
275
+ get(key: string, def?: string): string | undefined;
276
+ getNumber(key: string, def?: number): number | undefined;
277
+ getBoolean(key: string, def?: boolean): boolean | undefined;
155
278
  }
156
-
157
279
  /**
158
- * Environment types supported by Envapter
159
- * @public
280
+ * Parser class for handling environment variable template resolution and type conversion
281
+ * @internal
160
282
  */
161
- declare enum Environment {
162
- Development = 0,
163
- Staging = 1,
164
- Production = 2
283
+ declare class Parser {
284
+ private readonly envService;
285
+ private readonly TEMPLATE_REGEX;
286
+ constructor(envService: EnvapterService);
287
+ /**
288
+ * Resolve template variables in a string while handling circular references and missing variables
289
+ * @internal
290
+ */
291
+ resolveTemplate(key: string, value: string, stack?: Set<string>): string;
292
+ convertValue<TFallback>(key: string, fallback: TFallback | undefined, converter: EnvaptConverter<TFallback> | undefined, hasFallback: boolean): TFallback | null | undefined;
293
+ private processFallbackForConverter;
294
+ private convertPrimitiveToString;
295
+ private processBuiltInConverter;
296
+ private processArrayConverter;
297
+ private processCustomConverter;
298
+ private resolveConverter;
165
299
  }
300
+
166
301
  /**
167
- * Main configuration class for environment variable management.
168
- *
169
- * Provides both static and instance methods for retrieving typed environment variables
170
- * with support for template resolution, multiple .env files, and environment detection.
171
- *
172
- * Extend your own classes from this to define properties with \@Envapt decorators and provide access to environment variables methods.
173
- *
174
- * @example
175
- * ```ts
176
- * // Static usage
177
- * const port = Envapter.getNumber('PORT', 3000);
178
- * const url = Envapter.get('API_URL', 'http://localhost');
179
- *
180
- * // Instance usage
181
- * const env = new Envapter();
182
- * const dbUrl = env.get('DATABASE_URL', 'sqlite://memory');
183
- * ```
184
- *
185
- * @public
302
+ * Base class for environment variable management
303
+ * Handles configuration, caching, and basic environment loading
304
+ * @internal
186
305
  */
187
- declare class Envapter implements EnvapterService {
188
- private static readonly parser;
189
- private static _envPaths;
190
- private static internalEnvironment;
306
+ declare abstract class EnvapterBase {
307
+ protected static _envPaths: string[];
308
+ protected static _userDefinedDotenvConfig: PermittedDotenvConfig;
191
309
  /**
192
310
  * Set custom .env file paths. Accepts either a single path or array of paths.
193
311
  * Setting new paths clears the cache and reloads environment variables.
194
- *
195
- * @param paths - Single file path or array of file paths to load
196
- *
197
- * @example
198
- * ```ts
199
- * // Single file
200
- * Envapter.envPaths = '.env.production';
201
- *
202
- * // Multiple files (loaded in order)
203
- * Envapter.envPaths = ['.env', '.env.local', '.env.production'];
204
- * ```
205
312
  */
206
313
  static set envPaths(paths: string[] | string);
207
314
  /**
208
315
  * Get currently configured .env file paths
209
- * @returns Array of file paths being loaded
210
316
  */
211
317
  static get envPaths(): string[];
212
- private static determineEnvironment;
213
318
  /**
214
- * Set the application environment. Accepts either Environment enum or string value.
215
- *
216
- * @param env - Environment value ('development', 'staging', 'production') or Environment enum
217
- *
218
- * @example
219
- * ```ts
220
- * Envapter.environment = Environment.Production;
221
- * Envapter.environment = 'staging';
222
- * ```
319
+ * Set custom dotenv configuration options.
223
320
  */
224
- static set environment(env: string | Environment);
321
+ static set dotenvConfig(config: PermittedDotenvConfig);
322
+ /**
323
+ * Get current dotenv configuration options
324
+ */
325
+ static get dotenvConfig(): PermittedDotenvConfig;
326
+ protected static refreshCache(): void;
327
+ protected static get config(): Map<string, unknown>;
328
+ /**
329
+ * Get raw environment variable value without parsing or conversion.
330
+ */
331
+ getRaw(key: string): string | undefined;
332
+ }
333
+
334
+ /**
335
+ * Environment types supported by Envapter
336
+ * @public
337
+ */
338
+ declare enum Environment {
339
+ Development = 0,
340
+ Staging = 1,
341
+ Production = 2
342
+ }
343
+ /**
344
+ * Mixin for environment detection and checking methods
345
+ * @internal
346
+ */
347
+ declare class EnvironmentMethods extends EnvapterBase {
348
+ protected static _environment: Environment | undefined;
349
+ protected static determineEnvironment(env?: string | Environment): void;
350
+ private static getRawValue;
225
351
  /**
226
352
  * Get the current application environment
227
- * @returns Current environment enum value
228
353
  */
229
354
  static get environment(): Environment;
355
+ /**
356
+ * Set the application environment. Accepts either Environment enum or string value.
357
+ */
358
+ static set environment(env: string | Environment);
359
+ /**
360
+ * @see {@link EnvironmentMethods.environment}
361
+ */
362
+ get environment(): Environment;
363
+ /**
364
+ * @see {@link EnvironmentMethods.environment}
365
+ */
366
+ set environment(env: string | Environment);
230
367
  /**
231
368
  * Check if the current environment is production
232
- * @returns true if environment is production
233
369
  */
234
370
  static get isProduction(): boolean;
371
+ /**
372
+ * @see {@link EnvironmentMethods.isProduction}
373
+ */
374
+ get isProduction(): boolean;
235
375
  /**
236
376
  * Check if the current environment is staging
237
- * @returns true if environment is staging
238
377
  */
239
378
  static get isStaging(): boolean;
379
+ /**
380
+ * @see {@link EnvironmentMethods.isStaging}
381
+ */
382
+ get isStaging(): boolean;
240
383
  /**
241
384
  * Check if the current environment is development
242
- * @returns true if environment is development
243
385
  */
244
386
  static get isDevelopment(): boolean;
245
- private static get config();
387
+ /**
388
+ * @see {@link EnvironmentMethods.isDevelopment}
389
+ */
390
+ get isDevelopment(): boolean;
391
+ protected static refreshCache(): void;
392
+ }
393
+
394
+ /**
395
+ * Mixin for primitive environment variable getter methods
396
+ * @internal
397
+ */
398
+ declare class PrimitiveMethods extends EnvironmentMethods implements EnvapterService {
399
+ protected static readonly parser: Parser;
246
400
  private static _get;
247
401
  /**
248
402
  * Get a string environment variable with optional fallback.
249
403
  * Supports template variable resolution using ${VAR} syntax.
250
- *
251
- * @param key - Environment variable name
252
- * @param def - Default value if variable is not found
253
- * @returns The environment variable value or default
254
- *
255
- * @example
256
- * ```ts
257
- * const apiUrl = Envapter.get('API_URL', 'http://localhost:3000');
258
- * ```
259
- */
260
- static get(key: string, def?: string): string;
404
+ */
405
+ static get<Default extends string | undefined = undefined>(key: string, def?: Default): ConditionalReturn<string, Default>;
406
+ /**
407
+ * @see {@link PrimitiveMethods.get}
408
+ */
409
+ get<Default extends string | undefined = undefined>(key: string, def?: Default): ConditionalReturn<string, Default>;
261
410
  /**
262
411
  * Get a number environment variable with optional fallback.
263
412
  * Automatically converts string values to numbers.
264
- *
265
- * @param key - Environment variable name
266
- * @param def - Default value if variable is not found or cannot be converted
267
- * @returns The environment variable value as number or default
268
- *
269
- * @example
270
- * ```ts
271
- * const port = Envapter.getNumber('PORT', 3000);
272
- * ```
273
- */
274
- static getNumber(key: string, def?: number): number;
413
+ */
414
+ static getNumber<Default extends number | undefined = undefined>(key: string, def?: Default): ConditionalReturn<number, Default>;
415
+ /**
416
+ * @see {@link PrimitiveMethods.getNumber}
417
+ */
418
+ getNumber<Default extends number | undefined = undefined>(key: string, def?: Default): ConditionalReturn<number, Default>;
275
419
  /**
276
420
  * Get a boolean environment variable with optional fallback.
277
- * Recognizes: `1`, `yes`, `true` as **true**; `0`, `no`, `false` as **false** (case-insensitive).
278
- *
279
- * @param key - Environment variable name
280
- * @param def - Default value if variable is not found or cannot be converted
281
- * @returns The environment variable value as boolean or default
282
- *
283
- * @example
284
- * ```ts
285
- * const isProduction = Envapter.getBoolean('IS_PRODUCTION', false);
286
- * ```
287
- */
288
- static getBoolean(key: string, def?: boolean): boolean;
421
+ * Recognizes: `1`, `yes`, `true`, 'on' as **true**; `0`, `no`, `false`, 'off' as **false** (case-insensitive).
422
+ */
423
+ static getBoolean<Default extends boolean | undefined = undefined>(key: string, def?: Default): ConditionalReturn<boolean, Default>;
424
+ /**
425
+ * @see {@link PrimitiveMethods.getBoolean}
426
+ */
427
+ getBoolean<Default extends boolean | undefined = undefined>(key: string, def?: Default): ConditionalReturn<boolean, Default>;
289
428
  /**
290
429
  * Get a bigint environment variable with optional fallback.
291
430
  * Automatically converts string values to bigint.
292
- *
293
- * @param key - Environment variable name
294
- * @param def - Default value if variable is not found or cannot be converted
295
- * @returns The environment variable value as bigint or default
296
- *
297
- * @example
298
- * ```ts
299
- * const largeNumber = Envapter.getBigInt('LARGE_NUMBER', 123456789012345678901234567890n);
300
- * ```
301
- */
302
- static getBigInt(key: string, def?: bigint): bigint;
431
+ */
432
+ static getBigInt<Default extends bigint | undefined = undefined>(key: string, def?: Default): ConditionalReturn<bigint, Default>;
433
+ /**
434
+ * @see {@link PrimitiveMethods.getBigInt}
435
+ */
436
+ getBigInt<Default extends bigint | undefined = undefined>(key: string, def?: Default): ConditionalReturn<bigint, Default>;
303
437
  /**
304
438
  * Get a symbol environment variable with optional fallback.
305
439
  * Creates a symbol from the string value.
306
- *
307
- * @param key - Environment variable name
308
- * @param def - Default value if variable is not found
309
- * @returns The environment variable value as symbol or default
310
- *
311
- * @example
312
- * ```ts
313
- * const uniqueKey = Envapter.getSymbol('UNIQUE_KEY', Symbol('default'));
314
- * ```
315
- */
316
- static getSymbol(key: string, def?: symbol): symbol;
317
- get(key: string, def?: string): string;
318
- getNumber(key: string, def?: number): number;
319
- getBoolean(key: string, def?: boolean): boolean;
320
- getBigInt(key: string, def?: bigint): bigint;
321
- getSymbol(key: string, def?: symbol): symbol;
440
+ */
441
+ static getSymbol<Default extends symbol | undefined = undefined>(key: string, def?: Default): ConditionalReturn<symbol, Default>;
322
442
  /**
323
- * Get raw environment variable value without parsing or conversion.
324
- *
325
- * @internal
443
+ * @see {@link PrimitiveMethods.getSymbol}
326
444
  */
327
- getRaw(key: string): string | undefined;
445
+ getSymbol<Default extends symbol | undefined = undefined>(key: string, def?: Default): ConditionalReturn<symbol, Default>;
446
+ }
447
+
448
+ /**
449
+ * Mixin for advanced methods for environment variable conversion using built-in and custom converters
450
+ * @internal
451
+ */
452
+ declare class AdvancedMethods extends PrimitiveMethods {
453
+ /**
454
+ * Get an environment variable using a built-in converter.
455
+ * Supports both Converter enum values and array converter configurations.
456
+ */
457
+ static getUsing<TConverter extends BuiltInConverter | ArrayConverter, TFallback = undefined>(key: string, converter: TConverter, fallback?: TFallback): AdvancedConverterReturn<TConverter, TFallback>;
458
+ static getUsing<TReturn>(key: string, converter: BuiltInConverter | ArrayConverter, fallback?: TReturn): TReturn;
459
+ /**
460
+ * @see {@link AdvancedMethods.getUsing}
461
+ */
462
+ getUsing<TConverter extends BuiltInConverter | ArrayConverter, TFallback = undefined>(key: string, converter: TConverter, fallback?: TFallback): AdvancedConverterReturn<TConverter, TFallback>;
463
+ getUsing<TReturn>(key: string, converter: BuiltInConverter | ArrayConverter, fallback?: TReturn): TReturn;
464
+ /**
465
+ * Get an environment variable using a custom converter function.
466
+ */
467
+ static getWith<TReturnType, TFallback extends TReturnType | undefined = undefined>(key: string, converter: ConverterFunction<TReturnType>, fallback?: TFallback): ConditionalReturn<TReturnType, TFallback>;
468
+ /**
469
+ * @see {@link AdvancedMethods.getWith}
470
+ */
471
+ getWith<TReturnType, TFallback extends TReturnType | undefined = undefined>(key: string, converter: ConverterFunction<TReturnType>, fallback?: TFallback): ConditionalReturn<TReturnType, TFallback>;
472
+ }
473
+
474
+ /**
475
+ * Main configuration class for environment variable management.
476
+ *
477
+ * Provides both static and instance methods for retrieving typed environment variables
478
+ * with support for template resolution, multiple .env files, and environment detection.
479
+ *
480
+ * Extend your own classes from this to define properties with \@Envapt decorators and provide access to environment variables methods.
481
+ *
482
+ * @example
483
+ * ```ts
484
+ * // Static usage
485
+ * const port = Envapter.getNumber('PORT', 3000);
486
+ * const url = Envapter.get('API_URL', 'http://localhost');
487
+ *
488
+ * // Instance usage
489
+ * const env = new Envapter();
490
+ * const dbUrl = env.get('DATABASE_URL', 'sqlite://memory');
491
+ * ```
492
+ *
493
+ * @public
494
+ */
495
+ declare class Envapter extends AdvancedMethods {
496
+ }
497
+
498
+ declare enum EnvaptErrorCodes {
499
+ /** Thrown when an invalid fallback value is provided */
500
+ InvalidFallback = 101,
501
+ /** Thrown when fallback value type doesn't match expected converter type */
502
+ InvalidFallbackType = 102,
503
+ /** Thrown when array fallback contains elements of wrong type */
504
+ ArrayFallbackElementTypeMismatch = 103,
505
+ /** Thrown when fallback type doesn't match the specified converter */
506
+ FallbackConverterTypeMismatch = 104,
507
+ /** Thrown when invalid array converter configuration is provided */
508
+ InvalidArrayConverterType = 201,
509
+ /** Thrown when an invalid built-in converter is specified */
510
+ InvalidBuiltInConverter = 202,
511
+ /** Thrown when a custom converter is not a function */
512
+ InvalidCustomConverter = 203,
513
+ /** Thrown when converter type is not recognized */
514
+ InvalidConverterType = 204,
515
+ /** Thrown when primitive type coercion on fallback value fails */
516
+ PrimitiveCoercionFailed = 205,
517
+ /** Thrown when delimiter is missing in array converter configuration */
518
+ MissingDelimiter = 301,
519
+ /** Thrown when invalid user-defined configuration is provided */
520
+ InvalidUserDefinedConfig = 302,
521
+ /** Thrown when specified environment files don't exist */
522
+ EnvFilesNotFound = 303
328
523
  }
329
524
 
330
- export { type ArrayConverter, type BaseInput, type BuiltInConverter, type BuiltInConverterReturnType, type ConverterFunction, Envapt, type EnvaptConverter, type EnvaptOptions, Envapter, Environment, type PrimitiveConstructor, type ValidArrayConverterBuiltInType };
525
+ export { type AdvancedConverterReturn, type ArrayConverter, type BuiltInConverter, type BuiltInConverterFunction, type ConditionalReturn, type ConverterFunction, Converters, Envapt, type EnvaptConverter, EnvaptErrorCodes, type EnvaptOptions, Envapter, Environment, type InferConverterReturnType, type InferPrimitiveFallbackType, type InferPrimitiveReturnType, type JsonValue, type MapOfConverterFunctions, type PermittedDotenvConfig, type PrimitiveConstructor, type TimeUnit, type ValidArrayConverterBuiltInType };