@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.
- package/README.md +3 -2
- package/build/index.cjs +6912 -0
- package/build/index.d.ts +5284 -0
- package/build/index.mjs +6635 -0
- package/package.json +13 -292
- package/build/cjs/config.d.ts +0 -121
- package/build/cjs/config.js +0 -137
- package/build/cjs/index.d.ts +0 -26
- package/build/cjs/index.js +0 -68
- package/build/cjs/servers/server.builder.d.ts +0 -507
- package/build/cjs/servers/server.builder.js +0 -658
- package/build/cjs/servers/server.d.ts +0 -255
- package/build/cjs/servers/server.js +0 -970
- package/build/cjs/types/api-response.d.ts +0 -151
- package/build/cjs/types/api-response.js +0 -36
- package/build/cjs/types/index.d.ts +0 -124
- package/build/cjs/types/index.js +0 -25
- package/build/cjs/types/server.d.ts +0 -267
- package/build/cjs/types/server.js +0 -25
- package/build/cjs/utils/array.utils.d.ts +0 -167
- package/build/cjs/utils/array.utils.js +0 -363
- package/build/cjs/utils/async.utils.d.ts +0 -264
- package/build/cjs/utils/async.utils.js +0 -640
- package/build/cjs/utils/cache.utils.d.ts +0 -152
- package/build/cjs/utils/cache.utils.js +0 -298
- package/build/cjs/utils/context-store.utils.d.ts +0 -188
- package/build/cjs/utils/context-store.utils.js +0 -297
- package/build/cjs/utils/crypto.utils.d.ts +0 -159
- package/build/cjs/utils/crypto.utils.js +0 -295
- package/build/cjs/utils/date.utils.d.ts +0 -158
- package/build/cjs/utils/date.utils.js +0 -395
- package/build/cjs/utils/decorators.utils.d.ts +0 -511
- package/build/cjs/utils/decorators.utils.js +0 -1035
- package/build/cjs/utils/dir.utils.d.ts +0 -195
- package/build/cjs/utils/dir.utils.js +0 -500
- package/build/cjs/utils/env.utils.d.ts +0 -376
- package/build/cjs/utils/env.utils.js +0 -786
- package/build/cjs/utils/exception.utils.d.ts +0 -229
- package/build/cjs/utils/exception.utils.js +0 -408
- package/build/cjs/utils/fs.utils.d.ts +0 -163
- package/build/cjs/utils/fs.utils.js +0 -371
- package/build/cjs/utils/http-status-codes.d.ts +0 -265
- package/build/cjs/utils/http-status-codes.js +0 -297
- package/build/cjs/utils/id.utils.d.ts +0 -35
- package/build/cjs/utils/id.utils.js +0 -90
- package/build/cjs/utils/logger.utils.d.ts +0 -159
- package/build/cjs/utils/logger.utils.js +0 -350
- package/build/cjs/utils/middleware.utils.d.ts +0 -99
- package/build/cjs/utils/middleware.utils.js +0 -243
- package/build/cjs/utils/obj.utils.d.ts +0 -123
- package/build/cjs/utils/obj.utils.js +0 -428
- package/build/cjs/utils/performance.utils.d.ts +0 -135
- package/build/cjs/utils/performance.utils.js +0 -280
- package/build/cjs/utils/request.utils.d.ts +0 -85
- package/build/cjs/utils/request.utils.js +0 -199
- package/build/cjs/utils/response.utils.d.ts +0 -162
- package/build/cjs/utils/response.utils.js +0 -274
- package/build/cjs/utils/stream.utils.d.ts +0 -87
- package/build/cjs/utils/stream.utils.js +0 -217
- package/build/cjs/utils/string.utils.d.ts +0 -92
- package/build/cjs/utils/string.utils.js +0 -178
- package/build/cjs/utils/type.utils.d.ts +0 -89
- package/build/cjs/utils/type.utils.js +0 -195
- package/build/cjs/utils/url.utils.d.ts +0 -140
- package/build/cjs/utils/url.utils.js +0 -316
- package/build/cjs/utils/validate.utils.d.ts +0 -176
- package/build/cjs/utils/validate.utils.js +0 -344
- package/build/esm/config.d.ts +0 -121
- package/build/esm/config.js +0 -132
- package/build/esm/index.d.ts +0 -26
- package/build/esm/index.js +0 -49
- package/build/esm/servers/server.builder.d.ts +0 -507
- package/build/esm/servers/server.builder.js +0 -654
- package/build/esm/servers/server.d.ts +0 -255
- package/build/esm/servers/server.js +0 -963
- package/build/esm/types/api-response.d.ts +0 -151
- package/build/esm/types/api-response.js +0 -33
- package/build/esm/types/index.d.ts +0 -124
- package/build/esm/types/index.js +0 -24
- package/build/esm/types/server.d.ts +0 -267
- package/build/esm/types/server.js +0 -24
- package/build/esm/utils/array.utils.d.ts +0 -167
- package/build/esm/utils/array.utils.js +0 -344
- package/build/esm/utils/async.utils.d.ts +0 -264
- package/build/esm/utils/async.utils.js +0 -619
- package/build/esm/utils/cache.utils.d.ts +0 -152
- package/build/esm/utils/cache.utils.js +0 -294
- package/build/esm/utils/context-store.utils.d.ts +0 -188
- package/build/esm/utils/context-store.utils.js +0 -290
- package/build/esm/utils/crypto.utils.d.ts +0 -159
- package/build/esm/utils/crypto.utils.js +0 -278
- package/build/esm/utils/date.utils.d.ts +0 -158
- package/build/esm/utils/date.utils.js +0 -383
- package/build/esm/utils/decorators.utils.d.ts +0 -511
- package/build/esm/utils/decorators.utils.js +0 -1013
- package/build/esm/utils/dir.utils.d.ts +0 -195
- package/build/esm/utils/dir.utils.js +0 -476
- package/build/esm/utils/env.utils.d.ts +0 -376
- package/build/esm/utils/env.utils.js +0 -782
- package/build/esm/utils/exception.utils.d.ts +0 -229
- package/build/esm/utils/exception.utils.js +0 -382
- package/build/esm/utils/fs.utils.d.ts +0 -163
- package/build/esm/utils/fs.utils.js +0 -348
- package/build/esm/utils/http-status-codes.d.ts +0 -265
- package/build/esm/utils/http-status-codes.js +0 -294
- package/build/esm/utils/id.utils.d.ts +0 -35
- package/build/esm/utils/id.utils.js +0 -83
- package/build/esm/utils/logger.utils.d.ts +0 -159
- package/build/esm/utils/logger.utils.js +0 -304
- package/build/esm/utils/middleware.utils.d.ts +0 -99
- package/build/esm/utils/middleware.utils.js +0 -235
- package/build/esm/utils/obj.utils.d.ts +0 -123
- package/build/esm/utils/obj.utils.js +0 -412
- package/build/esm/utils/performance.utils.d.ts +0 -135
- package/build/esm/utils/performance.utils.js +0 -273
- package/build/esm/utils/request.utils.d.ts +0 -85
- package/build/esm/utils/request.utils.js +0 -190
- package/build/esm/utils/response.utils.d.ts +0 -162
- package/build/esm/utils/response.utils.js +0 -261
- package/build/esm/utils/stream.utils.d.ts +0 -87
- package/build/esm/utils/stream.utils.js +0 -209
- package/build/esm/utils/string.utils.d.ts +0 -92
- package/build/esm/utils/string.utils.js +0 -164
- package/build/esm/utils/type.utils.d.ts +0 -89
- package/build/esm/utils/type.utils.js +0 -186
- package/build/esm/utils/url.utils.d.ts +0 -140
- package/build/esm/utils/url.utils.js +0 -303
- package/build/esm/utils/validate.utils.d.ts +0 -176
- 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,261 +0,0 @@
|
|
|
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
|
-
import { getRequestId } from './context-store.utils';
|
|
25
|
-
import { Env } from './env.utils';
|
|
26
|
-
import { uuid } from './id.utils';
|
|
27
|
-
/**
|
|
28
|
-
* Standard HTTP response wrapper for successful responses.
|
|
29
|
-
* Implements the `ApiResponse<T>` interface and sets default values.
|
|
30
|
-
*
|
|
31
|
-
* @typeParam T - The shape of the data returned in the response.
|
|
32
|
-
*/
|
|
33
|
-
export class SuccessResponse {
|
|
34
|
-
/** Message describing the result of the operation. */
|
|
35
|
-
message = 'Success';
|
|
36
|
-
/** Whether the response is an error. Always false in success responses. */
|
|
37
|
-
error = false;
|
|
38
|
-
/** The payload returned from the API. */
|
|
39
|
-
data = null;
|
|
40
|
-
/** Timestamp when the response was generated, in ISO format. */
|
|
41
|
-
timestamp = new Date().toISOString();
|
|
42
|
-
/** Unique identifier for this response, useful for request tracing. */
|
|
43
|
-
requestId = getRequestId() ?? uuid();
|
|
44
|
-
/**
|
|
45
|
-
* Constructs a new success response.
|
|
46
|
-
*
|
|
47
|
-
* @param {string} message - Optional message to override the default.
|
|
48
|
-
* @param {T} [data] - Optional data payload.
|
|
49
|
-
*/
|
|
50
|
-
constructor(message, data) {
|
|
51
|
-
if (message)
|
|
52
|
-
this.message = message;
|
|
53
|
-
this.data = data ?? null;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
/**
|
|
57
|
-
* Wrapper for error responses that extends the native `Error` object.
|
|
58
|
-
* Implements `ApiResponse` but omits the `data` field (which should not be present in errors).
|
|
59
|
-
*/
|
|
60
|
-
export class ErrorResponse extends Error {
|
|
61
|
-
/** HTTP status code associated with the error (e.g., 404, 500). */
|
|
62
|
-
status;
|
|
63
|
-
/** Indicates that this is an error. Always true. */
|
|
64
|
-
error = true;
|
|
65
|
-
/** Timestamp when the error occurred, in ISO format. */
|
|
66
|
-
timestamp = new Date().toISOString();
|
|
67
|
-
/** Unique identifier for this error instance. */
|
|
68
|
-
requestId = getRequestId() ?? uuid();
|
|
69
|
-
/**
|
|
70
|
-
* Constructs a new error response.
|
|
71
|
-
*
|
|
72
|
-
* @param {string} message - The error message to display or log.
|
|
73
|
-
* @param {number} [status=500] - Optional HTTP status code (defaults to 500).
|
|
74
|
-
*/
|
|
75
|
-
constructor(message, status = 500) {
|
|
76
|
-
super(message);
|
|
77
|
-
this.status = status;
|
|
78
|
-
this.name = this.constructor.name;
|
|
79
|
-
Error.captureStackTrace(this, this.constructor);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Response with paginated data extending the standard success response.
|
|
84
|
-
* Useful for APIs that return large collections of data.
|
|
85
|
-
*
|
|
86
|
-
* @typeParam T - The shape of each item in the paginated collection.
|
|
87
|
-
*/
|
|
88
|
-
export class PaginatedResponse extends SuccessResponse {
|
|
89
|
-
/** Total number of items across all pages */
|
|
90
|
-
total;
|
|
91
|
-
/** Current page number (1-based) */
|
|
92
|
-
page;
|
|
93
|
-
/** Number of items per page */
|
|
94
|
-
pageSize;
|
|
95
|
-
/** Total number of pages */
|
|
96
|
-
totalPages;
|
|
97
|
-
/** Whether there's a next page available */
|
|
98
|
-
hasNext;
|
|
99
|
-
/** Whether there's a previous page available */
|
|
100
|
-
hasPrevious;
|
|
101
|
-
/**
|
|
102
|
-
* Constructs a new paginated response.
|
|
103
|
-
*
|
|
104
|
-
* @param {T[]} items - The current page of items.
|
|
105
|
-
* @param {Object} pagination - Pagination information.
|
|
106
|
-
* @param {number} pagination.total - Total number of items across all pages.
|
|
107
|
-
* @param {number} pagination.page - Current page number (1-based).
|
|
108
|
-
* @param {number} pagination.pageSize - Number of items per page.
|
|
109
|
-
* @param {string} [message="Success"] - Optional custom message.
|
|
110
|
-
*/
|
|
111
|
-
constructor(items, pagination, message = 'Success') {
|
|
112
|
-
super(message, items);
|
|
113
|
-
this.total = pagination.total;
|
|
114
|
-
this.page = pagination.page;
|
|
115
|
-
this.pageSize = pagination.pageSize;
|
|
116
|
-
this.totalPages = Math.ceil(this.total / this.pageSize);
|
|
117
|
-
this.hasNext = this.page < this.totalPages;
|
|
118
|
-
this.hasPrevious = this.page > 1;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
/**
|
|
122
|
-
* Specialized response for operations that don't return data (HTTP 204).
|
|
123
|
-
*/
|
|
124
|
-
export class NoContentResponse extends SuccessResponse {
|
|
125
|
-
/**
|
|
126
|
-
* Constructs a new no-content response.
|
|
127
|
-
*
|
|
128
|
-
* @param {string} [message="Operation completed successfully"] - Optional custom message.
|
|
129
|
-
*/
|
|
130
|
-
constructor(message = 'Operation completed successfully') {
|
|
131
|
-
super(message, null);
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Specialized response for redirects.
|
|
136
|
-
*/
|
|
137
|
-
export class RedirectResponse {
|
|
138
|
-
/** The URL to redirect to */
|
|
139
|
-
redirectUrl;
|
|
140
|
-
/** HTTP status code for the redirect (301, 302, 303, 307, 308) */
|
|
141
|
-
statusCode;
|
|
142
|
-
/** Whether the response is a redirect */
|
|
143
|
-
isRedirect = true;
|
|
144
|
-
/** Unique identifier for this response */
|
|
145
|
-
requestId = getRequestId() ?? uuid();
|
|
146
|
-
/**
|
|
147
|
-
* Constructs a new redirect response.
|
|
148
|
-
*
|
|
149
|
-
* @param {string} url - The URL to redirect to.
|
|
150
|
-
* @param {number} [statusCode=302] - HTTP status code for the redirect.
|
|
151
|
-
*/
|
|
152
|
-
constructor(url, statusCode = 302) {
|
|
153
|
-
this.redirectUrl = url;
|
|
154
|
-
this.statusCode = statusCode;
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
/**
|
|
158
|
-
* Creates a standard success response.
|
|
159
|
-
*
|
|
160
|
-
* @typeParam T - The shape of the data returned in the response.
|
|
161
|
-
* @param {T} data - The data to include in the response.
|
|
162
|
-
* @param {string} [message="Success"] - Optional custom message.
|
|
163
|
-
* @returns {SuccessResponse<T>} A properly formatted success response.
|
|
164
|
-
*/
|
|
165
|
-
export function createSuccessResponse(data, message = 'Success') {
|
|
166
|
-
return new SuccessResponse(message, data);
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Creates a standard error response.
|
|
170
|
-
*
|
|
171
|
-
* @param {string} message - The error message.
|
|
172
|
-
* @param {number} [statusCode=500] - HTTP status code for the error.
|
|
173
|
-
* @returns {ErrorResponse} A properly formatted error response.
|
|
174
|
-
*/
|
|
175
|
-
export function createErrorResponse(message, statusCode = 500) {
|
|
176
|
-
return new ErrorResponse(message, statusCode);
|
|
177
|
-
}
|
|
178
|
-
/**
|
|
179
|
-
* Creates a final error response with request information.
|
|
180
|
-
*
|
|
181
|
-
* @param req - The original request object.
|
|
182
|
-
* @param status - The HTTP status code.
|
|
183
|
-
* @param message - The error message.
|
|
184
|
-
* @param error - The original error object (optional).
|
|
185
|
-
* @param options - Additional options for the response (optional).
|
|
186
|
-
* @returns A properly formatted final error response.
|
|
187
|
-
*/
|
|
188
|
-
export function createFinalErrorResponse(req, status, message, error, options) {
|
|
189
|
-
const isDev = Env.isDev();
|
|
190
|
-
const includeDetails = options?.includeDetails && isDev;
|
|
191
|
-
const response = {
|
|
192
|
-
error: true,
|
|
193
|
-
message,
|
|
194
|
-
timestamp: new Date().toISOString(),
|
|
195
|
-
requestId: error?.requestId || req?.id || uuid(),
|
|
196
|
-
status,
|
|
197
|
-
path: req.originalUrl || req.url
|
|
198
|
-
};
|
|
199
|
-
// Include stack trace in development mode if requested
|
|
200
|
-
if (includeDetails && error?.stack) {
|
|
201
|
-
response.stack = error.stack.split('\n').map((line) => line.trim());
|
|
202
|
-
}
|
|
203
|
-
return response;
|
|
204
|
-
}
|
|
205
|
-
/**
|
|
206
|
-
* Creates a paginated response from array data.
|
|
207
|
-
*
|
|
208
|
-
* @typeParam T - The shape of each item in the collection.
|
|
209
|
-
* @param {T[]} allItems - The complete array of items to paginate.
|
|
210
|
-
* @param {number} page - The requested page (1-based).
|
|
211
|
-
* @param {number} pageSize - The number of items per page.
|
|
212
|
-
* @param {string} [message="Success"] - Optional custom message.
|
|
213
|
-
* @returns {PaginatedResponse<T>} A properly formatted paginated response.
|
|
214
|
-
*/
|
|
215
|
-
export function createPaginatedResponse(allItems, page, pageSize, message = 'Success') {
|
|
216
|
-
// Ensure valid pagination parameters
|
|
217
|
-
const validPage = Math.max(1, page);
|
|
218
|
-
const validPageSize = Math.max(1, pageSize);
|
|
219
|
-
// Calculate slice indices
|
|
220
|
-
const startIndex = (validPage - 1) * validPageSize;
|
|
221
|
-
const endIndex = startIndex + validPageSize;
|
|
222
|
-
// Get current page items
|
|
223
|
-
const pageItems = allItems.slice(startIndex, endIndex);
|
|
224
|
-
return new PaginatedResponse(pageItems, {
|
|
225
|
-
total: allItems.length,
|
|
226
|
-
page: validPage,
|
|
227
|
-
pageSize: validPageSize
|
|
228
|
-
}, message);
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* Adapter to convert API responses to Express.js response format.
|
|
232
|
-
*
|
|
233
|
-
* @param {any} res - Express response object.
|
|
234
|
-
* @param {SuccessResponse<any> | ErrorResponse | RedirectResponse} apiResponse - API response instance.
|
|
235
|
-
*/
|
|
236
|
-
export function sendResponse(res, apiResponse) {
|
|
237
|
-
// Handle redirects
|
|
238
|
-
if ('redirectUrl' in apiResponse) {
|
|
239
|
-
res.redirect(apiResponse.statusCode, apiResponse.redirectUrl);
|
|
240
|
-
return;
|
|
241
|
-
}
|
|
242
|
-
// Handle errors
|
|
243
|
-
if ('error' in apiResponse && apiResponse.error) {
|
|
244
|
-
const errorResponse = apiResponse;
|
|
245
|
-
res.status(errorResponse.status).json({
|
|
246
|
-
error: true,
|
|
247
|
-
message: errorResponse.message,
|
|
248
|
-
timestamp: errorResponse.timestamp,
|
|
249
|
-
requestId: errorResponse.requestId
|
|
250
|
-
});
|
|
251
|
-
return;
|
|
252
|
-
}
|
|
253
|
-
// Handle success responses
|
|
254
|
-
const successResponse = apiResponse;
|
|
255
|
-
// No content (204) shouldn't return a body
|
|
256
|
-
if (successResponse instanceof NoContentResponse) {
|
|
257
|
-
res.status(204).end();
|
|
258
|
-
return;
|
|
259
|
-
}
|
|
260
|
-
res.status(200).json(successResponse);
|
|
261
|
-
}
|
|
@@ -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;
|