@catbee/utils 0.0.8-next.0 → 0.0.8-rc.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 (284) hide show
  1. package/README.md +1 -2
  2. package/build/esm/config.d.ts +122 -0
  3. package/build/esm/config.js +133 -0
  4. package/build/esm/config.js.map +1 -0
  5. package/build/esm/index.d.ts +27 -0
  6. package/build/esm/index.js +50 -0
  7. package/build/esm/index.js.map +1 -0
  8. package/build/esm/servers/server.builder.d.ts +508 -0
  9. package/build/esm/servers/server.builder.js +702 -0
  10. package/build/esm/servers/server.builder.js.map +1 -0
  11. package/build/esm/servers/server.d.ts +256 -0
  12. package/build/esm/servers/server.js +1248 -0
  13. package/build/esm/servers/server.js.map +1 -0
  14. package/build/esm/types/api-response.d.ts +152 -0
  15. package/build/esm/types/api-response.js +34 -0
  16. package/build/esm/types/api-response.js.map +1 -0
  17. package/build/esm/types/index.d.ts +125 -0
  18. package/build/esm/types/index.js +25 -0
  19. package/build/esm/types/index.js.map +1 -0
  20. package/build/esm/types/server.d.ts +268 -0
  21. package/build/esm/types/server.js +25 -0
  22. package/build/esm/types/server.js.map +1 -0
  23. package/build/esm/utils/array.utils.d.ts +168 -0
  24. package/build/esm/utils/array.utils.js +382 -0
  25. package/build/esm/utils/array.utils.js.map +1 -0
  26. package/build/esm/utils/async.utils.d.ts +265 -0
  27. package/build/esm/utils/async.utils.js +896 -0
  28. package/build/esm/utils/async.utils.js.map +1 -0
  29. package/build/esm/utils/cache.utils.d.ts +153 -0
  30. package/build/esm/utils/cache.utils.js +499 -0
  31. package/build/esm/utils/cache.utils.js.map +1 -0
  32. package/build/esm/utils/context-store.utils.d.ts +189 -0
  33. package/build/esm/utils/context-store.utils.js +307 -0
  34. package/build/esm/utils/context-store.utils.js.map +1 -0
  35. package/build/esm/utils/crypto.utils.d.ts +160 -0
  36. package/build/esm/utils/crypto.utils.js +375 -0
  37. package/build/esm/utils/crypto.utils.js.map +1 -0
  38. package/build/esm/utils/date.utils.d.ts +159 -0
  39. package/build/esm/utils/date.utils.js +389 -0
  40. package/build/esm/utils/date.utils.js.map +1 -0
  41. package/build/esm/utils/decorators.utils.d.ts +508 -0
  42. package/build/esm/utils/decorators.utils.js +1057 -0
  43. package/build/esm/utils/decorators.utils.js.map +1 -0
  44. package/build/esm/utils/dir.utils.d.ts +196 -0
  45. package/build/esm/utils/dir.utils.js +998 -0
  46. package/build/esm/utils/dir.utils.js.map +1 -0
  47. package/build/esm/utils/env.utils.d.ts +377 -0
  48. package/build/esm/utils/env.utils.js +873 -0
  49. package/build/esm/utils/env.utils.js.map +1 -0
  50. package/build/esm/utils/exception.utils.d.ts +230 -0
  51. package/build/esm/utils/exception.utils.js +559 -0
  52. package/build/esm/utils/exception.utils.js.map +1 -0
  53. package/build/esm/utils/fs.utils.d.ts +164 -0
  54. package/build/esm/utils/fs.utils.js +580 -0
  55. package/build/esm/utils/fs.utils.js.map +1 -0
  56. package/build/esm/utils/http-status-codes.d.ts +266 -0
  57. package/build/esm/utils/http-status-codes.js +295 -0
  58. package/build/esm/utils/http-status-codes.js.map +1 -0
  59. package/build/esm/utils/id.utils.d.ts +36 -0
  60. package/build/esm/utils/id.utils.js +87 -0
  61. package/build/esm/utils/id.utils.js.map +1 -0
  62. package/build/esm/utils/logger.utils.d.ts +160 -0
  63. package/build/esm/utils/logger.utils.js +341 -0
  64. package/build/esm/utils/logger.utils.js.map +1 -0
  65. package/build/esm/utils/middleware.utils.d.ts +100 -0
  66. package/build/esm/utils/middleware.utils.js +325 -0
  67. package/build/esm/utils/middleware.utils.js.map +1 -0
  68. package/build/esm/utils/obj.utils.d.ts +124 -0
  69. package/build/esm/utils/obj.utils.js +519 -0
  70. package/build/esm/utils/obj.utils.js.map +1 -0
  71. package/build/esm/utils/performance.utils.d.ts +136 -0
  72. package/build/esm/utils/performance.utils.js +376 -0
  73. package/build/esm/utils/performance.utils.js.map +1 -0
  74. package/build/esm/utils/request.utils.d.ts +86 -0
  75. package/build/esm/utils/request.utils.js +223 -0
  76. package/build/esm/utils/request.utils.js.map +1 -0
  77. package/build/esm/utils/response.utils.d.ts +163 -0
  78. package/build/esm/utils/response.utils.js +284 -0
  79. package/build/esm/utils/response.utils.js.map +1 -0
  80. package/build/esm/utils/stream.utils.d.ts +88 -0
  81. package/build/esm/utils/stream.utils.js +284 -0
  82. package/build/esm/utils/stream.utils.js.map +1 -0
  83. package/build/esm/utils/string.utils.d.ts +93 -0
  84. package/build/esm/utils/string.utils.js +169 -0
  85. package/build/esm/utils/string.utils.js.map +1 -0
  86. package/build/esm/utils/type.utils.d.ts +90 -0
  87. package/build/esm/utils/type.utils.js +190 -0
  88. package/build/esm/utils/type.utils.js.map +1 -0
  89. package/build/esm/utils/url.utils.d.ts +141 -0
  90. package/build/esm/utils/url.utils.js +381 -0
  91. package/build/esm/utils/url.utils.js.map +1 -0
  92. package/build/esm/utils/validate.utils.d.ts +177 -0
  93. package/build/esm/utils/validate.utils.js +336 -0
  94. package/build/esm/utils/validate.utils.js.map +1 -0
  95. package/build/esnext/config.d.ts +122 -0
  96. package/build/esnext/config.js +133 -0
  97. package/build/esnext/config.js.map +1 -0
  98. package/build/esnext/index.d.ts +27 -0
  99. package/build/esnext/index.js +50 -0
  100. package/build/esnext/index.js.map +1 -0
  101. package/build/esnext/servers/server.builder.d.ts +508 -0
  102. package/build/esnext/servers/server.builder.js +657 -0
  103. package/build/esnext/servers/server.builder.js.map +1 -0
  104. package/build/esnext/servers/server.d.ts +256 -0
  105. package/build/esnext/servers/server.js +955 -0
  106. package/build/esnext/servers/server.js.map +1 -0
  107. package/build/esnext/types/api-response.d.ts +152 -0
  108. package/build/esnext/types/api-response.js +34 -0
  109. package/build/esnext/types/api-response.js.map +1 -0
  110. package/build/esnext/types/index.d.ts +125 -0
  111. package/build/esnext/types/index.js +25 -0
  112. package/build/esnext/types/index.js.map +1 -0
  113. package/build/esnext/types/server.d.ts +268 -0
  114. package/build/esnext/types/server.js +25 -0
  115. package/build/esnext/types/server.js.map +1 -0
  116. package/build/esnext/utils/array.utils.d.ts +168 -0
  117. package/build/esnext/utils/array.utils.js +345 -0
  118. package/build/esnext/utils/array.utils.js.map +1 -0
  119. package/build/esnext/utils/async.utils.d.ts +265 -0
  120. package/build/esnext/utils/async.utils.js +620 -0
  121. package/build/esnext/utils/async.utils.js.map +1 -0
  122. package/build/esnext/utils/cache.utils.d.ts +153 -0
  123. package/build/esnext/utils/cache.utils.js +293 -0
  124. package/build/esnext/utils/cache.utils.js.map +1 -0
  125. package/build/esnext/utils/context-store.utils.d.ts +189 -0
  126. package/build/esnext/utils/context-store.utils.js +289 -0
  127. package/build/esnext/utils/context-store.utils.js.map +1 -0
  128. package/build/esnext/utils/crypto.utils.d.ts +160 -0
  129. package/build/esnext/utils/crypto.utils.js +276 -0
  130. package/build/esnext/utils/crypto.utils.js.map +1 -0
  131. package/build/esnext/utils/date.utils.d.ts +159 -0
  132. package/build/esnext/utils/date.utils.js +384 -0
  133. package/build/esnext/utils/date.utils.js.map +1 -0
  134. package/build/esnext/utils/decorators.utils.d.ts +508 -0
  135. package/build/esnext/utils/decorators.utils.js +877 -0
  136. package/build/esnext/utils/decorators.utils.js.map +1 -0
  137. package/build/esnext/utils/dir.utils.d.ts +196 -0
  138. package/build/esnext/utils/dir.utils.js +477 -0
  139. package/build/esnext/utils/dir.utils.js.map +1 -0
  140. package/build/esnext/utils/env.utils.d.ts +377 -0
  141. package/build/esnext/utils/env.utils.js +785 -0
  142. package/build/esnext/utils/env.utils.js.map +1 -0
  143. package/build/esnext/utils/exception.utils.d.ts +230 -0
  144. package/build/esnext/utils/exception.utils.js +381 -0
  145. package/build/esnext/utils/exception.utils.js.map +1 -0
  146. package/build/esnext/utils/fs.utils.d.ts +164 -0
  147. package/build/esnext/utils/fs.utils.js +349 -0
  148. package/build/esnext/utils/fs.utils.js.map +1 -0
  149. package/build/esnext/utils/http-status-codes.d.ts +266 -0
  150. package/build/esnext/utils/http-status-codes.js +295 -0
  151. package/build/esnext/utils/http-status-codes.js.map +1 -0
  152. package/build/esnext/utils/id.utils.d.ts +36 -0
  153. package/build/esnext/utils/id.utils.js +84 -0
  154. package/build/esnext/utils/id.utils.js.map +1 -0
  155. package/build/esnext/utils/logger.utils.d.ts +160 -0
  156. package/build/esnext/utils/logger.utils.js +300 -0
  157. package/build/esnext/utils/logger.utils.js.map +1 -0
  158. package/build/esnext/utils/middleware.utils.d.ts +100 -0
  159. package/build/esnext/utils/middleware.utils.js +236 -0
  160. package/build/esnext/utils/middleware.utils.js.map +1 -0
  161. package/build/esnext/utils/obj.utils.d.ts +124 -0
  162. package/build/esnext/utils/obj.utils.js +413 -0
  163. package/build/esnext/utils/obj.utils.js.map +1 -0
  164. package/build/esnext/utils/performance.utils.d.ts +136 -0
  165. package/build/esnext/utils/performance.utils.js +271 -0
  166. package/build/esnext/utils/performance.utils.js.map +1 -0
  167. package/build/esnext/utils/request.utils.d.ts +86 -0
  168. package/build/esnext/utils/request.utils.js +186 -0
  169. package/build/esnext/utils/request.utils.js.map +1 -0
  170. package/build/esnext/utils/response.utils.d.ts +163 -0
  171. package/build/esnext/utils/response.utils.js +247 -0
  172. package/build/esnext/utils/response.utils.js.map +1 -0
  173. package/build/esnext/utils/stream.utils.d.ts +88 -0
  174. package/build/esnext/utils/stream.utils.js +210 -0
  175. package/build/esnext/utils/stream.utils.js.map +1 -0
  176. package/build/esnext/utils/string.utils.d.ts +93 -0
  177. package/build/esnext/utils/string.utils.js +165 -0
  178. package/build/esnext/utils/string.utils.js.map +1 -0
  179. package/build/esnext/utils/type.utils.d.ts +90 -0
  180. package/build/esnext/utils/type.utils.js +187 -0
  181. package/build/esnext/utils/type.utils.js.map +1 -0
  182. package/build/esnext/utils/url.utils.d.ts +141 -0
  183. package/build/esnext/utils/url.utils.js +304 -0
  184. package/build/esnext/utils/url.utils.js.map +1 -0
  185. package/build/esnext/utils/validate.utils.d.ts +177 -0
  186. package/build/esnext/utils/validate.utils.js +320 -0
  187. package/build/esnext/utils/validate.utils.js.map +1 -0
  188. package/build/src/config.d.ts +122 -0
  189. package/build/src/config.js +138 -0
  190. package/build/src/config.js.map +1 -0
  191. package/build/src/index.d.ts +27 -0
  192. package/build/src/index.js +69 -0
  193. package/build/src/index.js.map +1 -0
  194. package/build/src/servers/server.builder.d.ts +508 -0
  195. package/build/src/servers/server.builder.js +661 -0
  196. package/build/src/servers/server.builder.js.map +1 -0
  197. package/build/src/servers/server.d.ts +256 -0
  198. package/build/src/servers/server.js +962 -0
  199. package/build/src/servers/server.js.map +1 -0
  200. package/build/src/types/api-response.d.ts +152 -0
  201. package/build/src/types/api-response.js +37 -0
  202. package/build/src/types/api-response.js.map +1 -0
  203. package/build/src/types/index.d.ts +125 -0
  204. package/build/src/types/index.js +26 -0
  205. package/build/src/types/index.js.map +1 -0
  206. package/build/src/types/server.d.ts +268 -0
  207. package/build/src/types/server.js +26 -0
  208. package/build/src/types/server.js.map +1 -0
  209. package/build/src/utils/array.utils.d.ts +168 -0
  210. package/build/src/utils/array.utils.js +364 -0
  211. package/build/src/utils/array.utils.js.map +1 -0
  212. package/build/src/utils/async.utils.d.ts +265 -0
  213. package/build/src/utils/async.utils.js +641 -0
  214. package/build/src/utils/async.utils.js.map +1 -0
  215. package/build/src/utils/cache.utils.d.ts +153 -0
  216. package/build/src/utils/cache.utils.js +297 -0
  217. package/build/src/utils/cache.utils.js.map +1 -0
  218. package/build/src/utils/context-store.utils.d.ts +189 -0
  219. package/build/src/utils/context-store.utils.js +296 -0
  220. package/build/src/utils/context-store.utils.js.map +1 -0
  221. package/build/src/utils/crypto.utils.d.ts +160 -0
  222. package/build/src/utils/crypto.utils.js +293 -0
  223. package/build/src/utils/crypto.utils.js.map +1 -0
  224. package/build/src/utils/date.utils.d.ts +159 -0
  225. package/build/src/utils/date.utils.js +396 -0
  226. package/build/src/utils/date.utils.js.map +1 -0
  227. package/build/src/utils/decorators.utils.d.ts +508 -0
  228. package/build/src/utils/decorators.utils.js +896 -0
  229. package/build/src/utils/decorators.utils.js.map +1 -0
  230. package/build/src/utils/dir.utils.d.ts +196 -0
  231. package/build/src/utils/dir.utils.js +501 -0
  232. package/build/src/utils/dir.utils.js.map +1 -0
  233. package/build/src/utils/env.utils.d.ts +377 -0
  234. package/build/src/utils/env.utils.js +789 -0
  235. package/build/src/utils/env.utils.js.map +1 -0
  236. package/build/src/utils/exception.utils.d.ts +230 -0
  237. package/build/src/utils/exception.utils.js +407 -0
  238. package/build/src/utils/exception.utils.js.map +1 -0
  239. package/build/src/utils/fs.utils.d.ts +164 -0
  240. package/build/src/utils/fs.utils.js +372 -0
  241. package/build/src/utils/fs.utils.js.map +1 -0
  242. package/build/src/utils/http-status-codes.d.ts +266 -0
  243. package/build/src/utils/http-status-codes.js +298 -0
  244. package/build/src/utils/http-status-codes.js.map +1 -0
  245. package/build/src/utils/id.utils.d.ts +36 -0
  246. package/build/src/utils/id.utils.js +91 -0
  247. package/build/src/utils/id.utils.js.map +1 -0
  248. package/build/src/utils/logger.utils.d.ts +160 -0
  249. package/build/src/utils/logger.utils.js +346 -0
  250. package/build/src/utils/logger.utils.js.map +1 -0
  251. package/build/src/utils/middleware.utils.d.ts +100 -0
  252. package/build/src/utils/middleware.utils.js +244 -0
  253. package/build/src/utils/middleware.utils.js.map +1 -0
  254. package/build/src/utils/obj.utils.d.ts +124 -0
  255. package/build/src/utils/obj.utils.js +429 -0
  256. package/build/src/utils/obj.utils.js.map +1 -0
  257. package/build/src/utils/performance.utils.d.ts +136 -0
  258. package/build/src/utils/performance.utils.js +278 -0
  259. package/build/src/utils/performance.utils.js.map +1 -0
  260. package/build/src/utils/request.utils.d.ts +86 -0
  261. package/build/src/utils/request.utils.js +195 -0
  262. package/build/src/utils/request.utils.js.map +1 -0
  263. package/build/src/utils/response.utils.d.ts +163 -0
  264. package/build/src/utils/response.utils.js +260 -0
  265. package/build/src/utils/response.utils.js.map +1 -0
  266. package/build/src/utils/stream.utils.d.ts +88 -0
  267. package/build/src/utils/stream.utils.js +218 -0
  268. package/build/src/utils/stream.utils.js.map +1 -0
  269. package/build/src/utils/string.utils.d.ts +93 -0
  270. package/build/src/utils/string.utils.js +179 -0
  271. package/build/src/utils/string.utils.js.map +1 -0
  272. package/build/src/utils/type.utils.d.ts +90 -0
  273. package/build/src/utils/type.utils.js +196 -0
  274. package/build/src/utils/type.utils.js.map +1 -0
  275. package/build/src/utils/url.utils.d.ts +141 -0
  276. package/build/src/utils/url.utils.js +317 -0
  277. package/build/src/utils/url.utils.js.map +1 -0
  278. package/build/src/utils/validate.utils.d.ts +177 -0
  279. package/build/src/utils/validate.utils.js +345 -0
  280. package/build/src/utils/validate.utils.js.map +1 -0
  281. package/package.json +66 -12
  282. package/build/index.cjs +0 -6888
  283. package/build/index.d.ts +0 -5242
  284. package/build/index.mjs +0 -6615
@@ -0,0 +1,284 @@
1
+ /*
2
+ * The MIT License
3
+ *
4
+ * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/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
+ var __extends = (this && this.__extends) || (function () {
25
+ var extendStatics = function (d, b) {
26
+ extendStatics = Object.setPrototypeOf ||
27
+ ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
28
+ function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
29
+ return extendStatics(d, b);
30
+ };
31
+ return function (d, b) {
32
+ if (typeof b !== "function" && b !== null)
33
+ throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
34
+ extendStatics(d, b);
35
+ function __() { this.constructor = d; }
36
+ d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
37
+ };
38
+ })();
39
+ import { getRequestId } from './context-store.utils';
40
+ import { Env } from './env.utils';
41
+ import { uuid } from './id.utils';
42
+ /**
43
+ * Standard HTTP response wrapper for successful responses.
44
+ * Implements the `ApiResponse<T>` interface and sets default values.
45
+ *
46
+ * @typeParam T - The shape of the data returned in the response.
47
+ */
48
+ var SuccessResponse = /** @class */ (function () {
49
+ /**
50
+ * Constructs a new success response.
51
+ *
52
+ * @param {string} message - Optional message to override the default.
53
+ * @param {T} [data] - Optional data payload.
54
+ */
55
+ function SuccessResponse(message, data) {
56
+ var _a;
57
+ /** Message describing the result of the operation. */
58
+ this.message = 'Success';
59
+ /** Whether the response is an error. Always false in success responses. */
60
+ this.error = false;
61
+ /** The payload returned from the API. */
62
+ this.data = null;
63
+ /** Timestamp when the response was generated, in ISO format. */
64
+ this.timestamp = new Date().toISOString();
65
+ /** Unique identifier for this response, useful for request tracing. */
66
+ this.requestId = (_a = getRequestId()) !== null && _a !== void 0 ? _a : uuid();
67
+ if (message)
68
+ this.message = message;
69
+ this.data = data !== null && data !== void 0 ? data : null;
70
+ }
71
+ return SuccessResponse;
72
+ }());
73
+ export { SuccessResponse };
74
+ /**
75
+ * Wrapper for error responses that extends the native `Error` object.
76
+ * Implements `ApiResponse` but omits the `data` field (which should not be present in errors).
77
+ */
78
+ var ErrorResponse = /** @class */ (function (_super) {
79
+ __extends(ErrorResponse, _super);
80
+ /**
81
+ * Constructs a new error response.
82
+ *
83
+ * @param {string} message - The error message to display or log.
84
+ * @param {number} [status=500] - Optional HTTP status code (defaults to 500).
85
+ */
86
+ function ErrorResponse(message, status) {
87
+ if (status === void 0) { status = 500; }
88
+ var _a;
89
+ var _this = _super.call(this, message) || this;
90
+ /** Indicates that this is an error. Always true. */
91
+ _this.error = true;
92
+ /** Timestamp when the error occurred, in ISO format. */
93
+ _this.timestamp = new Date().toISOString();
94
+ /** Unique identifier for this error instance. */
95
+ _this.requestId = (_a = getRequestId()) !== null && _a !== void 0 ? _a : uuid();
96
+ _this.status = status;
97
+ _this.name = _this.constructor.name;
98
+ Error.captureStackTrace(_this, _this.constructor);
99
+ return _this;
100
+ }
101
+ return ErrorResponse;
102
+ }(Error));
103
+ export { ErrorResponse };
104
+ /**
105
+ * Response with paginated data extending the standard success response.
106
+ * Useful for APIs that return large collections of data.
107
+ *
108
+ * @typeParam T - The shape of each item in the paginated collection.
109
+ */
110
+ var PaginatedResponse = /** @class */ (function (_super) {
111
+ __extends(PaginatedResponse, _super);
112
+ /**
113
+ * Constructs a new paginated response.
114
+ *
115
+ * @param {T[]} items - The current page of items.
116
+ * @param {Object} pagination - Pagination information.
117
+ * @param {number} pagination.total - Total number of items across all pages.
118
+ * @param {number} pagination.page - Current page number (1-based).
119
+ * @param {number} pagination.pageSize - Number of items per page.
120
+ * @param {string} [message="Success"] - Optional custom message.
121
+ */
122
+ function PaginatedResponse(items, pagination, message) {
123
+ if (message === void 0) { message = 'Success'; }
124
+ var _this = _super.call(this, message, items) || this;
125
+ _this.total = pagination.total;
126
+ _this.page = pagination.page;
127
+ _this.pageSize = pagination.pageSize;
128
+ _this.totalPages = Math.ceil(_this.total / _this.pageSize);
129
+ _this.hasNext = _this.page < _this.totalPages;
130
+ _this.hasPrevious = _this.page > 1;
131
+ return _this;
132
+ }
133
+ return PaginatedResponse;
134
+ }(SuccessResponse));
135
+ export { PaginatedResponse };
136
+ /**
137
+ * Specialized response for operations that don't return data (HTTP 204).
138
+ */
139
+ var NoContentResponse = /** @class */ (function (_super) {
140
+ __extends(NoContentResponse, _super);
141
+ /**
142
+ * Constructs a new no-content response.
143
+ *
144
+ * @param {string} [message="Operation completed successfully"] - Optional custom message.
145
+ */
146
+ function NoContentResponse(message) {
147
+ if (message === void 0) { message = 'Operation completed successfully'; }
148
+ return _super.call(this, message, null) || this;
149
+ }
150
+ return NoContentResponse;
151
+ }(SuccessResponse));
152
+ export { NoContentResponse };
153
+ /**
154
+ * Specialized response for redirects.
155
+ */
156
+ var RedirectResponse = /** @class */ (function () {
157
+ /**
158
+ * Constructs a new redirect response.
159
+ *
160
+ * @param {string} url - The URL to redirect to.
161
+ * @param {number} [statusCode=302] - HTTP status code for the redirect.
162
+ */
163
+ function RedirectResponse(url, statusCode) {
164
+ if (statusCode === void 0) { statusCode = 302; }
165
+ var _a;
166
+ /** Whether the response is a redirect */
167
+ this.isRedirect = true;
168
+ /** Unique identifier for this response */
169
+ this.requestId = (_a = getRequestId()) !== null && _a !== void 0 ? _a : uuid();
170
+ this.redirectUrl = url;
171
+ this.statusCode = statusCode;
172
+ }
173
+ return RedirectResponse;
174
+ }());
175
+ export { RedirectResponse };
176
+ /**
177
+ * Creates a standard success response.
178
+ *
179
+ * @typeParam T - The shape of the data returned in the response.
180
+ * @param {T} data - The data to include in the response.
181
+ * @param {string} [message="Success"] - Optional custom message.
182
+ * @returns {SuccessResponse<T>} A properly formatted success response.
183
+ */
184
+ export function createSuccessResponse(data, message) {
185
+ if (message === void 0) { message = 'Success'; }
186
+ return new SuccessResponse(message, data);
187
+ }
188
+ /**
189
+ * Creates a standard error response.
190
+ *
191
+ * @param {string} message - The error message.
192
+ * @param {number} [statusCode=500] - HTTP status code for the error.
193
+ * @returns {ErrorResponse} A properly formatted error response.
194
+ */
195
+ export function createErrorResponse(message, statusCode) {
196
+ if (statusCode === void 0) { statusCode = 500; }
197
+ return new ErrorResponse(message, statusCode);
198
+ }
199
+ /**
200
+ * Creates a final error response with request information.
201
+ *
202
+ * @param req - The original request object.
203
+ * @param status - The HTTP status code.
204
+ * @param message - The error message.
205
+ * @param error - The original error object (optional).
206
+ * @param options - Additional options for the response (optional).
207
+ * @returns A properly formatted final error response.
208
+ */
209
+ export function createFinalErrorResponse(req, status, message, error, options) {
210
+ var isDev = Env.isDev();
211
+ var includeDetails = (options === null || options === void 0 ? void 0 : options.includeDetails) && isDev;
212
+ var response = {
213
+ error: true,
214
+ message: message,
215
+ timestamp: new Date().toISOString(),
216
+ requestId: (error === null || error === void 0 ? void 0 : error.requestId) || (req === null || req === void 0 ? void 0 : req.id) || uuid(),
217
+ status: status,
218
+ path: req.originalUrl || req.url
219
+ };
220
+ // Include stack trace in development mode if requested
221
+ if (includeDetails && (error === null || error === void 0 ? void 0 : error.stack)) {
222
+ response.stack = error.stack.split('\n').map(function (line) { return line.trim(); });
223
+ }
224
+ return response;
225
+ }
226
+ /**
227
+ * Creates a paginated response from array data.
228
+ *
229
+ * @typeParam T - The shape of each item in the collection.
230
+ * @param {T[]} allItems - The complete array of items to paginate.
231
+ * @param {number} page - The requested page (1-based).
232
+ * @param {number} pageSize - The number of items per page.
233
+ * @param {string} [message="Success"] - Optional custom message.
234
+ * @returns {PaginatedResponse<T>} A properly formatted paginated response.
235
+ */
236
+ export function createPaginatedResponse(allItems, page, pageSize, message) {
237
+ if (message === void 0) { message = 'Success'; }
238
+ // Ensure valid pagination parameters
239
+ var validPage = Math.max(1, page);
240
+ var validPageSize = Math.max(1, pageSize);
241
+ // Calculate slice indices
242
+ var startIndex = (validPage - 1) * validPageSize;
243
+ var endIndex = startIndex + validPageSize;
244
+ // Get current page items
245
+ var pageItems = allItems.slice(startIndex, endIndex);
246
+ return new PaginatedResponse(pageItems, {
247
+ total: allItems.length,
248
+ page: validPage,
249
+ pageSize: validPageSize
250
+ }, message);
251
+ }
252
+ /**
253
+ * Adapter to convert API responses to Express.js response format.
254
+ *
255
+ * @param {any} res - Express response object.
256
+ * @param {SuccessResponse<any> | ErrorResponse | RedirectResponse} apiResponse - API response instance.
257
+ */
258
+ export function sendResponse(res, apiResponse) {
259
+ // Handle redirects
260
+ if ('redirectUrl' in apiResponse) {
261
+ res.redirect(apiResponse.statusCode, apiResponse.redirectUrl);
262
+ return;
263
+ }
264
+ // Handle errors
265
+ if ('error' in apiResponse && apiResponse.error) {
266
+ var errorResponse = apiResponse;
267
+ res.status(errorResponse.status).json({
268
+ error: true,
269
+ message: errorResponse.message,
270
+ timestamp: errorResponse.timestamp,
271
+ requestId: errorResponse.requestId
272
+ });
273
+ return;
274
+ }
275
+ // Handle success responses
276
+ var successResponse = apiResponse;
277
+ // No content (204) shouldn't return a body
278
+ if (successResponse instanceof NoContentResponse) {
279
+ res.status(204).end();
280
+ return;
281
+ }
282
+ res.status(200).json(successResponse);
283
+ }
284
+ //# sourceMappingURL=response.utils.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"response.utils.js","sourceRoot":"","sources":["../../../src/utils/response.utils.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;GAsBG;;;;;;;;;;;;;;;;AAIH,OAAO,EAAE,YAAY,EAAE,MAAM,uBAAuB,CAAC;AACrD,OAAO,EAAE,GAAG,EAAE,MAAM,aAAa,CAAC;AAClC,OAAO,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAElC;;;;;GAKG;AACH;IAgBE;;;;;OAKG;IACH,yBAAY,OAAe,EAAE,IAAQ;;QArBrC,sDAAsD;QACtD,YAAO,GAAW,SAAS,CAAC;QAE5B,2EAA2E;QAC3E,UAAK,GAAY,KAAK,CAAC;QAEvB,yCAAyC;QACzC,SAAI,GAAa,IAAI,CAAC;QAEtB,gEAAgE;QAChE,cAAS,GAAW,IAAI,IAAI,EAAE,CAAC,WAAW,EAAE,CAAC;QAE7C,u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,eAAe,CAAC,CAAC;AACxC,CAAC","sourcesContent":["/*\n * The MIT License\n *\n * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license\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 { Request } from 'express';\nimport { ApiErrorResponse, ApiResponse } from '../types/api-response';\nimport { getRequestId } from './context-store.utils';\nimport { Env } from './env.utils';\nimport { uuid } from './id.utils';\n\n/**\n * Standard HTTP response wrapper for successful responses.\n * Implements the `ApiResponse<T>` interface and sets default values.\n *\n * @typeParam T - The shape of the data returned in the response.\n */\nexport class SuccessResponse<T> implements ApiResponse<T> {\n /** Message describing the result of the operation. */\n message: string = 'Success';\n\n /** Whether the response is an error. Always false in success responses. */\n error: boolean = false;\n\n /** The payload returned from the API. */\n data: T | null = null;\n\n /** Timestamp when the response was generated, in ISO format. */\n timestamp: string = new Date().toISOString();\n\n /** Unique identifier for this response, useful for request tracing. */\n requestId: string = getRequestId() ?? uuid();\n\n /**\n * Constructs a new success response.\n *\n * @param {string} message - Optional message to override the default.\n * @param {T} [data] - Optional data payload.\n */\n constructor(message: string, data?: T) {\n if (message) this.message = message;\n this.data = data ?? null;\n }\n}\n\n/**\n * Wrapper for error responses that extends the native `Error` object.\n * Implements `ApiResponse` but omits the `data` field (which should not be present in errors).\n */\nexport class ErrorResponse extends Error implements Omit<ApiResponse<never>, 'data'> {\n /** HTTP status code associated with the error (e.g., 404, 500). */\n status: number;\n\n /** Indicates that this is an error. Always true. */\n error: boolean = true;\n\n /** Timestamp when the error occurred, in ISO format. */\n timestamp: string = new Date().toISOString();\n\n /** Unique identifier for this error instance. */\n requestId: string = getRequestId() ?? uuid();\n\n /**\n * Constructs a new error response.\n *\n * @param {string} message - The error message to display or log.\n * @param {number} [status=500] - Optional HTTP status code (defaults to 500).\n */\n constructor(message: string, status: number = 500) {\n super(message);\n this.status = status;\n this.name = this.constructor.name;\n Error.captureStackTrace(this, this.constructor);\n }\n}\n\n/**\n * Response with paginated data extending the standard success response.\n * Useful for APIs that return large collections of data.\n *\n * @typeParam T - The shape of each item in the paginated collection.\n */\nexport class PaginatedResponse<T> extends SuccessResponse<T[]> {\n /** Total number of items across all pages */\n total: number;\n\n /** Current page number (1-based) */\n page: number;\n\n /** Number of items per page */\n pageSize: number;\n\n /** Total number of pages */\n totalPages: number;\n\n /** Whether there's a next page available */\n hasNext: boolean;\n\n /** Whether there's a previous page available */\n hasPrevious: boolean;\n\n /**\n * Constructs a new paginated response.\n *\n * @param {T[]} items - The current page of items.\n * @param {Object} pagination - Pagination information.\n * @param {number} pagination.total - Total number of items across all pages.\n * @param {number} pagination.page - Current page number (1-based).\n * @param {number} pagination.pageSize - Number of items per page.\n * @param {string} [message=\"Success\"] - Optional custom message.\n */\n constructor(items: T[], pagination: { total: number; page: number; pageSize: number }, message: string = 'Success') {\n super(message, items);\n\n this.total = pagination.total;\n this.page = pagination.page;\n this.pageSize = pagination.pageSize;\n\n this.totalPages = Math.ceil(this.total / this.pageSize);\n this.hasNext = this.page < this.totalPages;\n this.hasPrevious = this.page > 1;\n }\n}\n\n/**\n * Specialized response for operations that don't return data (HTTP 204).\n */\nexport class NoContentResponse extends SuccessResponse<null> {\n /**\n * Constructs a new no-content response.\n *\n * @param {string} [message=\"Operation completed successfully\"] - Optional custom message.\n */\n constructor(message: string = 'Operation completed successfully') {\n super(message, null);\n }\n}\n\n/**\n * Specialized response for redirects.\n */\nexport class RedirectResponse {\n /** The URL to redirect to */\n redirectUrl: string;\n\n /** HTTP status code for the redirect (301, 302, 303, 307, 308) */\n statusCode: number;\n\n /** Whether the response is a redirect */\n isRedirect: boolean = true;\n\n /** Unique identifier for this response */\n requestId: string = getRequestId() ?? uuid();\n\n /**\n * Constructs a new redirect response.\n *\n * @param {string} url - The URL to redirect to.\n * @param {number} [statusCode=302] - HTTP status code for the redirect.\n */\n constructor(url: string, statusCode: number = 302) {\n this.redirectUrl = url;\n this.statusCode = statusCode;\n }\n}\n\n/**\n * Creates a standard success response.\n *\n * @typeParam T - The shape of the data returned in the response.\n * @param {T} data - The data to include in the response.\n * @param {string} [message=\"Success\"] - Optional custom message.\n * @returns {SuccessResponse<T>} A properly formatted success response.\n */\nexport function createSuccessResponse<T>(data: T, message: string = 'Success'): SuccessResponse<T> {\n return new SuccessResponse<T>(message, data);\n}\n\n/**\n * Creates a standard error response.\n *\n * @param {string} message - The error message.\n * @param {number} [statusCode=500] - HTTP status code for the error.\n * @returns {ErrorResponse} A properly formatted error response.\n */\nexport function createErrorResponse(message: string, statusCode: number = 500): ErrorResponse {\n return new ErrorResponse(message, statusCode);\n}\n\n/**\n * Creates a final error response with request information.\n *\n * @param req - The original request object.\n * @param status - The HTTP status code.\n * @param message - The error message.\n * @param error - The original error object (optional).\n * @param options - Additional options for the response (optional).\n * @returns A properly formatted final error response.\n */\nexport function createFinalErrorResponse(\n req: Request,\n status: number,\n message: string,\n error?: any,\n options?: { includeDetails?: boolean }\n) {\n const isDev = Env.isDev();\n const includeDetails = options?.includeDetails && isDev;\n\n const response: ApiErrorResponse = {\n error: true,\n message,\n timestamp: new Date().toISOString(),\n requestId: error?.requestId || req?.id || uuid(),\n status,\n path: req.originalUrl || req.url\n };\n\n // Include stack trace in development mode if requested\n if (includeDetails && error?.stack) {\n response.stack = error.stack.split('\\n').map((line: string) => line.trim());\n }\n\n return response;\n}\n\n/**\n * Creates a paginated response from array data.\n *\n * @typeParam T - The shape of each item in the collection.\n * @param {T[]} allItems - The complete array of items to paginate.\n * @param {number} page - The requested page (1-based).\n * @param {number} pageSize - The number of items per page.\n * @param {string} [message=\"Success\"] - Optional custom message.\n * @returns {PaginatedResponse<T>} A properly formatted paginated response.\n */\nexport function createPaginatedResponse<T>(\n allItems: T[],\n page: number,\n pageSize: number,\n message: string = 'Success'\n): PaginatedResponse<T> {\n // Ensure valid pagination parameters\n const validPage = Math.max(1, page);\n const validPageSize = Math.max(1, pageSize);\n\n // Calculate slice indices\n const startIndex = (validPage - 1) * validPageSize;\n const endIndex = startIndex + validPageSize;\n\n // Get current page items\n const pageItems = allItems.slice(startIndex, endIndex);\n\n return new PaginatedResponse<T>(\n pageItems,\n {\n total: allItems.length,\n page: validPage,\n pageSize: validPageSize\n },\n message\n );\n}\n\n/**\n * Adapter to convert API responses to Express.js response format.\n *\n * @param {any} res - Express response object.\n * @param {SuccessResponse<any> | ErrorResponse | RedirectResponse} apiResponse - API response instance.\n */\nexport function sendResponse(res: any, apiResponse: SuccessResponse<any> | ErrorResponse | RedirectResponse): void {\n // Handle redirects\n if ('redirectUrl' in apiResponse) {\n res.redirect(apiResponse.statusCode, apiResponse.redirectUrl);\n return;\n }\n\n // Handle errors\n if ('error' in apiResponse && apiResponse.error) {\n const errorResponse = apiResponse as ErrorResponse;\n res.status(errorResponse.status).json({\n error: true,\n message: errorResponse.message,\n timestamp: errorResponse.timestamp,\n requestId: errorResponse.requestId\n });\n return;\n }\n\n // Handle success responses\n const successResponse = apiResponse as SuccessResponse<any>;\n\n // No content (204) shouldn't return a body\n if (successResponse instanceof NoContentResponse) {\n res.status(204).end();\n return;\n }\n\n res.status(200).json(successResponse);\n}\n"]}
@@ -0,0 +1,88 @@
1
+ import { Readable, Transform } from 'stream';
2
+ import { BufferEncoding } from './crypto.utils';
3
+ /**
4
+ * Convert a buffer or string to a readable stream.
5
+ *
6
+ * @param data - Buffer or string to convert
7
+ * @returns Readable stream containing the data
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * const stream = bufferToStream(Buffer.from('Hello world'));
12
+ * // or
13
+ * const stream = bufferToStream('Hello world');
14
+ * ```
15
+ */
16
+ export declare function bufferToStream(data: Buffer | string): Readable;
17
+ /**
18
+ * Convert a readable stream to a buffer.
19
+ *
20
+ * @param stream - Readable stream to convert
21
+ * @returns Promise resolving to a buffer containing all stream data
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * const buffer = await streamToBuffer(fs.createReadStream('file.txt'));
26
+ * console.log(buffer.toString()); // Contents of file.txt
27
+ * ```
28
+ */
29
+ export declare function streamToBuffer(stream: Readable): Promise<Buffer>;
30
+ /**
31
+ * Convert a readable stream to a string.
32
+ *
33
+ * @param stream - Readable stream to convert
34
+ * @param encoding - Character encoding (default: 'utf8')
35
+ * @returns Promise resolving to a string containing all stream data
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * const content = await streamToString(fs.createReadStream('file.txt'));
40
+ * console.log(content); // Contents of file.txt as string
41
+ * ```
42
+ */
43
+ export declare function streamToString(stream: Readable, encoding?: BufferEncoding): Promise<string>;
44
+ /**
45
+ * Create a transform stream that limits the rate of data flow.
46
+ *
47
+ * @param bytesPerSecond - Maximum bytes per second
48
+ * @returns Transform stream that throttles data flow
49
+ */
50
+ export declare function createThrottleStream(bytesPerSecond: number): Transform;
51
+ /**
52
+ * Create a transform stream that batches data into chunks of specified size.
53
+ *
54
+ * @param size - Size of each batch (items for object mode, bytes for binary mode)
55
+ * @param options - Stream options
56
+ * @returns Transform stream that batches data
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * // Batch lines from a file into arrays of 100 lines each
61
+ * createReadStream('large-file.txt')
62
+ * .pipe(createLineStream())
63
+ * .pipe(createBatchStream(100))
64
+ * .on('data', batch => console.log(`Processing batch of ${batch.length} lines`));
65
+ * ```
66
+ */
67
+ export declare function createBatchStream(size: number, options?: {
68
+ objectMode?: boolean;
69
+ }): Transform;
70
+ /**
71
+ * Create a transform stream that splits text data by newlines.
72
+ *
73
+ * @param options - Options for the line stream
74
+ * @returns Transform stream that emits lines
75
+ *
76
+ * @example
77
+ * ```typescript
78
+ * // Process a file line by line
79
+ * createReadStream('file.txt')
80
+ * .pipe(createLineStream())
81
+ * .on('data', line => console.log(`Line: ${line}`));
82
+ * ```
83
+ */
84
+ export declare function createLineStream(options?: {
85
+ encoding?: BufferEncoding;
86
+ includeNewlines?: boolean;
87
+ }): Transform;
88
+ //# sourceMappingURL=stream.utils.d.ts.map