@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,218 @@
|
|
|
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
|
+
var Env = /** @class */ (function () {
|
|
17
|
+
function Env() {
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Checks if the current NODE_ENV is 'development'.
|
|
21
|
+
*
|
|
22
|
+
* @returns {boolean} `true` if NODE_ENV is 'development', else `false`.
|
|
23
|
+
*/
|
|
24
|
+
Env.isDev = function () {
|
|
25
|
+
return (Env.get("NODE_ENV", Environment.DEVELOPMENT) === Environment.DEVELOPMENT);
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* Sets an environment variable (only affects runtime memory).
|
|
29
|
+
*
|
|
30
|
+
* @param {string} key - The environment variable key.
|
|
31
|
+
* @param {string} value - The value to set.
|
|
32
|
+
*/
|
|
33
|
+
Env.set = function (key, value) {
|
|
34
|
+
process.env[key] = value;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* Returns all environment variables as an object.
|
|
38
|
+
*
|
|
39
|
+
* @returns {object} The current `process.env` object.
|
|
40
|
+
*/
|
|
41
|
+
Env.getAll = function () {
|
|
42
|
+
return process.env;
|
|
43
|
+
};
|
|
44
|
+
/**
|
|
45
|
+
* Retrieves a string environment variable with a fallback default.
|
|
46
|
+
*
|
|
47
|
+
* @param {string} key - The environment variable key.
|
|
48
|
+
* @param {string} [defaultValue] - Value to return if the key is missing.
|
|
49
|
+
* @returns {string | undefined} The env value or the fallback.
|
|
50
|
+
*/
|
|
51
|
+
Env.get = function (key, defaultValue) {
|
|
52
|
+
var _a;
|
|
53
|
+
return (_a = process.env[key]) !== null && _a !== void 0 ? _a : defaultValue;
|
|
54
|
+
};
|
|
55
|
+
/**
|
|
56
|
+
* Retrieves a string environment variable and throws if it's missing.
|
|
57
|
+
*
|
|
58
|
+
* @param {string} key - The environment variable key.
|
|
59
|
+
* @returns {string} The environment variable's value.
|
|
60
|
+
* @throws {Error} If the variable is not defined.
|
|
61
|
+
*/
|
|
62
|
+
Env.getRequired = function (key) {
|
|
63
|
+
var value = process.env[key];
|
|
64
|
+
if (value === undefined) {
|
|
65
|
+
throw new Error("Required env ".concat(key, " is missing"));
|
|
66
|
+
}
|
|
67
|
+
return value;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* Retrieves an environment variable as a number, or returns a default.
|
|
71
|
+
*
|
|
72
|
+
* @param {string} key - The environment variable key.
|
|
73
|
+
* @param {number} defaultValue - Fallback number if key is not present.
|
|
74
|
+
* @returns {number} Parsed numeric value or default.
|
|
75
|
+
* @throws {Error} If the value is not a valid number.
|
|
76
|
+
*/
|
|
77
|
+
Env.getNumber = function (key, defaultValue) {
|
|
78
|
+
var _a;
|
|
79
|
+
var value = (_a = process.env[key]) !== null && _a !== void 0 ? _a : defaultValue;
|
|
80
|
+
var numberValue = Number(value);
|
|
81
|
+
if (isNaN(numberValue)) {
|
|
82
|
+
throw new Error("Env ".concat(key, " is not a number"));
|
|
83
|
+
}
|
|
84
|
+
return numberValue;
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Retrieves a required environment variable as a number.
|
|
88
|
+
*
|
|
89
|
+
* @param {string} key - The environment variable key.
|
|
90
|
+
* @returns {number} Parsed number.
|
|
91
|
+
* @throws {Error} If the value is missing or not a number.
|
|
92
|
+
*/
|
|
93
|
+
Env.getNumberRequired = function (key) {
|
|
94
|
+
var value = process.env[key];
|
|
95
|
+
if (value === undefined) {
|
|
96
|
+
throw new Error("Required env ".concat(key, " is missing"));
|
|
97
|
+
}
|
|
98
|
+
var numberValue = Number(value);
|
|
99
|
+
if (isNaN(numberValue)) {
|
|
100
|
+
throw new Error("Required env ".concat(key, " is not a number"));
|
|
101
|
+
}
|
|
102
|
+
return numberValue;
|
|
103
|
+
};
|
|
104
|
+
/**
|
|
105
|
+
* Retrieves an environment variable as a boolean.
|
|
106
|
+
* Accepts `true`, `1`, `yes`, `on` as true; `false`, `0`, `no`, `off` as false.
|
|
107
|
+
*
|
|
108
|
+
* @param {string} key - The environment variable key.
|
|
109
|
+
* @param {boolean} [defaultValue=false] - Optional fallback value if key is missing.
|
|
110
|
+
* @returns {boolean} Parsed boolean.
|
|
111
|
+
* @throws {Error} If the value is not a recognized boolean string.
|
|
112
|
+
*/
|
|
113
|
+
Env.getBoolean = function (key, defaultValue) {
|
|
114
|
+
var _a;
|
|
115
|
+
if (defaultValue === void 0) { defaultValue = false; }
|
|
116
|
+
var value = ((_a = process.env[key]) !== null && _a !== void 0 ? _a : defaultValue).toString().toLowerCase();
|
|
117
|
+
if (["true", "1", "yes", "on"].includes(value))
|
|
118
|
+
return true;
|
|
119
|
+
if (["false", "0", "no", "off"].includes(value))
|
|
120
|
+
return false;
|
|
121
|
+
throw new Error("Env variable ".concat(key, " is not a boolean"));
|
|
122
|
+
};
|
|
123
|
+
/**
|
|
124
|
+
* Retrieves a required environment variable as a boolean.
|
|
125
|
+
*
|
|
126
|
+
* @param {string} key - The environment variable key.
|
|
127
|
+
* @returns {boolean} Parsed boolean value.
|
|
128
|
+
* @throws {Error} If missing or invalid.
|
|
129
|
+
*/
|
|
130
|
+
Env.getBooleanRequired = function (key) {
|
|
131
|
+
var value = process.env[key];
|
|
132
|
+
if (value === undefined) {
|
|
133
|
+
throw new Error("Required env ".concat(key, " is missing"));
|
|
134
|
+
}
|
|
135
|
+
return this.getBoolean(key); // reuse logic
|
|
136
|
+
};
|
|
137
|
+
/**
|
|
138
|
+
* Parses a stringified JSON object from an environment variable.
|
|
139
|
+
*
|
|
140
|
+
* @typeParam T - The type to parse as (defaults to `object`).
|
|
141
|
+
* @param {string} key - The environment variable key.
|
|
142
|
+
* @param {T} defaultValue - Value to return if key is missing.
|
|
143
|
+
* @returns {T} Parsed object or default.
|
|
144
|
+
* @throws {Error} If the value is not valid JSON.
|
|
145
|
+
*/
|
|
146
|
+
Env.getJSON = function (key, defaultValue) {
|
|
147
|
+
var v = process.env[key];
|
|
148
|
+
if (v !== undefined) {
|
|
149
|
+
try {
|
|
150
|
+
return JSON.parse(v);
|
|
151
|
+
}
|
|
152
|
+
catch (_a) {
|
|
153
|
+
throw new Error("Env variable ".concat(key, " is not a valid JSON string"));
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
return defaultValue;
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
159
|
+
* Parses a comma-separated string as an array.
|
|
160
|
+
*
|
|
161
|
+
* @typeParam T - The item type (optional, defaults to string).
|
|
162
|
+
* @param {string} key - The environment variable key.
|
|
163
|
+
* @param {T[]} [defaultValue=[]] - Array to return if value is empty or missing.
|
|
164
|
+
* @param {string} [splitter=','] - Delimiter to split on.
|
|
165
|
+
* @returns {string[] | T[]} An array of strings.
|
|
166
|
+
*/
|
|
167
|
+
Env.getArray = function (key, defaultValue, splitter) {
|
|
168
|
+
if (defaultValue === void 0) { defaultValue = []; }
|
|
169
|
+
if (splitter === void 0) { splitter = ","; }
|
|
170
|
+
var value = process.env[key];
|
|
171
|
+
if (!value || value.trim() === "") {
|
|
172
|
+
return defaultValue;
|
|
173
|
+
}
|
|
174
|
+
return value
|
|
175
|
+
.split(splitter)
|
|
176
|
+
.map(function (item) { return item.trim(); })
|
|
177
|
+
.filter(function (item) { return item.length > 0; });
|
|
178
|
+
};
|
|
179
|
+
/**
|
|
180
|
+
* Retrieves an enum-like environment variable value, validating against allowed values.
|
|
181
|
+
*
|
|
182
|
+
* @typeParam T - The allowed value type (string literal types).
|
|
183
|
+
* @param {string} key - The environment variable key.
|
|
184
|
+
* @param {T[]} allowedValues - Array of accepted string values.
|
|
185
|
+
* @param {T} [defaultValue] - Optional fallback value.
|
|
186
|
+
* @returns {T} The validated environment value.
|
|
187
|
+
* @throws {Error} If missing or invalid.
|
|
188
|
+
*/
|
|
189
|
+
Env.getEnum = function (key, allowedValues, defaultValue) {
|
|
190
|
+
var _a;
|
|
191
|
+
var value = (_a = process.env[key]) !== null && _a !== void 0 ? _a : defaultValue;
|
|
192
|
+
if (!value || !allowedValues.includes(value)) {
|
|
193
|
+
throw new Error("Env ".concat(key, " must be one of ").concat(allowedValues.join(", "), ". Received: ").concat(value));
|
|
194
|
+
}
|
|
195
|
+
return value;
|
|
196
|
+
};
|
|
197
|
+
/**
|
|
198
|
+
* Checks whether the specified environment variable exists.
|
|
199
|
+
*
|
|
200
|
+
* @param {string} key - The environment variable key.
|
|
201
|
+
* @returns {boolean} `true` if the variable is defined, otherwise `false`.
|
|
202
|
+
*/
|
|
203
|
+
Env.has = function (key) {
|
|
204
|
+
return process.env[key] !== undefined;
|
|
205
|
+
};
|
|
206
|
+
/**
|
|
207
|
+
* Deletes the given environment variable (useful in tests).
|
|
208
|
+
*
|
|
209
|
+
* @param {string} key - The environment variable key to delete.
|
|
210
|
+
* @returns {void}
|
|
211
|
+
*/
|
|
212
|
+
Env.delete = function (key) {
|
|
213
|
+
delete process.env[key];
|
|
214
|
+
};
|
|
215
|
+
return Env;
|
|
216
|
+
}());
|
|
217
|
+
export { Env };
|
|
218
|
+
//# sourceMappingURL=env.utils.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.utils.js","sourceRoot":"","sources":["../../../src/utils/env.utils.ts"],"names":[],"mappings":"AAAA,qCAAqC;AAErC;;GAEG;AACH,MAAM,CAAN,IAAY,WAKX;AALD,WAAY,WAAW;IACrB,wCAAyB,CAAA;IACzB,0CAA2B,CAAA;IAC3B,kCAAmB,CAAA;IACnB,kCAAmB,CAAA;AACrB,CAAC,EALW,WAAW,KAAX,WAAW,QAKtB;AAED;;;GAGG;AACH;IAAA;IAoNA,CAAC;IAnNC;;;;OAIG;IACI,SAAK,GAAZ;QACE,OAAO,CACL,GAAG,CAAC,GAAG,CAAC,UAAU,EAAE,WAAW,CAAC,WAAW,CAAC,KAAK,WAAW,CAAC,WAAW,CACzE,CAAC;IACJ,CAAC;IAED;;;;;OAKG;IACI,OAAG,GAAV,UAAW,GAAW,EAAE,KAAa;QACnC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACI,UAAM,GAAb;QACE,OAAO,OAAO,CAAC,GAAG,CAAC;IACrB,CAAC;IAED;;;;;;OAMG;IACI,OAAG,GAAV,UAAW,GAAW,EAAE,YAAqB;;QAC3C,OAAO,MAAA,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,mCAAI,YAAY,CAAC;IAC1C,CAAC;IAED;;;;;;OAMG;IACI,eAAW,GAAlB,UAAmB,GAAW;QAC5B,IAAM,KAAK,GAA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/B,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,EAAE,CAAC;YAClC,OAAO,YAAY,CAAC;QACtB,CAAC;QACD,OAAO,KAAK;aACT,KAAK,CAAC,QAAQ,CAAC;aACf,GAAG,CAAC,UAAC,IAAI,IAAK,OAAA,IAAI,CAAC,IAAI,EAAE,EAAX,CAAW,CAAC;aAC1B,MAAM,CAAC,UAAC,IAAI,IAAK,OAAA,IAAI,CAAC,MAAM,GAAG,CAAC,EAAf,CAAe,CAAC,CAAC;IACvC,CAAC;IAED;;;;;;;;;OASG;IACI,WAAO,GAAd,UACE,GAAW,EACX,aAAkB,EAClB,YAAgB;;QAEhB,IAAM,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,cAAO,GAAG,6BAAmB,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,yBAAe,KAAK,CAAE,CAC5E,CAAC;QACJ,CAAC;QACD,OAAO,KAAU,CAAC;IACpB,CAAC;IAED;;;;;OAKG;IACI,OAAG,GAAV,UAAW,GAAW;QACpB,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,KAAK,SAAS,CAAC;IACxC,CAAC;IAED;;;;;OAKG;IACI,UAAM,GAAb,UAAc,GAAW;QACvB,OAAO,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;IAC1B,CAAC;IACH,UAAC;AAAD,CAAC,AApND,IAoNC","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,217 @@
|
|
|
1
|
+
var __extends = (this && this.__extends) || (function () {
|
|
2
|
+
var extendStatics = function (d, b) {
|
|
3
|
+
extendStatics = Object.setPrototypeOf ||
|
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
5
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
6
|
+
return extendStatics(d, b);
|
|
7
|
+
};
|
|
8
|
+
return function (d, b) {
|
|
9
|
+
if (typeof b !== "function" && b !== null)
|
|
10
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
11
|
+
extendStatics(d, b);
|
|
12
|
+
function __() { this.constructor = d; }
|
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
|
+
};
|
|
15
|
+
})();
|
|
16
|
+
import { HttpStatusCodes } from "./http-status-codes";
|
|
17
|
+
import { ErrorResponse } from "./response.utils";
|
|
18
|
+
/**
|
|
19
|
+
* Generic HTTP error class used for custom exceptions with any status code.
|
|
20
|
+
* Inherit this when you need to throw errors dynamically at runtime.
|
|
21
|
+
*/
|
|
22
|
+
var HttpError = /** @class */ (function (_super) {
|
|
23
|
+
__extends(HttpError, _super);
|
|
24
|
+
/**
|
|
25
|
+
* Creates a new HTTP error instance.
|
|
26
|
+
* @param status - A valid HTTP status code (e.g., 400, 500).
|
|
27
|
+
* @param message - The error message to return to the client.
|
|
28
|
+
*/
|
|
29
|
+
function HttpError(status, message) {
|
|
30
|
+
var _this = _super.call(this, message) || this;
|
|
31
|
+
_this.status = status;
|
|
32
|
+
return _this;
|
|
33
|
+
}
|
|
34
|
+
return HttpError;
|
|
35
|
+
}(ErrorResponse));
|
|
36
|
+
export { HttpError };
|
|
37
|
+
/**
|
|
38
|
+
* Represents a 500 Internal Server Error.
|
|
39
|
+
* Use this for unexpected backend errors that are not caused by the client.
|
|
40
|
+
*/
|
|
41
|
+
var InternalServerErrorException = /** @class */ (function (_super) {
|
|
42
|
+
__extends(InternalServerErrorException, _super);
|
|
43
|
+
/**
|
|
44
|
+
* @param message - Optional custom message. Defaults to "Internal server error".
|
|
45
|
+
*/
|
|
46
|
+
function InternalServerErrorException(message) {
|
|
47
|
+
if (message === void 0) { message = "Internal server error"; }
|
|
48
|
+
var _this = _super.call(this, message) || this;
|
|
49
|
+
_this.status = HttpStatusCodes.INTERNAL_SERVER_ERROR;
|
|
50
|
+
return _this;
|
|
51
|
+
}
|
|
52
|
+
return InternalServerErrorException;
|
|
53
|
+
}(ErrorResponse));
|
|
54
|
+
export { InternalServerErrorException };
|
|
55
|
+
/**
|
|
56
|
+
* Represents a 401 Unauthorized Error.
|
|
57
|
+
* Indicates that authentication is required and has failed or not been provided.
|
|
58
|
+
*/
|
|
59
|
+
var UnauthorizedException = /** @class */ (function (_super) {
|
|
60
|
+
__extends(UnauthorizedException, _super);
|
|
61
|
+
/**
|
|
62
|
+
* @param message - Optional custom message. Defaults to "Unauthorized".
|
|
63
|
+
*/
|
|
64
|
+
function UnauthorizedException(message) {
|
|
65
|
+
if (message === void 0) { message = "Unauthorized"; }
|
|
66
|
+
var _this = _super.call(this, message) || this;
|
|
67
|
+
_this.status = HttpStatusCodes.UNAUTHORIZED;
|
|
68
|
+
return _this;
|
|
69
|
+
}
|
|
70
|
+
return UnauthorizedException;
|
|
71
|
+
}(ErrorResponse));
|
|
72
|
+
export { UnauthorizedException };
|
|
73
|
+
/**
|
|
74
|
+
* Represents a 400 Bad Request Error.
|
|
75
|
+
* Indicates that the client has sent invalid data (e.g., malformed request body).
|
|
76
|
+
*/
|
|
77
|
+
var BadRequestException = /** @class */ (function (_super) {
|
|
78
|
+
__extends(BadRequestException, _super);
|
|
79
|
+
/**
|
|
80
|
+
* @param message - Optional custom message. Defaults to "Bad request".
|
|
81
|
+
*/
|
|
82
|
+
function BadRequestException(message) {
|
|
83
|
+
if (message === void 0) { message = "Bad request"; }
|
|
84
|
+
var _this = _super.call(this, message) || this;
|
|
85
|
+
_this.status = HttpStatusCodes.BAD_REQUEST;
|
|
86
|
+
return _this;
|
|
87
|
+
}
|
|
88
|
+
return BadRequestException;
|
|
89
|
+
}(ErrorResponse));
|
|
90
|
+
export { BadRequestException };
|
|
91
|
+
/**
|
|
92
|
+
* Represents a 404 Not Found Error.
|
|
93
|
+
* Used when a requested resource (e.g., user, file) could not be located.
|
|
94
|
+
*/
|
|
95
|
+
var NotFoundException = /** @class */ (function (_super) {
|
|
96
|
+
__extends(NotFoundException, _super);
|
|
97
|
+
/**
|
|
98
|
+
* @param message - Optional custom message. Defaults to "Resource not found".
|
|
99
|
+
*/
|
|
100
|
+
function NotFoundException(message) {
|
|
101
|
+
if (message === void 0) { message = "Resource not found"; }
|
|
102
|
+
var _this = _super.call(this, message) || this;
|
|
103
|
+
_this.status = HttpStatusCodes.NOT_FOUND;
|
|
104
|
+
return _this;
|
|
105
|
+
}
|
|
106
|
+
return NotFoundException;
|
|
107
|
+
}(ErrorResponse));
|
|
108
|
+
export { NotFoundException };
|
|
109
|
+
/**
|
|
110
|
+
* Represents a 403 Forbidden Error.
|
|
111
|
+
* Indicates that the client is authenticated but not allowed to access the resource.
|
|
112
|
+
*/
|
|
113
|
+
var ForbiddenException = /** @class */ (function (_super) {
|
|
114
|
+
__extends(ForbiddenException, _super);
|
|
115
|
+
/**
|
|
116
|
+
* @param message - Optional custom message. Defaults to "Forbidden".
|
|
117
|
+
*/
|
|
118
|
+
function ForbiddenException(message) {
|
|
119
|
+
if (message === void 0) { message = "Forbidden"; }
|
|
120
|
+
var _this = _super.call(this, message) || this;
|
|
121
|
+
_this.status = HttpStatusCodes.FORBIDDEN;
|
|
122
|
+
return _this;
|
|
123
|
+
}
|
|
124
|
+
return ForbiddenException;
|
|
125
|
+
}(ErrorResponse));
|
|
126
|
+
export { ForbiddenException };
|
|
127
|
+
/**
|
|
128
|
+
* Represents a 409 Conflict Error.
|
|
129
|
+
* Commonly used when a resource already exists or a versioning conflict occurs.
|
|
130
|
+
*/
|
|
131
|
+
var ConflictException = /** @class */ (function (_super) {
|
|
132
|
+
__extends(ConflictException, _super);
|
|
133
|
+
/**
|
|
134
|
+
* @param message - Optional custom message. Defaults to "Conflict".
|
|
135
|
+
*/
|
|
136
|
+
function ConflictException(message) {
|
|
137
|
+
if (message === void 0) { message = "Conflict"; }
|
|
138
|
+
var _this = _super.call(this, message) || this;
|
|
139
|
+
_this.status = HttpStatusCodes.CONFLICT;
|
|
140
|
+
return _this;
|
|
141
|
+
}
|
|
142
|
+
return ConflictException;
|
|
143
|
+
}(ErrorResponse));
|
|
144
|
+
export { ConflictException };
|
|
145
|
+
/**
|
|
146
|
+
* Represents a 502 Bad Gateway Error.
|
|
147
|
+
* Used when the server receives an invalid response from an upstream server.
|
|
148
|
+
*/
|
|
149
|
+
var BadGatewayException = /** @class */ (function (_super) {
|
|
150
|
+
__extends(BadGatewayException, _super);
|
|
151
|
+
/**
|
|
152
|
+
* @param message - Optional custom message. Defaults to "Bad Gateway".
|
|
153
|
+
*/
|
|
154
|
+
function BadGatewayException(message) {
|
|
155
|
+
if (message === void 0) { message = "Bad Gateway"; }
|
|
156
|
+
var _this = _super.call(this, message) || this;
|
|
157
|
+
_this.status = HttpStatusCodes.BAD_GATEWAY;
|
|
158
|
+
return _this;
|
|
159
|
+
}
|
|
160
|
+
return BadGatewayException;
|
|
161
|
+
}(ErrorResponse));
|
|
162
|
+
export { BadGatewayException };
|
|
163
|
+
/**
|
|
164
|
+
* Represents a 429 Too Many Requests Error.
|
|
165
|
+
* Returned when the client has hit a rate limit.
|
|
166
|
+
*/
|
|
167
|
+
var TooManyRequestsException = /** @class */ (function (_super) {
|
|
168
|
+
__extends(TooManyRequestsException, _super);
|
|
169
|
+
/**
|
|
170
|
+
* @param message - Optional custom message. Defaults to "Too many requests".
|
|
171
|
+
*/
|
|
172
|
+
function TooManyRequestsException(message) {
|
|
173
|
+
if (message === void 0) { message = "Too many requests"; }
|
|
174
|
+
var _this = _super.call(this, message) || this;
|
|
175
|
+
_this.status = HttpStatusCodes.TOO_MANY_REQUESTS;
|
|
176
|
+
return _this;
|
|
177
|
+
}
|
|
178
|
+
return TooManyRequestsException;
|
|
179
|
+
}(ErrorResponse));
|
|
180
|
+
export { TooManyRequestsException };
|
|
181
|
+
/**
|
|
182
|
+
* Represents a 503 Service Unavailable Error.
|
|
183
|
+
* Indicates that the server is temporarily unavailable (e.g., for maintenance).
|
|
184
|
+
*/
|
|
185
|
+
var ServiceUnavailableException = /** @class */ (function (_super) {
|
|
186
|
+
__extends(ServiceUnavailableException, _super);
|
|
187
|
+
/**
|
|
188
|
+
* @param message - Optional custom message. Defaults to "Service Unavailable".
|
|
189
|
+
*/
|
|
190
|
+
function ServiceUnavailableException(message) {
|
|
191
|
+
if (message === void 0) { message = "Service Unavailable"; }
|
|
192
|
+
var _this = _super.call(this, message) || this;
|
|
193
|
+
_this.status = HttpStatusCodes.SERVICE_UNAVAILABLE;
|
|
194
|
+
return _this;
|
|
195
|
+
}
|
|
196
|
+
return ServiceUnavailableException;
|
|
197
|
+
}(ErrorResponse));
|
|
198
|
+
export { ServiceUnavailableException };
|
|
199
|
+
/**
|
|
200
|
+
* Represents a 504 Gateway Timeout Error.
|
|
201
|
+
* Returned when the server acting as a gateway times out waiting for a response.
|
|
202
|
+
*/
|
|
203
|
+
var GatewayTimeoutException = /** @class */ (function (_super) {
|
|
204
|
+
__extends(GatewayTimeoutException, _super);
|
|
205
|
+
/**
|
|
206
|
+
* @param message - Optional custom message. Defaults to "Gateway Timeout".
|
|
207
|
+
*/
|
|
208
|
+
function GatewayTimeoutException(message) {
|
|
209
|
+
if (message === void 0) { message = "Gateway Timeout"; }
|
|
210
|
+
var _this = _super.call(this, message) || this;
|
|
211
|
+
_this.status = HttpStatusCodes.GATEWAY_TIMEOUT;
|
|
212
|
+
return _this;
|
|
213
|
+
}
|
|
214
|
+
return GatewayTimeoutException;
|
|
215
|
+
}(ErrorResponse));
|
|
216
|
+
export { GatewayTimeoutException };
|
|
217
|
+
//# 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,EAAE,MAAM,kBAAkB,CAAC;AAEjD;;;GAGG;AACH;IAA+B,6BAAa;IAC1C;;;;OAIG;IACH,mBAAY,MAAc,EAAE,OAAe;QACzC,YAAA,MAAK,YAAC,OAAO,CAAC,SAAC;QACf,KAAI,CAAC,MAAM,GAAG,MAAM,CAAC;;IACvB,CAAC;IACH,gBAAC;AAAD,CAAC,AAVD,CAA+B,aAAa,GAU3C;;AAED;;;GAGG;AACH;IAAkD,gDAAa;IAC7D;;OAEG;IACH,sCAAY,OAAyC;QAAzC,wBAAA,EAAA,iCAAyC;QACnD,YAAA,MAAK,YAAC,OAAO,CAAC,SAAC;QACf,KAAI,CAAC,MAAM,GAAG,eAAe,CAAC,qBAAqB,CAAC;;IACtD,CAAC;IACH,mCAAC;AAAD,CAAC,AARD,CAAkD,aAAa,GAQ9D;;AAED;;;GAGG;AACH;IAA2C,yCAAa;IACtD;;OAEG;IACH,+BAAY,OAAgC;QAAhC,wBAAA,EAAA,wBAAgC;QAC1C,YAAA,MAAK,YAAC,OAAO,CAAC,SAAC;QACf,KAAI,CAAC,MAAM,GAAG,eAAe,CAAC,YAAY,CAAC;;IAC7C,CAAC;IACH,4BAAC;AAAD,CAAC,AARD,CAA2C,aAAa,GAQvD;;AAED;;;GAGG;AACH;IAAyC,uCAAa;IACpD;;OAEG;IACH,6BAAY,OAA+B;QAA/B,wBAAA,EAAA,uBAA+B;QACzC,YAAA,MAAK,YAAC,OAAO,CAAC,SAAC;QACf,KAAI,CAAC,MAAM,GAAG,eAAe,CAAC,WAAW,CAAC;;IAC5C,CAAC;IACH,0BAAC;AAAD,CAAC,AARD,CAAyC,aAAa,GAQrD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C;AAAD,CAAC,AARD,CAA8C,aAAa,GAQ1D;;AAED;;;GAGG;AACH;IAAiD,+CAAa;IAC5D;;OAEG;IACH,qCAAY,OAAuC;QAAvC,wBAAA,EAAA,+BAAuC;QACjD,YAAA,MAAK,YAAC,OAAO,CAAC,SAAC;QACf,KAAI,CAAC,MAAM,GAAG,eAAe,CAAC,mBAAmB,CAAC;;IACpD,CAAC;IACH,kCAAC;AAAD,CAAC,AARD,CAAiD,aAAa,GAQ7D;;AAED;;;GAGG;AACH;IAA6C,2CAAa;IACxD;;OAEG;IACH,iCAAY,OAAmC;QAAnC,wBAAA,EAAA,2BAAmC;QAC7C,YAAA,MAAK,YAAC,OAAO,CAAC,SAAC;QACf,KAAI,CAAC,MAAM,GAAG,eAAe,CAAC,eAAe,CAAC;;IAChD,CAAC;IACH,8BAAC;AAAD,CAAC,AARD,CAA6C,aAAa,GAQzD","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
|