@catbee/utils 0.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.
- package/LICENSE +201 -0
- package/README.md +295 -0
- package/build/esm/config.d.ts +22 -0
- package/build/esm/config.js +21 -0
- package/build/esm/config.js.map +1 -0
- package/build/esm/index.d.ts +19 -0
- package/build/esm/index.js +19 -0
- package/build/esm/index.js.map +1 -0
- package/build/esm/types/api-response.d.ts +44 -0
- package/build/esm/types/api-response.js +2 -0
- package/build/esm/types/api-response.js.map +1 -0
- package/build/esm/utils/array.utils.d.ts +105 -0
- package/build/esm/utils/array.utils.js +232 -0
- package/build/esm/utils/array.utils.js.map +1 -0
- package/build/esm/utils/async.utils.d.ts +119 -0
- package/build/esm/utils/async.utils.js +474 -0
- package/build/esm/utils/async.utils.js.map +1 -0
- package/build/esm/utils/cache.utils.d.ts +90 -0
- package/build/esm/utils/cache.utils.js +294 -0
- package/build/esm/utils/cache.utils.js.map +1 -0
- package/build/esm/utils/context-store.utils.d.ts +85 -0
- package/build/esm/utils/context-store.utils.js +99 -0
- package/build/esm/utils/context-store.utils.js.map +1 -0
- package/build/esm/utils/crypto.utils.d.ts +58 -0
- package/build/esm/utils/crypto.utils.js +76 -0
- package/build/esm/utils/crypto.utils.js.map +1 -0
- package/build/esm/utils/dir.utils.d.ts +75 -0
- package/build/esm/utils/dir.utils.js +400 -0
- package/build/esm/utils/dir.utils.js.map +1 -0
- package/build/esm/utils/env.utils.d.ts +131 -0
- package/build/esm/utils/env.utils.js +218 -0
- package/build/esm/utils/env.utils.js.map +1 -0
- package/build/esm/utils/exception.utils.d.ts +114 -0
- package/build/esm/utils/exception.utils.js +217 -0
- package/build/esm/utils/exception.utils.js.map +1 -0
- package/build/esm/utils/fs.utils.d.ts +32 -0
- package/build/esm/utils/fs.utils.js +140 -0
- package/build/esm/utils/fs.utils.js.map +1 -0
- package/build/esm/utils/http-status-codes.d.ts +357 -0
- package/build/esm/utils/http-status-codes.js +358 -0
- package/build/esm/utils/http-status-codes.js.map +1 -0
- package/build/esm/utils/id.utils.d.ts +35 -0
- package/build/esm/utils/id.utils.js +55 -0
- package/build/esm/utils/id.utils.js.map +1 -0
- package/build/esm/utils/logger.utils.d.ts +15 -0
- package/build/esm/utils/logger.utils.js +59 -0
- package/build/esm/utils/logger.utils.js.map +1 -0
- package/build/esm/utils/obj.utils.d.ts +55 -0
- package/build/esm/utils/obj.utils.js +121 -0
- package/build/esm/utils/obj.utils.js.map +1 -0
- package/build/esm/utils/response.utils.d.ts +48 -0
- package/build/esm/utils/response.utils.js +80 -0
- package/build/esm/utils/response.utils.js.map +1 -0
- package/build/esm/utils/string.utils.d.ts +37 -0
- package/build/esm/utils/string.utils.js +56 -0
- package/build/esm/utils/string.utils.js.map +1 -0
- package/build/esm/utils/url.utils.d.ts +24 -0
- package/build/esm/utils/url.utils.js +73 -0
- package/build/esm/utils/url.utils.js.map +1 -0
- package/build/esm/utils/validate.utils.d.ts +60 -0
- package/build/esm/utils/validate.utils.js +121 -0
- package/build/esm/utils/validate.utils.js.map +1 -0
- package/build/esnext/config.d.ts +22 -0
- package/build/esnext/config.js +21 -0
- package/build/esnext/config.js.map +1 -0
- package/build/esnext/index.d.ts +19 -0
- package/build/esnext/index.js +19 -0
- package/build/esnext/index.js.map +1 -0
- package/build/esnext/types/api-response.d.ts +44 -0
- package/build/esnext/types/api-response.js +2 -0
- package/build/esnext/types/api-response.js.map +1 -0
- package/build/esnext/utils/array.utils.d.ts +105 -0
- package/build/esnext/utils/array.utils.js +203 -0
- package/build/esnext/utils/array.utils.js.map +1 -0
- package/build/esnext/utils/async.utils.d.ts +119 -0
- package/build/esnext/utils/async.utils.js +291 -0
- package/build/esnext/utils/async.utils.js.map +1 -0
- package/build/esnext/utils/cache.utils.d.ts +90 -0
- package/build/esnext/utils/cache.utils.js +142 -0
- package/build/esnext/utils/cache.utils.js.map +1 -0
- package/build/esnext/utils/context-store.utils.d.ts +85 -0
- package/build/esnext/utils/context-store.utils.js +95 -0
- package/build/esnext/utils/context-store.utils.js.map +1 -0
- package/build/esnext/utils/crypto.utils.d.ts +58 -0
- package/build/esnext/utils/crypto.utils.js +72 -0
- package/build/esnext/utils/crypto.utils.js.map +1 -0
- package/build/esnext/utils/dir.utils.d.ts +75 -0
- package/build/esnext/utils/dir.utils.js +150 -0
- package/build/esnext/utils/dir.utils.js.map +1 -0
- package/build/esnext/utils/env.utils.d.ts +131 -0
- package/build/esnext/utils/env.utils.js +211 -0
- package/build/esnext/utils/env.utils.js.map +1 -0
- package/build/esnext/utils/exception.utils.d.ts +114 -0
- package/build/esnext/utils/exception.utils.js +148 -0
- package/build/esnext/utils/exception.utils.js.map +1 -0
- package/build/esnext/utils/fs.utils.d.ts +32 -0
- package/build/esnext/utils/fs.utils.js +62 -0
- package/build/esnext/utils/fs.utils.js.map +1 -0
- package/build/esnext/utils/http-status-codes.d.ts +357 -0
- package/build/esnext/utils/http-status-codes.js +358 -0
- package/build/esnext/utils/http-status-codes.js.map +1 -0
- package/build/esnext/utils/id.utils.d.ts +35 -0
- package/build/esnext/utils/id.utils.js +53 -0
- package/build/esnext/utils/id.utils.js.map +1 -0
- package/build/esnext/utils/logger.utils.d.ts +15 -0
- package/build/esnext/utils/logger.utils.js +59 -0
- package/build/esnext/utils/logger.utils.js.map +1 -0
- package/build/esnext/utils/obj.utils.d.ts +55 -0
- package/build/esnext/utils/obj.utils.js +94 -0
- package/build/esnext/utils/obj.utils.js.map +1 -0
- package/build/esnext/utils/response.utils.d.ts +48 -0
- package/build/esnext/utils/response.utils.js +58 -0
- package/build/esnext/utils/response.utils.js.map +1 -0
- package/build/esnext/utils/string.utils.d.ts +37 -0
- package/build/esnext/utils/string.utils.js +46 -0
- package/build/esnext/utils/string.utils.js.map +1 -0
- package/build/esnext/utils/url.utils.d.ts +24 -0
- package/build/esnext/utils/url.utils.js +35 -0
- package/build/esnext/utils/url.utils.js.map +1 -0
- package/build/esnext/utils/validate.utils.d.ts +60 -0
- package/build/esnext/utils/validate.utils.js +105 -0
- package/build/esnext/utils/validate.utils.js.map +1 -0
- package/build/src/config.d.ts +22 -0
- package/build/src/config.js +24 -0
- package/build/src/config.js.map +1 -0
- package/build/src/index.d.ts +19 -0
- package/build/src/index.js +35 -0
- package/build/src/index.js.map +1 -0
- package/build/src/types/api-response.d.ts +44 -0
- package/build/src/types/api-response.js +3 -0
- package/build/src/types/api-response.js.map +1 -0
- package/build/src/utils/array.utils.d.ts +105 -0
- package/build/src/utils/array.utils.js +216 -0
- package/build/src/utils/array.utils.js.map +1 -0
- package/build/src/utils/async.utils.d.ts +119 -0
- package/build/src/utils/async.utils.js +304 -0
- package/build/src/utils/async.utils.js.map +1 -0
- package/build/src/utils/cache.utils.d.ts +90 -0
- package/build/src/utils/cache.utils.js +146 -0
- package/build/src/utils/cache.utils.js.map +1 -0
- package/build/src/utils/context-store.utils.d.ts +85 -0
- package/build/src/utils/context-store.utils.js +100 -0
- package/build/src/utils/context-store.utils.js.map +1 -0
- package/build/src/utils/crypto.utils.d.ts +58 -0
- package/build/src/utils/crypto.utils.js +82 -0
- package/build/src/utils/crypto.utils.js.map +1 -0
- package/build/src/utils/dir.utils.d.ts +75 -0
- package/build/src/utils/dir.utils.js +164 -0
- package/build/src/utils/dir.utils.js.map +1 -0
- package/build/src/utils/env.utils.d.ts +131 -0
- package/build/src/utils/env.utils.js +215 -0
- package/build/src/utils/env.utils.js.map +1 -0
- package/build/src/utils/exception.utils.d.ts +114 -0
- package/build/src/utils/exception.utils.js +162 -0
- package/build/src/utils/exception.utils.js.map +1 -0
- package/build/src/utils/fs.utils.d.ts +32 -0
- package/build/src/utils/fs.utils.js +71 -0
- package/build/src/utils/fs.utils.js.map +1 -0
- package/build/src/utils/http-status-codes.d.ts +357 -0
- package/build/src/utils/http-status-codes.js +361 -0
- package/build/src/utils/http-status-codes.js.map +1 -0
- package/build/src/utils/id.utils.d.ts +35 -0
- package/build/src/utils/id.utils.js +60 -0
- package/build/src/utils/id.utils.js.map +1 -0
- package/build/src/utils/logger.utils.d.ts +15 -0
- package/build/src/utils/logger.utils.js +96 -0
- package/build/src/utils/logger.utils.js.map +1 -0
- package/build/src/utils/obj.utils.d.ts +55 -0
- package/build/src/utils/obj.utils.js +103 -0
- package/build/src/utils/obj.utils.js.map +1 -0
- package/build/src/utils/response.utils.d.ts +48 -0
- package/build/src/utils/response.utils.js +63 -0
- package/build/src/utils/response.utils.js.map +1 -0
- package/build/src/utils/string.utils.d.ts +37 -0
- package/build/src/utils/string.utils.js +54 -0
- package/build/src/utils/string.utils.js.map +1 -0
- package/build/src/utils/url.utils.d.ts +24 -0
- package/build/src/utils/url.utils.js +39 -0
- package/build/src/utils/url.utils.js.map +1 -0
- package/build/src/utils/validate.utils.d.ts +60 -0
- package/build/src/utils/validate.utils.js +115 -0
- package/build/src/utils/validate.utils.js.map +1 -0
- package/package.json +80 -0
|
@@ -0,0 +1,131 @@
|
|
|
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
|
+
* Utility class for accessing and managing environment variables.
|
|
12
|
+
* Provides typed getters with fallback defaults and validation.
|
|
13
|
+
*/
|
|
14
|
+
export declare class Env {
|
|
15
|
+
/**
|
|
16
|
+
* Checks if the current NODE_ENV is 'development'.
|
|
17
|
+
*
|
|
18
|
+
* @returns {boolean} `true` if NODE_ENV is 'development', else `false`.
|
|
19
|
+
*/
|
|
20
|
+
static isDev(): boolean;
|
|
21
|
+
/**
|
|
22
|
+
* Sets an environment variable (only affects runtime memory).
|
|
23
|
+
*
|
|
24
|
+
* @param {string} key - The environment variable key.
|
|
25
|
+
* @param {string} value - The value to set.
|
|
26
|
+
*/
|
|
27
|
+
static set(key: string, value: string): void;
|
|
28
|
+
/**
|
|
29
|
+
* Returns all environment variables as an object.
|
|
30
|
+
*
|
|
31
|
+
* @returns {object} The current `process.env` object.
|
|
32
|
+
*/
|
|
33
|
+
static getAll(): object;
|
|
34
|
+
/**
|
|
35
|
+
* Retrieves a string environment variable with a fallback default.
|
|
36
|
+
*
|
|
37
|
+
* @param {string} key - The environment variable key.
|
|
38
|
+
* @param {string} [defaultValue] - Value to return if the key is missing.
|
|
39
|
+
* @returns {string | undefined} The env value or the fallback.
|
|
40
|
+
*/
|
|
41
|
+
static get(key: string, defaultValue?: string): string | undefined;
|
|
42
|
+
/**
|
|
43
|
+
* Retrieves a string environment variable and throws if it's missing.
|
|
44
|
+
*
|
|
45
|
+
* @param {string} key - The environment variable key.
|
|
46
|
+
* @returns {string} The environment variable's value.
|
|
47
|
+
* @throws {Error} If the variable is not defined.
|
|
48
|
+
*/
|
|
49
|
+
static getRequired(key: string): string;
|
|
50
|
+
/**
|
|
51
|
+
* Retrieves an environment variable as a number, or returns a default.
|
|
52
|
+
*
|
|
53
|
+
* @param {string} key - The environment variable key.
|
|
54
|
+
* @param {number} defaultValue - Fallback number if key is not present.
|
|
55
|
+
* @returns {number} Parsed numeric value or default.
|
|
56
|
+
* @throws {Error} If the value is not a valid number.
|
|
57
|
+
*/
|
|
58
|
+
static getNumber(key: string, defaultValue: number): number;
|
|
59
|
+
/**
|
|
60
|
+
* Retrieves a required environment variable as a number.
|
|
61
|
+
*
|
|
62
|
+
* @param {string} key - The environment variable key.
|
|
63
|
+
* @returns {number} Parsed number.
|
|
64
|
+
* @throws {Error} If the value is missing or not a number.
|
|
65
|
+
*/
|
|
66
|
+
static getNumberRequired(key: string): number;
|
|
67
|
+
/**
|
|
68
|
+
* Retrieves an environment variable as a boolean.
|
|
69
|
+
* Accepts `true`, `1`, `yes`, `on` as true; `false`, `0`, `no`, `off` as false.
|
|
70
|
+
*
|
|
71
|
+
* @param {string} key - The environment variable key.
|
|
72
|
+
* @param {boolean} [defaultValue=false] - Optional fallback value if key is missing.
|
|
73
|
+
* @returns {boolean} Parsed boolean.
|
|
74
|
+
* @throws {Error} If the value is not a recognized boolean string.
|
|
75
|
+
*/
|
|
76
|
+
static getBoolean(key: string, defaultValue?: boolean): boolean;
|
|
77
|
+
/**
|
|
78
|
+
* Retrieves a required environment variable as a boolean.
|
|
79
|
+
*
|
|
80
|
+
* @param {string} key - The environment variable key.
|
|
81
|
+
* @returns {boolean} Parsed boolean value.
|
|
82
|
+
* @throws {Error} If missing or invalid.
|
|
83
|
+
*/
|
|
84
|
+
static getBooleanRequired(key: string): boolean;
|
|
85
|
+
/**
|
|
86
|
+
* Parses a stringified JSON object from an environment variable.
|
|
87
|
+
*
|
|
88
|
+
* @typeParam T - The type to parse as (defaults to `object`).
|
|
89
|
+
* @param {string} key - The environment variable key.
|
|
90
|
+
* @param {T} defaultValue - Value to return if key is missing.
|
|
91
|
+
* @returns {T} Parsed object or default.
|
|
92
|
+
* @throws {Error} If the value is not valid JSON.
|
|
93
|
+
*/
|
|
94
|
+
static getJSON<T extends object = object>(key: string, defaultValue: T): T;
|
|
95
|
+
/**
|
|
96
|
+
* Parses a comma-separated string as an array.
|
|
97
|
+
*
|
|
98
|
+
* @typeParam T - The item type (optional, defaults to string).
|
|
99
|
+
* @param {string} key - The environment variable key.
|
|
100
|
+
* @param {T[]} [defaultValue=[]] - Array to return if value is empty or missing.
|
|
101
|
+
* @param {string} [splitter=','] - Delimiter to split on.
|
|
102
|
+
* @returns {string[] | T[]} An array of strings.
|
|
103
|
+
*/
|
|
104
|
+
static getArray<T = string>(key: string, defaultValue?: T[], splitter?: string): string[] | T[];
|
|
105
|
+
/**
|
|
106
|
+
* Retrieves an enum-like environment variable value, validating against allowed values.
|
|
107
|
+
*
|
|
108
|
+
* @typeParam T - The allowed value type (string literal types).
|
|
109
|
+
* @param {string} key - The environment variable key.
|
|
110
|
+
* @param {T[]} allowedValues - Array of accepted string values.
|
|
111
|
+
* @param {T} [defaultValue] - Optional fallback value.
|
|
112
|
+
* @returns {T} The validated environment value.
|
|
113
|
+
* @throws {Error} If missing or invalid.
|
|
114
|
+
*/
|
|
115
|
+
static getEnum<T extends string>(key: string, allowedValues: T[], defaultValue?: T): T;
|
|
116
|
+
/**
|
|
117
|
+
* Checks whether the specified environment variable exists.
|
|
118
|
+
*
|
|
119
|
+
* @param {string} key - The environment variable key.
|
|
120
|
+
* @returns {boolean} `true` if the variable is defined, otherwise `false`.
|
|
121
|
+
*/
|
|
122
|
+
static has(key: string): boolean;
|
|
123
|
+
/**
|
|
124
|
+
* Deletes the given environment variable (useful in tests).
|
|
125
|
+
*
|
|
126
|
+
* @param {string} key - The environment variable key to delete.
|
|
127
|
+
* @returns {void}
|
|
128
|
+
*/
|
|
129
|
+
static delete(key: string): void;
|
|
130
|
+
}
|
|
131
|
+
//# sourceMappingURL=env.utils.d.ts.map
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
/* eslint-disable n/no-process-env */
|
|
2
|
+
/**
|
|
3
|
+
* Enum representing valid application environments.
|
|
4
|
+
*/
|
|
5
|
+
export var Environment;
|
|
6
|
+
(function (Environment) {
|
|
7
|
+
Environment["PRODUCTION"] = "production";
|
|
8
|
+
Environment["DEVELOPMENT"] = "development";
|
|
9
|
+
Environment["STAGING"] = "staging";
|
|
10
|
+
Environment["TESTING"] = "testing";
|
|
11
|
+
})(Environment || (Environment = {}));
|
|
12
|
+
/**
|
|
13
|
+
* Utility class for accessing and managing environment variables.
|
|
14
|
+
* Provides typed getters with fallback defaults and validation.
|
|
15
|
+
*/
|
|
16
|
+
export class Env {
|
|
17
|
+
/**
|
|
18
|
+
* Checks if the current NODE_ENV is 'development'.
|
|
19
|
+
*
|
|
20
|
+
* @returns {boolean} `true` if NODE_ENV is 'development', else `false`.
|
|
21
|
+
*/
|
|
22
|
+
static isDev() {
|
|
23
|
+
return (Env.get("NODE_ENV", Environment.DEVELOPMENT) === Environment.DEVELOPMENT);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Sets an environment variable (only affects runtime memory).
|
|
27
|
+
*
|
|
28
|
+
* @param {string} key - The environment variable key.
|
|
29
|
+
* @param {string} value - The value to set.
|
|
30
|
+
*/
|
|
31
|
+
static set(key, value) {
|
|
32
|
+
process.env[key] = value;
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Returns all environment variables as an object.
|
|
36
|
+
*
|
|
37
|
+
* @returns {object} The current `process.env` object.
|
|
38
|
+
*/
|
|
39
|
+
static getAll() {
|
|
40
|
+
return process.env;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Retrieves a string environment variable with a fallback default.
|
|
44
|
+
*
|
|
45
|
+
* @param {string} key - The environment variable key.
|
|
46
|
+
* @param {string} [defaultValue] - Value to return if the key is missing.
|
|
47
|
+
* @returns {string | undefined} The env value or the fallback.
|
|
48
|
+
*/
|
|
49
|
+
static get(key, defaultValue) {
|
|
50
|
+
var _a;
|
|
51
|
+
return (_a = process.env[key]) !== null && _a !== void 0 ? _a : defaultValue;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Retrieves a string environment variable and throws if it's missing.
|
|
55
|
+
*
|
|
56
|
+
* @param {string} key - The environment variable key.
|
|
57
|
+
* @returns {string} The environment variable's value.
|
|
58
|
+
* @throws {Error} If the variable is not defined.
|
|
59
|
+
*/
|
|
60
|
+
static getRequired(key) {
|
|
61
|
+
const value = process.env[key];
|
|
62
|
+
if (value === undefined) {
|
|
63
|
+
throw new Error(`Required env ${key} is missing`);
|
|
64
|
+
}
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Retrieves an environment variable as a number, or returns a default.
|
|
69
|
+
*
|
|
70
|
+
* @param {string} key - The environment variable key.
|
|
71
|
+
* @param {number} defaultValue - Fallback number if key is not present.
|
|
72
|
+
* @returns {number} Parsed numeric value or default.
|
|
73
|
+
* @throws {Error} If the value is not a valid number.
|
|
74
|
+
*/
|
|
75
|
+
static getNumber(key, defaultValue) {
|
|
76
|
+
var _a;
|
|
77
|
+
const value = (_a = process.env[key]) !== null && _a !== void 0 ? _a : defaultValue;
|
|
78
|
+
const numberValue = Number(value);
|
|
79
|
+
if (isNaN(numberValue)) {
|
|
80
|
+
throw new Error(`Env ${key} is not a number`);
|
|
81
|
+
}
|
|
82
|
+
return numberValue;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Retrieves a required environment variable as a number.
|
|
86
|
+
*
|
|
87
|
+
* @param {string} key - The environment variable key.
|
|
88
|
+
* @returns {number} Parsed number.
|
|
89
|
+
* @throws {Error} If the value is missing or not a number.
|
|
90
|
+
*/
|
|
91
|
+
static getNumberRequired(key) {
|
|
92
|
+
const value = process.env[key];
|
|
93
|
+
if (value === undefined) {
|
|
94
|
+
throw new Error(`Required env ${key} is missing`);
|
|
95
|
+
}
|
|
96
|
+
const numberValue = Number(value);
|
|
97
|
+
if (isNaN(numberValue)) {
|
|
98
|
+
throw new Error(`Required env ${key} is not a number`);
|
|
99
|
+
}
|
|
100
|
+
return numberValue;
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Retrieves an environment variable as a boolean.
|
|
104
|
+
* Accepts `true`, `1`, `yes`, `on` as true; `false`, `0`, `no`, `off` as false.
|
|
105
|
+
*
|
|
106
|
+
* @param {string} key - The environment variable key.
|
|
107
|
+
* @param {boolean} [defaultValue=false] - Optional fallback value if key is missing.
|
|
108
|
+
* @returns {boolean} Parsed boolean.
|
|
109
|
+
* @throws {Error} If the value is not a recognized boolean string.
|
|
110
|
+
*/
|
|
111
|
+
static getBoolean(key, defaultValue = false) {
|
|
112
|
+
var _a;
|
|
113
|
+
const value = ((_a = process.env[key]) !== null && _a !== void 0 ? _a : defaultValue).toString().toLowerCase();
|
|
114
|
+
if (["true", "1", "yes", "on"].includes(value))
|
|
115
|
+
return true;
|
|
116
|
+
if (["false", "0", "no", "off"].includes(value))
|
|
117
|
+
return false;
|
|
118
|
+
throw new Error(`Env variable ${key} is not a boolean`);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Retrieves a required environment variable as a boolean.
|
|
122
|
+
*
|
|
123
|
+
* @param {string} key - The environment variable key.
|
|
124
|
+
* @returns {boolean} Parsed boolean value.
|
|
125
|
+
* @throws {Error} If missing or invalid.
|
|
126
|
+
*/
|
|
127
|
+
static getBooleanRequired(key) {
|
|
128
|
+
const value = process.env[key];
|
|
129
|
+
if (value === undefined) {
|
|
130
|
+
throw new Error(`Required env ${key} is missing`);
|
|
131
|
+
}
|
|
132
|
+
return this.getBoolean(key); // reuse logic
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Parses a stringified JSON object from an environment variable.
|
|
136
|
+
*
|
|
137
|
+
* @typeParam T - The type to parse as (defaults to `object`).
|
|
138
|
+
* @param {string} key - The environment variable key.
|
|
139
|
+
* @param {T} defaultValue - Value to return if key is missing.
|
|
140
|
+
* @returns {T} Parsed object or default.
|
|
141
|
+
* @throws {Error} If the value is not valid JSON.
|
|
142
|
+
*/
|
|
143
|
+
static getJSON(key, defaultValue) {
|
|
144
|
+
const v = process.env[key];
|
|
145
|
+
if (v !== undefined) {
|
|
146
|
+
try {
|
|
147
|
+
return JSON.parse(v);
|
|
148
|
+
}
|
|
149
|
+
catch (_a) {
|
|
150
|
+
throw new Error(`Env variable ${key} is not a valid JSON string`);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return defaultValue;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Parses a comma-separated string as an array.
|
|
157
|
+
*
|
|
158
|
+
* @typeParam T - The item type (optional, defaults to string).
|
|
159
|
+
* @param {string} key - The environment variable key.
|
|
160
|
+
* @param {T[]} [defaultValue=[]] - Array to return if value is empty or missing.
|
|
161
|
+
* @param {string} [splitter=','] - Delimiter to split on.
|
|
162
|
+
* @returns {string[] | T[]} An array of strings.
|
|
163
|
+
*/
|
|
164
|
+
static getArray(key, defaultValue = [], splitter = ",") {
|
|
165
|
+
const value = process.env[key];
|
|
166
|
+
if (!value || value.trim() === "") {
|
|
167
|
+
return defaultValue;
|
|
168
|
+
}
|
|
169
|
+
return value
|
|
170
|
+
.split(splitter)
|
|
171
|
+
.map((item) => item.trim())
|
|
172
|
+
.filter((item) => item.length > 0);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Retrieves an enum-like environment variable value, validating against allowed values.
|
|
176
|
+
*
|
|
177
|
+
* @typeParam T - The allowed value type (string literal types).
|
|
178
|
+
* @param {string} key - The environment variable key.
|
|
179
|
+
* @param {T[]} allowedValues - Array of accepted string values.
|
|
180
|
+
* @param {T} [defaultValue] - Optional fallback value.
|
|
181
|
+
* @returns {T} The validated environment value.
|
|
182
|
+
* @throws {Error} If missing or invalid.
|
|
183
|
+
*/
|
|
184
|
+
static getEnum(key, allowedValues, defaultValue) {
|
|
185
|
+
var _a;
|
|
186
|
+
const value = (_a = process.env[key]) !== null && _a !== void 0 ? _a : defaultValue;
|
|
187
|
+
if (!value || !allowedValues.includes(value)) {
|
|
188
|
+
throw new Error(`Env ${key} must be one of ${allowedValues.join(", ")}. Received: ${value}`);
|
|
189
|
+
}
|
|
190
|
+
return value;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Checks whether the specified environment variable exists.
|
|
194
|
+
*
|
|
195
|
+
* @param {string} key - The environment variable key.
|
|
196
|
+
* @returns {boolean} `true` if the variable is defined, otherwise `false`.
|
|
197
|
+
*/
|
|
198
|
+
static has(key) {
|
|
199
|
+
return process.env[key] !== undefined;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Deletes the given environment variable (useful in tests).
|
|
203
|
+
*
|
|
204
|
+
* @param {string} key - The environment variable key to delete.
|
|
205
|
+
* @returns {void}
|
|
206
|
+
*/
|
|
207
|
+
static delete(key) {
|
|
208
|
+
delete process.env[key];
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
//# sourceMappingURL=env.utils.js.map
|
|
@@ -0,0 +1 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;IACvC,CAAC;IAED;;;;;;;;;OASG;IACH,MAAM,CAAC,OAAO,CACZ,GAAW,EACX,aAAkB,EAClB,YAAgB;;QAEhB,MAAM,KAAK,GAAG,MAAA,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,mCAAI,YAAY,CAAC;QAC/C,IAAI,CAAC,KAAK,IAAI,CAAC,aAAa,CAAC,QAAQ,CAAC,KAAU,CAAC,EAAE,CAAC;YAClD,MAAM,IAAI,KAAK,CACb,OAAO,GAAG,mBAAmB,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,eAAe,KAAK,EAAE,CAC5E,CAAC;QACJ,CAAC;QACD,OAAO,KAAU,CAAC;IACpB,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,GAAG,CAAC,GAAW;QACpB,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,SAAS,CAAC;IACxC,CAAC;IAED;;;;;OAKG;IACH,MAAM,CAAC,MAAM,CAAC,GAAW;QACvB,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;CACF","sourcesContent":["/* eslint-disable n/no-process-env */\n\n/**\n * Enum representing valid application environments.\n */\nexport enum Environment {\n PRODUCTION = \"production\",\n DEVELOPMENT = \"development\",\n STAGING = \"staging\",\n TESTING = \"testing\",\n}\n\n/**\n * Utility class for accessing and managing environment variables.\n * Provides typed getters with fallback defaults and validation.\n */\nexport class Env {\n /**\n * Checks if the current NODE_ENV is 'development'.\n *\n * @returns {boolean} `true` if NODE_ENV is 'development', else `false`.\n */\n static isDev(): boolean {\n return (\n Env.get(\"NODE_ENV\", Environment.DEVELOPMENT) === Environment.DEVELOPMENT\n );\n }\n\n /**\n * Sets an environment variable (only affects runtime memory).\n *\n * @param {string} key - The environment variable key.\n * @param {string} value - The value to set.\n */\n static set(key: string, value: string): void {\n process.env[key] = value;\n }\n\n /**\n * Returns all environment variables as an object.\n *\n * @returns {object} The current `process.env` object.\n */\n static getAll(): object {\n return process.env;\n }\n\n /**\n * Retrieves a string environment variable with a fallback default.\n *\n * @param {string} key - The environment variable key.\n * @param {string} [defaultValue] - Value to return if the key is missing.\n * @returns {string | undefined} The env value or the fallback.\n */\n static get(key: string, defaultValue?: string): string | undefined {\n return process.env[key] ?? defaultValue;\n }\n\n /**\n * Retrieves a string environment variable and throws if it's missing.\n *\n * @param {string} key - The environment variable key.\n * @returns {string} The environment variable's value.\n * @throws {Error} If the variable is not defined.\n */\n static getRequired(key: string): string {\n const value = process.env[key];\n if (value === undefined) {\n throw new Error(`Required env ${key} is missing`);\n }\n return value;\n }\n\n /**\n * Retrieves an environment variable as a number, or returns a default.\n *\n * @param {string} key - The environment variable key.\n * @param {number} defaultValue - Fallback number if key is not present.\n * @returns {number} Parsed numeric value or default.\n * @throws {Error} If the value is not a valid number.\n */\n static getNumber(key: string, defaultValue: number): number {\n const value = process.env[key] ?? defaultValue;\n const numberValue = Number(value);\n if (isNaN(numberValue)) {\n throw new Error(`Env ${key} is not a number`);\n }\n return numberValue;\n }\n\n /**\n * Retrieves a required environment variable as a number.\n *\n * @param {string} key - The environment variable key.\n * @returns {number} Parsed number.\n * @throws {Error} If the value is missing or not a number.\n */\n static getNumberRequired(key: string): number {\n const value = process.env[key];\n if (value === undefined) {\n throw new Error(`Required env ${key} is missing`);\n }\n const numberValue = Number(value);\n if (isNaN(numberValue)) {\n throw new Error(`Required env ${key} is not a number`);\n }\n return numberValue;\n }\n\n /**\n * Retrieves an environment variable as a boolean.\n * Accepts `true`, `1`, `yes`, `on` as true; `false`, `0`, `no`, `off` as false.\n *\n * @param {string} key - The environment variable key.\n * @param {boolean} [defaultValue=false] - Optional fallback value if key is missing.\n * @returns {boolean} Parsed boolean.\n * @throws {Error} If the value is not a recognized boolean string.\n */\n static getBoolean(key: string, defaultValue = false): boolean {\n const value = (process.env[key] ?? defaultValue).toString().toLowerCase();\n if ([\"true\", \"1\", \"yes\", \"on\"].includes(value)) return true;\n if ([\"false\", \"0\", \"no\", \"off\"].includes(value)) return false;\n throw new Error(`Env variable ${key} is not a boolean`);\n }\n\n /**\n * Retrieves a required environment variable as a boolean.\n *\n * @param {string} key - The environment variable key.\n * @returns {boolean} Parsed boolean value.\n * @throws {Error} If missing or invalid.\n */\n static getBooleanRequired(key: string): boolean {\n const value = process.env[key];\n if (value === undefined) {\n throw new Error(`Required env ${key} is missing`);\n }\n return this.getBoolean(key); // reuse logic\n }\n\n /**\n * Parses a stringified JSON object from an environment variable.\n *\n * @typeParam T - The type to parse as (defaults to `object`).\n * @param {string} key - The environment variable key.\n * @param {T} defaultValue - Value to return if key is missing.\n * @returns {T} Parsed object or default.\n * @throws {Error} If the value is not valid JSON.\n */\n static getJSON<T extends object = object>(key: string, defaultValue: T): T {\n const v = process.env[key];\n if (v !== undefined) {\n try {\n return JSON.parse(v);\n } catch {\n throw new Error(`Env variable ${key} is not a valid JSON string`);\n }\n }\n return defaultValue;\n }\n\n /**\n * Parses a comma-separated string as an array.\n *\n * @typeParam T - The item type (optional, defaults to string).\n * @param {string} key - The environment variable key.\n * @param {T[]} [defaultValue=[]] - Array to return if value is empty or missing.\n * @param {string} [splitter=','] - Delimiter to split on.\n * @returns {string[] | T[]} An array of strings.\n */\n static getArray<T = string>(\n key: string,\n defaultValue: T[] = [],\n splitter = \",\",\n ): string[] | T[] {\n const value = process.env[key];\n if (!value || value.trim() === \"\") {\n return defaultValue;\n }\n return value\n .split(splitter)\n .map((item) => item.trim())\n .filter((item) => item.length > 0);\n }\n\n /**\n * Retrieves an enum-like environment variable value, validating against allowed values.\n *\n * @typeParam T - The allowed value type (string literal types).\n * @param {string} key - The environment variable key.\n * @param {T[]} allowedValues - Array of accepted string values.\n * @param {T} [defaultValue] - Optional fallback value.\n * @returns {T} The validated environment value.\n * @throws {Error} If missing or invalid.\n */\n static getEnum<T extends string>(\n key: string,\n allowedValues: T[],\n defaultValue?: T,\n ): T {\n const value = process.env[key] ?? defaultValue;\n if (!value || !allowedValues.includes(value as T)) {\n throw new Error(\n `Env ${key} must be one of ${allowedValues.join(\", \")}. Received: ${value}`,\n );\n }\n return value as T;\n }\n\n /**\n * Checks whether the specified environment variable exists.\n *\n * @param {string} key - The environment variable key.\n * @returns {boolean} `true` if the variable is defined, otherwise `false`.\n */\n static has(key: string): boolean {\n return process.env[key] !== undefined;\n }\n\n /**\n * Deletes the given environment variable (useful in tests).\n *\n * @param {string} key - The environment variable key to delete.\n * @returns {void}\n */\n static delete(key: string): void {\n delete process.env[key];\n }\n}\n"]}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { ErrorResponse } from "./response.utils";
|
|
2
|
+
/**
|
|
3
|
+
* Generic HTTP error class used for custom exceptions with any status code.
|
|
4
|
+
* Inherit this when you need to throw errors dynamically at runtime.
|
|
5
|
+
*/
|
|
6
|
+
export declare class HttpError extends ErrorResponse {
|
|
7
|
+
/**
|
|
8
|
+
* Creates a new HTTP error instance.
|
|
9
|
+
* @param status - A valid HTTP status code (e.g., 400, 500).
|
|
10
|
+
* @param message - The error message to return to the client.
|
|
11
|
+
*/
|
|
12
|
+
constructor(status: number, message: string);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Represents a 500 Internal Server Error.
|
|
16
|
+
* Use this for unexpected backend errors that are not caused by the client.
|
|
17
|
+
*/
|
|
18
|
+
export declare class InternalServerErrorException extends ErrorResponse {
|
|
19
|
+
/**
|
|
20
|
+
* @param message - Optional custom message. Defaults to "Internal server error".
|
|
21
|
+
*/
|
|
22
|
+
constructor(message?: string);
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Represents a 401 Unauthorized Error.
|
|
26
|
+
* Indicates that authentication is required and has failed or not been provided.
|
|
27
|
+
*/
|
|
28
|
+
export declare class UnauthorizedException extends ErrorResponse {
|
|
29
|
+
/**
|
|
30
|
+
* @param message - Optional custom message. Defaults to "Unauthorized".
|
|
31
|
+
*/
|
|
32
|
+
constructor(message?: string);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* Represents a 400 Bad Request Error.
|
|
36
|
+
* Indicates that the client has sent invalid data (e.g., malformed request body).
|
|
37
|
+
*/
|
|
38
|
+
export declare class BadRequestException extends ErrorResponse {
|
|
39
|
+
/**
|
|
40
|
+
* @param message - Optional custom message. Defaults to "Bad request".
|
|
41
|
+
*/
|
|
42
|
+
constructor(message?: string);
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Represents a 404 Not Found Error.
|
|
46
|
+
* Used when a requested resource (e.g., user, file) could not be located.
|
|
47
|
+
*/
|
|
48
|
+
export declare class NotFoundException extends ErrorResponse {
|
|
49
|
+
/**
|
|
50
|
+
* @param message - Optional custom message. Defaults to "Resource not found".
|
|
51
|
+
*/
|
|
52
|
+
constructor(message?: string);
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Represents a 403 Forbidden Error.
|
|
56
|
+
* Indicates that the client is authenticated but not allowed to access the resource.
|
|
57
|
+
*/
|
|
58
|
+
export declare class ForbiddenException extends ErrorResponse {
|
|
59
|
+
/**
|
|
60
|
+
* @param message - Optional custom message. Defaults to "Forbidden".
|
|
61
|
+
*/
|
|
62
|
+
constructor(message?: string);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Represents a 409 Conflict Error.
|
|
66
|
+
* Commonly used when a resource already exists or a versioning conflict occurs.
|
|
67
|
+
*/
|
|
68
|
+
export declare class ConflictException extends ErrorResponse {
|
|
69
|
+
/**
|
|
70
|
+
* @param message - Optional custom message. Defaults to "Conflict".
|
|
71
|
+
*/
|
|
72
|
+
constructor(message?: string);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Represents a 502 Bad Gateway Error.
|
|
76
|
+
* Used when the server receives an invalid response from an upstream server.
|
|
77
|
+
*/
|
|
78
|
+
export declare class BadGatewayException extends ErrorResponse {
|
|
79
|
+
/**
|
|
80
|
+
* @param message - Optional custom message. Defaults to "Bad Gateway".
|
|
81
|
+
*/
|
|
82
|
+
constructor(message?: string);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Represents a 429 Too Many Requests Error.
|
|
86
|
+
* Returned when the client has hit a rate limit.
|
|
87
|
+
*/
|
|
88
|
+
export declare class TooManyRequestsException extends ErrorResponse {
|
|
89
|
+
/**
|
|
90
|
+
* @param message - Optional custom message. Defaults to "Too many requests".
|
|
91
|
+
*/
|
|
92
|
+
constructor(message?: string);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Represents a 503 Service Unavailable Error.
|
|
96
|
+
* Indicates that the server is temporarily unavailable (e.g., for maintenance).
|
|
97
|
+
*/
|
|
98
|
+
export declare class ServiceUnavailableException extends ErrorResponse {
|
|
99
|
+
/**
|
|
100
|
+
* @param message - Optional custom message. Defaults to "Service Unavailable".
|
|
101
|
+
*/
|
|
102
|
+
constructor(message?: string);
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Represents a 504 Gateway Timeout Error.
|
|
106
|
+
* Returned when the server acting as a gateway times out waiting for a response.
|
|
107
|
+
*/
|
|
108
|
+
export declare class GatewayTimeoutException extends ErrorResponse {
|
|
109
|
+
/**
|
|
110
|
+
* @param message - Optional custom message. Defaults to "Gateway Timeout".
|
|
111
|
+
*/
|
|
112
|
+
constructor(message?: string);
|
|
113
|
+
}
|
|
114
|
+
//# sourceMappingURL=exception.utils.d.ts.map
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { HttpStatusCodes } from "./http-status-codes";
|
|
2
|
+
import { ErrorResponse } from "./response.utils";
|
|
3
|
+
/**
|
|
4
|
+
* Generic HTTP error class used for custom exceptions with any status code.
|
|
5
|
+
* Inherit this when you need to throw errors dynamically at runtime.
|
|
6
|
+
*/
|
|
7
|
+
export class HttpError extends ErrorResponse {
|
|
8
|
+
/**
|
|
9
|
+
* Creates a new HTTP error instance.
|
|
10
|
+
* @param status - A valid HTTP status code (e.g., 400, 500).
|
|
11
|
+
* @param message - The error message to return to the client.
|
|
12
|
+
*/
|
|
13
|
+
constructor(status, message) {
|
|
14
|
+
super(message);
|
|
15
|
+
this.status = status;
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Represents a 500 Internal Server Error.
|
|
20
|
+
* Use this for unexpected backend errors that are not caused by the client.
|
|
21
|
+
*/
|
|
22
|
+
export class InternalServerErrorException extends ErrorResponse {
|
|
23
|
+
/**
|
|
24
|
+
* @param message - Optional custom message. Defaults to "Internal server error".
|
|
25
|
+
*/
|
|
26
|
+
constructor(message = "Internal server error") {
|
|
27
|
+
super(message);
|
|
28
|
+
this.status = HttpStatusCodes.INTERNAL_SERVER_ERROR;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Represents a 401 Unauthorized Error.
|
|
33
|
+
* Indicates that authentication is required and has failed or not been provided.
|
|
34
|
+
*/
|
|
35
|
+
export class UnauthorizedException extends ErrorResponse {
|
|
36
|
+
/**
|
|
37
|
+
* @param message - Optional custom message. Defaults to "Unauthorized".
|
|
38
|
+
*/
|
|
39
|
+
constructor(message = "Unauthorized") {
|
|
40
|
+
super(message);
|
|
41
|
+
this.status = HttpStatusCodes.UNAUTHORIZED;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Represents a 400 Bad Request Error.
|
|
46
|
+
* Indicates that the client has sent invalid data (e.g., malformed request body).
|
|
47
|
+
*/
|
|
48
|
+
export class BadRequestException extends ErrorResponse {
|
|
49
|
+
/**
|
|
50
|
+
* @param message - Optional custom message. Defaults to "Bad request".
|
|
51
|
+
*/
|
|
52
|
+
constructor(message = "Bad request") {
|
|
53
|
+
super(message);
|
|
54
|
+
this.status = HttpStatusCodes.BAD_REQUEST;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Represents a 404 Not Found Error.
|
|
59
|
+
* Used when a requested resource (e.g., user, file) could not be located.
|
|
60
|
+
*/
|
|
61
|
+
export class NotFoundException extends ErrorResponse {
|
|
62
|
+
/**
|
|
63
|
+
* @param message - Optional custom message. Defaults to "Resource not found".
|
|
64
|
+
*/
|
|
65
|
+
constructor(message = "Resource not found") {
|
|
66
|
+
super(message);
|
|
67
|
+
this.status = HttpStatusCodes.NOT_FOUND;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Represents a 403 Forbidden Error.
|
|
72
|
+
* Indicates that the client is authenticated but not allowed to access the resource.
|
|
73
|
+
*/
|
|
74
|
+
export class ForbiddenException extends ErrorResponse {
|
|
75
|
+
/**
|
|
76
|
+
* @param message - Optional custom message. Defaults to "Forbidden".
|
|
77
|
+
*/
|
|
78
|
+
constructor(message = "Forbidden") {
|
|
79
|
+
super(message);
|
|
80
|
+
this.status = HttpStatusCodes.FORBIDDEN;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* Represents a 409 Conflict Error.
|
|
85
|
+
* Commonly used when a resource already exists or a versioning conflict occurs.
|
|
86
|
+
*/
|
|
87
|
+
export class ConflictException extends ErrorResponse {
|
|
88
|
+
/**
|
|
89
|
+
* @param message - Optional custom message. Defaults to "Conflict".
|
|
90
|
+
*/
|
|
91
|
+
constructor(message = "Conflict") {
|
|
92
|
+
super(message);
|
|
93
|
+
this.status = HttpStatusCodes.CONFLICT;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Represents a 502 Bad Gateway Error.
|
|
98
|
+
* Used when the server receives an invalid response from an upstream server.
|
|
99
|
+
*/
|
|
100
|
+
export class BadGatewayException extends ErrorResponse {
|
|
101
|
+
/**
|
|
102
|
+
* @param message - Optional custom message. Defaults to "Bad Gateway".
|
|
103
|
+
*/
|
|
104
|
+
constructor(message = "Bad Gateway") {
|
|
105
|
+
super(message);
|
|
106
|
+
this.status = HttpStatusCodes.BAD_GATEWAY;
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Represents a 429 Too Many Requests Error.
|
|
111
|
+
* Returned when the client has hit a rate limit.
|
|
112
|
+
*/
|
|
113
|
+
export class TooManyRequestsException extends ErrorResponse {
|
|
114
|
+
/**
|
|
115
|
+
* @param message - Optional custom message. Defaults to "Too many requests".
|
|
116
|
+
*/
|
|
117
|
+
constructor(message = "Too many requests") {
|
|
118
|
+
super(message);
|
|
119
|
+
this.status = HttpStatusCodes.TOO_MANY_REQUESTS;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Represents a 503 Service Unavailable Error.
|
|
124
|
+
* Indicates that the server is temporarily unavailable (e.g., for maintenance).
|
|
125
|
+
*/
|
|
126
|
+
export class ServiceUnavailableException extends ErrorResponse {
|
|
127
|
+
/**
|
|
128
|
+
* @param message - Optional custom message. Defaults to "Service Unavailable".
|
|
129
|
+
*/
|
|
130
|
+
constructor(message = "Service Unavailable") {
|
|
131
|
+
super(message);
|
|
132
|
+
this.status = HttpStatusCodes.SERVICE_UNAVAILABLE;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/**
|
|
136
|
+
* Represents a 504 Gateway Timeout Error.
|
|
137
|
+
* Returned when the server acting as a gateway times out waiting for a response.
|
|
138
|
+
*/
|
|
139
|
+
export class GatewayTimeoutException extends ErrorResponse {
|
|
140
|
+
/**
|
|
141
|
+
* @param message - Optional custom message. Defaults to "Gateway Timeout".
|
|
142
|
+
*/
|
|
143
|
+
constructor(message = "Gateway Timeout") {
|
|
144
|
+
super(message);
|
|
145
|
+
this.status = HttpStatusCodes.GATEWAY_TIMEOUT;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
//# sourceMappingURL=exception.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"exception.utils.js","sourceRoot":"","sources":["../../../src/utils/exception.utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,aAAa,EA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O,iBAAkB,SAAQ,aAAa;IAClD;;OAEG;IACH,YAAY,UAAkB,UAAU;QACtC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC,QAAQ,CAAC;IACzC,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,mBAAoB,SAAQ,aAAa;IACpD;;OAEG;IACH,YAAY,UAAkB,aAAa;QACzC,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC,WAAW,CAAC;IAC5C,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,wBAAyB,SAAQ,aAAa;IACzD;;OAEG;IACH,YAAY,UAAkB,mBAAmB;QAC/C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC,iBAAiB,CAAC;IAClD,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,2BAA4B,SAAQ,aAAa;IAC5D;;OAEG;IACH,YAAY,UAAkB,qBAAqB;QACjD,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC,mBAAmB,CAAC;IACpD,CAAC;CACF;AAED;;;GAGG;AACH,MAAM,OAAO,uBAAwB,SAAQ,aAAa;IACxD;;OAEG;IACH,YAAY,UAAkB,iBAAiB;QAC7C,KAAK,CAAC,OAAO,CAAC,CAAC;QACf,IAAI,CAAC,MAAM,GAAG,eAAe,CAAC,eAAe,CAAC;IAChD,CAAC;CACF","sourcesContent":["import { HttpStatusCodes } from \"./http-status-codes\";\nimport { ErrorResponse } from \"./response.utils\";\n\n/**\n * Generic HTTP error class used for custom exceptions with any status code.\n * Inherit this when you need to throw errors dynamically at runtime.\n */\nexport class HttpError extends ErrorResponse {\n /**\n * Creates a new HTTP error instance.\n * @param status - A valid HTTP status code (e.g., 400, 500).\n * @param message - The error message to return to the client.\n */\n constructor(status: number, message: string) {\n super(message);\n this.status = status;\n }\n}\n\n/**\n * Represents a 500 Internal Server Error.\n * Use this for unexpected backend errors that are not caused by the client.\n */\nexport class InternalServerErrorException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Internal server error\".\n */\n constructor(message: string = \"Internal server error\") {\n super(message);\n this.status = HttpStatusCodes.INTERNAL_SERVER_ERROR;\n }\n}\n\n/**\n * Represents a 401 Unauthorized Error.\n * Indicates that authentication is required and has failed or not been provided.\n */\nexport class UnauthorizedException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Unauthorized\".\n */\n constructor(message: string = \"Unauthorized\") {\n super(message);\n this.status = HttpStatusCodes.UNAUTHORIZED;\n }\n}\n\n/**\n * Represents a 400 Bad Request Error.\n * Indicates that the client has sent invalid data (e.g., malformed request body).\n */\nexport class BadRequestException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Bad request\".\n */\n constructor(message: string = \"Bad request\") {\n super(message);\n this.status = HttpStatusCodes.BAD_REQUEST;\n }\n}\n\n/**\n * Represents a 404 Not Found Error.\n * Used when a requested resource (e.g., user, file) could not be located.\n */\nexport class NotFoundException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Resource not found\".\n */\n constructor(message: string = \"Resource not found\") {\n super(message);\n this.status = HttpStatusCodes.NOT_FOUND;\n }\n}\n\n/**\n * Represents a 403 Forbidden Error.\n * Indicates that the client is authenticated but not allowed to access the resource.\n */\nexport class ForbiddenException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Forbidden\".\n */\n constructor(message: string = \"Forbidden\") {\n super(message);\n this.status = HttpStatusCodes.FORBIDDEN;\n }\n}\n\n/**\n * Represents a 409 Conflict Error.\n * Commonly used when a resource already exists or a versioning conflict occurs.\n */\nexport class ConflictException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Conflict\".\n */\n constructor(message: string = \"Conflict\") {\n super(message);\n this.status = HttpStatusCodes.CONFLICT;\n }\n}\n\n/**\n * Represents a 502 Bad Gateway Error.\n * Used when the server receives an invalid response from an upstream server.\n */\nexport class BadGatewayException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Bad Gateway\".\n */\n constructor(message: string = \"Bad Gateway\") {\n super(message);\n this.status = HttpStatusCodes.BAD_GATEWAY;\n }\n}\n\n/**\n * Represents a 429 Too Many Requests Error.\n * Returned when the client has hit a rate limit.\n */\nexport class TooManyRequestsException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Too many requests\".\n */\n constructor(message: string = \"Too many requests\") {\n super(message);\n this.status = HttpStatusCodes.TOO_MANY_REQUESTS;\n }\n}\n\n/**\n * Represents a 503 Service Unavailable Error.\n * Indicates that the server is temporarily unavailable (e.g., for maintenance).\n */\nexport class ServiceUnavailableException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Service Unavailable\".\n */\n constructor(message: string = \"Service Unavailable\") {\n super(message);\n this.status = HttpStatusCodes.SERVICE_UNAVAILABLE;\n }\n}\n\n/**\n * Represents a 504 Gateway Timeout Error.\n * Returned when the server acting as a gateway times out waiting for a response.\n */\nexport class GatewayTimeoutException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Gateway Timeout\".\n */\n constructor(message: string = \"Gateway Timeout\") {\n super(message);\n this.status = HttpStatusCodes.GATEWAY_TIMEOUT;\n }\n}\n"]}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Checks whether a file or directory exists at the given path.
|
|
3
|
+
*
|
|
4
|
+
* @param {string} path - The file or directory path to check.
|
|
5
|
+
* @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.
|
|
6
|
+
*/
|
|
7
|
+
export declare function fileExists(path: string): Promise<boolean>;
|
|
8
|
+
/**
|
|
9
|
+
* Reads and parses a JSON file from the specified path.
|
|
10
|
+
*
|
|
11
|
+
* @typeParam T - The expected type of the parsed JSON object.
|
|
12
|
+
* @param {string} path - The path to the JSON file.
|
|
13
|
+
* @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.
|
|
14
|
+
*/
|
|
15
|
+
export declare function readJsonFile<T = any>(path: string): Promise<T | null>;
|
|
16
|
+
/**
|
|
17
|
+
* Writes a JavaScript object to a file as formatted JSON.
|
|
18
|
+
*
|
|
19
|
+
* @param {string} path - The destination file path.
|
|
20
|
+
* @param {any} data - The object to serialize and write.
|
|
21
|
+
* @param {number} [space=2] - Number of spaces for JSON formatting.
|
|
22
|
+
* @returns {Promise<void>} Resolves when writing is complete.
|
|
23
|
+
*/
|
|
24
|
+
export declare function writeJsonFile(path: string, data: any, space?: number): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Deletes a file if it exists.
|
|
27
|
+
*
|
|
28
|
+
* @param {string} path - The file path to delete.
|
|
29
|
+
* @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.
|
|
30
|
+
*/
|
|
31
|
+
export declare function deleteFileIfExists(path: string): Promise<boolean>;
|
|
32
|
+
//# sourceMappingURL=fs.utils.d.ts.map
|