@catbee/utils 0.0.8-rc.3 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (129) hide show
  1. package/README.md +3 -2
  2. package/build/index.cjs +6912 -0
  3. package/build/index.d.ts +5284 -0
  4. package/build/index.mjs +6635 -0
  5. package/package.json +13 -292
  6. package/build/cjs/config.d.ts +0 -121
  7. package/build/cjs/config.js +0 -137
  8. package/build/cjs/index.d.ts +0 -26
  9. package/build/cjs/index.js +0 -68
  10. package/build/cjs/servers/server.builder.d.ts +0 -507
  11. package/build/cjs/servers/server.builder.js +0 -658
  12. package/build/cjs/servers/server.d.ts +0 -255
  13. package/build/cjs/servers/server.js +0 -970
  14. package/build/cjs/types/api-response.d.ts +0 -151
  15. package/build/cjs/types/api-response.js +0 -36
  16. package/build/cjs/types/index.d.ts +0 -124
  17. package/build/cjs/types/index.js +0 -25
  18. package/build/cjs/types/server.d.ts +0 -267
  19. package/build/cjs/types/server.js +0 -25
  20. package/build/cjs/utils/array.utils.d.ts +0 -167
  21. package/build/cjs/utils/array.utils.js +0 -363
  22. package/build/cjs/utils/async.utils.d.ts +0 -264
  23. package/build/cjs/utils/async.utils.js +0 -640
  24. package/build/cjs/utils/cache.utils.d.ts +0 -152
  25. package/build/cjs/utils/cache.utils.js +0 -298
  26. package/build/cjs/utils/context-store.utils.d.ts +0 -188
  27. package/build/cjs/utils/context-store.utils.js +0 -297
  28. package/build/cjs/utils/crypto.utils.d.ts +0 -159
  29. package/build/cjs/utils/crypto.utils.js +0 -295
  30. package/build/cjs/utils/date.utils.d.ts +0 -158
  31. package/build/cjs/utils/date.utils.js +0 -395
  32. package/build/cjs/utils/decorators.utils.d.ts +0 -511
  33. package/build/cjs/utils/decorators.utils.js +0 -1035
  34. package/build/cjs/utils/dir.utils.d.ts +0 -195
  35. package/build/cjs/utils/dir.utils.js +0 -500
  36. package/build/cjs/utils/env.utils.d.ts +0 -376
  37. package/build/cjs/utils/env.utils.js +0 -786
  38. package/build/cjs/utils/exception.utils.d.ts +0 -229
  39. package/build/cjs/utils/exception.utils.js +0 -408
  40. package/build/cjs/utils/fs.utils.d.ts +0 -163
  41. package/build/cjs/utils/fs.utils.js +0 -371
  42. package/build/cjs/utils/http-status-codes.d.ts +0 -265
  43. package/build/cjs/utils/http-status-codes.js +0 -297
  44. package/build/cjs/utils/id.utils.d.ts +0 -35
  45. package/build/cjs/utils/id.utils.js +0 -90
  46. package/build/cjs/utils/logger.utils.d.ts +0 -159
  47. package/build/cjs/utils/logger.utils.js +0 -350
  48. package/build/cjs/utils/middleware.utils.d.ts +0 -99
  49. package/build/cjs/utils/middleware.utils.js +0 -243
  50. package/build/cjs/utils/obj.utils.d.ts +0 -123
  51. package/build/cjs/utils/obj.utils.js +0 -428
  52. package/build/cjs/utils/performance.utils.d.ts +0 -135
  53. package/build/cjs/utils/performance.utils.js +0 -280
  54. package/build/cjs/utils/request.utils.d.ts +0 -85
  55. package/build/cjs/utils/request.utils.js +0 -199
  56. package/build/cjs/utils/response.utils.d.ts +0 -162
  57. package/build/cjs/utils/response.utils.js +0 -274
  58. package/build/cjs/utils/stream.utils.d.ts +0 -87
  59. package/build/cjs/utils/stream.utils.js +0 -217
  60. package/build/cjs/utils/string.utils.d.ts +0 -92
  61. package/build/cjs/utils/string.utils.js +0 -178
  62. package/build/cjs/utils/type.utils.d.ts +0 -89
  63. package/build/cjs/utils/type.utils.js +0 -195
  64. package/build/cjs/utils/url.utils.d.ts +0 -140
  65. package/build/cjs/utils/url.utils.js +0 -316
  66. package/build/cjs/utils/validate.utils.d.ts +0 -176
  67. package/build/cjs/utils/validate.utils.js +0 -344
  68. package/build/esm/config.d.ts +0 -121
  69. package/build/esm/config.js +0 -132
  70. package/build/esm/index.d.ts +0 -26
  71. package/build/esm/index.js +0 -49
  72. package/build/esm/servers/server.builder.d.ts +0 -507
  73. package/build/esm/servers/server.builder.js +0 -654
  74. package/build/esm/servers/server.d.ts +0 -255
  75. package/build/esm/servers/server.js +0 -963
  76. package/build/esm/types/api-response.d.ts +0 -151
  77. package/build/esm/types/api-response.js +0 -33
  78. package/build/esm/types/index.d.ts +0 -124
  79. package/build/esm/types/index.js +0 -24
  80. package/build/esm/types/server.d.ts +0 -267
  81. package/build/esm/types/server.js +0 -24
  82. package/build/esm/utils/array.utils.d.ts +0 -167
  83. package/build/esm/utils/array.utils.js +0 -344
  84. package/build/esm/utils/async.utils.d.ts +0 -264
  85. package/build/esm/utils/async.utils.js +0 -619
  86. package/build/esm/utils/cache.utils.d.ts +0 -152
  87. package/build/esm/utils/cache.utils.js +0 -294
  88. package/build/esm/utils/context-store.utils.d.ts +0 -188
  89. package/build/esm/utils/context-store.utils.js +0 -290
  90. package/build/esm/utils/crypto.utils.d.ts +0 -159
  91. package/build/esm/utils/crypto.utils.js +0 -278
  92. package/build/esm/utils/date.utils.d.ts +0 -158
  93. package/build/esm/utils/date.utils.js +0 -383
  94. package/build/esm/utils/decorators.utils.d.ts +0 -511
  95. package/build/esm/utils/decorators.utils.js +0 -1013
  96. package/build/esm/utils/dir.utils.d.ts +0 -195
  97. package/build/esm/utils/dir.utils.js +0 -476
  98. package/build/esm/utils/env.utils.d.ts +0 -376
  99. package/build/esm/utils/env.utils.js +0 -782
  100. package/build/esm/utils/exception.utils.d.ts +0 -229
  101. package/build/esm/utils/exception.utils.js +0 -382
  102. package/build/esm/utils/fs.utils.d.ts +0 -163
  103. package/build/esm/utils/fs.utils.js +0 -348
  104. package/build/esm/utils/http-status-codes.d.ts +0 -265
  105. package/build/esm/utils/http-status-codes.js +0 -294
  106. package/build/esm/utils/id.utils.d.ts +0 -35
  107. package/build/esm/utils/id.utils.js +0 -83
  108. package/build/esm/utils/logger.utils.d.ts +0 -159
  109. package/build/esm/utils/logger.utils.js +0 -304
  110. package/build/esm/utils/middleware.utils.d.ts +0 -99
  111. package/build/esm/utils/middleware.utils.js +0 -235
  112. package/build/esm/utils/obj.utils.d.ts +0 -123
  113. package/build/esm/utils/obj.utils.js +0 -412
  114. package/build/esm/utils/performance.utils.d.ts +0 -135
  115. package/build/esm/utils/performance.utils.js +0 -273
  116. package/build/esm/utils/request.utils.d.ts +0 -85
  117. package/build/esm/utils/request.utils.js +0 -190
  118. package/build/esm/utils/response.utils.d.ts +0 -162
  119. package/build/esm/utils/response.utils.js +0 -261
  120. package/build/esm/utils/stream.utils.d.ts +0 -87
  121. package/build/esm/utils/stream.utils.js +0 -209
  122. package/build/esm/utils/string.utils.d.ts +0 -92
  123. package/build/esm/utils/string.utils.js +0 -164
  124. package/build/esm/utils/type.utils.d.ts +0 -89
  125. package/build/esm/utils/type.utils.js +0 -186
  126. package/build/esm/utils/url.utils.d.ts +0 -140
  127. package/build/esm/utils/url.utils.js +0 -303
  128. package/build/esm/utils/validate.utils.d.ts +0 -176
  129. package/build/esm/utils/validate.utils.js +0 -319
@@ -1,162 +0,0 @@
1
- import { Request } from 'express';
2
- import { ApiErrorResponse, ApiResponse } from '../types/api-response';
3
- /**
4
- * Standard HTTP response wrapper for successful responses.
5
- * Implements the `ApiResponse<T>` interface and sets default values.
6
- *
7
- * @typeParam T - The shape of the data returned in the response.
8
- */
9
- export declare class SuccessResponse<T> implements ApiResponse<T> {
10
- /** Message describing the result of the operation. */
11
- message: string;
12
- /** Whether the response is an error. Always false in success responses. */
13
- error: boolean;
14
- /** The payload returned from the API. */
15
- data: T | null;
16
- /** Timestamp when the response was generated, in ISO format. */
17
- timestamp: string;
18
- /** Unique identifier for this response, useful for request tracing. */
19
- requestId: string;
20
- /**
21
- * Constructs a new success response.
22
- *
23
- * @param {string} message - Optional message to override the default.
24
- * @param {T} [data] - Optional data payload.
25
- */
26
- constructor(message: string, data?: T);
27
- }
28
- /**
29
- * Wrapper for error responses that extends the native `Error` object.
30
- * Implements `ApiResponse` but omits the `data` field (which should not be present in errors).
31
- */
32
- export declare class ErrorResponse extends Error implements Omit<ApiResponse<never>, 'data'> {
33
- /** HTTP status code associated with the error (e.g., 404, 500). */
34
- status: number;
35
- /** Indicates that this is an error. Always true. */
36
- error: boolean;
37
- /** Timestamp when the error occurred, in ISO format. */
38
- timestamp: string;
39
- /** Unique identifier for this error instance. */
40
- requestId: string;
41
- /**
42
- * Constructs a new error response.
43
- *
44
- * @param {string} message - The error message to display or log.
45
- * @param {number} [status=500] - Optional HTTP status code (defaults to 500).
46
- */
47
- constructor(message: string, status?: number);
48
- }
49
- /**
50
- * Response with paginated data extending the standard success response.
51
- * Useful for APIs that return large collections of data.
52
- *
53
- * @typeParam T - The shape of each item in the paginated collection.
54
- */
55
- export declare class PaginatedResponse<T> extends SuccessResponse<T[]> {
56
- /** Total number of items across all pages */
57
- total: number;
58
- /** Current page number (1-based) */
59
- page: number;
60
- /** Number of items per page */
61
- pageSize: number;
62
- /** Total number of pages */
63
- totalPages: number;
64
- /** Whether there's a next page available */
65
- hasNext: boolean;
66
- /** Whether there's a previous page available */
67
- hasPrevious: boolean;
68
- /**
69
- * Constructs a new paginated response.
70
- *
71
- * @param {T[]} items - The current page of items.
72
- * @param {Object} pagination - Pagination information.
73
- * @param {number} pagination.total - Total number of items across all pages.
74
- * @param {number} pagination.page - Current page number (1-based).
75
- * @param {number} pagination.pageSize - Number of items per page.
76
- * @param {string} [message="Success"] - Optional custom message.
77
- */
78
- constructor(items: T[], pagination: {
79
- total: number;
80
- page: number;
81
- pageSize: number;
82
- }, message?: string);
83
- }
84
- /**
85
- * Specialized response for operations that don't return data (HTTP 204).
86
- */
87
- export declare class NoContentResponse extends SuccessResponse<null> {
88
- /**
89
- * Constructs a new no-content response.
90
- *
91
- * @param {string} [message="Operation completed successfully"] - Optional custom message.
92
- */
93
- constructor(message?: string);
94
- }
95
- /**
96
- * Specialized response for redirects.
97
- */
98
- export declare class RedirectResponse {
99
- /** The URL to redirect to */
100
- redirectUrl: string;
101
- /** HTTP status code for the redirect (301, 302, 303, 307, 308) */
102
- statusCode: number;
103
- /** Whether the response is a redirect */
104
- isRedirect: boolean;
105
- /** Unique identifier for this response */
106
- requestId: string;
107
- /**
108
- * Constructs a new redirect response.
109
- *
110
- * @param {string} url - The URL to redirect to.
111
- * @param {number} [statusCode=302] - HTTP status code for the redirect.
112
- */
113
- constructor(url: string, statusCode?: number);
114
- }
115
- /**
116
- * Creates a standard success response.
117
- *
118
- * @typeParam T - The shape of the data returned in the response.
119
- * @param {T} data - The data to include in the response.
120
- * @param {string} [message="Success"] - Optional custom message.
121
- * @returns {SuccessResponse<T>} A properly formatted success response.
122
- */
123
- export declare function createSuccessResponse<T>(data: T, message?: string): SuccessResponse<T>;
124
- /**
125
- * Creates a standard error response.
126
- *
127
- * @param {string} message - The error message.
128
- * @param {number} [statusCode=500] - HTTP status code for the error.
129
- * @returns {ErrorResponse} A properly formatted error response.
130
- */
131
- export declare function createErrorResponse(message: string, statusCode?: number): ErrorResponse;
132
- /**
133
- * Creates a final error response with request information.
134
- *
135
- * @param req - The original request object.
136
- * @param status - The HTTP status code.
137
- * @param message - The error message.
138
- * @param error - The original error object (optional).
139
- * @param options - Additional options for the response (optional).
140
- * @returns A properly formatted final error response.
141
- */
142
- export declare function createFinalErrorResponse(req: Request, status: number, message: string, error?: any, options?: {
143
- includeDetails?: boolean;
144
- }): ApiErrorResponse;
145
- /**
146
- * Creates a paginated response from array data.
147
- *
148
- * @typeParam T - The shape of each item in the collection.
149
- * @param {T[]} allItems - The complete array of items to paginate.
150
- * @param {number} page - The requested page (1-based).
151
- * @param {number} pageSize - The number of items per page.
152
- * @param {string} [message="Success"] - Optional custom message.
153
- * @returns {PaginatedResponse<T>} A properly formatted paginated response.
154
- */
155
- export declare function createPaginatedResponse<T>(allItems: T[], page: number, pageSize: number, message?: string): PaginatedResponse<T>;
156
- /**
157
- * Adapter to convert API responses to Express.js response format.
158
- *
159
- * @param {any} res - Express response object.
160
- * @param {SuccessResponse<any> | ErrorResponse | RedirectResponse} apiResponse - API response instance.
161
- */
162
- export declare function sendResponse(res: any, apiResponse: SuccessResponse<any> | ErrorResponse | RedirectResponse): void;
@@ -1,274 +0,0 @@
1
- "use strict";
2
- /*
3
- * The MIT License
4
- *
5
- * Copyright (c) 2025 Catbee Technologies. https://catbee-utils.npm.hprasath.com/license
6
- *
7
- * Permission is hereby granted, free of charge, to any person obtaining a copy
8
- * of this software and associated documentation files (the "Software"), to deal
9
- * in the Software without restriction, including without limitation the rights
10
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- * copies of the Software, and to permit persons to whom the Software is
12
- * furnished to do so, subject to the following conditions:
13
- *
14
- * The above copyright notice and this permission notice shall be included in all
15
- * copies or substantial portions of the Software.
16
- *
17
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
- * SOFTWARE.
24
- */
25
- Object.defineProperty(exports, "__esModule", { value: true });
26
- exports.RedirectResponse = exports.NoContentResponse = exports.PaginatedResponse = exports.ErrorResponse = exports.SuccessResponse = void 0;
27
- exports.createSuccessResponse = createSuccessResponse;
28
- exports.createErrorResponse = createErrorResponse;
29
- exports.createFinalErrorResponse = createFinalErrorResponse;
30
- exports.createPaginatedResponse = createPaginatedResponse;
31
- exports.sendResponse = sendResponse;
32
- const context_store_utils_1 = require("./context-store.utils");
33
- const env_utils_1 = require("./env.utils");
34
- const id_utils_1 = require("./id.utils");
35
- /**
36
- * Standard HTTP response wrapper for successful responses.
37
- * Implements the `ApiResponse<T>` interface and sets default values.
38
- *
39
- * @typeParam T - The shape of the data returned in the response.
40
- */
41
- class SuccessResponse {
42
- /** Message describing the result of the operation. */
43
- message = 'Success';
44
- /** Whether the response is an error. Always false in success responses. */
45
- error = false;
46
- /** The payload returned from the API. */
47
- data = null;
48
- /** Timestamp when the response was generated, in ISO format. */
49
- timestamp = new Date().toISOString();
50
- /** Unique identifier for this response, useful for request tracing. */
51
- requestId = (0, context_store_utils_1.getRequestId)() ?? (0, id_utils_1.uuid)();
52
- /**
53
- * Constructs a new success response.
54
- *
55
- * @param {string} message - Optional message to override the default.
56
- * @param {T} [data] - Optional data payload.
57
- */
58
- constructor(message, data) {
59
- if (message)
60
- this.message = message;
61
- this.data = data ?? null;
62
- }
63
- }
64
- exports.SuccessResponse = SuccessResponse;
65
- /**
66
- * Wrapper for error responses that extends the native `Error` object.
67
- * Implements `ApiResponse` but omits the `data` field (which should not be present in errors).
68
- */
69
- class ErrorResponse extends Error {
70
- /** HTTP status code associated with the error (e.g., 404, 500). */
71
- status;
72
- /** Indicates that this is an error. Always true. */
73
- error = true;
74
- /** Timestamp when the error occurred, in ISO format. */
75
- timestamp = new Date().toISOString();
76
- /** Unique identifier for this error instance. */
77
- requestId = (0, context_store_utils_1.getRequestId)() ?? (0, id_utils_1.uuid)();
78
- /**
79
- * Constructs a new error response.
80
- *
81
- * @param {string} message - The error message to display or log.
82
- * @param {number} [status=500] - Optional HTTP status code (defaults to 500).
83
- */
84
- constructor(message, status = 500) {
85
- super(message);
86
- this.status = status;
87
- this.name = this.constructor.name;
88
- Error.captureStackTrace(this, this.constructor);
89
- }
90
- }
91
- exports.ErrorResponse = ErrorResponse;
92
- /**
93
- * Response with paginated data extending the standard success response.
94
- * Useful for APIs that return large collections of data.
95
- *
96
- * @typeParam T - The shape of each item in the paginated collection.
97
- */
98
- class PaginatedResponse extends SuccessResponse {
99
- /** Total number of items across all pages */
100
- total;
101
- /** Current page number (1-based) */
102
- page;
103
- /** Number of items per page */
104
- pageSize;
105
- /** Total number of pages */
106
- totalPages;
107
- /** Whether there's a next page available */
108
- hasNext;
109
- /** Whether there's a previous page available */
110
- hasPrevious;
111
- /**
112
- * Constructs a new paginated response.
113
- *
114
- * @param {T[]} items - The current page of items.
115
- * @param {Object} pagination - Pagination information.
116
- * @param {number} pagination.total - Total number of items across all pages.
117
- * @param {number} pagination.page - Current page number (1-based).
118
- * @param {number} pagination.pageSize - Number of items per page.
119
- * @param {string} [message="Success"] - Optional custom message.
120
- */
121
- constructor(items, pagination, message = 'Success') {
122
- super(message, items);
123
- this.total = pagination.total;
124
- this.page = pagination.page;
125
- this.pageSize = pagination.pageSize;
126
- this.totalPages = Math.ceil(this.total / this.pageSize);
127
- this.hasNext = this.page < this.totalPages;
128
- this.hasPrevious = this.page > 1;
129
- }
130
- }
131
- exports.PaginatedResponse = PaginatedResponse;
132
- /**
133
- * Specialized response for operations that don't return data (HTTP 204).
134
- */
135
- class NoContentResponse extends SuccessResponse {
136
- /**
137
- * Constructs a new no-content response.
138
- *
139
- * @param {string} [message="Operation completed successfully"] - Optional custom message.
140
- */
141
- constructor(message = 'Operation completed successfully') {
142
- super(message, null);
143
- }
144
- }
145
- exports.NoContentResponse = NoContentResponse;
146
- /**
147
- * Specialized response for redirects.
148
- */
149
- class RedirectResponse {
150
- /** The URL to redirect to */
151
- redirectUrl;
152
- /** HTTP status code for the redirect (301, 302, 303, 307, 308) */
153
- statusCode;
154
- /** Whether the response is a redirect */
155
- isRedirect = true;
156
- /** Unique identifier for this response */
157
- requestId = (0, context_store_utils_1.getRequestId)() ?? (0, id_utils_1.uuid)();
158
- /**
159
- * Constructs a new redirect response.
160
- *
161
- * @param {string} url - The URL to redirect to.
162
- * @param {number} [statusCode=302] - HTTP status code for the redirect.
163
- */
164
- constructor(url, statusCode = 302) {
165
- this.redirectUrl = url;
166
- this.statusCode = statusCode;
167
- }
168
- }
169
- exports.RedirectResponse = RedirectResponse;
170
- /**
171
- * Creates a standard success response.
172
- *
173
- * @typeParam T - The shape of the data returned in the response.
174
- * @param {T} data - The data to include in the response.
175
- * @param {string} [message="Success"] - Optional custom message.
176
- * @returns {SuccessResponse<T>} A properly formatted success response.
177
- */
178
- function createSuccessResponse(data, message = 'Success') {
179
- return new SuccessResponse(message, data);
180
- }
181
- /**
182
- * Creates a standard error response.
183
- *
184
- * @param {string} message - The error message.
185
- * @param {number} [statusCode=500] - HTTP status code for the error.
186
- * @returns {ErrorResponse} A properly formatted error response.
187
- */
188
- function createErrorResponse(message, statusCode = 500) {
189
- return new ErrorResponse(message, statusCode);
190
- }
191
- /**
192
- * Creates a final error response with request information.
193
- *
194
- * @param req - The original request object.
195
- * @param status - The HTTP status code.
196
- * @param message - The error message.
197
- * @param error - The original error object (optional).
198
- * @param options - Additional options for the response (optional).
199
- * @returns A properly formatted final error response.
200
- */
201
- function createFinalErrorResponse(req, status, message, error, options) {
202
- const isDev = env_utils_1.Env.isDev();
203
- const includeDetails = options?.includeDetails && isDev;
204
- const response = {
205
- error: true,
206
- message,
207
- timestamp: new Date().toISOString(),
208
- requestId: error?.requestId || req?.id || (0, id_utils_1.uuid)(),
209
- status,
210
- path: req.originalUrl || req.url
211
- };
212
- // Include stack trace in development mode if requested
213
- if (includeDetails && error?.stack) {
214
- response.stack = error.stack.split('\n').map((line) => line.trim());
215
- }
216
- return response;
217
- }
218
- /**
219
- * Creates a paginated response from array data.
220
- *
221
- * @typeParam T - The shape of each item in the collection.
222
- * @param {T[]} allItems - The complete array of items to paginate.
223
- * @param {number} page - The requested page (1-based).
224
- * @param {number} pageSize - The number of items per page.
225
- * @param {string} [message="Success"] - Optional custom message.
226
- * @returns {PaginatedResponse<T>} A properly formatted paginated response.
227
- */
228
- function createPaginatedResponse(allItems, page, pageSize, message = 'Success') {
229
- // Ensure valid pagination parameters
230
- const validPage = Math.max(1, page);
231
- const validPageSize = Math.max(1, pageSize);
232
- // Calculate slice indices
233
- const startIndex = (validPage - 1) * validPageSize;
234
- const endIndex = startIndex + validPageSize;
235
- // Get current page items
236
- const pageItems = allItems.slice(startIndex, endIndex);
237
- return new PaginatedResponse(pageItems, {
238
- total: allItems.length,
239
- page: validPage,
240
- pageSize: validPageSize
241
- }, message);
242
- }
243
- /**
244
- * Adapter to convert API responses to Express.js response format.
245
- *
246
- * @param {any} res - Express response object.
247
- * @param {SuccessResponse<any> | ErrorResponse | RedirectResponse} apiResponse - API response instance.
248
- */
249
- function sendResponse(res, apiResponse) {
250
- // Handle redirects
251
- if ('redirectUrl' in apiResponse) {
252
- res.redirect(apiResponse.statusCode, apiResponse.redirectUrl);
253
- return;
254
- }
255
- // Handle errors
256
- if ('error' in apiResponse && apiResponse.error) {
257
- const errorResponse = apiResponse;
258
- res.status(errorResponse.status).json({
259
- error: true,
260
- message: errorResponse.message,
261
- timestamp: errorResponse.timestamp,
262
- requestId: errorResponse.requestId
263
- });
264
- return;
265
- }
266
- // Handle success responses
267
- const successResponse = apiResponse;
268
- // No content (204) shouldn't return a body
269
- if (successResponse instanceof NoContentResponse) {
270
- res.status(204).end();
271
- return;
272
- }
273
- res.status(200).json(successResponse);
274
- }
@@ -1,87 +0,0 @@
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;