@catbee/utils 1.1.0 → 2.0.0-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (121) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +27 -27
  3. package/array/array.utils.d.ts +191 -0
  4. package/array/index.cjs +246 -0
  5. package/array/index.d.ts +25 -0
  6. package/array/index.mjs +228 -0
  7. package/async/async.utils.d.ts +296 -0
  8. package/async/index.cjs +428 -0
  9. package/async/index.d.ts +25 -0
  10. package/async/index.mjs +407 -0
  11. package/cache/cache.utils.d.ts +176 -0
  12. package/cache/index.cjs +292 -0
  13. package/cache/index.d.ts +25 -0
  14. package/cache/index.mjs +290 -0
  15. package/config/config.d.ts +57 -0
  16. package/config/index.cjs +136 -0
  17. package/config/index.d.ts +26 -0
  18. package/config/index.mjs +131 -0
  19. package/context-store/context-store.utils.d.ts +212 -0
  20. package/context-store/index.cjs +267 -0
  21. package/context-store/index.d.ts +25 -0
  22. package/context-store/index.mjs +261 -0
  23. package/crypto/crypto.utils.d.ts +183 -0
  24. package/crypto/index.cjs +182 -0
  25. package/crypto/index.d.ts +25 -0
  26. package/crypto/index.mjs +166 -0
  27. package/date/date.utils.d.ts +190 -0
  28. package/date/index.cjs +295 -0
  29. package/date/index.d.ts +25 -0
  30. package/date/index.mjs +283 -0
  31. package/decorators/decorators.utils.d.ts +705 -0
  32. package/decorators/index.cjs +913 -0
  33. package/decorators/index.d.ts +25 -0
  34. package/decorators/index.mjs +872 -0
  35. package/dir/dir.utils.d.ts +216 -0
  36. package/dir/index.cjs +416 -0
  37. package/dir/index.d.ts +25 -0
  38. package/dir/index.mjs +389 -0
  39. package/env/env.utils.d.ts +400 -0
  40. package/env/index.cjs +761 -0
  41. package/env/index.d.ts +25 -0
  42. package/env/index.mjs +758 -0
  43. package/exception/exception.utils.d.ts +253 -0
  44. package/exception/index.cjs +362 -0
  45. package/exception/index.d.ts +25 -0
  46. package/exception/index.mjs +338 -0
  47. package/fs/fs.utils.d.ts +196 -0
  48. package/fs/index.cjs +253 -0
  49. package/fs/index.d.ts +25 -0
  50. package/fs/index.mjs +228 -0
  51. package/http-status-codes/http-status-codes.d.ts +289 -0
  52. package/http-status-codes/index.cjs +96 -0
  53. package/http-status-codes/index.d.ts +25 -0
  54. package/http-status-codes/index.mjs +94 -0
  55. package/id/id.utils.d.ts +59 -0
  56. package/id/index.cjs +62 -0
  57. package/id/index.d.ts +25 -0
  58. package/id/index.mjs +56 -0
  59. package/index.cjs +218 -0
  60. package/index.d.ts +51 -0
  61. package/index.mjs +51 -0
  62. package/logger/index.cjs +334 -0
  63. package/logger/index.d.ts +25 -0
  64. package/logger/index.mjs +313 -0
  65. package/logger/logger.utils.d.ts +210 -0
  66. package/middleware/index.cjs +177 -0
  67. package/middleware/index.d.ts +25 -0
  68. package/middleware/index.mjs +170 -0
  69. package/middleware/middleware.utils.d.ts +123 -0
  70. package/obj/index.cjs +317 -0
  71. package/obj/index.d.ts +25 -0
  72. package/obj/index.mjs +301 -0
  73. package/obj/obj.utils.d.ts +156 -0
  74. package/package.json +171 -19
  75. package/performance/index.cjs +231 -0
  76. package/performance/index.d.ts +25 -0
  77. package/performance/index.mjs +225 -0
  78. package/performance/performance.utils.d.ts +159 -0
  79. package/request/index.cjs +202 -0
  80. package/request/index.d.ts +26 -0
  81. package/request/index.mjs +194 -0
  82. package/request/request.utils.d.ts +109 -0
  83. package/response/index.cjs +234 -0
  84. package/response/index.d.ts +26 -0
  85. package/response/index.mjs +222 -0
  86. package/response/response.utils.d.ts +186 -0
  87. package/server/index.cjs +1627 -0
  88. package/server/index.d.ts +28 -0
  89. package/server/index.mjs +1617 -0
  90. package/server/server.builder.d.ts +531 -0
  91. package/server/server.d.ts +303 -0
  92. package/stream/index.cjs +151 -0
  93. package/stream/index.d.ts +25 -0
  94. package/stream/index.mjs +144 -0
  95. package/stream/stream.utils.d.ts +111 -0
  96. package/string/index.cjs +109 -0
  97. package/string/index.d.ts +25 -0
  98. package/string/index.mjs +95 -0
  99. package/string/string.utils.d.ts +124 -0
  100. package/type/index.cjs +129 -0
  101. package/type/index.d.ts +25 -0
  102. package/type/index.mjs +119 -0
  103. package/type/type.utils.d.ts +129 -0
  104. package/types/api-response.d.ts +175 -0
  105. package/types/common.d.ts +148 -0
  106. package/types/config.d.ts +88 -0
  107. package/types/index.cjs +34 -0
  108. package/types/index.d.ts +28 -0
  109. package/types/index.mjs +32 -0
  110. package/types/server.d.ts +291 -0
  111. package/url/index.cjs +201 -0
  112. package/url/index.d.ts +25 -0
  113. package/url/index.mjs +189 -0
  114. package/url/url.utils.d.ts +164 -0
  115. package/validate/index.cjs +212 -0
  116. package/validate/index.d.ts +25 -0
  117. package/validate/index.mjs +188 -0
  118. package/validate/validate.utils.d.ts +200 -0
  119. package/build/index.cjs +0 -7694
  120. package/build/index.d.ts +0 -5792
  121. package/build/index.mjs +0 -7386
@@ -0,0 +1,253 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { ErrorResponse } from '@catbee/utils/response';
26
+ /**
27
+ * Generic HTTP error class used for custom exceptions with any status code.
28
+ * Inherit this when you need to throw errors dynamically at runtime.
29
+ */
30
+ export declare class HttpError extends ErrorResponse {
31
+ /**
32
+ * Creates a new HTTP error instance.
33
+ * @param status - A valid HTTP status code (e.g., 400, 500).
34
+ * @param message - The error message to return to the client.
35
+ */
36
+ constructor(status: number, message: string);
37
+ }
38
+ /**
39
+ * Represents a 500 Internal Server Error.
40
+ * Use this for unexpected backend errors that are not caused by the client.
41
+ */
42
+ export declare class InternalServerErrorException extends ErrorResponse {
43
+ /**
44
+ * @param message - Optional custom message. Defaults to "Internal server error".
45
+ */
46
+ constructor(message?: string);
47
+ }
48
+ /**
49
+ * Represents a 401 Unauthorized Error.
50
+ * Indicates that authentication is required and has failed or not been provided.
51
+ */
52
+ export declare class UnauthorizedException extends ErrorResponse {
53
+ /**
54
+ * @param message - Optional custom message. Defaults to "Unauthorized".
55
+ */
56
+ constructor(message?: string);
57
+ }
58
+ /**
59
+ * Represents a 400 Bad Request Error.
60
+ * Indicates that the client has sent invalid data (e.g., malformed request body).
61
+ */
62
+ export declare class BadRequestException extends ErrorResponse {
63
+ /**
64
+ * @param message - Optional custom message. Defaults to "Bad request".
65
+ */
66
+ constructor(message?: string);
67
+ }
68
+ /**
69
+ * Represents a 404 Not Found Error.
70
+ * Used when a requested resource (e.g., user, file) could not be located.
71
+ */
72
+ export declare class NotFoundException extends ErrorResponse {
73
+ /**
74
+ * @param message - Optional custom message. Defaults to "Resource not found".
75
+ */
76
+ constructor(message?: string);
77
+ }
78
+ /**
79
+ * Represents a 403 Forbidden Error.
80
+ * Indicates that the client is authenticated but not allowed to access the resource.
81
+ */
82
+ export declare class ForbiddenException extends ErrorResponse {
83
+ /**
84
+ * @param message - Optional custom message. Defaults to "Forbidden".
85
+ */
86
+ constructor(message?: string);
87
+ }
88
+ /**
89
+ * Represents a 409 Conflict Error.
90
+ * Commonly used when a resource already exists or a versioning conflict occurs.
91
+ */
92
+ export declare class ConflictException extends ErrorResponse {
93
+ /**
94
+ * @param message - Optional custom message. Defaults to "Conflict".
95
+ */
96
+ constructor(message?: string);
97
+ }
98
+ /**
99
+ * Represents a 502 Bad Gateway Error.
100
+ * Used when the server receives an invalid response from an upstream server.
101
+ */
102
+ export declare class BadGatewayException extends ErrorResponse {
103
+ /**
104
+ * @param message - Optional custom message. Defaults to "Bad Gateway".
105
+ */
106
+ constructor(message?: string);
107
+ }
108
+ /**
109
+ * Represents a 429 Too Many Requests Error.
110
+ * Returned when the client has hit a rate limit.
111
+ */
112
+ export declare class TooManyRequestsException extends ErrorResponse {
113
+ /**
114
+ * @param message - Optional custom message. Defaults to "Too many requests".
115
+ */
116
+ constructor(message?: string);
117
+ }
118
+ /**
119
+ * Represents a 503 Service Unavailable Error.
120
+ * Indicates that the server is temporarily unavailable (e.g., for maintenance).
121
+ */
122
+ export declare class ServiceUnavailableException extends ErrorResponse {
123
+ /**
124
+ * @param message - Optional custom message. Defaults to "Service Unavailable".
125
+ */
126
+ constructor(message?: string);
127
+ }
128
+ /**
129
+ * Represents a 504 Gateway Timeout Error.
130
+ * Returned when the server acting as a gateway times out waiting for a response.
131
+ */
132
+ export declare class GatewayTimeoutException extends ErrorResponse {
133
+ /**
134
+ * @param message - Optional custom message. Defaults to "Gateway Timeout".
135
+ */
136
+ constructor(message?: string);
137
+ }
138
+ /**
139
+ * Represents a 422 Unprocessable Entity Error.
140
+ * Used when the server understands the content type but cannot process the contained instructions.
141
+ */
142
+ export declare class UnprocessableEntityException extends ErrorResponse {
143
+ details?: Record<string, any> | undefined;
144
+ /**
145
+ * @param message - Optional custom message. Defaults to "Unprocessable Entity".
146
+ * @param details - Optional validation details or additional error context.
147
+ */
148
+ constructor(message?: string, details?: Record<string, any> | undefined);
149
+ }
150
+ /**
151
+ * Represents a 405 Method Not Allowed Error.
152
+ * Used when the request method is not supported for the requested resource.
153
+ */
154
+ export declare class MethodNotAllowedException extends ErrorResponse {
155
+ allowedMethods?: string[] | undefined;
156
+ /**
157
+ * @param message - Optional custom message. Defaults to "Method Not Allowed".
158
+ * @param allowedMethods - Optional array of allowed HTTP methods.
159
+ */
160
+ constructor(message?: string, allowedMethods?: string[] | undefined);
161
+ }
162
+ /**
163
+ * Represents a 406 Not Acceptable Error.
164
+ * Used when the server cannot produce a response matching the client's accepted content types.
165
+ */
166
+ export declare class NotAcceptableException extends ErrorResponse {
167
+ /**
168
+ * @param message - Optional custom message. Defaults to "Not Acceptable".
169
+ */
170
+ constructor(message?: string);
171
+ }
172
+ /**
173
+ * Represents a 408 Request Timeout Error.
174
+ * Used when the server times out waiting for the client to send a request.
175
+ */
176
+ export declare class RequestTimeoutException extends ErrorResponse {
177
+ /**
178
+ * @param message - Optional custom message. Defaults to "Request Timeout".
179
+ */
180
+ constructor(message?: string);
181
+ }
182
+ /**
183
+ * Represents a 415 Unsupported Media Type Error.
184
+ * Used when the request's Content-Type is not supported.
185
+ */
186
+ export declare class UnsupportedMediaTypeException extends ErrorResponse {
187
+ /**
188
+ * @param message - Optional custom message. Defaults to "Unsupported Media Type".
189
+ */
190
+ constructor(message?: string);
191
+ }
192
+ /**
193
+ * Represents a 413 Payload Too Large Error.
194
+ * Used when the request body exceeds server limits.
195
+ */
196
+ export declare class PayloadTooLargeException extends ErrorResponse {
197
+ /**
198
+ * @param message - Optional custom message. Defaults to "Payload Too Large".
199
+ */
200
+ constructor(message?: string);
201
+ }
202
+ /**
203
+ * Represents a 507 Insufficient Storage Error.
204
+ * Used when the server has insufficient storage to complete the request.
205
+ */
206
+ export declare class InsufficientStorageException extends ErrorResponse {
207
+ /**
208
+ * @param message - Optional custom message. Defaults to "Insufficient Storage".
209
+ */
210
+ constructor(message?: string);
211
+ }
212
+ /**
213
+ * Checks if an error is an instance of HttpError or its subclasses.
214
+ *
215
+ * @param error - The error to check.
216
+ * @returns True if the error is an HttpError, false otherwise.
217
+ */
218
+ export declare function isHttpError(error: unknown): error is ErrorResponse;
219
+ /**
220
+ * Creates an HTTP error with the specified status code and message.
221
+ * Factory function for creating appropriate HTTP error instances.
222
+ *
223
+ * @param status - HTTP status code.
224
+ * @param message - Optional custom error message.
225
+ * @returns An instance of the appropriate HTTP error class.
226
+ */
227
+ export declare function createHttpError(status: number, message?: string): ErrorResponse;
228
+ /**
229
+ * Type guard to check if an object has specific error properties.
230
+ *
231
+ * @param error - The object to check.
232
+ * @returns True if object has error-like structure.
233
+ */
234
+ export declare function hasErrorShape(error: unknown): error is {
235
+ message: string;
236
+ status?: number;
237
+ code?: string;
238
+ };
239
+ /**
240
+ * Safely extracts message from any error type.
241
+ *
242
+ * @param error - Any error object or value.
243
+ * @returns A string error message.
244
+ */
245
+ export declare function getErrorMessage(error: unknown): string;
246
+ /**
247
+ * Wraps a function and transforms any errors into HTTP errors.
248
+ * Useful for API handlers to ensure consistent error responses.
249
+ *
250
+ * @param handler - The async function to wrap.
251
+ * @returns A function that always returns or throws an HTTP error.
252
+ */
253
+ export declare function withErrorHandling<T extends (...args: any[]) => Promise<any>>(handler: T): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>>;
@@ -0,0 +1,362 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ 'use strict';
26
+
27
+ var httpStatusCodes = require('@catbee/utils/http-status-codes');
28
+ var logger = require('@catbee/utils/logger');
29
+ var response = require('@catbee/utils/response');
30
+
31
+ var __defProp = Object.defineProperty;
32
+ var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
33
+ var HttpError = class extends response.ErrorResponse {
34
+ static {
35
+ __name(this, "HttpError");
36
+ }
37
+ /**
38
+ * Creates a new HTTP error instance.
39
+ * @param status - A valid HTTP status code (e.g., 400, 500).
40
+ * @param message - The error message to return to the client.
41
+ */
42
+ constructor(status, message) {
43
+ super(message);
44
+ this.status = status;
45
+ }
46
+ };
47
+ var InternalServerErrorException = class extends response.ErrorResponse {
48
+ static {
49
+ __name(this, "InternalServerErrorException");
50
+ }
51
+ /**
52
+ * @param message - Optional custom message. Defaults to "Internal server error".
53
+ */
54
+ constructor(message = "Internal server error") {
55
+ super(message);
56
+ this.status = httpStatusCodes.HttpStatusCodes.INTERNAL_SERVER_ERROR;
57
+ }
58
+ };
59
+ var UnauthorizedException = class extends response.ErrorResponse {
60
+ static {
61
+ __name(this, "UnauthorizedException");
62
+ }
63
+ /**
64
+ * @param message - Optional custom message. Defaults to "Unauthorized".
65
+ */
66
+ constructor(message = "Unauthorized") {
67
+ super(message);
68
+ this.status = httpStatusCodes.HttpStatusCodes.UNAUTHORIZED;
69
+ }
70
+ };
71
+ var BadRequestException = class extends response.ErrorResponse {
72
+ static {
73
+ __name(this, "BadRequestException");
74
+ }
75
+ /**
76
+ * @param message - Optional custom message. Defaults to "Bad request".
77
+ */
78
+ constructor(message = "Bad request") {
79
+ super(message);
80
+ this.status = httpStatusCodes.HttpStatusCodes.BAD_REQUEST;
81
+ }
82
+ };
83
+ var NotFoundException = class extends response.ErrorResponse {
84
+ static {
85
+ __name(this, "NotFoundException");
86
+ }
87
+ /**
88
+ * @param message - Optional custom message. Defaults to "Resource not found".
89
+ */
90
+ constructor(message = "Resource not found") {
91
+ super(message);
92
+ this.status = httpStatusCodes.HttpStatusCodes.NOT_FOUND;
93
+ }
94
+ };
95
+ var ForbiddenException = class extends response.ErrorResponse {
96
+ static {
97
+ __name(this, "ForbiddenException");
98
+ }
99
+ /**
100
+ * @param message - Optional custom message. Defaults to "Forbidden".
101
+ */
102
+ constructor(message = "Forbidden") {
103
+ super(message);
104
+ this.status = httpStatusCodes.HttpStatusCodes.FORBIDDEN;
105
+ }
106
+ };
107
+ var ConflictException = class extends response.ErrorResponse {
108
+ static {
109
+ __name(this, "ConflictException");
110
+ }
111
+ /**
112
+ * @param message - Optional custom message. Defaults to "Conflict".
113
+ */
114
+ constructor(message = "Conflict") {
115
+ super(message);
116
+ this.status = httpStatusCodes.HttpStatusCodes.CONFLICT;
117
+ }
118
+ };
119
+ var BadGatewayException = class extends response.ErrorResponse {
120
+ static {
121
+ __name(this, "BadGatewayException");
122
+ }
123
+ /**
124
+ * @param message - Optional custom message. Defaults to "Bad Gateway".
125
+ */
126
+ constructor(message = "Bad Gateway") {
127
+ super(message);
128
+ this.status = httpStatusCodes.HttpStatusCodes.BAD_GATEWAY;
129
+ }
130
+ };
131
+ var TooManyRequestsException = class extends response.ErrorResponse {
132
+ static {
133
+ __name(this, "TooManyRequestsException");
134
+ }
135
+ /**
136
+ * @param message - Optional custom message. Defaults to "Too many requests".
137
+ */
138
+ constructor(message = "Too many requests") {
139
+ super(message);
140
+ this.status = httpStatusCodes.HttpStatusCodes.TOO_MANY_REQUESTS;
141
+ }
142
+ };
143
+ var ServiceUnavailableException = class extends response.ErrorResponse {
144
+ static {
145
+ __name(this, "ServiceUnavailableException");
146
+ }
147
+ /**
148
+ * @param message - Optional custom message. Defaults to "Service Unavailable".
149
+ */
150
+ constructor(message = "Service Unavailable") {
151
+ super(message);
152
+ this.status = httpStatusCodes.HttpStatusCodes.SERVICE_UNAVAILABLE;
153
+ }
154
+ };
155
+ var GatewayTimeoutException = class extends response.ErrorResponse {
156
+ static {
157
+ __name(this, "GatewayTimeoutException");
158
+ }
159
+ /**
160
+ * @param message - Optional custom message. Defaults to "Gateway Timeout".
161
+ */
162
+ constructor(message = "Gateway Timeout") {
163
+ super(message);
164
+ this.status = httpStatusCodes.HttpStatusCodes.GATEWAY_TIMEOUT;
165
+ }
166
+ };
167
+ var UnprocessableEntityException = class extends response.ErrorResponse {
168
+ static {
169
+ __name(this, "UnprocessableEntityException");
170
+ }
171
+ details;
172
+ /**
173
+ * @param message - Optional custom message. Defaults to "Unprocessable Entity".
174
+ * @param details - Optional validation details or additional error context.
175
+ */
176
+ constructor(message = "Unprocessable Entity", details) {
177
+ super(message), this.details = details;
178
+ this.status = httpStatusCodes.HttpStatusCodes.UNPROCESSABLE_ENTITY;
179
+ }
180
+ };
181
+ var MethodNotAllowedException = class extends response.ErrorResponse {
182
+ static {
183
+ __name(this, "MethodNotAllowedException");
184
+ }
185
+ allowedMethods;
186
+ /**
187
+ * @param message - Optional custom message. Defaults to "Method Not Allowed".
188
+ * @param allowedMethods - Optional array of allowed HTTP methods.
189
+ */
190
+ constructor(message = "Method Not Allowed", allowedMethods) {
191
+ super(message), this.allowedMethods = allowedMethods;
192
+ this.status = httpStatusCodes.HttpStatusCodes.METHOD_NOT_ALLOWED;
193
+ }
194
+ };
195
+ var NotAcceptableException = class extends response.ErrorResponse {
196
+ static {
197
+ __name(this, "NotAcceptableException");
198
+ }
199
+ /**
200
+ * @param message - Optional custom message. Defaults to "Not Acceptable".
201
+ */
202
+ constructor(message = "Not Acceptable") {
203
+ super(message);
204
+ this.status = httpStatusCodes.HttpStatusCodes.NOT_ACCEPTABLE;
205
+ }
206
+ };
207
+ var RequestTimeoutException = class extends response.ErrorResponse {
208
+ static {
209
+ __name(this, "RequestTimeoutException");
210
+ }
211
+ /**
212
+ * @param message - Optional custom message. Defaults to "Request Timeout".
213
+ */
214
+ constructor(message = "Request Timeout") {
215
+ super(message);
216
+ this.status = httpStatusCodes.HttpStatusCodes.REQUEST_TIMEOUT;
217
+ }
218
+ };
219
+ var UnsupportedMediaTypeException = class extends response.ErrorResponse {
220
+ static {
221
+ __name(this, "UnsupportedMediaTypeException");
222
+ }
223
+ /**
224
+ * @param message - Optional custom message. Defaults to "Unsupported Media Type".
225
+ */
226
+ constructor(message = "Unsupported Media Type") {
227
+ super(message);
228
+ this.status = httpStatusCodes.HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE;
229
+ }
230
+ };
231
+ var PayloadTooLargeException = class extends response.ErrorResponse {
232
+ static {
233
+ __name(this, "PayloadTooLargeException");
234
+ }
235
+ /**
236
+ * @param message - Optional custom message. Defaults to "Payload Too Large".
237
+ */
238
+ constructor(message = "Payload Too Large") {
239
+ super(message);
240
+ this.status = httpStatusCodes.HttpStatusCodes.PAYLOAD_TOO_LARGE;
241
+ }
242
+ };
243
+ var InsufficientStorageException = class extends response.ErrorResponse {
244
+ static {
245
+ __name(this, "InsufficientStorageException");
246
+ }
247
+ /**
248
+ * @param message - Optional custom message. Defaults to "Insufficient Storage".
249
+ */
250
+ constructor(message = "Insufficient Storage") {
251
+ super(message);
252
+ this.status = httpStatusCodes.HttpStatusCodes.INSUFFICIENT_STORAGE;
253
+ }
254
+ };
255
+ function isHttpError(error) {
256
+ return error instanceof response.ErrorResponse;
257
+ }
258
+ __name(isHttpError, "isHttpError");
259
+ function createHttpError(status, message) {
260
+ switch (status) {
261
+ case httpStatusCodes.HttpStatusCodes.BAD_REQUEST:
262
+ return new BadRequestException(message);
263
+ case httpStatusCodes.HttpStatusCodes.UNAUTHORIZED:
264
+ return new UnauthorizedException(message);
265
+ case httpStatusCodes.HttpStatusCodes.FORBIDDEN:
266
+ return new ForbiddenException(message);
267
+ case httpStatusCodes.HttpStatusCodes.NOT_FOUND:
268
+ return new NotFoundException(message);
269
+ case httpStatusCodes.HttpStatusCodes.METHOD_NOT_ALLOWED:
270
+ return new MethodNotAllowedException(message);
271
+ case httpStatusCodes.HttpStatusCodes.NOT_ACCEPTABLE:
272
+ return new NotAcceptableException(message);
273
+ case httpStatusCodes.HttpStatusCodes.REQUEST_TIMEOUT:
274
+ return new RequestTimeoutException(message);
275
+ case httpStatusCodes.HttpStatusCodes.CONFLICT:
276
+ return new ConflictException(message);
277
+ case httpStatusCodes.HttpStatusCodes.GONE:
278
+ return new HttpError(httpStatusCodes.HttpStatusCodes.GONE, message || "Gone");
279
+ case httpStatusCodes.HttpStatusCodes.PAYLOAD_TOO_LARGE:
280
+ return new PayloadTooLargeException(message);
281
+ case httpStatusCodes.HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE:
282
+ return new UnsupportedMediaTypeException(message);
283
+ case httpStatusCodes.HttpStatusCodes.UNPROCESSABLE_ENTITY:
284
+ return new UnprocessableEntityException(message);
285
+ case httpStatusCodes.HttpStatusCodes.TOO_MANY_REQUESTS:
286
+ return new TooManyRequestsException(message);
287
+ case httpStatusCodes.HttpStatusCodes.INTERNAL_SERVER_ERROR:
288
+ return new InternalServerErrorException(message);
289
+ case httpStatusCodes.HttpStatusCodes.BAD_GATEWAY:
290
+ return new BadGatewayException(message);
291
+ case httpStatusCodes.HttpStatusCodes.SERVICE_UNAVAILABLE:
292
+ return new ServiceUnavailableException(message);
293
+ case httpStatusCodes.HttpStatusCodes.GATEWAY_TIMEOUT:
294
+ return new GatewayTimeoutException(message);
295
+ case httpStatusCodes.HttpStatusCodes.INSUFFICIENT_STORAGE:
296
+ return new InsufficientStorageException(message);
297
+ default:
298
+ return new HttpError(status, message || `HTTP Error ${status}`);
299
+ }
300
+ }
301
+ __name(createHttpError, "createHttpError");
302
+ function hasErrorShape(error) {
303
+ return typeof error === "object" && error !== null && "message" in error && typeof error.message === "string";
304
+ }
305
+ __name(hasErrorShape, "hasErrorShape");
306
+ function getErrorMessage(error) {
307
+ if (typeof error === "string") {
308
+ return error;
309
+ }
310
+ if (error instanceof Error) {
311
+ return error.message;
312
+ }
313
+ if (hasErrorShape(error)) {
314
+ return error.message;
315
+ }
316
+ return "Unknown error occurred";
317
+ }
318
+ __name(getErrorMessage, "getErrorMessage");
319
+ function withErrorHandling(handler) {
320
+ return async (...args) => {
321
+ try {
322
+ return await handler(...args);
323
+ } catch (err) {
324
+ if (isHttpError(err)) {
325
+ throw err;
326
+ }
327
+ logger.getLogger().error({
328
+ err
329
+ }, "Caught error in handler");
330
+ let status = httpStatusCodes.HttpStatusCodes.INTERNAL_SERVER_ERROR;
331
+ if (hasErrorShape(err) && typeof err.status === "number") {
332
+ status = err.status;
333
+ }
334
+ throw createHttpError(status, getErrorMessage(err));
335
+ }
336
+ };
337
+ }
338
+ __name(withErrorHandling, "withErrorHandling");
339
+
340
+ exports.BadGatewayException = BadGatewayException;
341
+ exports.BadRequestException = BadRequestException;
342
+ exports.ConflictException = ConflictException;
343
+ exports.ForbiddenException = ForbiddenException;
344
+ exports.GatewayTimeoutException = GatewayTimeoutException;
345
+ exports.HttpError = HttpError;
346
+ exports.InsufficientStorageException = InsufficientStorageException;
347
+ exports.InternalServerErrorException = InternalServerErrorException;
348
+ exports.MethodNotAllowedException = MethodNotAllowedException;
349
+ exports.NotAcceptableException = NotAcceptableException;
350
+ exports.NotFoundException = NotFoundException;
351
+ exports.PayloadTooLargeException = PayloadTooLargeException;
352
+ exports.RequestTimeoutException = RequestTimeoutException;
353
+ exports.ServiceUnavailableException = ServiceUnavailableException;
354
+ exports.TooManyRequestsException = TooManyRequestsException;
355
+ exports.UnauthorizedException = UnauthorizedException;
356
+ exports.UnprocessableEntityException = UnprocessableEntityException;
357
+ exports.UnsupportedMediaTypeException = UnsupportedMediaTypeException;
358
+ exports.createHttpError = createHttpError;
359
+ exports.getErrorMessage = getErrorMessage;
360
+ exports.hasErrorShape = hasErrorShape;
361
+ exports.isHttpError = isHttpError;
362
+ exports.withErrorHandling = withErrorHandling;
@@ -0,0 +1,25 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2025 Catbee Technologies. https://catbee.in/license
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ export * from './exception.utils';