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