@catbee/utils 0.0.8-rc.3 → 1.0.1

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