@catbee/utils 2.0.0-next.0 → 2.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 +1 -1
- package/README.md +102 -52
- package/array/index.cjs +215 -74
- package/array/index.d.ts +345 -2
- package/array/index.mjs +201 -74
- package/async/index.cjs +116 -39
- package/async/index.d.ts +292 -2
- package/async/index.mjs +116 -40
- package/cache/index.cjs +2 -2
- package/cache/index.d.ts +156 -2
- package/cache/index.mjs +3 -3
- package/config/index.cjs +80 -66
- package/config/index.d.ts +65 -3
- package/config/index.mjs +77 -65
- package/context-store/index.cjs +2 -3
- package/context-store/index.d.ts +193 -2
- package/context-store/index.mjs +2 -3
- package/crypto/index.cjs +55 -5
- package/crypto/index.d.ts +225 -2
- package/crypto/index.mjs +52 -7
- package/date/index.cjs +676 -2
- package/date/index.d.ts +676 -2
- package/date/index.mjs +665 -3
- package/decorator/index.cjs +2172 -0
- package/{decorators/decorators.utils.d.ts → decorator/index.d.ts} +58 -54
- package/decorator/index.mjs +2131 -0
- package/{dir → directory}/index.cjs +5 -4
- package/{dir/dir.utils.d.ts → directory/index.d.ts} +24 -21
- package/{dir → directory}/index.mjs +5 -4
- package/env/index.cjs +100 -68
- package/env/index.d.ts +391 -2
- package/env/index.mjs +100 -68
- package/exception/index.cjs +1 -1
- package/exception/index.d.ts +233 -2
- package/exception/index.mjs +1 -1
- package/fs/index.cjs +71 -37
- package/fs/index.d.ts +206 -2
- package/fs/index.mjs +65 -35
- package/http-status-codes/index.cjs +1 -1
- package/http-status-codes/index.d.ts +268 -2
- package/http-status-codes/index.mjs +1 -1
- package/id/index.cjs +1 -1
- package/id/index.d.ts +38 -2
- package/id/index.mjs +1 -1
- package/index.cjs +13 -13
- package/index.d.ts +5 -5
- package/index.mjs +5 -5
- package/logger/index.cjs +13 -15
- package/logger/index.d.ts +190 -2
- package/logger/index.mjs +14 -16
- package/middleware/index.cjs +1 -1
- package/middleware/index.d.ts +104 -2
- package/middleware/index.mjs +1 -1
- package/object/index.cjs +379 -0
- package/{obj/obj.utils.d.ts → object/index.d.ts} +73 -33
- package/object/index.mjs +360 -0
- package/package.json +41 -23
- package/performance/index.cjs +4 -4
- package/performance/index.d.ts +139 -2
- package/performance/index.mjs +4 -4
- package/request/index.cjs +37 -25
- package/request/index.d.ts +242 -3
- package/request/index.mjs +37 -25
- package/response/index.cjs +1 -1
- package/response/index.d.ts +319 -3
- package/response/index.mjs +1 -1
- package/server/index.cjs +249 -146
- package/server/index.d.ts +866 -5
- package/server/index.mjs +248 -144
- package/stream/index.cjs +1 -1
- package/stream/index.d.ts +91 -2
- package/stream/index.mjs +1 -1
- package/string/index.cjs +34 -1
- package/string/index.d.ts +146 -2
- package/string/index.mjs +31 -2
- package/type/index.cjs +19 -2
- package/type/index.d.ts +144 -2
- package/type/index.mjs +17 -3
- package/types/index.cjs +1 -1
- package/types/index.d.ts +775 -5
- package/types/index.mjs +1 -1
- package/url/index.cjs +63 -5
- package/url/index.d.ts +200 -2
- package/url/index.mjs +59 -6
- package/{validate → validation}/index.cjs +91 -44
- package/{validate/validate.utils.d.ts → validation/index.d.ts} +33 -24
- package/{validate → validation}/index.mjs +87 -44
- package/array/array.utils.d.ts +0 -191
- package/async/async.utils.d.ts +0 -296
- package/cache/cache.utils.d.ts +0 -176
- package/config/config.d.ts +0 -57
- package/context-store/context-store.utils.d.ts +0 -212
- package/crypto/crypto.utils.d.ts +0 -183
- package/date/date.utils.d.ts +0 -190
- package/decorators/index.cjs +0 -913
- package/decorators/index.d.ts +0 -25
- package/decorators/index.mjs +0 -872
- package/dir/index.d.ts +0 -25
- package/env/env.utils.d.ts +0 -400
- package/exception/exception.utils.d.ts +0 -253
- package/fs/fs.utils.d.ts +0 -196
- package/http-status-codes/http-status-codes.d.ts +0 -289
- package/id/id.utils.d.ts +0 -59
- package/logger/logger.utils.d.ts +0 -210
- package/middleware/middleware.utils.d.ts +0 -123
- package/obj/index.cjs +0 -317
- package/obj/index.d.ts +0 -25
- package/obj/index.mjs +0 -301
- package/performance/performance.utils.d.ts +0 -159
- package/request/request.utils.d.ts +0 -109
- package/response/response.utils.d.ts +0 -186
- package/server/server.builder.d.ts +0 -531
- package/server/server.d.ts +0 -303
- package/stream/stream.utils.d.ts +0 -111
- package/string/string.utils.d.ts +0 -124
- package/type/type.utils.d.ts +0 -129
- package/types/api-response.d.ts +0 -175
- package/types/common.d.ts +0 -148
- package/types/config.d.ts +0 -88
- package/types/server.d.ts +0 -291
- package/url/url.utils.d.ts +0 -164
- package/validate/index.d.ts +0 -25
package/dir/index.d.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
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
|
-
export * from './dir.utils';
|
package/env/env.utils.d.ts
DELETED
|
@@ -1,400 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,253 +0,0 @@
|
|
|
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
|
-
import { ErrorResponse } from '@catbee/utils/response';
|
|
26
|
-
/**
|
|
27
|
-
* Generic HTTP error class used for custom exceptions with any status code.
|
|
28
|
-
* Inherit this when you need to throw errors dynamically at runtime.
|
|
29
|
-
*/
|
|
30
|
-
export declare class HttpError extends ErrorResponse {
|
|
31
|
-
/**
|
|
32
|
-
* Creates a new HTTP error instance.
|
|
33
|
-
* @param status - A valid HTTP status code (e.g., 400, 500).
|
|
34
|
-
* @param message - The error message to return to the client.
|
|
35
|
-
*/
|
|
36
|
-
constructor(status: number, message: string);
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Represents a 500 Internal Server Error.
|
|
40
|
-
* Use this for unexpected backend errors that are not caused by the client.
|
|
41
|
-
*/
|
|
42
|
-
export declare class InternalServerErrorException extends ErrorResponse {
|
|
43
|
-
/**
|
|
44
|
-
* @param message - Optional custom message. Defaults to "Internal server error".
|
|
45
|
-
*/
|
|
46
|
-
constructor(message?: string);
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Represents a 401 Unauthorized Error.
|
|
50
|
-
* Indicates that authentication is required and has failed or not been provided.
|
|
51
|
-
*/
|
|
52
|
-
export declare class UnauthorizedException extends ErrorResponse {
|
|
53
|
-
/**
|
|
54
|
-
* @param message - Optional custom message. Defaults to "Unauthorized".
|
|
55
|
-
*/
|
|
56
|
-
constructor(message?: string);
|
|
57
|
-
}
|
|
58
|
-
/**
|
|
59
|
-
* Represents a 400 Bad Request Error.
|
|
60
|
-
* Indicates that the client has sent invalid data (e.g., malformed request body).
|
|
61
|
-
*/
|
|
62
|
-
export declare class BadRequestException extends ErrorResponse {
|
|
63
|
-
/**
|
|
64
|
-
* @param message - Optional custom message. Defaults to "Bad request".
|
|
65
|
-
*/
|
|
66
|
-
constructor(message?: string);
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Represents a 404 Not Found Error.
|
|
70
|
-
* Used when a requested resource (e.g., user, file) could not be located.
|
|
71
|
-
*/
|
|
72
|
-
export declare class NotFoundException extends ErrorResponse {
|
|
73
|
-
/**
|
|
74
|
-
* @param message - Optional custom message. Defaults to "Resource not found".
|
|
75
|
-
*/
|
|
76
|
-
constructor(message?: string);
|
|
77
|
-
}
|
|
78
|
-
/**
|
|
79
|
-
* Represents a 403 Forbidden Error.
|
|
80
|
-
* Indicates that the client is authenticated but not allowed to access the resource.
|
|
81
|
-
*/
|
|
82
|
-
export declare class ForbiddenException extends ErrorResponse {
|
|
83
|
-
/**
|
|
84
|
-
* @param message - Optional custom message. Defaults to "Forbidden".
|
|
85
|
-
*/
|
|
86
|
-
constructor(message?: string);
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Represents a 409 Conflict Error.
|
|
90
|
-
* Commonly used when a resource already exists or a versioning conflict occurs.
|
|
91
|
-
*/
|
|
92
|
-
export declare class ConflictException extends ErrorResponse {
|
|
93
|
-
/**
|
|
94
|
-
* @param message - Optional custom message. Defaults to "Conflict".
|
|
95
|
-
*/
|
|
96
|
-
constructor(message?: string);
|
|
97
|
-
}
|
|
98
|
-
/**
|
|
99
|
-
* Represents a 502 Bad Gateway Error.
|
|
100
|
-
* Used when the server receives an invalid response from an upstream server.
|
|
101
|
-
*/
|
|
102
|
-
export declare class BadGatewayException extends ErrorResponse {
|
|
103
|
-
/**
|
|
104
|
-
* @param message - Optional custom message. Defaults to "Bad Gateway".
|
|
105
|
-
*/
|
|
106
|
-
constructor(message?: string);
|
|
107
|
-
}
|
|
108
|
-
/**
|
|
109
|
-
* Represents a 429 Too Many Requests Error.
|
|
110
|
-
* Returned when the client has hit a rate limit.
|
|
111
|
-
*/
|
|
112
|
-
export declare class TooManyRequestsException extends ErrorResponse {
|
|
113
|
-
/**
|
|
114
|
-
* @param message - Optional custom message. Defaults to "Too many requests".
|
|
115
|
-
*/
|
|
116
|
-
constructor(message?: string);
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Represents a 503 Service Unavailable Error.
|
|
120
|
-
* Indicates that the server is temporarily unavailable (e.g., for maintenance).
|
|
121
|
-
*/
|
|
122
|
-
export declare class ServiceUnavailableException extends ErrorResponse {
|
|
123
|
-
/**
|
|
124
|
-
* @param message - Optional custom message. Defaults to "Service Unavailable".
|
|
125
|
-
*/
|
|
126
|
-
constructor(message?: string);
|
|
127
|
-
}
|
|
128
|
-
/**
|
|
129
|
-
* Represents a 504 Gateway Timeout Error.
|
|
130
|
-
* Returned when the server acting as a gateway times out waiting for a response.
|
|
131
|
-
*/
|
|
132
|
-
export declare class GatewayTimeoutException extends ErrorResponse {
|
|
133
|
-
/**
|
|
134
|
-
* @param message - Optional custom message. Defaults to "Gateway Timeout".
|
|
135
|
-
*/
|
|
136
|
-
constructor(message?: string);
|
|
137
|
-
}
|
|
138
|
-
/**
|
|
139
|
-
* Represents a 422 Unprocessable Entity Error.
|
|
140
|
-
* Used when the server understands the content type but cannot process the contained instructions.
|
|
141
|
-
*/
|
|
142
|
-
export declare class UnprocessableEntityException extends ErrorResponse {
|
|
143
|
-
details?: Record<string, any> | undefined;
|
|
144
|
-
/**
|
|
145
|
-
* @param message - Optional custom message. Defaults to "Unprocessable Entity".
|
|
146
|
-
* @param details - Optional validation details or additional error context.
|
|
147
|
-
*/
|
|
148
|
-
constructor(message?: string, details?: Record<string, any> | undefined);
|
|
149
|
-
}
|
|
150
|
-
/**
|
|
151
|
-
* Represents a 405 Method Not Allowed Error.
|
|
152
|
-
* Used when the request method is not supported for the requested resource.
|
|
153
|
-
*/
|
|
154
|
-
export declare class MethodNotAllowedException extends ErrorResponse {
|
|
155
|
-
allowedMethods?: string[] | undefined;
|
|
156
|
-
/**
|
|
157
|
-
* @param message - Optional custom message. Defaults to "Method Not Allowed".
|
|
158
|
-
* @param allowedMethods - Optional array of allowed HTTP methods.
|
|
159
|
-
*/
|
|
160
|
-
constructor(message?: string, allowedMethods?: string[] | undefined);
|
|
161
|
-
}
|
|
162
|
-
/**
|
|
163
|
-
* Represents a 406 Not Acceptable Error.
|
|
164
|
-
* Used when the server cannot produce a response matching the client's accepted content types.
|
|
165
|
-
*/
|
|
166
|
-
export declare class NotAcceptableException extends ErrorResponse {
|
|
167
|
-
/**
|
|
168
|
-
* @param message - Optional custom message. Defaults to "Not Acceptable".
|
|
169
|
-
*/
|
|
170
|
-
constructor(message?: string);
|
|
171
|
-
}
|
|
172
|
-
/**
|
|
173
|
-
* Represents a 408 Request Timeout Error.
|
|
174
|
-
* Used when the server times out waiting for the client to send a request.
|
|
175
|
-
*/
|
|
176
|
-
export declare class RequestTimeoutException extends ErrorResponse {
|
|
177
|
-
/**
|
|
178
|
-
* @param message - Optional custom message. Defaults to "Request Timeout".
|
|
179
|
-
*/
|
|
180
|
-
constructor(message?: string);
|
|
181
|
-
}
|
|
182
|
-
/**
|
|
183
|
-
* Represents a 415 Unsupported Media Type Error.
|
|
184
|
-
* Used when the request's Content-Type is not supported.
|
|
185
|
-
*/
|
|
186
|
-
export declare class UnsupportedMediaTypeException extends ErrorResponse {
|
|
187
|
-
/**
|
|
188
|
-
* @param message - Optional custom message. Defaults to "Unsupported Media Type".
|
|
189
|
-
*/
|
|
190
|
-
constructor(message?: string);
|
|
191
|
-
}
|
|
192
|
-
/**
|
|
193
|
-
* Represents a 413 Payload Too Large Error.
|
|
194
|
-
* Used when the request body exceeds server limits.
|
|
195
|
-
*/
|
|
196
|
-
export declare class PayloadTooLargeException extends ErrorResponse {
|
|
197
|
-
/**
|
|
198
|
-
* @param message - Optional custom message. Defaults to "Payload Too Large".
|
|
199
|
-
*/
|
|
200
|
-
constructor(message?: string);
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* Represents a 507 Insufficient Storage Error.
|
|
204
|
-
* Used when the server has insufficient storage to complete the request.
|
|
205
|
-
*/
|
|
206
|
-
export declare class InsufficientStorageException extends ErrorResponse {
|
|
207
|
-
/**
|
|
208
|
-
* @param message - Optional custom message. Defaults to "Insufficient Storage".
|
|
209
|
-
*/
|
|
210
|
-
constructor(message?: string);
|
|
211
|
-
}
|
|
212
|
-
/**
|
|
213
|
-
* Checks if an error is an instance of HttpError or its subclasses.
|
|
214
|
-
*
|
|
215
|
-
* @param error - The error to check.
|
|
216
|
-
* @returns True if the error is an HttpError, false otherwise.
|
|
217
|
-
*/
|
|
218
|
-
export declare function isHttpError(error: unknown): error is ErrorResponse;
|
|
219
|
-
/**
|
|
220
|
-
* Creates an HTTP error with the specified status code and message.
|
|
221
|
-
* Factory function for creating appropriate HTTP error instances.
|
|
222
|
-
*
|
|
223
|
-
* @param status - HTTP status code.
|
|
224
|
-
* @param message - Optional custom error message.
|
|
225
|
-
* @returns An instance of the appropriate HTTP error class.
|
|
226
|
-
*/
|
|
227
|
-
export declare function createHttpError(status: number, message?: string): ErrorResponse;
|
|
228
|
-
/**
|
|
229
|
-
* Type guard to check if an object has specific error properties.
|
|
230
|
-
*
|
|
231
|
-
* @param error - The object to check.
|
|
232
|
-
* @returns True if object has error-like structure.
|
|
233
|
-
*/
|
|
234
|
-
export declare function hasErrorShape(error: unknown): error is {
|
|
235
|
-
message: string;
|
|
236
|
-
status?: number;
|
|
237
|
-
code?: string;
|
|
238
|
-
};
|
|
239
|
-
/**
|
|
240
|
-
* Safely extracts message from any error type.
|
|
241
|
-
*
|
|
242
|
-
* @param error - Any error object or value.
|
|
243
|
-
* @returns A string error message.
|
|
244
|
-
*/
|
|
245
|
-
export declare function getErrorMessage(error: unknown): string;
|
|
246
|
-
/**
|
|
247
|
-
* Wraps a function and transforms any errors into HTTP errors.
|
|
248
|
-
* Useful for API handlers to ensure consistent error responses.
|
|
249
|
-
*
|
|
250
|
-
* @param handler - The async function to wrap.
|
|
251
|
-
* @returns A function that always returns or throws an HTTP error.
|
|
252
|
-
*/
|
|
253
|
-
export declare function withErrorHandling<T extends (...args: any[]) => Promise<any>>(handler: T): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>>;
|