@catbee/utils 0.0.6 → 0.0.8-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 (249) hide show
  1. package/LICENSE +1 -1
  2. package/README.md +77 -1107
  3. package/build/index.cjs +6888 -0
  4. package/build/index.d.ts +5242 -0
  5. package/build/index.mjs +6615 -0
  6. package/package.json +26 -95
  7. package/build/esm/config.d.ts +0 -130
  8. package/build/esm/config.js +0 -145
  9. package/build/esm/config.js.map +0 -1
  10. package/build/esm/index.d.ts +0 -27
  11. package/build/esm/index.js +0 -50
  12. package/build/esm/index.js.map +0 -1
  13. package/build/esm/servers/server.builder.d.ts +0 -625
  14. package/build/esm/servers/server.builder.js +0 -722
  15. package/build/esm/servers/server.builder.js.map +0 -1
  16. package/build/esm/servers/server.d.ts +0 -256
  17. package/build/esm/servers/server.js +0 -1211
  18. package/build/esm/servers/server.js.map +0 -1
  19. package/build/esm/types/api-response.d.ts +0 -124
  20. package/build/esm/types/api-response.js +0 -25
  21. package/build/esm/types/api-response.js.map +0 -1
  22. package/build/esm/types/index.d.ts +0 -125
  23. package/build/esm/types/index.js +0 -25
  24. package/build/esm/types/index.js.map +0 -1
  25. package/build/esm/types/server.d.ts +0 -285
  26. package/build/esm/types/server.js +0 -25
  27. package/build/esm/types/server.js.map +0 -1
  28. package/build/esm/utils/array.utils.d.ts +0 -168
  29. package/build/esm/utils/array.utils.js +0 -382
  30. package/build/esm/utils/array.utils.js.map +0 -1
  31. package/build/esm/utils/async.utils.d.ts +0 -174
  32. package/build/esm/utils/async.utils.js +0 -652
  33. package/build/esm/utils/async.utils.js.map +0 -1
  34. package/build/esm/utils/cache.utils.d.ts +0 -153
  35. package/build/esm/utils/cache.utils.js +0 -499
  36. package/build/esm/utils/cache.utils.js.map +0 -1
  37. package/build/esm/utils/context-store.utils.d.ts +0 -186
  38. package/build/esm/utils/context-store.utils.js +0 -304
  39. package/build/esm/utils/context-store.utils.js.map +0 -1
  40. package/build/esm/utils/crypto.utils.d.ts +0 -160
  41. package/build/esm/utils/crypto.utils.js +0 -375
  42. package/build/esm/utils/crypto.utils.js.map +0 -1
  43. package/build/esm/utils/decorators.utils.d.ts +0 -315
  44. package/build/esm/utils/decorators.utils.js +0 -666
  45. package/build/esm/utils/decorators.utils.js.map +0 -1
  46. package/build/esm/utils/dir.utils.d.ts +0 -196
  47. package/build/esm/utils/dir.utils.js +0 -998
  48. package/build/esm/utils/dir.utils.js.map +0 -1
  49. package/build/esm/utils/env.utils.d.ts +0 -377
  50. package/build/esm/utils/env.utils.js +0 -873
  51. package/build/esm/utils/env.utils.js.map +0 -1
  52. package/build/esm/utils/exception.utils.d.ts +0 -230
  53. package/build/esm/utils/exception.utils.js +0 -559
  54. package/build/esm/utils/exception.utils.js.map +0 -1
  55. package/build/esm/utils/fs.utils.d.ts +0 -164
  56. package/build/esm/utils/fs.utils.js +0 -580
  57. package/build/esm/utils/fs.utils.js.map +0 -1
  58. package/build/esm/utils/http-status-codes.d.ts +0 -266
  59. package/build/esm/utils/http-status-codes.js +0 -295
  60. package/build/esm/utils/http-status-codes.js.map +0 -1
  61. package/build/esm/utils/id.utils.d.ts +0 -36
  62. package/build/esm/utils/id.utils.js +0 -87
  63. package/build/esm/utils/id.utils.js.map +0 -1
  64. package/build/esm/utils/logger.utils.d.ts +0 -166
  65. package/build/esm/utils/logger.utils.js +0 -347
  66. package/build/esm/utils/logger.utils.js.map +0 -1
  67. package/build/esm/utils/middleware.utils.d.ts +0 -83
  68. package/build/esm/utils/middleware.utils.js +0 -205
  69. package/build/esm/utils/middleware.utils.js.map +0 -1
  70. package/build/esm/utils/obj.utils.d.ts +0 -124
  71. package/build/esm/utils/obj.utils.js +0 -519
  72. package/build/esm/utils/obj.utils.js.map +0 -1
  73. package/build/esm/utils/request.utils.d.ts +0 -77
  74. package/build/esm/utils/request.utils.js +0 -196
  75. package/build/esm/utils/request.utils.js.map +0 -1
  76. package/build/esm/utils/response.utils.d.ts +0 -163
  77. package/build/esm/utils/response.utils.js +0 -284
  78. package/build/esm/utils/response.utils.js.map +0 -1
  79. package/build/esm/utils/string.utils.d.ts +0 -93
  80. package/build/esm/utils/string.utils.js +0 -169
  81. package/build/esm/utils/string.utils.js.map +0 -1
  82. package/build/esm/utils/url.utils.d.ts +0 -141
  83. package/build/esm/utils/url.utils.js +0 -381
  84. package/build/esm/utils/url.utils.js.map +0 -1
  85. package/build/esm/utils/validate.utils.d.ts +0 -177
  86. package/build/esm/utils/validate.utils.js +0 -336
  87. package/build/esm/utils/validate.utils.js.map +0 -1
  88. package/build/esnext/config.d.ts +0 -130
  89. package/build/esnext/config.js +0 -145
  90. package/build/esnext/config.js.map +0 -1
  91. package/build/esnext/index.d.ts +0 -27
  92. package/build/esnext/index.js +0 -50
  93. package/build/esnext/index.js.map +0 -1
  94. package/build/esnext/servers/server.builder.d.ts +0 -625
  95. package/build/esnext/servers/server.builder.js +0 -677
  96. package/build/esnext/servers/server.builder.js.map +0 -1
  97. package/build/esnext/servers/server.d.ts +0 -256
  98. package/build/esnext/servers/server.js +0 -918
  99. package/build/esnext/servers/server.js.map +0 -1
  100. package/build/esnext/types/api-response.d.ts +0 -124
  101. package/build/esnext/types/api-response.js +0 -25
  102. package/build/esnext/types/api-response.js.map +0 -1
  103. package/build/esnext/types/index.d.ts +0 -125
  104. package/build/esnext/types/index.js +0 -25
  105. package/build/esnext/types/index.js.map +0 -1
  106. package/build/esnext/types/server.d.ts +0 -285
  107. package/build/esnext/types/server.js +0 -25
  108. package/build/esnext/types/server.js.map +0 -1
  109. package/build/esnext/utils/array.utils.d.ts +0 -168
  110. package/build/esnext/utils/array.utils.js +0 -345
  111. package/build/esnext/utils/array.utils.js.map +0 -1
  112. package/build/esnext/utils/async.utils.d.ts +0 -174
  113. package/build/esnext/utils/async.utils.js +0 -430
  114. package/build/esnext/utils/async.utils.js.map +0 -1
  115. package/build/esnext/utils/cache.utils.d.ts +0 -153
  116. package/build/esnext/utils/cache.utils.js +0 -293
  117. package/build/esnext/utils/cache.utils.js.map +0 -1
  118. package/build/esnext/utils/context-store.utils.d.ts +0 -186
  119. package/build/esnext/utils/context-store.utils.js +0 -286
  120. package/build/esnext/utils/context-store.utils.js.map +0 -1
  121. package/build/esnext/utils/crypto.utils.d.ts +0 -160
  122. package/build/esnext/utils/crypto.utils.js +0 -276
  123. package/build/esnext/utils/crypto.utils.js.map +0 -1
  124. package/build/esnext/utils/decorators.utils.d.ts +0 -315
  125. package/build/esnext/utils/decorators.utils.js +0 -504
  126. package/build/esnext/utils/decorators.utils.js.map +0 -1
  127. package/build/esnext/utils/dir.utils.d.ts +0 -196
  128. package/build/esnext/utils/dir.utils.js +0 -477
  129. package/build/esnext/utils/dir.utils.js.map +0 -1
  130. package/build/esnext/utils/env.utils.d.ts +0 -377
  131. package/build/esnext/utils/env.utils.js +0 -785
  132. package/build/esnext/utils/env.utils.js.map +0 -1
  133. package/build/esnext/utils/exception.utils.d.ts +0 -230
  134. package/build/esnext/utils/exception.utils.js +0 -381
  135. package/build/esnext/utils/exception.utils.js.map +0 -1
  136. package/build/esnext/utils/fs.utils.d.ts +0 -164
  137. package/build/esnext/utils/fs.utils.js +0 -349
  138. package/build/esnext/utils/fs.utils.js.map +0 -1
  139. package/build/esnext/utils/http-status-codes.d.ts +0 -266
  140. package/build/esnext/utils/http-status-codes.js +0 -295
  141. package/build/esnext/utils/http-status-codes.js.map +0 -1
  142. package/build/esnext/utils/id.utils.d.ts +0 -36
  143. package/build/esnext/utils/id.utils.js +0 -84
  144. package/build/esnext/utils/id.utils.js.map +0 -1
  145. package/build/esnext/utils/logger.utils.d.ts +0 -166
  146. package/build/esnext/utils/logger.utils.js +0 -306
  147. package/build/esnext/utils/logger.utils.js.map +0 -1
  148. package/build/esnext/utils/middleware.utils.d.ts +0 -83
  149. package/build/esnext/utils/middleware.utils.js +0 -198
  150. package/build/esnext/utils/middleware.utils.js.map +0 -1
  151. package/build/esnext/utils/obj.utils.d.ts +0 -124
  152. package/build/esnext/utils/obj.utils.js +0 -413
  153. package/build/esnext/utils/obj.utils.js.map +0 -1
  154. package/build/esnext/utils/request.utils.d.ts +0 -77
  155. package/build/esnext/utils/request.utils.js +0 -168
  156. package/build/esnext/utils/request.utils.js.map +0 -1
  157. package/build/esnext/utils/response.utils.d.ts +0 -163
  158. package/build/esnext/utils/response.utils.js +0 -247
  159. package/build/esnext/utils/response.utils.js.map +0 -1
  160. package/build/esnext/utils/string.utils.d.ts +0 -93
  161. package/build/esnext/utils/string.utils.js +0 -165
  162. package/build/esnext/utils/string.utils.js.map +0 -1
  163. package/build/esnext/utils/url.utils.d.ts +0 -141
  164. package/build/esnext/utils/url.utils.js +0 -304
  165. package/build/esnext/utils/url.utils.js.map +0 -1
  166. package/build/esnext/utils/validate.utils.d.ts +0 -177
  167. package/build/esnext/utils/validate.utils.js +0 -320
  168. package/build/esnext/utils/validate.utils.js.map +0 -1
  169. package/build/src/config.d.ts +0 -130
  170. package/build/src/config.js +0 -150
  171. package/build/src/config.js.map +0 -1
  172. package/build/src/index.d.ts +0 -27
  173. package/build/src/index.js +0 -69
  174. package/build/src/index.js.map +0 -1
  175. package/build/src/servers/server.builder.d.ts +0 -625
  176. package/build/src/servers/server.builder.js +0 -681
  177. package/build/src/servers/server.builder.js.map +0 -1
  178. package/build/src/servers/server.d.ts +0 -256
  179. package/build/src/servers/server.js +0 -958
  180. package/build/src/servers/server.js.map +0 -1
  181. package/build/src/types/api-response.d.ts +0 -124
  182. package/build/src/types/api-response.js +0 -26
  183. package/build/src/types/api-response.js.map +0 -1
  184. package/build/src/types/index.d.ts +0 -125
  185. package/build/src/types/index.js +0 -26
  186. package/build/src/types/index.js.map +0 -1
  187. package/build/src/types/server.d.ts +0 -285
  188. package/build/src/types/server.js +0 -26
  189. package/build/src/types/server.js.map +0 -1
  190. package/build/src/utils/array.utils.d.ts +0 -168
  191. package/build/src/utils/array.utils.js +0 -364
  192. package/build/src/utils/array.utils.js.map +0 -1
  193. package/build/src/utils/async.utils.d.ts +0 -174
  194. package/build/src/utils/async.utils.js +0 -447
  195. package/build/src/utils/async.utils.js.map +0 -1
  196. package/build/src/utils/cache.utils.d.ts +0 -153
  197. package/build/src/utils/cache.utils.js +0 -297
  198. package/build/src/utils/cache.utils.js.map +0 -1
  199. package/build/src/utils/context-store.utils.d.ts +0 -186
  200. package/build/src/utils/context-store.utils.js +0 -293
  201. package/build/src/utils/context-store.utils.js.map +0 -1
  202. package/build/src/utils/crypto.utils.d.ts +0 -160
  203. package/build/src/utils/crypto.utils.js +0 -293
  204. package/build/src/utils/crypto.utils.js.map +0 -1
  205. package/build/src/utils/decorators.utils.d.ts +0 -315
  206. package/build/src/utils/decorators.utils.js +0 -516
  207. package/build/src/utils/decorators.utils.js.map +0 -1
  208. package/build/src/utils/dir.utils.d.ts +0 -196
  209. package/build/src/utils/dir.utils.js +0 -501
  210. package/build/src/utils/dir.utils.js.map +0 -1
  211. package/build/src/utils/env.utils.d.ts +0 -377
  212. package/build/src/utils/env.utils.js +0 -789
  213. package/build/src/utils/env.utils.js.map +0 -1
  214. package/build/src/utils/exception.utils.d.ts +0 -230
  215. package/build/src/utils/exception.utils.js +0 -407
  216. package/build/src/utils/exception.utils.js.map +0 -1
  217. package/build/src/utils/fs.utils.d.ts +0 -164
  218. package/build/src/utils/fs.utils.js +0 -372
  219. package/build/src/utils/fs.utils.js.map +0 -1
  220. package/build/src/utils/http-status-codes.d.ts +0 -266
  221. package/build/src/utils/http-status-codes.js +0 -298
  222. package/build/src/utils/http-status-codes.js.map +0 -1
  223. package/build/src/utils/id.utils.d.ts +0 -36
  224. package/build/src/utils/id.utils.js +0 -91
  225. package/build/src/utils/id.utils.js.map +0 -1
  226. package/build/src/utils/logger.utils.d.ts +0 -166
  227. package/build/src/utils/logger.utils.js +0 -352
  228. package/build/src/utils/logger.utils.js.map +0 -1
  229. package/build/src/utils/middleware.utils.d.ts +0 -83
  230. package/build/src/utils/middleware.utils.js +0 -205
  231. package/build/src/utils/middleware.utils.js.map +0 -1
  232. package/build/src/utils/obj.utils.d.ts +0 -124
  233. package/build/src/utils/obj.utils.js +0 -429
  234. package/build/src/utils/obj.utils.js.map +0 -1
  235. package/build/src/utils/request.utils.d.ts +0 -77
  236. package/build/src/utils/request.utils.js +0 -175
  237. package/build/src/utils/request.utils.js.map +0 -1
  238. package/build/src/utils/response.utils.d.ts +0 -163
  239. package/build/src/utils/response.utils.js +0 -260
  240. package/build/src/utils/response.utils.js.map +0 -1
  241. package/build/src/utils/string.utils.d.ts +0 -93
  242. package/build/src/utils/string.utils.js +0 -179
  243. package/build/src/utils/string.utils.js.map +0 -1
  244. package/build/src/utils/url.utils.d.ts +0 -141
  245. package/build/src/utils/url.utils.js +0 -317
  246. package/build/src/utils/url.utils.js.map +0 -1
  247. package/build/src/utils/validate.utils.d.ts +0 -177
  248. package/build/src/utils/validate.utils.js +0 -345
  249. package/build/src/utils/validate.utils.js.map +0 -1
@@ -1,407 +0,0 @@
1
- "use strict";
2
- /*
3
- * The MIT License
4
- *
5
- * Copyright (c) 2025 Catbee Technologies
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
- /**
197
- * @param message - Optional custom message. Defaults to "Unprocessable Entity".
198
- * @param details - Optional validation details or additional error context.
199
- */
200
- constructor(message = 'Unprocessable Entity', details) {
201
- super(message);
202
- this.details = details;
203
- this.status = http_status_codes_1.HttpStatusCodes.UNPROCESSABLE_ENTITY;
204
- }
205
- }
206
- exports.UnprocessableEntityException = UnprocessableEntityException;
207
- /**
208
- * Represents a 405 Method Not Allowed Error.
209
- * Used when the request method is not supported for the requested resource.
210
- */
211
- class MethodNotAllowedException extends response_utils_1.ErrorResponse {
212
- /**
213
- * @param message - Optional custom message. Defaults to "Method Not Allowed".
214
- * @param allowedMethods - Optional array of allowed HTTP methods.
215
- */
216
- constructor(message = 'Method Not Allowed', allowedMethods) {
217
- super(message);
218
- this.allowedMethods = allowedMethods;
219
- this.status = http_status_codes_1.HttpStatusCodes.METHOD_NOT_ALLOWED;
220
- }
221
- }
222
- exports.MethodNotAllowedException = MethodNotAllowedException;
223
- /**
224
- * Represents a 406 Not Acceptable Error.
225
- * Used when the server cannot produce a response matching the client's accepted content types.
226
- */
227
- class NotAcceptableException extends response_utils_1.ErrorResponse {
228
- /**
229
- * @param message - Optional custom message. Defaults to "Not Acceptable".
230
- */
231
- constructor(message = 'Not Acceptable') {
232
- super(message);
233
- this.status = http_status_codes_1.HttpStatusCodes.NOT_ACCEPTABLE;
234
- }
235
- }
236
- exports.NotAcceptableException = NotAcceptableException;
237
- /**
238
- * Represents a 408 Request Timeout Error.
239
- * Used when the server times out waiting for the client to send a request.
240
- */
241
- class RequestTimeoutException extends response_utils_1.ErrorResponse {
242
- /**
243
- * @param message - Optional custom message. Defaults to "Request Timeout".
244
- */
245
- constructor(message = 'Request Timeout') {
246
- super(message);
247
- this.status = http_status_codes_1.HttpStatusCodes.REQUEST_TIMEOUT;
248
- }
249
- }
250
- exports.RequestTimeoutException = RequestTimeoutException;
251
- /**
252
- * Represents a 415 Unsupported Media Type Error.
253
- * Used when the request's Content-Type is not supported.
254
- */
255
- class UnsupportedMediaTypeException extends response_utils_1.ErrorResponse {
256
- /**
257
- * @param message - Optional custom message. Defaults to "Unsupported Media Type".
258
- */
259
- constructor(message = 'Unsupported Media Type') {
260
- super(message);
261
- this.status = http_status_codes_1.HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE;
262
- }
263
- }
264
- exports.UnsupportedMediaTypeException = UnsupportedMediaTypeException;
265
- /**
266
- * Represents a 413 Payload Too Large Error.
267
- * Used when the request body exceeds server limits.
268
- */
269
- class PayloadTooLargeException extends response_utils_1.ErrorResponse {
270
- /**
271
- * @param message - Optional custom message. Defaults to "Payload Too Large".
272
- */
273
- constructor(message = 'Payload Too Large') {
274
- super(message);
275
- this.status = http_status_codes_1.HttpStatusCodes.PAYLOAD_TOO_LARGE;
276
- }
277
- }
278
- exports.PayloadTooLargeException = PayloadTooLargeException;
279
- /**
280
- * Represents a 507 Insufficient Storage Error.
281
- * Used when the server has insufficient storage to complete the request.
282
- */
283
- class InsufficientStorageException extends response_utils_1.ErrorResponse {
284
- /**
285
- * @param message - Optional custom message. Defaults to "Insufficient Storage".
286
- */
287
- constructor(message = 'Insufficient Storage') {
288
- super(message);
289
- this.status = http_status_codes_1.HttpStatusCodes.INSUFFICIENT_STORAGE;
290
- }
291
- }
292
- exports.InsufficientStorageException = InsufficientStorageException;
293
- /**
294
- * Checks if an error is an instance of HttpError or its subclasses.
295
- *
296
- * @param error - The error to check.
297
- * @returns True if the error is an HttpError, false otherwise.
298
- */
299
- function isHttpError(error) {
300
- return error instanceof response_utils_1.ErrorResponse;
301
- }
302
- /**
303
- * Creates an HTTP error with the specified status code and message.
304
- * Factory function for creating appropriate HTTP error instances.
305
- *
306
- * @param status - HTTP status code.
307
- * @param message - Optional custom error message.
308
- * @returns An instance of the appropriate HTTP error class.
309
- */
310
- function createHttpError(status, message) {
311
- switch (status) {
312
- case http_status_codes_1.HttpStatusCodes.BAD_REQUEST:
313
- return new BadRequestException(message);
314
- case http_status_codes_1.HttpStatusCodes.UNAUTHORIZED:
315
- return new UnauthorizedException(message);
316
- case http_status_codes_1.HttpStatusCodes.FORBIDDEN:
317
- return new ForbiddenException(message);
318
- case http_status_codes_1.HttpStatusCodes.NOT_FOUND:
319
- return new NotFoundException(message);
320
- case http_status_codes_1.HttpStatusCodes.METHOD_NOT_ALLOWED:
321
- return new MethodNotAllowedException(message);
322
- case http_status_codes_1.HttpStatusCodes.NOT_ACCEPTABLE:
323
- return new NotAcceptableException(message);
324
- case http_status_codes_1.HttpStatusCodes.REQUEST_TIMEOUT:
325
- return new RequestTimeoutException(message);
326
- case http_status_codes_1.HttpStatusCodes.CONFLICT:
327
- return new ConflictException(message);
328
- case http_status_codes_1.HttpStatusCodes.GONE:
329
- return new HttpError(http_status_codes_1.HttpStatusCodes.GONE, message || 'Gone');
330
- case http_status_codes_1.HttpStatusCodes.PAYLOAD_TOO_LARGE:
331
- return new PayloadTooLargeException(message);
332
- case http_status_codes_1.HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE:
333
- return new UnsupportedMediaTypeException(message);
334
- case http_status_codes_1.HttpStatusCodes.UNPROCESSABLE_ENTITY:
335
- return new UnprocessableEntityException(message);
336
- case http_status_codes_1.HttpStatusCodes.TOO_MANY_REQUESTS:
337
- return new TooManyRequestsException(message);
338
- case http_status_codes_1.HttpStatusCodes.INTERNAL_SERVER_ERROR:
339
- return new InternalServerErrorException(message);
340
- case http_status_codes_1.HttpStatusCodes.BAD_GATEWAY:
341
- return new BadGatewayException(message);
342
- case http_status_codes_1.HttpStatusCodes.SERVICE_UNAVAILABLE:
343
- return new ServiceUnavailableException(message);
344
- case http_status_codes_1.HttpStatusCodes.GATEWAY_TIMEOUT:
345
- return new GatewayTimeoutException(message);
346
- case http_status_codes_1.HttpStatusCodes.INSUFFICIENT_STORAGE:
347
- return new InsufficientStorageException(message);
348
- default:
349
- return new HttpError(status, message || `HTTP Error ${status}`);
350
- }
351
- }
352
- /**
353
- * Type guard to check if an object has specific error properties.
354
- *
355
- * @param error - The object to check.
356
- * @returns True if object has error-like structure.
357
- */
358
- function hasErrorShape(error) {
359
- return (typeof error === 'object' && error !== null && 'message' in error && typeof error.message === 'string');
360
- }
361
- /**
362
- * Safely extracts message from any error type.
363
- *
364
- * @param error - Any error object or value.
365
- * @returns A string error message.
366
- */
367
- function getErrorMessage(error) {
368
- if (typeof error === 'string') {
369
- return error;
370
- }
371
- if (error instanceof Error) {
372
- return error.message;
373
- }
374
- if (hasErrorShape(error)) {
375
- return error.message;
376
- }
377
- return 'Unknown error occurred';
378
- }
379
- /**
380
- * Wraps a function and transforms any errors into HTTP errors.
381
- * Useful for API handlers to ensure consistent error responses.
382
- *
383
- * @param handler - The async function to wrap.
384
- * @returns A function that always returns or throws an HTTP error.
385
- */
386
- function withErrorHandling(handler) {
387
- return async (...args) => {
388
- try {
389
- return await handler(...args);
390
- }
391
- catch (err) {
392
- if (isHttpError(err)) {
393
- throw err;
394
- }
395
- // Log the original error (consider using a proper logger)
396
- (0, logger_utils_1.getLogger)().error({ err }, 'Caught error in handler');
397
- // Extract status code if it exists, otherwise use 500
398
- let status = http_status_codes_1.HttpStatusCodes.INTERNAL_SERVER_ERROR;
399
- if (hasErrorShape(err) && typeof err.status === 'number') {
400
- status = err.status;
401
- }
402
- // Create appropriate HTTP error
403
- throw createHttpError(status, getErrorMessage(err));
404
- }
405
- };
406
- }
407
- //# sourceMappingURL=exception.utils.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"exception.utils.js","sourceRoot":"","sources":["../../../src/utils/exception.utils.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;;;AAkRH,kCAEC;AAUD,0CAyCC;AAQD,sCAIC;AAQD,0CAcC;AASD,8CAwBC;AAxYD,2DAAsD;AACtD,iDAA2C;AAC3C,qDAAiD;AAEjD;;;GAGG;AACH,MAAa,SAAU,SAAQ,8BAAa;IAC1C;;;;OAIG;IACH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AAC;AAED;;;;;;GAMG;AACH,SAAgB,iBAAiB,CAC/B,OAAU;IAEV,OAAO,KAAK,EAAE,GAAG,IAAmB,EAAE,EAAE;QACtC,IAAI,CAAC;YACH,OAAO,MAAM,OAAO,CAAC,GAAG,IAAI,CAAC,CAAC;QAChC,CAAC;QAAC,OAAO,GAAG,EAAE,CAAC;YACb,IAAI,WAAW,CAAC,GAAG,CAAC,EAAE,CAAC;gBACrB,MAAM,GAAG,CAAC;YACZ,CAAC;YAED,0DAA0D;YAC1D,IAAA,wBAAS,GAAE,CAAC,KAAK,CAAC,EAAE,GAAG,EAAE,EAAE,yBAAyB,CAAC,CAAC;YAEtD,sDAAsD;YACtD,IAAI,MAAM,GAAG,mCAAe,CAAC,qBAAqB,CAAC;YACnD,IAAI,aAAa,CAAC,GAAG,CAAC,IAAI,OAAO,GAAG,CAAC,MAAM,KAAK,QAAQ,EAAE,CAAC;gBACzD,MAAM,GAAG,GAAG,CAAC,MAAM,CAAC;YACtB,CAAC;YAED,gCAAgC;YAChC,MAAM,eAAe,CAAC,MAAM,EAAE,eAAe,CAAC,GAAG,CAAC,CAAC,CAAC;QACtD,CAAC;IACH,CAAC,CAAC;AACJ,CAAC","sourcesContent":["/*\n * The MIT License\n *\n * Copyright (c) 2025 Catbee Technologies\n *\n * Permission is hereby granted, free of charge, to any person obtaining a copy\n * of this software and associated documentation files (the \"Software\"), to deal\n * in the Software without restriction, including without limitation the rights\n * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n * copies of the Software, and to permit persons to whom the Software is\n * furnished to do so, subject to the following conditions:\n *\n * The above copyright notice and this permission notice shall be included in all\n * copies or substantial portions of the Software.\n *\n * THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n * SOFTWARE.\n */\n\nimport { HttpStatusCodes } from './http-status-codes';\nimport { getLogger } from './logger.utils';\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\n/**\n * Represents a 422 Unprocessable Entity Error.\n * Used when the server understands the content type but cannot process the contained instructions.\n */\nexport class UnprocessableEntityException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Unprocessable Entity\".\n * @param details - Optional validation details or additional error context.\n */\n constructor(\n message: string = 'Unprocessable Entity',\n public details?: Record<string, any>\n ) {\n super(message);\n this.status = HttpStatusCodes.UNPROCESSABLE_ENTITY;\n }\n}\n\n/**\n * Represents a 405 Method Not Allowed Error.\n * Used when the request method is not supported for the requested resource.\n */\nexport class MethodNotAllowedException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Method Not Allowed\".\n * @param allowedMethods - Optional array of allowed HTTP methods.\n */\n constructor(\n message: string = 'Method Not Allowed',\n public allowedMethods?: string[]\n ) {\n super(message);\n this.status = HttpStatusCodes.METHOD_NOT_ALLOWED;\n }\n}\n\n/**\n * Represents a 406 Not Acceptable Error.\n * Used when the server cannot produce a response matching the client's accepted content types.\n */\nexport class NotAcceptableException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Not Acceptable\".\n */\n constructor(message: string = 'Not Acceptable') {\n super(message);\n this.status = HttpStatusCodes.NOT_ACCEPTABLE;\n }\n}\n\n/**\n * Represents a 408 Request Timeout Error.\n * Used when the server times out waiting for the client to send a request.\n */\nexport class RequestTimeoutException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Request Timeout\".\n */\n constructor(message: string = 'Request Timeout') {\n super(message);\n this.status = HttpStatusCodes.REQUEST_TIMEOUT;\n }\n}\n\n/**\n * Represents a 415 Unsupported Media Type Error.\n * Used when the request's Content-Type is not supported.\n */\nexport class UnsupportedMediaTypeException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Unsupported Media Type\".\n */\n constructor(message: string = 'Unsupported Media Type') {\n super(message);\n this.status = HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE;\n }\n}\n\n/**\n * Represents a 413 Payload Too Large Error.\n * Used when the request body exceeds server limits.\n */\nexport class PayloadTooLargeException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Payload Too Large\".\n */\n constructor(message: string = 'Payload Too Large') {\n super(message);\n this.status = HttpStatusCodes.PAYLOAD_TOO_LARGE;\n }\n}\n\n/**\n * Represents a 507 Insufficient Storage Error.\n * Used when the server has insufficient storage to complete the request.\n */\nexport class InsufficientStorageException extends ErrorResponse {\n /**\n * @param message - Optional custom message. Defaults to \"Insufficient Storage\".\n */\n constructor(message: string = 'Insufficient Storage') {\n super(message);\n this.status = HttpStatusCodes.INSUFFICIENT_STORAGE;\n }\n}\n\n/**\n * Checks if an error is an instance of HttpError or its subclasses.\n *\n * @param error - The error to check.\n * @returns True if the error is an HttpError, false otherwise.\n */\nexport function isHttpError(error: unknown): error is ErrorResponse {\n return error instanceof ErrorResponse;\n}\n\n/**\n * Creates an HTTP error with the specified status code and message.\n * Factory function for creating appropriate HTTP error instances.\n *\n * @param status - HTTP status code.\n * @param message - Optional custom error message.\n * @returns An instance of the appropriate HTTP error class.\n */\nexport function createHttpError(status: number, message?: string): ErrorResponse {\n switch (status) {\n case HttpStatusCodes.BAD_REQUEST:\n return new BadRequestException(message);\n case HttpStatusCodes.UNAUTHORIZED:\n return new UnauthorizedException(message);\n case HttpStatusCodes.FORBIDDEN:\n return new ForbiddenException(message);\n case HttpStatusCodes.NOT_FOUND:\n return new NotFoundException(message);\n case HttpStatusCodes.METHOD_NOT_ALLOWED:\n return new MethodNotAllowedException(message);\n case HttpStatusCodes.NOT_ACCEPTABLE:\n return new NotAcceptableException(message);\n case HttpStatusCodes.REQUEST_TIMEOUT:\n return new RequestTimeoutException(message);\n case HttpStatusCodes.CONFLICT:\n return new ConflictException(message);\n case HttpStatusCodes.GONE:\n return new HttpError(HttpStatusCodes.GONE, message || 'Gone');\n case HttpStatusCodes.PAYLOAD_TOO_LARGE:\n return new PayloadTooLargeException(message);\n case HttpStatusCodes.UNSUPPORTED_MEDIA_TYPE:\n return new UnsupportedMediaTypeException(message);\n case HttpStatusCodes.UNPROCESSABLE_ENTITY:\n return new UnprocessableEntityException(message);\n case HttpStatusCodes.TOO_MANY_REQUESTS:\n return new TooManyRequestsException(message);\n case HttpStatusCodes.INTERNAL_SERVER_ERROR:\n return new InternalServerErrorException(message);\n case HttpStatusCodes.BAD_GATEWAY:\n return new BadGatewayException(message);\n case HttpStatusCodes.SERVICE_UNAVAILABLE:\n return new ServiceUnavailableException(message);\n case HttpStatusCodes.GATEWAY_TIMEOUT:\n return new GatewayTimeoutException(message);\n case HttpStatusCodes.INSUFFICIENT_STORAGE:\n return new InsufficientStorageException(message);\n default:\n return new HttpError(status, message || `HTTP Error ${status}`);\n }\n}\n\n/**\n * Type guard to check if an object has specific error properties.\n *\n * @param error - The object to check.\n * @returns True if object has error-like structure.\n */\nexport function hasErrorShape(error: unknown): error is { message: string; status?: number; code?: string } {\n return (\n typeof error === 'object' && error !== null && 'message' in error && typeof (error as any).message === 'string'\n );\n}\n\n/**\n * Safely extracts message from any error type.\n *\n * @param error - Any error object or value.\n * @returns A string error message.\n */\nexport function getErrorMessage(error: unknown): string {\n if (typeof error === 'string') {\n return error;\n }\n\n if (error instanceof Error) {\n return error.message;\n }\n\n if (hasErrorShape(error)) {\n return error.message;\n }\n\n return 'Unknown error occurred';\n}\n\n/**\n * Wraps a function and transforms any errors into HTTP errors.\n * Useful for API handlers to ensure consistent error responses.\n *\n * @param handler - The async function to wrap.\n * @returns A function that always returns or throws an HTTP error.\n */\nexport function withErrorHandling<T extends (...args: any[]) => Promise<any>>(\n handler: T\n): (...args: Parameters<T>) => Promise<Awaited<ReturnType<T>>> {\n return async (...args: Parameters<T>) => {\n try {\n return await handler(...args);\n } catch (err) {\n if (isHttpError(err)) {\n throw err;\n }\n\n // Log the original error (consider using a proper logger)\n getLogger().error({ err }, 'Caught error in handler');\n\n // Extract status code if it exists, otherwise use 500\n let status = HttpStatusCodes.INTERNAL_SERVER_ERROR;\n if (hasErrorShape(err) && typeof err.status === 'number') {\n status = err.status;\n }\n\n // Create appropriate HTTP error\n throw createHttpError(status, getErrorMessage(err));\n }\n };\n}\n"]}
@@ -1,164 +0,0 @@
1
- import { BufferEncoding } from './crypto.utils';
2
- import { Stats } from 'fs';
3
- /**
4
- * Checks whether a file or directory exists at the given path.
5
- *
6
- * @param {string} path - The file or directory path to check.
7
- * @returns {Promise<boolean>} Resolves to `true` if the path exists, `false` otherwise.
8
- */
9
- export declare function fileExists(path: string): Promise<boolean>;
10
- /**
11
- * Reads and parses a JSON file from the specified path.
12
- *
13
- * @typeParam T - The expected type of the parsed JSON object.
14
- * @param {string} path - The path to the JSON file.
15
- * @returns {Promise<T | null>} The parsed object, or `null` if reading or parsing fails.
16
- */
17
- export declare function readJsonFile<T = any>(path: string): Promise<T | null>;
18
- /**
19
- * Writes a JavaScript object to a file as formatted JSON.
20
- *
21
- * @param {string} path - The destination file path.
22
- * @param {any} data - The object to serialize and write.
23
- * @param {number} [space=2] - Number of spaces for JSON formatting.
24
- * @returns {Promise<void>} Resolves when writing is complete.
25
- */
26
- export declare function writeJsonFile(path: string, data: any, space?: number): Promise<void>;
27
- /**
28
- * Deletes a file if it exists.
29
- *
30
- * @param {string} path - The file path to delete.
31
- * @returns {Promise<boolean>} Resolves to `true` if the file was deleted or didn't exist, `false` if deletion failed.
32
- */
33
- export declare function deleteFileIfExists(path: string): Promise<boolean>;
34
- /**
35
- * Reads a text file from the specified path.
36
- *
37
- * @param {string} path - The path to the text file.
38
- * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
39
- * @returns {Promise<string | null>} The file contents, or `null` if reading fails.
40
- */
41
- export declare function readTextFile(path: string, encoding?: BufferEncoding): Promise<string | null>;
42
- /**
43
- * Writes text content to a file.
44
- *
45
- * @param {string} path - The destination file path.
46
- * @param {string} content - The text content to write.
47
- * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
48
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
49
- */
50
- export declare function writeTextFile(path: string, content: string, encoding?: BufferEncoding): Promise<boolean>;
51
- /**
52
- * Appends text content to a file.
53
- *
54
- * @param {string} path - The file path to append to.
55
- * @param {string} content - The text content to append.
56
- * @param {BufferEncoding} [encoding="utf-8"] - The encoding to use.
57
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
58
- */
59
- export declare function appendTextFile(path: string, content: string, encoding?: BufferEncoding): Promise<boolean>;
60
- /**
61
- * Copies a file from source to destination.
62
- *
63
- * @param {string} source - Source file path.
64
- * @param {string} destination - Destination file path.
65
- * @param {boolean} [overwrite=false] - Whether to overwrite if destination exists.
66
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
67
- */
68
- export declare function copyFile(source: string, destination: string, overwrite?: boolean): Promise<boolean>;
69
- /**
70
- * Renames/moves a file.
71
- *
72
- * @param {string} oldPath - Current file path.
73
- * @param {string} newPath - New file path.
74
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
75
- */
76
- export declare function moveFile(oldPath: string, newPath: string): Promise<boolean>;
77
- /**
78
- * Gets file stats if the file exists.
79
- *
80
- * @param {string} path - Path to the file.
81
- * @returns {Promise<fs.Stats | null>} File stats object or null if file doesn't exist.
82
- */
83
- export declare function getFileStats(path: string): Promise<Stats | null>;
84
- /**
85
- * Creates a temporary file with optional content.
86
- *
87
- * @param {object} [options] - Options for creating the temp file.
88
- * @param {string} [options.prefix="tmp-"] - Filename prefix.
89
- * @param {string} [options.extension=""] - File extension.
90
- * @param {string} [options.dir] - Directory to create the file in (defaults to OS temp dir).
91
- * @param {string | Buffer} [options.content] - Optional content to write to the file.
92
- * @returns {Promise<string>} Path to the created temporary file.
93
- */
94
- export declare function createTempFile({ prefix, extension, dir, content }?: {
95
- prefix?: string;
96
- extension?: string;
97
- dir?: string;
98
- content?: string | Buffer;
99
- }): Promise<string>;
100
- /**
101
- * Streams a file from source to destination.
102
- * Useful for large files to avoid loading the entire file into memory.
103
- *
104
- * @param {string} source - Source file path.
105
- * @param {string} destination - Destination file path.
106
- * @returns {Promise<void>} Resolves when streaming completes.
107
- * @throws {Error} If streaming fails.
108
- */
109
- export declare function streamFile(source: string, destination: string): Promise<void>;
110
- /**
111
- * Reads a directory and returns file names.
112
- *
113
- * @param {string} dirPath - Path to the directory.
114
- * @param {object} [options] - Options for reading the directory.
115
- * @param {boolean} [options.fullPaths=false] - Whether to return full paths.
116
- * @param {RegExp} [options.filter] - Optional regex to filter files.
117
- * @returns {Promise<string[]>} Array of file names or paths.
118
- * @throws {Error} If directory cannot be read.
119
- */
120
- export declare function readDirectory(dirPath: string, options?: {
121
- fullPaths?: boolean;
122
- filter?: RegExp;
123
- }): Promise<string[]>;
124
- /**
125
- * Creates a directory if it doesn't exist.
126
- *
127
- * @param {string} dirPath - Path to the directory.
128
- * @param {boolean} [recursive=true] - Whether to create parent directories.
129
- * @returns {Promise<boolean>} Resolves to `true` if successful, `false` otherwise.
130
- */
131
- export declare function createDirectory(dirPath: string, recursive?: boolean): Promise<boolean>;
132
- /**
133
- * Safely reads and parses a JSON file with error details.
134
- *
135
- * @typeParam T - The expected type of the parsed JSON object.
136
- * @param {string} path - The path to the JSON file.
137
- * @returns {Promise<{ data: T | null; error: Error | null }>} Object with data and error properties.
138
- */
139
- export declare function safeReadJsonFile<T = any>(path: string): Promise<{
140
- data: T | null;
141
- error: Error | null;
142
- }>;
143
- /**
144
- * Checks if a path points to a file (not a directory).
145
- *
146
- * @param {string} path - Path to check.
147
- * @returns {Promise<boolean>} True if the path is a file, false otherwise.
148
- */
149
- export declare function isFile(path: string): Promise<boolean>;
150
- /**
151
- * Gets the size of a file in bytes.
152
- *
153
- * @param {string} path - Path to the file.
154
- * @returns {Promise<number>} Size in bytes or -1 if file doesn't exist.
155
- */
156
- export declare function getFileSize(path: string): Promise<number>;
157
- /**
158
- * Reads a file as a Buffer.
159
- *
160
- * @param {string} path - Path to the file.
161
- * @returns {Promise<Buffer | null>} File contents as Buffer or null if reading fails.
162
- */
163
- export declare function readFileBuffer(path: string): Promise<Buffer | null>;
164
- //# sourceMappingURL=fs.utils.d.ts.map