envapt 1.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/LICENSE +197 -0
- package/NOTICE +2 -0
- package/README.md +445 -0
- package/dist/index.d.mts +330 -0
- package/dist/index.d.ts +330 -0
- package/dist/index.js +625 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +621 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +97 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* List of built-in converters for Envapt.
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
declare const ListOfBuiltInConverters: readonly ["string", "number", "boolean", "bigint", "symbol", "integer", "float", "json", "array", "url", "regexp", "date", "time"];
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Built-in converter types for common environment variable patterns
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
type BuiltInConverter = (typeof ListOfBuiltInConverters)[number];
|
|
12
|
+
/**
|
|
13
|
+
* Primitive types supported by Envapter
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
type PrimitiveConstructor = typeof String | typeof Number | typeof Boolean | typeof BigInt | typeof Symbol;
|
|
17
|
+
type ValidArrayConverterBuiltInType = Exclude<BuiltInConverter, 'array' | 'json' | 'regexp'>;
|
|
18
|
+
/**
|
|
19
|
+
* Array converter configuration for custom delimiters and element types
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
interface ArrayConverter {
|
|
23
|
+
/**
|
|
24
|
+
* Delimiter to split the string by
|
|
25
|
+
*/
|
|
26
|
+
delimiter: string;
|
|
27
|
+
/**
|
|
28
|
+
* Type to convert each array element to (excludes array, json, and regexp types)
|
|
29
|
+
*/
|
|
30
|
+
type?: ValidArrayConverterBuiltInType;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* String value from a .env file or environment variable
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
type BaseInput = string | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Custom parser function type for environment variables
|
|
39
|
+
* @param raw - Raw string value from environment
|
|
40
|
+
* @param fallback - Fallback value if parsing fails
|
|
41
|
+
* @returns Parsed value of type T
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
type ConverterFunction<FallbackType = unknown> = (raw: BaseInput, fallback?: FallbackType) => FallbackType;
|
|
45
|
+
/**
|
|
46
|
+
* Environment variable converter - can be a primitive constructor, built-in converter string, array converter object, or custom parser function
|
|
47
|
+
* @see {@link PrimitiveConstructor} for primitive types
|
|
48
|
+
* @see {@link BuiltInConverter} for built-in types
|
|
49
|
+
* @see {@link ArrayConverter} for array converter configuration
|
|
50
|
+
* @see {@link ConverterFunction} for custom parser functions
|
|
51
|
+
* @public
|
|
52
|
+
*/
|
|
53
|
+
type EnvaptConverter<FallbackType> = PrimitiveConstructor | BuiltInConverter | ArrayConverter | ConverterFunction<FallbackType>;
|
|
54
|
+
/**
|
|
55
|
+
* Options for the \@Envapt decorator (modern API)
|
|
56
|
+
* @public
|
|
57
|
+
*/
|
|
58
|
+
interface EnvaptOptions<FallbackType = string> {
|
|
59
|
+
/**
|
|
60
|
+
* Default value to use if environment variable is not found
|
|
61
|
+
*/
|
|
62
|
+
fallback?: FallbackType;
|
|
63
|
+
/**
|
|
64
|
+
* Built-in converter, custom converter function, or boxed-primitives (String, Number, Boolean)
|
|
65
|
+
*/
|
|
66
|
+
converter?: EnvaptConverter<FallbackType>;
|
|
67
|
+
}
|
|
68
|
+
interface ConverterMap {
|
|
69
|
+
string: string;
|
|
70
|
+
number: number;
|
|
71
|
+
boolean: boolean;
|
|
72
|
+
bigint: bigint;
|
|
73
|
+
symbol: symbol;
|
|
74
|
+
integer: number;
|
|
75
|
+
float: number;
|
|
76
|
+
json: unknown;
|
|
77
|
+
array: string[];
|
|
78
|
+
url: URL;
|
|
79
|
+
regexp: RegExp;
|
|
80
|
+
date: Date;
|
|
81
|
+
time: number;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Type mapping for built-in converters to their return types
|
|
85
|
+
*/
|
|
86
|
+
type BuiltInConverterReturnType<ConverterKey extends BuiltInConverter = BuiltInConverter> = ConverterMap[ConverterKey];
|
|
87
|
+
|
|
88
|
+
/**
|
|
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.
|
|
95
|
+
*
|
|
96
|
+
* @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[];
|
|
107
|
+
*
|
|
108
|
+
* \@Envapt('CONFIG', { converter: 'json' })
|
|
109
|
+
* static readonly config: object;
|
|
110
|
+
*
|
|
111
|
+
* \@Envapt('API_URL', { converter: 'url' })
|
|
112
|
+
* declare public readonly apiUrl: URL;
|
|
113
|
+
*
|
|
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
|
+
* ```
|
|
124
|
+
*
|
|
125
|
+
* @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
|
|
128
|
+
*
|
|
129
|
+
* @example Classic API
|
|
130
|
+
* ```ts
|
|
131
|
+
* class HealthCheck {
|
|
132
|
+
* \@Envapt('PORT', 3000) declare readonly port: number;
|
|
133
|
+
*
|
|
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
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @public
|
|
143
|
+
*/
|
|
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;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @internal
|
|
149
|
+
*/
|
|
150
|
+
interface EnvapterService {
|
|
151
|
+
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;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Environment types supported by Envapter
|
|
159
|
+
* @public
|
|
160
|
+
*/
|
|
161
|
+
declare enum Environment {
|
|
162
|
+
Development = 0,
|
|
163
|
+
Staging = 1,
|
|
164
|
+
Production = 2
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
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
|
|
186
|
+
*/
|
|
187
|
+
declare class Envapter implements EnvapterService {
|
|
188
|
+
private static readonly parser;
|
|
189
|
+
private static _envPaths;
|
|
190
|
+
private static internalEnvironment;
|
|
191
|
+
/**
|
|
192
|
+
* Set custom .env file paths. Accepts either a single path or array of paths.
|
|
193
|
+
* 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
|
+
*/
|
|
206
|
+
static set envPaths(paths: string[] | string);
|
|
207
|
+
/**
|
|
208
|
+
* Get currently configured .env file paths
|
|
209
|
+
* @returns Array of file paths being loaded
|
|
210
|
+
*/
|
|
211
|
+
static get envPaths(): string[];
|
|
212
|
+
private static determineEnvironment;
|
|
213
|
+
/**
|
|
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
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
static set environment(env: string | Environment);
|
|
225
|
+
/**
|
|
226
|
+
* Get the current application environment
|
|
227
|
+
* @returns Current environment enum value
|
|
228
|
+
*/
|
|
229
|
+
static get environment(): Environment;
|
|
230
|
+
/**
|
|
231
|
+
* Check if the current environment is production
|
|
232
|
+
* @returns true if environment is production
|
|
233
|
+
*/
|
|
234
|
+
static get isProduction(): boolean;
|
|
235
|
+
/**
|
|
236
|
+
* Check if the current environment is staging
|
|
237
|
+
* @returns true if environment is staging
|
|
238
|
+
*/
|
|
239
|
+
static get isStaging(): boolean;
|
|
240
|
+
/**
|
|
241
|
+
* Check if the current environment is development
|
|
242
|
+
* @returns true if environment is development
|
|
243
|
+
*/
|
|
244
|
+
static get isDevelopment(): boolean;
|
|
245
|
+
private static get config();
|
|
246
|
+
private static _get;
|
|
247
|
+
/**
|
|
248
|
+
* Get a string environment variable with optional fallback.
|
|
249
|
+
* 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;
|
|
261
|
+
/**
|
|
262
|
+
* Get a number environment variable with optional fallback.
|
|
263
|
+
* 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;
|
|
275
|
+
/**
|
|
276
|
+
* 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;
|
|
289
|
+
/**
|
|
290
|
+
* Get a bigint environment variable with optional fallback.
|
|
291
|
+
* 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;
|
|
303
|
+
/**
|
|
304
|
+
* Get a symbol environment variable with optional fallback.
|
|
305
|
+
* 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;
|
|
322
|
+
/**
|
|
323
|
+
* Get raw environment variable value without parsing or conversion.
|
|
324
|
+
*
|
|
325
|
+
* @internal
|
|
326
|
+
*/
|
|
327
|
+
getRaw(key: string): string | undefined;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export { type ArrayConverter, type BaseInput, type BuiltInConverter, type BuiltInConverterReturnType, type ConverterFunction, Envapt, type EnvaptConverter, type EnvaptOptions, Envapter, Environment, type PrimitiveConstructor, type ValidArrayConverterBuiltInType };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* List of built-in converters for Envapt.
|
|
3
|
+
* @internal
|
|
4
|
+
*/
|
|
5
|
+
declare const ListOfBuiltInConverters: readonly ["string", "number", "boolean", "bigint", "symbol", "integer", "float", "json", "array", "url", "regexp", "date", "time"];
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Built-in converter types for common environment variable patterns
|
|
9
|
+
* @public
|
|
10
|
+
*/
|
|
11
|
+
type BuiltInConverter = (typeof ListOfBuiltInConverters)[number];
|
|
12
|
+
/**
|
|
13
|
+
* Primitive types supported by Envapter
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
type PrimitiveConstructor = typeof String | typeof Number | typeof Boolean | typeof BigInt | typeof Symbol;
|
|
17
|
+
type ValidArrayConverterBuiltInType = Exclude<BuiltInConverter, 'array' | 'json' | 'regexp'>;
|
|
18
|
+
/**
|
|
19
|
+
* Array converter configuration for custom delimiters and element types
|
|
20
|
+
* @public
|
|
21
|
+
*/
|
|
22
|
+
interface ArrayConverter {
|
|
23
|
+
/**
|
|
24
|
+
* Delimiter to split the string by
|
|
25
|
+
*/
|
|
26
|
+
delimiter: string;
|
|
27
|
+
/**
|
|
28
|
+
* Type to convert each array element to (excludes array, json, and regexp types)
|
|
29
|
+
*/
|
|
30
|
+
type?: ValidArrayConverterBuiltInType;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* String value from a .env file or environment variable
|
|
34
|
+
* @public
|
|
35
|
+
*/
|
|
36
|
+
type BaseInput = string | undefined;
|
|
37
|
+
/**
|
|
38
|
+
* Custom parser function type for environment variables
|
|
39
|
+
* @param raw - Raw string value from environment
|
|
40
|
+
* @param fallback - Fallback value if parsing fails
|
|
41
|
+
* @returns Parsed value of type T
|
|
42
|
+
* @public
|
|
43
|
+
*/
|
|
44
|
+
type ConverterFunction<FallbackType = unknown> = (raw: BaseInput, fallback?: FallbackType) => FallbackType;
|
|
45
|
+
/**
|
|
46
|
+
* Environment variable converter - can be a primitive constructor, built-in converter string, array converter object, or custom parser function
|
|
47
|
+
* @see {@link PrimitiveConstructor} for primitive types
|
|
48
|
+
* @see {@link BuiltInConverter} for built-in types
|
|
49
|
+
* @see {@link ArrayConverter} for array converter configuration
|
|
50
|
+
* @see {@link ConverterFunction} for custom parser functions
|
|
51
|
+
* @public
|
|
52
|
+
*/
|
|
53
|
+
type EnvaptConverter<FallbackType> = PrimitiveConstructor | BuiltInConverter | ArrayConverter | ConverterFunction<FallbackType>;
|
|
54
|
+
/**
|
|
55
|
+
* Options for the \@Envapt decorator (modern API)
|
|
56
|
+
* @public
|
|
57
|
+
*/
|
|
58
|
+
interface EnvaptOptions<FallbackType = string> {
|
|
59
|
+
/**
|
|
60
|
+
* Default value to use if environment variable is not found
|
|
61
|
+
*/
|
|
62
|
+
fallback?: FallbackType;
|
|
63
|
+
/**
|
|
64
|
+
* Built-in converter, custom converter function, or boxed-primitives (String, Number, Boolean)
|
|
65
|
+
*/
|
|
66
|
+
converter?: EnvaptConverter<FallbackType>;
|
|
67
|
+
}
|
|
68
|
+
interface ConverterMap {
|
|
69
|
+
string: string;
|
|
70
|
+
number: number;
|
|
71
|
+
boolean: boolean;
|
|
72
|
+
bigint: bigint;
|
|
73
|
+
symbol: symbol;
|
|
74
|
+
integer: number;
|
|
75
|
+
float: number;
|
|
76
|
+
json: unknown;
|
|
77
|
+
array: string[];
|
|
78
|
+
url: URL;
|
|
79
|
+
regexp: RegExp;
|
|
80
|
+
date: Date;
|
|
81
|
+
time: number;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Type mapping for built-in converters to their return types
|
|
85
|
+
*/
|
|
86
|
+
type BuiltInConverterReturnType<ConverterKey extends BuiltInConverter = BuiltInConverter> = ConverterMap[ConverterKey];
|
|
87
|
+
|
|
88
|
+
/**
|
|
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.
|
|
95
|
+
*
|
|
96
|
+
* @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[];
|
|
107
|
+
*
|
|
108
|
+
* \@Envapt('CONFIG', { converter: 'json' })
|
|
109
|
+
* static readonly config: object;
|
|
110
|
+
*
|
|
111
|
+
* \@Envapt('API_URL', { converter: 'url' })
|
|
112
|
+
* declare public readonly apiUrl: URL;
|
|
113
|
+
*
|
|
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
|
+
* ```
|
|
124
|
+
*
|
|
125
|
+
* @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
|
|
128
|
+
*
|
|
129
|
+
* @example Classic API
|
|
130
|
+
* ```ts
|
|
131
|
+
* class HealthCheck {
|
|
132
|
+
* \@Envapt('PORT', 3000) declare readonly port: number;
|
|
133
|
+
*
|
|
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
|
+
* ```
|
|
141
|
+
*
|
|
142
|
+
* @public
|
|
143
|
+
*/
|
|
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;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* @internal
|
|
149
|
+
*/
|
|
150
|
+
interface EnvapterService {
|
|
151
|
+
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;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* Environment types supported by Envapter
|
|
159
|
+
* @public
|
|
160
|
+
*/
|
|
161
|
+
declare enum Environment {
|
|
162
|
+
Development = 0,
|
|
163
|
+
Staging = 1,
|
|
164
|
+
Production = 2
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
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
|
|
186
|
+
*/
|
|
187
|
+
declare class Envapter implements EnvapterService {
|
|
188
|
+
private static readonly parser;
|
|
189
|
+
private static _envPaths;
|
|
190
|
+
private static internalEnvironment;
|
|
191
|
+
/**
|
|
192
|
+
* Set custom .env file paths. Accepts either a single path or array of paths.
|
|
193
|
+
* 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
|
+
*/
|
|
206
|
+
static set envPaths(paths: string[] | string);
|
|
207
|
+
/**
|
|
208
|
+
* Get currently configured .env file paths
|
|
209
|
+
* @returns Array of file paths being loaded
|
|
210
|
+
*/
|
|
211
|
+
static get envPaths(): string[];
|
|
212
|
+
private static determineEnvironment;
|
|
213
|
+
/**
|
|
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
|
+
* ```
|
|
223
|
+
*/
|
|
224
|
+
static set environment(env: string | Environment);
|
|
225
|
+
/**
|
|
226
|
+
* Get the current application environment
|
|
227
|
+
* @returns Current environment enum value
|
|
228
|
+
*/
|
|
229
|
+
static get environment(): Environment;
|
|
230
|
+
/**
|
|
231
|
+
* Check if the current environment is production
|
|
232
|
+
* @returns true if environment is production
|
|
233
|
+
*/
|
|
234
|
+
static get isProduction(): boolean;
|
|
235
|
+
/**
|
|
236
|
+
* Check if the current environment is staging
|
|
237
|
+
* @returns true if environment is staging
|
|
238
|
+
*/
|
|
239
|
+
static get isStaging(): boolean;
|
|
240
|
+
/**
|
|
241
|
+
* Check if the current environment is development
|
|
242
|
+
* @returns true if environment is development
|
|
243
|
+
*/
|
|
244
|
+
static get isDevelopment(): boolean;
|
|
245
|
+
private static get config();
|
|
246
|
+
private static _get;
|
|
247
|
+
/**
|
|
248
|
+
* Get a string environment variable with optional fallback.
|
|
249
|
+
* 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;
|
|
261
|
+
/**
|
|
262
|
+
* Get a number environment variable with optional fallback.
|
|
263
|
+
* 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;
|
|
275
|
+
/**
|
|
276
|
+
* 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;
|
|
289
|
+
/**
|
|
290
|
+
* Get a bigint environment variable with optional fallback.
|
|
291
|
+
* 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;
|
|
303
|
+
/**
|
|
304
|
+
* Get a symbol environment variable with optional fallback.
|
|
305
|
+
* 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;
|
|
322
|
+
/**
|
|
323
|
+
* Get raw environment variable value without parsing or conversion.
|
|
324
|
+
*
|
|
325
|
+
* @internal
|
|
326
|
+
*/
|
|
327
|
+
getRaw(key: string): string | undefined;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
export { type ArrayConverter, type BaseInput, type BuiltInConverter, type BuiltInConverterReturnType, type ConverterFunction, Envapt, type EnvaptConverter, type EnvaptOptions, Envapter, Environment, type PrimitiveConstructor, type ValidArrayConverterBuiltInType };
|