@catbee/utils 2.0.0-next.0 → 2.0.0-next.1

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.
Files changed (97) hide show
  1. package/README.md +52 -16
  2. package/array/index.cjs +180 -71
  3. package/array/index.d.ts +293 -1
  4. package/array/index.mjs +171 -72
  5. package/async/index.cjs +92 -36
  6. package/async/index.d.ts +275 -1
  7. package/async/index.mjs +92 -36
  8. package/cache/index.cjs +1 -1
  9. package/cache/index.d.ts +155 -1
  10. package/cache/index.mjs +2 -2
  11. package/config/index.cjs +78 -64
  12. package/config/index.d.ts +64 -2
  13. package/config/index.mjs +76 -64
  14. package/context-store/index.d.ts +192 -1
  15. package/crypto/index.d.ts +163 -1
  16. package/date/index.cjs +46 -1
  17. package/date/index.d.ts +190 -1
  18. package/date/index.mjs +45 -2
  19. package/decorators/index.cjs +1156 -18
  20. package/decorators/index.d.ts +684 -1
  21. package/decorators/index.mjs +1156 -18
  22. package/dir/index.cjs +4 -3
  23. package/dir/index.d.ts +195 -1
  24. package/dir/index.mjs +4 -3
  25. package/env/index.cjs +10 -26
  26. package/env/index.d.ts +379 -1
  27. package/env/index.mjs +10 -26
  28. package/exception/index.d.ts +232 -1
  29. package/fs/index.cjs +70 -36
  30. package/fs/index.d.ts +205 -1
  31. package/fs/index.mjs +64 -34
  32. package/http-status-codes/index.d.ts +267 -1
  33. package/id/index.d.ts +37 -1
  34. package/index.cjs +3 -3
  35. package/index.d.ts +1 -1
  36. package/index.mjs +1 -1
  37. package/logger/index.cjs +11 -11
  38. package/logger/index.d.ts +189 -1
  39. package/logger/index.mjs +12 -12
  40. package/middleware/index.d.ts +103 -1
  41. package/obj/index.cjs +150 -162
  42. package/obj/index.d.ts +136 -1
  43. package/obj/index.mjs +150 -162
  44. package/package.json +11 -11
  45. package/performance/index.cjs +2 -2
  46. package/performance/index.d.ts +138 -1
  47. package/performance/index.mjs +2 -2
  48. package/request/index.cjs +1 -1
  49. package/request/index.d.ts +241 -2
  50. package/request/index.mjs +1 -1
  51. package/response/index.d.ts +318 -2
  52. package/server/index.cjs +27 -23
  53. package/server/index.d.ts +785 -4
  54. package/server/index.mjs +28 -23
  55. package/stream/index.d.ts +90 -1
  56. package/string/index.d.ts +102 -1
  57. package/type/index.cjs +1 -1
  58. package/type/index.d.ts +107 -1
  59. package/type/index.mjs +1 -1
  60. package/types/index.d.ts +774 -4
  61. package/url/index.cjs +2 -4
  62. package/url/index.d.ts +142 -1
  63. package/url/index.mjs +2 -4
  64. package/{validate → validation}/index.cjs +89 -42
  65. package/{validate/validate.utils.d.ts → validation/index.d.ts} +32 -23
  66. package/{validate → validation}/index.mjs +85 -42
  67. package/array/array.utils.d.ts +0 -191
  68. package/async/async.utils.d.ts +0 -296
  69. package/cache/cache.utils.d.ts +0 -176
  70. package/config/config.d.ts +0 -57
  71. package/context-store/context-store.utils.d.ts +0 -212
  72. package/crypto/crypto.utils.d.ts +0 -183
  73. package/date/date.utils.d.ts +0 -190
  74. package/decorators/decorators.utils.d.ts +0 -705
  75. package/dir/dir.utils.d.ts +0 -216
  76. package/env/env.utils.d.ts +0 -400
  77. package/exception/exception.utils.d.ts +0 -253
  78. package/fs/fs.utils.d.ts +0 -196
  79. package/http-status-codes/http-status-codes.d.ts +0 -289
  80. package/id/id.utils.d.ts +0 -59
  81. package/logger/logger.utils.d.ts +0 -210
  82. package/middleware/middleware.utils.d.ts +0 -123
  83. package/obj/obj.utils.d.ts +0 -156
  84. package/performance/performance.utils.d.ts +0 -159
  85. package/request/request.utils.d.ts +0 -109
  86. package/response/response.utils.d.ts +0 -186
  87. package/server/server.builder.d.ts +0 -531
  88. package/server/server.d.ts +0 -303
  89. package/stream/stream.utils.d.ts +0 -111
  90. package/string/string.utils.d.ts +0 -124
  91. package/type/type.utils.d.ts +0 -129
  92. package/types/api-response.d.ts +0 -175
  93. package/types/common.d.ts +0 -148
  94. package/types/config.d.ts +0 -88
  95. package/types/server.d.ts +0 -291
  96. package/url/url.utils.d.ts +0 -164
  97. package/validate/index.d.ts +0 -25
package/env/index.d.ts CHANGED
@@ -22,4 +22,382 @@
22
22
  * SOFTWARE.
23
23
  */
24
24
 
25
- export * from './env.utils';
25
+ /**
26
+ * Enum representing valid application environments.
27
+ */
28
+ 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
+ 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
+ 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
+ declare class Env {
63
+ private static readonly 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
+ }
401
+
402
+ export { Env, Environment };
403
+ export type { PathOptions, UrlOptions };
package/env/index.mjs CHANGED
@@ -24,6 +24,8 @@
24
24
 
25
25
  import { existsSync, readFileSync } from 'fs';
26
26
  import { isAbsolute, resolve } from 'path';
27
+ import { parseDuration } from '@catbee/utils/date';
28
+ import { isEmail } from '@catbee/utils/validation';
27
29
 
28
30
  var __defProp = Object.defineProperty;
29
31
  var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
@@ -112,7 +114,7 @@ var Env = class _Env {
112
114
  */
113
115
  static get(key, defaultValue) {
114
116
  let value = process.env[key] ?? defaultValue;
115
- if (value && value.includes("${")) {
117
+ if (value?.includes("${")) {
116
118
  value = value.replace(/\${([A-Za-z0-9_]+)}/g, (_, varName) => {
117
119
  return process.env[varName] ?? "";
118
120
  });
@@ -170,7 +172,7 @@ var Env = class _Env {
170
172
  return defaultValue;
171
173
  }
172
174
  const numberValue = Number(value);
173
- if (isNaN(numberValue)) {
175
+ if (Number.isNaN(numberValue)) {
174
176
  throw new Error(`Environment variable '${key}' is not a valid number, got: "${value}"`);
175
177
  }
176
178
  this.cache.set(`number:${key}`, numberValue);
@@ -361,7 +363,7 @@ var Env = class _Env {
361
363
  static getNumberArray(key, defaultValue = [], splitter = ",") {
362
364
  return _Env.getArray(key, defaultValue, splitter, (item) => {
363
365
  const num = Number(item);
364
- if (isNaN(num)) {
366
+ if (Number.isNaN(num)) {
365
367
  throw new Error(`Value "${item}" in array '${key}' is not a valid number`);
366
368
  }
367
369
  return num;
@@ -488,8 +490,7 @@ var Env = class _Env {
488
490
  }
489
491
  return defaultValue;
490
492
  }
491
- const emailRegex = /^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/;
492
- if (!emailRegex.test(value)) {
493
+ if (!isEmail(value)) {
493
494
  throw new Error(`Environment variable '${key}' is not a valid email address: "${value}"`);
494
495
  }
495
496
  this.cache.set(`email:${key}`, value);
@@ -577,7 +578,7 @@ var Env = class _Env {
577
578
  return defaultValue instanceof Date ? defaultValue : /* @__PURE__ */ new Date();
578
579
  }
579
580
  const date = new Date(value);
580
- if (isNaN(date.getTime())) {
581
+ if (Number.isNaN(date.getTime())) {
581
582
  throw new Error(`Environment variable '${key}' is not a valid date: "${value}"`);
582
583
  }
583
584
  return date;
@@ -601,26 +602,9 @@ var Env = class _Env {
601
602
  if (this.cache.has(cacheKey)) {
602
603
  return this.cache.get(cacheKey);
603
604
  }
604
- const value = _Env.get(key, String(defaultValue));
605
- if (!value) return 0;
606
- if (/^\d+$/.test(value)) {
607
- const ms2 = parseInt(value, 10);
608
- this.cache.set(cacheKey, ms2);
609
- return ms2;
610
- }
611
- const durationRegex = /^(?:(\d+)y)?(?:(\d+)w)?(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?(?:(\d+)ms)?$/;
612
- const matches = value.match(durationRegex);
613
- if (!matches || matches[0] === "") {
614
- throw new Error(`Environment variable '${key}' has invalid duration format: "${value}". Use formats like 1y, 2w, 3d, 4h, 5m, 6s, or 7ms.`);
615
- }
616
- let ms = 0;
617
- if (matches[1]) ms += parseInt(matches[1], 10) * 31536e6;
618
- if (matches[2]) ms += parseInt(matches[2], 10) * 6048e5;
619
- if (matches[3]) ms += parseInt(matches[3], 10) * 864e5;
620
- if (matches[4]) ms += parseInt(matches[4], 10) * 36e5;
621
- if (matches[5]) ms += parseInt(matches[5], 10) * 6e4;
622
- if (matches[6]) ms += parseInt(matches[6], 10) * 1e3;
623
- if (matches[7]) ms += parseInt(matches[7], 10);
605
+ const raw = _Env.get(key, String(defaultValue));
606
+ if (!raw) return 0;
607
+ const ms = parseDuration(raw);
624
608
  this.cache.set(cacheKey, ms);
625
609
  return ms;
626
610
  }