@catbee/utils 1.0.5 → 2.0.0-next.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 +1 -1
- package/README.md +27 -27
- package/array/array.utils.d.ts +191 -0
- package/array/index.cjs +246 -0
- package/array/index.d.ts +25 -0
- package/array/index.mjs +228 -0
- package/async/async.utils.d.ts +296 -0
- package/async/index.cjs +428 -0
- package/async/index.d.ts +25 -0
- package/async/index.mjs +407 -0
- package/cache/cache.utils.d.ts +176 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +25 -0
- package/cache/index.mjs +290 -0
- package/config/config.d.ts +57 -0
- package/config/index.cjs +136 -0
- package/config/index.d.ts +26 -0
- package/config/index.mjs +131 -0
- package/context-store/context-store.utils.d.ts +212 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +25 -0
- package/context-store/index.mjs +261 -0
- package/crypto/crypto.utils.d.ts +183 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +25 -0
- package/crypto/index.mjs +166 -0
- package/date/date.utils.d.ts +190 -0
- package/date/index.cjs +295 -0
- package/date/index.d.ts +25 -0
- package/date/index.mjs +283 -0
- package/decorators/decorators.utils.d.ts +705 -0
- package/decorators/index.cjs +913 -0
- package/decorators/index.d.ts +25 -0
- package/decorators/index.mjs +872 -0
- package/dir/dir.utils.d.ts +216 -0
- package/dir/index.cjs +416 -0
- package/dir/index.d.ts +25 -0
- package/dir/index.mjs +389 -0
- package/env/env.utils.d.ts +400 -0
- package/env/index.cjs +761 -0
- package/env/index.d.ts +25 -0
- package/env/index.mjs +758 -0
- package/exception/exception.utils.d.ts +253 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +25 -0
- package/exception/index.mjs +338 -0
- package/fs/fs.utils.d.ts +196 -0
- package/fs/index.cjs +253 -0
- package/fs/index.d.ts +25 -0
- package/fs/index.mjs +228 -0
- package/http-status-codes/http-status-codes.d.ts +289 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +25 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/id.utils.d.ts +59 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +25 -0
- package/id/index.mjs +56 -0
- package/index.cjs +218 -0
- package/index.d.ts +51 -0
- package/index.mjs +51 -0
- package/logger/index.cjs +334 -0
- package/logger/index.d.ts +25 -0
- package/logger/index.mjs +313 -0
- package/logger/logger.utils.d.ts +210 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +25 -0
- package/middleware/index.mjs +170 -0
- package/middleware/middleware.utils.d.ts +123 -0
- package/obj/index.cjs +317 -0
- package/obj/index.d.ts +25 -0
- package/obj/index.mjs +301 -0
- package/obj/obj.utils.d.ts +156 -0
- package/package.json +172 -20
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +25 -0
- package/performance/index.mjs +225 -0
- package/performance/performance.utils.d.ts +159 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +26 -0
- package/request/index.mjs +194 -0
- package/request/request.utils.d.ts +109 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +26 -0
- package/response/index.mjs +222 -0
- package/response/response.utils.d.ts +186 -0
- package/server/index.cjs +1627 -0
- package/server/index.d.ts +28 -0
- package/server/index.mjs +1617 -0
- package/server/server.builder.d.ts +531 -0
- package/server/server.d.ts +303 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +25 -0
- package/stream/index.mjs +144 -0
- package/stream/stream.utils.d.ts +111 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +25 -0
- package/string/index.mjs +95 -0
- package/string/string.utils.d.ts +124 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +25 -0
- package/type/index.mjs +119 -0
- package/type/type.utils.d.ts +129 -0
- package/types/api-response.d.ts +175 -0
- package/types/common.d.ts +148 -0
- package/types/config.d.ts +88 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +28 -0
- package/types/index.mjs +32 -0
- package/types/server.d.ts +291 -0
- package/url/index.cjs +201 -0
- package/url/index.d.ts +25 -0
- package/url/index.mjs +189 -0
- package/url/url.utils.d.ts +164 -0
- package/validate/index.cjs +212 -0
- package/validate/index.d.ts +25 -0
- package/validate/index.mjs +188 -0
- package/validate/validate.utils.d.ts +200 -0
- package/build/index.cjs +0 -7579
- package/build/index.d.ts +0 -5774
- package/build/index.mjs +0 -7274
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
|
|
5
|
+
*
|
|
6
|
+
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
7
|
+
* of this software and associated documentation files (the "Software"), to deal
|
|
8
|
+
* in the Software without restriction, including without limitation the rights
|
|
9
|
+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
10
|
+
* copies of the Software, and to permit persons to whom the Software is
|
|
11
|
+
* furnished to do so, subject to the following conditions:
|
|
12
|
+
*
|
|
13
|
+
* The above copyright notice and this permission notice shall be included in all
|
|
14
|
+
* copies or substantial portions of the Software.
|
|
15
|
+
*
|
|
16
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
17
|
+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
18
|
+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
19
|
+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
20
|
+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
21
|
+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
|
+
* SOFTWARE.
|
|
23
|
+
*/
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Enum representing valid application environments.
|
|
27
|
+
*/
|
|
28
|
+
export declare enum Environment {
|
|
29
|
+
PRODUCTION = "production",
|
|
30
|
+
DEVELOPMENT = "development",
|
|
31
|
+
STAGING = "staging",
|
|
32
|
+
TESTING = "testing"
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Options for validating URL environment variables
|
|
36
|
+
*/
|
|
37
|
+
export interface UrlOptions {
|
|
38
|
+
/** List of allowed protocols (e.g., ['http', 'https']) */
|
|
39
|
+
protocols?: string[];
|
|
40
|
+
/** Whether a top-level domain is required (default: true) */
|
|
41
|
+
requireTld?: boolean;
|
|
42
|
+
/** Whether to allow IP addresses (default: true) */
|
|
43
|
+
allowIp?: boolean;
|
|
44
|
+
/** Whether to allow localhost (default: true) */
|
|
45
|
+
allowLocalhost?: boolean;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Options for validating path environment variables
|
|
49
|
+
*/
|
|
50
|
+
export interface PathOptions {
|
|
51
|
+
/** Whether the path must exist on the filesystem (default: false) */
|
|
52
|
+
mustExist?: boolean;
|
|
53
|
+
/** Whether to convert relative paths to absolute (default: true) */
|
|
54
|
+
makeAbsolute?: boolean;
|
|
55
|
+
/** List of allowed file extensions */
|
|
56
|
+
allowedExtensions?: string[];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Utility class for accessing and managing environment variables.
|
|
60
|
+
* Provides typed getters with fallback defaults and validation.
|
|
61
|
+
*/
|
|
62
|
+
export declare class Env {
|
|
63
|
+
private static cache;
|
|
64
|
+
/**
|
|
65
|
+
* Checks if the current NODE_ENV is 'development'.
|
|
66
|
+
*
|
|
67
|
+
* @returns {boolean} `true` if NODE_ENV is 'development', else `false`.
|
|
68
|
+
*/
|
|
69
|
+
static isDev(): boolean;
|
|
70
|
+
/**
|
|
71
|
+
* Checks if the current NODE_ENV is 'production'.
|
|
72
|
+
*
|
|
73
|
+
* @returns {boolean} `true` if NODE_ENV is 'production', else `false`.
|
|
74
|
+
*/
|
|
75
|
+
static isProd(): boolean;
|
|
76
|
+
/**
|
|
77
|
+
* Checks if the current NODE_ENV is 'testing'.
|
|
78
|
+
*
|
|
79
|
+
* @returns {boolean} `true` if NODE_ENV is 'testing', else `false`.
|
|
80
|
+
*/
|
|
81
|
+
static isTest(): boolean;
|
|
82
|
+
/**
|
|
83
|
+
* Checks if the current NODE_ENV is 'staging'.
|
|
84
|
+
*
|
|
85
|
+
* @returns {boolean} `true` if NODE_ENV is 'staging', else `false`.
|
|
86
|
+
*/
|
|
87
|
+
static isStaging(): boolean;
|
|
88
|
+
/**
|
|
89
|
+
* Sets an environment variable (only affects runtime memory).
|
|
90
|
+
*
|
|
91
|
+
* @param {string} key - The environment variable key.
|
|
92
|
+
* @param {string} value - The value to set.
|
|
93
|
+
*/
|
|
94
|
+
static set(key: string, value: string): void;
|
|
95
|
+
/**
|
|
96
|
+
* Returns all environment variables as an object.
|
|
97
|
+
*
|
|
98
|
+
* @returns {object} The current `process.env` object.
|
|
99
|
+
*/
|
|
100
|
+
static getAll(): object;
|
|
101
|
+
/**
|
|
102
|
+
* Retrieves a string environment variable with a fallback default.
|
|
103
|
+
* Supports variable expansion with ${VAR_NAME} syntax.
|
|
104
|
+
*
|
|
105
|
+
* @param {string} key - The environment variable key.
|
|
106
|
+
* @param {string} [defaultValue] - Value to return if the key is missing.
|
|
107
|
+
* @returns {string} The env value or the fallback.
|
|
108
|
+
*
|
|
109
|
+
* @example
|
|
110
|
+
* // If DATABASE_URL is "postgres://localhost:5432/${DB_NAME}"
|
|
111
|
+
* // and DB_NAME is "myapp"
|
|
112
|
+
* const url = Env.get('DATABASE_URL', ''); // "postgres://localhost:5432/myapp"
|
|
113
|
+
*/
|
|
114
|
+
static get(key: string, defaultValue: string): string;
|
|
115
|
+
/**
|
|
116
|
+
* Retrieves a string environment variable and throws if it's missing.
|
|
117
|
+
*
|
|
118
|
+
* @param {string} key - The environment variable key.
|
|
119
|
+
* @returns {string} The environment variable's value.
|
|
120
|
+
* @throws {Error} If the variable is not defined.
|
|
121
|
+
*/
|
|
122
|
+
static getRequired(key: string): string;
|
|
123
|
+
/**
|
|
124
|
+
* Retrieves a value using a default generating function if the key doesn't exist.
|
|
125
|
+
* Useful for expensive default calculations.
|
|
126
|
+
*
|
|
127
|
+
* @param {string} key - The environment variable key.
|
|
128
|
+
* @param {() => string} defaultFn - Function that generates default value.
|
|
129
|
+
* @returns {string} The environment value or generated default.
|
|
130
|
+
*
|
|
131
|
+
* @example
|
|
132
|
+
* const hostname = Env.getWithDefault('HOSTNAME', () => {
|
|
133
|
+
* // Only called if HOSTNAME is not set
|
|
134
|
+
* return require('os').hostname();
|
|
135
|
+
* });
|
|
136
|
+
*/
|
|
137
|
+
static getWithDefault(key: string, defaultFn: () => string): string;
|
|
138
|
+
/**
|
|
139
|
+
* Retrieves an environment variable as a number, or returns a default.
|
|
140
|
+
*
|
|
141
|
+
* @param {string} key - The environment variable key.
|
|
142
|
+
* @param {number} defaultValue - Fallback number if key is not present.
|
|
143
|
+
* @returns {number} Parsed numeric value or default.
|
|
144
|
+
* @throws {Error} If the value is not a valid number.
|
|
145
|
+
*/
|
|
146
|
+
static getNumber(key: string, defaultValue: number): number;
|
|
147
|
+
/**
|
|
148
|
+
* Retrieves a required environment variable as a number.
|
|
149
|
+
*
|
|
150
|
+
* @param {string} key - The environment variable key.
|
|
151
|
+
* @returns {number} Parsed number.
|
|
152
|
+
* @throws {Error} If the value is missing or not a number.
|
|
153
|
+
*/
|
|
154
|
+
static getNumberRequired(key: string): number;
|
|
155
|
+
/**
|
|
156
|
+
* Retrieves an integer environment variable and validates it.
|
|
157
|
+
*
|
|
158
|
+
* @param {string} key - The environment variable key.
|
|
159
|
+
* @param {number} defaultValue - Fallback number if key is not present.
|
|
160
|
+
* @param {object} [options] - Validation options.
|
|
161
|
+
* @param {number} [options.min] - Minimum allowed value.
|
|
162
|
+
* @param {number} [options.max] - Maximum allowed value.
|
|
163
|
+
* @returns {number} The parsed integer.
|
|
164
|
+
*
|
|
165
|
+
* @example
|
|
166
|
+
* // Require PORT to be between 1000 and 9999
|
|
167
|
+
* const port = Env.getInteger('PORT', 3000, { min: 1000, max: 9999 });
|
|
168
|
+
*/
|
|
169
|
+
static getInteger(key: string, defaultValue: number, options?: {
|
|
170
|
+
min?: number;
|
|
171
|
+
max?: number;
|
|
172
|
+
}): number;
|
|
173
|
+
/**
|
|
174
|
+
* Retrieves an environment variable as a boolean.
|
|
175
|
+
* Accepts `true`, `1`, `yes`, `on` as true; `false`, `0`, `no`, `off` as false.
|
|
176
|
+
*
|
|
177
|
+
* @param {string} key - The environment variable key.
|
|
178
|
+
* @param {boolean} [defaultValue=false] - Optional fallback value if key is missing.
|
|
179
|
+
* @returns {boolean} Parsed boolean.
|
|
180
|
+
* @throws {Error} If the value is not a recognized boolean string.
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* // If DEBUG=yes
|
|
184
|
+
* const isDebug = Env.getBoolean('DEBUG', false); // true
|
|
185
|
+
*/
|
|
186
|
+
static getBoolean(key: string, defaultValue?: boolean): boolean;
|
|
187
|
+
/**
|
|
188
|
+
* Retrieves a required environment variable as a boolean.
|
|
189
|
+
*
|
|
190
|
+
* @param {string} key - The environment variable key.
|
|
191
|
+
* @returns {boolean} Parsed boolean value.
|
|
192
|
+
* @throws {Error} If missing or invalid.
|
|
193
|
+
*/
|
|
194
|
+
static getBooleanRequired(key: string): boolean;
|
|
195
|
+
/**
|
|
196
|
+
* Parses a stringified JSON object from an environment variable.
|
|
197
|
+
*
|
|
198
|
+
* @typeParam T - The type to parse as (defaults to `object`).
|
|
199
|
+
* @param {string} key - The environment variable key.
|
|
200
|
+
* @param {T} defaultValue - Value to return if key is missing.
|
|
201
|
+
* @returns {T} Parsed object or default.
|
|
202
|
+
* @throws {Error} If the value is not valid JSON.
|
|
203
|
+
*
|
|
204
|
+
* @example
|
|
205
|
+
* // If CONFIG='{"debug":true,"api":{"url":"https://api.example.com"}}'
|
|
206
|
+
* const config = Env.getJSON('CONFIG', { debug: false });
|
|
207
|
+
* // { debug: true, api: { url: "https://api.example.com" }}
|
|
208
|
+
*/
|
|
209
|
+
static getJSON<T extends object = object>(key: string, defaultValue: T): T;
|
|
210
|
+
/**
|
|
211
|
+
* Parses a comma-separated string as an array.
|
|
212
|
+
*
|
|
213
|
+
* @typeParam T - The item type (optional, defaults to string).
|
|
214
|
+
* @param {string} key - The environment variable key.
|
|
215
|
+
* @param {T[]} [defaultValue=[]] - Array to return if value is empty or missing.
|
|
216
|
+
* @param {string} [splitter=','] - Delimiter to split on.
|
|
217
|
+
* @param {(item: string) => T} [transform] - Optional function to transform each item.
|
|
218
|
+
* @returns {T[]} An array of items.
|
|
219
|
+
*
|
|
220
|
+
* @example
|
|
221
|
+
* // If ALLOWED_IPS=127.0.0.1,192.168.1.1,10.0.0.1
|
|
222
|
+
* const ips = Env.getArray('ALLOWED_IPS');
|
|
223
|
+
* // ["127.0.0.1", "192.168.1.1", "10.0.0.1"]
|
|
224
|
+
*
|
|
225
|
+
* // With transformation function
|
|
226
|
+
* const ports = Env.getArray('PORTS', [], ',', (p) => parseInt(p, 10));
|
|
227
|
+
*/
|
|
228
|
+
static getArray<T = string>(key: string, defaultValue?: T[], splitter?: string, transform?: (item: string) => T): T[];
|
|
229
|
+
/**
|
|
230
|
+
* Parses a comma-separated list of numbers.
|
|
231
|
+
*
|
|
232
|
+
* @param {string} key - The environment variable key.
|
|
233
|
+
* @param {number[]} [defaultValue=[]] - Default value if not present.
|
|
234
|
+
* @param {string} [splitter=','] - Delimiter to split on.
|
|
235
|
+
* @returns {number[]} Array of parsed numbers.
|
|
236
|
+
*
|
|
237
|
+
* @example
|
|
238
|
+
* // If ALLOWED_PORTS=80,443,3000,8080
|
|
239
|
+
* const ports = Env.getNumberArray('ALLOWED_PORTS');
|
|
240
|
+
* // [80, 443, 3000, 8080]
|
|
241
|
+
*/
|
|
242
|
+
static getNumberArray(key: string, defaultValue?: number[], splitter?: string): number[];
|
|
243
|
+
/**
|
|
244
|
+
* Retrieves an enum-like environment variable value, validating against allowed values.
|
|
245
|
+
*
|
|
246
|
+
* @typeParam T - The allowed value type (string literal types).
|
|
247
|
+
* @param {string} key - The environment variable key.
|
|
248
|
+
* @param {T} [defaultValue] - Optional fallback value.
|
|
249
|
+
* @param {T[]} allowedValues - Array of accepted string values.
|
|
250
|
+
* @returns {T} The validated environment value.
|
|
251
|
+
* @throws {Error} If missing or invalid.
|
|
252
|
+
*
|
|
253
|
+
* @example
|
|
254
|
+
* // If LOG_LEVEL=debug
|
|
255
|
+
* const level = Env.getEnum('LOG_LEVEL', 'info', ['debug', 'info', 'warn', 'error'] as const);
|
|
256
|
+
* // 'debug' (typed as 'debug' | 'info' | 'warn' | 'error')
|
|
257
|
+
*/
|
|
258
|
+
static getEnum<T extends string>(key: string, defaultValue: T, allowedValues: readonly T[]): T;
|
|
259
|
+
/**
|
|
260
|
+
* Retrieves an enum-like numeric environment variable.
|
|
261
|
+
*
|
|
262
|
+
* @param {string} key - The environment variable key.
|
|
263
|
+
* @param {number} defaultValue - Default value if not present.
|
|
264
|
+
* @param {number[]} allowedValues - Array of accepted values.
|
|
265
|
+
* @returns {number} The validated value.
|
|
266
|
+
*
|
|
267
|
+
* @example
|
|
268
|
+
* // If NODE_VERSION=16
|
|
269
|
+
* const version = Env.getNumberEnum('NODE_VERSION', 16, [14, 16, 18]);
|
|
270
|
+
*/
|
|
271
|
+
static getNumberEnum(key: string, defaultValue: number, allowedValues: number[]): number;
|
|
272
|
+
/**
|
|
273
|
+
* Retrieves a URL environment variable and validates it.
|
|
274
|
+
*
|
|
275
|
+
* @param {string} key - The environment variable key.
|
|
276
|
+
* @param {string} [defaultValue] - Optional fallback value.
|
|
277
|
+
* @param {UrlOptions} [options] - Validation options.
|
|
278
|
+
* @returns {string} The validated URL.
|
|
279
|
+
* @throws {Error} If URL is invalid.
|
|
280
|
+
*
|
|
281
|
+
* @example
|
|
282
|
+
* // Validate API URL requires HTTPS
|
|
283
|
+
* const apiUrl = Env.getUrl('API_URL', 'https://api.example.com', {
|
|
284
|
+
* protocols: ['https'],
|
|
285
|
+
* requireTld: true,
|
|
286
|
+
* allowIp: false
|
|
287
|
+
* });
|
|
288
|
+
*/
|
|
289
|
+
static getUrl(key: string, defaultValue: string, options?: UrlOptions): string;
|
|
290
|
+
/**
|
|
291
|
+
* Retrieves an email environment variable and validates it.
|
|
292
|
+
*
|
|
293
|
+
* @param {string} key - The environment variable key.
|
|
294
|
+
* @param {string} [defaultValue] - Optional fallback value.
|
|
295
|
+
* @returns {string} The validated email address.
|
|
296
|
+
* @throws {Error} If email is invalid.
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* const supportEmail = Env.getEmail('SUPPORT_EMAIL', 'support@example.com');
|
|
300
|
+
*/
|
|
301
|
+
static getEmail(key: string, defaultValue: string): string;
|
|
302
|
+
/**
|
|
303
|
+
* Retrieves a path environment variable and validates it exists.
|
|
304
|
+
*
|
|
305
|
+
* @param {string} key - The environment variable key.
|
|
306
|
+
* @param {string} [defaultValue] - Optional fallback value.
|
|
307
|
+
* @param {PathOptions} [options] - Validation options.
|
|
308
|
+
* @returns {string} The validated path.
|
|
309
|
+
* @throws {Error} If path is invalid.
|
|
310
|
+
*
|
|
311
|
+
* @example
|
|
312
|
+
* // Require that the path exists and is a .json file
|
|
313
|
+
* const configPath = Env.getPath('CONFIG_PATH', './config.json', {
|
|
314
|
+
* mustExist: true,
|
|
315
|
+
* allowedExtensions: ['.json', '.yaml']
|
|
316
|
+
* });
|
|
317
|
+
*/
|
|
318
|
+
static getPath(key: string, defaultValue: string, options?: PathOptions): string;
|
|
319
|
+
/**
|
|
320
|
+
* Retrieves a port environment variable and validates it.
|
|
321
|
+
*
|
|
322
|
+
* @param {string} key - The environment variable key.
|
|
323
|
+
* @param {number} [defaultValue] - Optional fallback value.
|
|
324
|
+
* @returns {number} The validated port number.
|
|
325
|
+
* @throws {Error} If port is invalid.
|
|
326
|
+
*
|
|
327
|
+
* @example
|
|
328
|
+
* const serverPort = Env.getPort('PORT', 3000);
|
|
329
|
+
*/
|
|
330
|
+
static getPort(key: string, defaultValue: number): number;
|
|
331
|
+
/**
|
|
332
|
+
* Retrieves an ISO date string and converts it to a Date object.
|
|
333
|
+
*
|
|
334
|
+
* @param {string} key - The environment variable key.
|
|
335
|
+
* @param {string|Date} [defaultValue] - Optional fallback value.
|
|
336
|
+
* @returns {Date} The parsed Date object.
|
|
337
|
+
*
|
|
338
|
+
* @example
|
|
339
|
+
* // If EXPIRY_DATE=2023-12-31T23:59:59Z
|
|
340
|
+
* const expiryDate = Env.getDate('EXPIRY_DATE', new Date());
|
|
341
|
+
*/
|
|
342
|
+
static getDate(key: string, defaultValue?: string | Date): Date;
|
|
343
|
+
/**
|
|
344
|
+
* Retrieves a duration string and converts it to milliseconds.
|
|
345
|
+
* Supports formats like "1d", "2h", "30m", "45s", "100ms" or combinations like "1h30m".
|
|
346
|
+
*
|
|
347
|
+
* @param {string} key - The environment variable key.
|
|
348
|
+
* @param {string|number} [defaultValue='0'] - Optional fallback value.
|
|
349
|
+
* @returns {number} The duration in milliseconds.
|
|
350
|
+
* @throws {Error} If duration format is invalid.
|
|
351
|
+
*
|
|
352
|
+
* @example
|
|
353
|
+
* // If CACHE_TTL=2h30m
|
|
354
|
+
* const cacheTtlMs = Env.getDuration('CACHE_TTL', '1h');
|
|
355
|
+
* // 9000000 (2.5 hours in milliseconds)
|
|
356
|
+
*/
|
|
357
|
+
static getDuration(key: string, defaultValue?: string | number): number;
|
|
358
|
+
/**
|
|
359
|
+
* Gets all environment variables with sensitive values masked for safe logging.
|
|
360
|
+
*
|
|
361
|
+
* @param {string[]} [sensitiveKeys=['password', 'secret', 'key', 'token', 'auth']] - Keys to mask.
|
|
362
|
+
* @returns {Record<string, string>} Environment variables with sensitive values masked.
|
|
363
|
+
*
|
|
364
|
+
* @example
|
|
365
|
+
* console.log(Env.getSafeEnv(['password', 'secret', 'key']));
|
|
366
|
+
* // { DATABASE_URL: "postgres://...", API_KEY: "******", ... }
|
|
367
|
+
*/
|
|
368
|
+
static getSafeEnv(sensitiveKeys?: string[]): Record<string, string>;
|
|
369
|
+
/**
|
|
370
|
+
* Loads environment variables from a .env file.
|
|
371
|
+
* Does not override existing variables.
|
|
372
|
+
*
|
|
373
|
+
* @param {string} [path='.env'] - Path to the .env file.
|
|
374
|
+
* @returns {Record<string, string>} Loaded environment variables.
|
|
375
|
+
*
|
|
376
|
+
* @example
|
|
377
|
+
* // Load variables from .env.development
|
|
378
|
+
* Env.loadFromFile('.env.development');
|
|
379
|
+
*/
|
|
380
|
+
static loadFromFile(path?: string): Record<string, string>;
|
|
381
|
+
/**
|
|
382
|
+
* Checks whether the specified environment variable exists.
|
|
383
|
+
*
|
|
384
|
+
* @param {string} key - The environment variable key.
|
|
385
|
+
* @returns {boolean} `true` if the variable is defined, otherwise `false`.
|
|
386
|
+
*/
|
|
387
|
+
static has(key: string): boolean;
|
|
388
|
+
/**
|
|
389
|
+
* Deletes the given environment variable (useful in tests).
|
|
390
|
+
*
|
|
391
|
+
* @param {string} key - The environment variable key to delete.
|
|
392
|
+
* @returns {void}
|
|
393
|
+
*/
|
|
394
|
+
static delete(key: string): void;
|
|
395
|
+
/**
|
|
396
|
+
* Clears the internal cache of parsed environment values.
|
|
397
|
+
* Useful for testing or when environment variables might change.
|
|
398
|
+
*/
|
|
399
|
+
static clearCache(): void;
|
|
400
|
+
}
|