@catbee/utils 1.1.0 → 2.0.0-next.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/LICENSE +1 -1
- package/README.md +79 -43
- package/array/index.cjs +355 -0
- package/array/index.d.ts +317 -0
- package/array/index.mjs +327 -0
- package/async/index.cjs +484 -0
- package/async/index.d.ts +299 -0
- package/async/index.mjs +463 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +179 -0
- package/cache/index.mjs +290 -0
- package/config/index.cjs +150 -0
- package/config/index.d.ts +88 -0
- package/config/index.mjs +143 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +216 -0
- package/context-store/index.mjs +261 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +187 -0
- package/crypto/index.mjs +166 -0
- package/date/index.cjs +340 -0
- package/date/index.d.ts +214 -0
- package/date/index.mjs +326 -0
- package/decorators/index.cjs +2051 -0
- package/decorators/index.d.ts +708 -0
- package/decorators/index.mjs +2010 -0
- package/dir/index.cjs +417 -0
- package/dir/index.d.ts +219 -0
- package/dir/index.mjs +390 -0
- package/env/index.cjs +745 -0
- package/env/index.d.ts +403 -0
- package/env/index.mjs +742 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +256 -0
- package/exception/index.mjs +338 -0
- package/fs/index.cjs +287 -0
- package/fs/index.d.ts +229 -0
- package/fs/index.mjs +258 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +291 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +61 -0
- package/id/index.mjs +56 -0
- package/index.cjs +218 -0
- package/index.d.ts +51 -0
- package/index.mjs +51 -0
- package/logger/index.cjs +334 -0
- package/logger/index.d.ts +213 -0
- package/logger/index.mjs +313 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +127 -0
- package/middleware/index.mjs +170 -0
- package/obj/index.cjs +305 -0
- package/obj/index.d.ts +160 -0
- package/obj/index.mjs +289 -0
- package/package.json +172 -20
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +162 -0
- package/performance/index.mjs +225 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +265 -0
- package/request/index.mjs +194 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +342 -0
- package/response/index.mjs +222 -0
- package/server/index.cjs +1631 -0
- package/server/index.d.ts +809 -0
- package/server/index.mjs +1622 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +114 -0
- package/stream/index.mjs +144 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +126 -0
- package/string/index.mjs +95 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +131 -0
- package/type/index.mjs +119 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +798 -0
- package/types/index.mjs +32 -0
- package/url/index.cjs +199 -0
- package/url/index.d.ts +166 -0
- package/url/index.mjs +187 -0
- package/validation/index.cjs +259 -0
- package/validation/index.d.ts +209 -0
- package/validation/index.mjs +231 -0
- package/build/index.cjs +0 -7694
- package/build/index.d.ts +0 -5792
- package/build/index.mjs +0 -7386
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/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
|
+
|
|
25
|
+
'use strict';
|
|
26
|
+
|
|
27
|
+
var contextStore = require('@catbee/utils/context-store');
|
|
28
|
+
var env = require('@catbee/utils/env');
|
|
29
|
+
var id = require('@catbee/utils/id');
|
|
30
|
+
|
|
31
|
+
var __defProp = Object.defineProperty;
|
|
32
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
33
|
+
var SuccessResponse = class {
|
|
34
|
+
static {
|
|
35
|
+
__name(this, "SuccessResponse");
|
|
36
|
+
}
|
|
37
|
+
/** Message describing the result of the operation. */
|
|
38
|
+
message = "Success";
|
|
39
|
+
/** Whether the response is an error. Always false in success responses. */
|
|
40
|
+
error = false;
|
|
41
|
+
/** The payload returned from the API. */
|
|
42
|
+
data = null;
|
|
43
|
+
/** Timestamp when the response was generated, in ISO format. */
|
|
44
|
+
timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
45
|
+
/** Unique identifier for this response, useful for request tracing. */
|
|
46
|
+
requestId = contextStore.getRequestId() ?? id.uuid();
|
|
47
|
+
/**
|
|
48
|
+
* Constructs a new success response.
|
|
49
|
+
*
|
|
50
|
+
* @param {string} message - Optional message to override the default.
|
|
51
|
+
* @param {T} [data] - Optional data payload.
|
|
52
|
+
*/
|
|
53
|
+
constructor(message, data) {
|
|
54
|
+
if (message) this.message = message;
|
|
55
|
+
this.data = data ?? null;
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
var ErrorResponse = class extends Error {
|
|
59
|
+
static {
|
|
60
|
+
__name(this, "ErrorResponse");
|
|
61
|
+
}
|
|
62
|
+
/** HTTP status code associated with the error (e.g., 404, 500). */
|
|
63
|
+
status;
|
|
64
|
+
/** Indicates that this is an error. Always true. */
|
|
65
|
+
error = true;
|
|
66
|
+
/** Timestamp when the error occurred, in ISO format. */
|
|
67
|
+
timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
68
|
+
/** Unique identifier for this error instance. */
|
|
69
|
+
requestId = contextStore.getRequestId() ?? id.uuid();
|
|
70
|
+
/**
|
|
71
|
+
* Constructs a new error response.
|
|
72
|
+
*
|
|
73
|
+
* @param {string} message - The error message to display or log.
|
|
74
|
+
* @param {number} [status=500] - Optional HTTP status code (defaults to 500).
|
|
75
|
+
*/
|
|
76
|
+
constructor(message, status = 500) {
|
|
77
|
+
super(message);
|
|
78
|
+
this.status = status;
|
|
79
|
+
this.name = this.constructor.name;
|
|
80
|
+
Error.captureStackTrace(this, this.constructor);
|
|
81
|
+
}
|
|
82
|
+
};
|
|
83
|
+
var PaginatedResponse = class extends SuccessResponse {
|
|
84
|
+
static {
|
|
85
|
+
__name(this, "PaginatedResponse");
|
|
86
|
+
}
|
|
87
|
+
/** Total number of items across all pages */
|
|
88
|
+
total;
|
|
89
|
+
/** Current page number (1-based) */
|
|
90
|
+
page;
|
|
91
|
+
/** Number of items per page */
|
|
92
|
+
pageSize;
|
|
93
|
+
/** Total number of pages */
|
|
94
|
+
totalPages;
|
|
95
|
+
/** Whether there's a next page available */
|
|
96
|
+
hasNext;
|
|
97
|
+
/** Whether there's a previous page available */
|
|
98
|
+
hasPrevious;
|
|
99
|
+
/**
|
|
100
|
+
* Constructs a new paginated response.
|
|
101
|
+
*
|
|
102
|
+
* @param {T[]} items - The current page of items.
|
|
103
|
+
* @param {Object} pagination - Pagination information.
|
|
104
|
+
* @param {number} pagination.total - Total number of items across all pages.
|
|
105
|
+
* @param {number} pagination.page - Current page number (1-based).
|
|
106
|
+
* @param {number} pagination.pageSize - Number of items per page.
|
|
107
|
+
* @param {string} [message="Success"] - Optional custom message.
|
|
108
|
+
*/
|
|
109
|
+
constructor(items, pagination, message = "Success") {
|
|
110
|
+
super(message, items);
|
|
111
|
+
this.total = pagination.total;
|
|
112
|
+
this.page = pagination.page;
|
|
113
|
+
this.pageSize = pagination.pageSize;
|
|
114
|
+
this.totalPages = Math.ceil(this.total / this.pageSize);
|
|
115
|
+
this.hasNext = this.page < this.totalPages;
|
|
116
|
+
this.hasPrevious = this.page > 1;
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
var NoContentResponse = class extends SuccessResponse {
|
|
120
|
+
static {
|
|
121
|
+
__name(this, "NoContentResponse");
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Constructs a new no-content response.
|
|
125
|
+
*
|
|
126
|
+
* @param {string} [message="Operation completed successfully"] - Optional custom message.
|
|
127
|
+
*/
|
|
128
|
+
constructor(message = "Operation completed successfully") {
|
|
129
|
+
super(message, null);
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
var RedirectResponse = class {
|
|
133
|
+
static {
|
|
134
|
+
__name(this, "RedirectResponse");
|
|
135
|
+
}
|
|
136
|
+
/** The URL to redirect to */
|
|
137
|
+
redirectUrl;
|
|
138
|
+
/** HTTP status code for the redirect (301, 302, 303, 307, 308) */
|
|
139
|
+
statusCode;
|
|
140
|
+
/** Whether the response is a redirect */
|
|
141
|
+
isRedirect = true;
|
|
142
|
+
/** Unique identifier for this response */
|
|
143
|
+
requestId = contextStore.getRequestId() ?? id.uuid();
|
|
144
|
+
/**
|
|
145
|
+
* Constructs a new redirect response.
|
|
146
|
+
*
|
|
147
|
+
* @param {string} url - The URL to redirect to.
|
|
148
|
+
* @param {number} [statusCode=302] - HTTP status code for the redirect.
|
|
149
|
+
*/
|
|
150
|
+
constructor(url, statusCode = 302) {
|
|
151
|
+
this.redirectUrl = url;
|
|
152
|
+
this.statusCode = statusCode;
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
function createSuccessResponse(data, message = "Success") {
|
|
156
|
+
return new SuccessResponse(message, data);
|
|
157
|
+
}
|
|
158
|
+
__name(createSuccessResponse, "createSuccessResponse");
|
|
159
|
+
function createErrorResponse(message, statusCode = 500) {
|
|
160
|
+
return new ErrorResponse(message, statusCode);
|
|
161
|
+
}
|
|
162
|
+
__name(createErrorResponse, "createErrorResponse");
|
|
163
|
+
function createFinalErrorResponse(req, status, message, error, options) {
|
|
164
|
+
const isDev = env.Env.isDev();
|
|
165
|
+
const includeDetails = options?.includeDetails && isDev;
|
|
166
|
+
const response = {
|
|
167
|
+
error: true,
|
|
168
|
+
message,
|
|
169
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
170
|
+
requestId: error?.requestId || req?.id || id.uuid(),
|
|
171
|
+
status,
|
|
172
|
+
path: req.originalUrl || req.url
|
|
173
|
+
};
|
|
174
|
+
if (includeDetails && error?.stack) {
|
|
175
|
+
response.stack = error.stack.split("\n").map((line) => line.trim());
|
|
176
|
+
}
|
|
177
|
+
return response;
|
|
178
|
+
}
|
|
179
|
+
__name(createFinalErrorResponse, "createFinalErrorResponse");
|
|
180
|
+
function createPaginatedResponse(allItems, page, pageSize, message = "Success") {
|
|
181
|
+
const validPage = Math.max(1, page);
|
|
182
|
+
const validPageSize = Math.max(1, pageSize);
|
|
183
|
+
const startIndex = (validPage - 1) * validPageSize;
|
|
184
|
+
const endIndex = startIndex + validPageSize;
|
|
185
|
+
const pageItems = allItems.slice(startIndex, endIndex);
|
|
186
|
+
return new PaginatedResponse(pageItems, {
|
|
187
|
+
total: allItems.length,
|
|
188
|
+
page: validPage,
|
|
189
|
+
pageSize: validPageSize
|
|
190
|
+
}, message);
|
|
191
|
+
}
|
|
192
|
+
__name(createPaginatedResponse, "createPaginatedResponse");
|
|
193
|
+
function sendResponse(res, apiResponse) {
|
|
194
|
+
if ("redirectUrl" in apiResponse) {
|
|
195
|
+
res.redirect(apiResponse.statusCode, apiResponse.redirectUrl);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
if ("error" in apiResponse && apiResponse.error) {
|
|
199
|
+
const errorResponse = apiResponse;
|
|
200
|
+
res.status(errorResponse.status).json({
|
|
201
|
+
error: true,
|
|
202
|
+
message: errorResponse.message,
|
|
203
|
+
timestamp: errorResponse.timestamp,
|
|
204
|
+
requestId: errorResponse.requestId
|
|
205
|
+
});
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
const successResponse = apiResponse;
|
|
209
|
+
if (successResponse instanceof NoContentResponse) {
|
|
210
|
+
res.status(204).end();
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
res.status(200).json(successResponse);
|
|
214
|
+
}
|
|
215
|
+
__name(sendResponse, "sendResponse");
|
|
216
|
+
|
|
217
|
+
// src/types/api-response.ts
|
|
218
|
+
var SortDirection = /* @__PURE__ */ (function(SortDirection2) {
|
|
219
|
+
SortDirection2["ASC"] = "asc";
|
|
220
|
+
SortDirection2["DESC"] = "desc";
|
|
221
|
+
return SortDirection2;
|
|
222
|
+
})({});
|
|
223
|
+
|
|
224
|
+
exports.ErrorResponse = ErrorResponse;
|
|
225
|
+
exports.NoContentResponse = NoContentResponse;
|
|
226
|
+
exports.PaginatedResponse = PaginatedResponse;
|
|
227
|
+
exports.RedirectResponse = RedirectResponse;
|
|
228
|
+
exports.SortDirection = SortDirection;
|
|
229
|
+
exports.SuccessResponse = SuccessResponse;
|
|
230
|
+
exports.createErrorResponse = createErrorResponse;
|
|
231
|
+
exports.createFinalErrorResponse = createFinalErrorResponse;
|
|
232
|
+
exports.createPaginatedResponse = createPaginatedResponse;
|
|
233
|
+
exports.createSuccessResponse = createSuccessResponse;
|
|
234
|
+
exports.sendResponse = sendResponse;
|
|
@@ -0,0 +1,342 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* The MIT License
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2025 Catbee Technologies. https://catbee.in/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
|
+
|
|
25
|
+
import { Request } from 'express';
|
|
26
|
+
import { ApiResponse as ApiResponse$1, ApiErrorResponse as ApiErrorResponse$1 } from '@catbee/utils/response';
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Standard HTTP response wrapper for successful responses.
|
|
30
|
+
* Implements the `ApiResponse<T>` interface and sets default values.
|
|
31
|
+
*
|
|
32
|
+
* @typeParam T - The shape of the data returned in the response.
|
|
33
|
+
*/
|
|
34
|
+
declare class SuccessResponse<T> implements ApiResponse$1<T> {
|
|
35
|
+
/** Message describing the result of the operation. */
|
|
36
|
+
message: string;
|
|
37
|
+
/** Whether the response is an error. Always false in success responses. */
|
|
38
|
+
error: boolean;
|
|
39
|
+
/** The payload returned from the API. */
|
|
40
|
+
data: T | null;
|
|
41
|
+
/** Timestamp when the response was generated, in ISO format. */
|
|
42
|
+
timestamp: string;
|
|
43
|
+
/** Unique identifier for this response, useful for request tracing. */
|
|
44
|
+
requestId: string;
|
|
45
|
+
/**
|
|
46
|
+
* Constructs a new success response.
|
|
47
|
+
*
|
|
48
|
+
* @param {string} message - Optional message to override the default.
|
|
49
|
+
* @param {T} [data] - Optional data payload.
|
|
50
|
+
*/
|
|
51
|
+
constructor(message: string, data?: T);
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Wrapper for error responses that extends the native `Error` object.
|
|
55
|
+
* Implements `ApiResponse` but omits the `data` field (which should not be present in errors).
|
|
56
|
+
*/
|
|
57
|
+
declare class ErrorResponse extends Error implements Omit<ApiResponse$1<never>, 'data'> {
|
|
58
|
+
/** HTTP status code associated with the error (e.g., 404, 500). */
|
|
59
|
+
status: number;
|
|
60
|
+
/** Indicates that this is an error. Always true. */
|
|
61
|
+
error: boolean;
|
|
62
|
+
/** Timestamp when the error occurred, in ISO format. */
|
|
63
|
+
timestamp: string;
|
|
64
|
+
/** Unique identifier for this error instance. */
|
|
65
|
+
requestId: string;
|
|
66
|
+
/**
|
|
67
|
+
* Constructs a new error response.
|
|
68
|
+
*
|
|
69
|
+
* @param {string} message - The error message to display or log.
|
|
70
|
+
* @param {number} [status=500] - Optional HTTP status code (defaults to 500).
|
|
71
|
+
*/
|
|
72
|
+
constructor(message: string, status?: number);
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Response with paginated data extending the standard success response.
|
|
76
|
+
* Useful for APIs that return large collections of data.
|
|
77
|
+
*
|
|
78
|
+
* @typeParam T - The shape of each item in the paginated collection.
|
|
79
|
+
*/
|
|
80
|
+
declare class PaginatedResponse<T> extends SuccessResponse<T[]> {
|
|
81
|
+
/** Total number of items across all pages */
|
|
82
|
+
total: number;
|
|
83
|
+
/** Current page number (1-based) */
|
|
84
|
+
page: number;
|
|
85
|
+
/** Number of items per page */
|
|
86
|
+
pageSize: number;
|
|
87
|
+
/** Total number of pages */
|
|
88
|
+
totalPages: number;
|
|
89
|
+
/** Whether there's a next page available */
|
|
90
|
+
hasNext: boolean;
|
|
91
|
+
/** Whether there's a previous page available */
|
|
92
|
+
hasPrevious: boolean;
|
|
93
|
+
/**
|
|
94
|
+
* Constructs a new paginated response.
|
|
95
|
+
*
|
|
96
|
+
* @param {T[]} items - The current page of items.
|
|
97
|
+
* @param {Object} pagination - Pagination information.
|
|
98
|
+
* @param {number} pagination.total - Total number of items across all pages.
|
|
99
|
+
* @param {number} pagination.page - Current page number (1-based).
|
|
100
|
+
* @param {number} pagination.pageSize - Number of items per page.
|
|
101
|
+
* @param {string} [message="Success"] - Optional custom message.
|
|
102
|
+
*/
|
|
103
|
+
constructor(items: T[], pagination: {
|
|
104
|
+
total: number;
|
|
105
|
+
page: number;
|
|
106
|
+
pageSize: number;
|
|
107
|
+
}, message?: string);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Specialized response for operations that don't return data (HTTP 204).
|
|
111
|
+
*/
|
|
112
|
+
declare class NoContentResponse extends SuccessResponse<null> {
|
|
113
|
+
/**
|
|
114
|
+
* Constructs a new no-content response.
|
|
115
|
+
*
|
|
116
|
+
* @param {string} [message="Operation completed successfully"] - Optional custom message.
|
|
117
|
+
*/
|
|
118
|
+
constructor(message?: string);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Specialized response for redirects.
|
|
122
|
+
*/
|
|
123
|
+
declare class RedirectResponse {
|
|
124
|
+
/** The URL to redirect to */
|
|
125
|
+
redirectUrl: string;
|
|
126
|
+
/** HTTP status code for the redirect (301, 302, 303, 307, 308) */
|
|
127
|
+
statusCode: number;
|
|
128
|
+
/** Whether the response is a redirect */
|
|
129
|
+
isRedirect: boolean;
|
|
130
|
+
/** Unique identifier for this response */
|
|
131
|
+
requestId: string;
|
|
132
|
+
/**
|
|
133
|
+
* Constructs a new redirect response.
|
|
134
|
+
*
|
|
135
|
+
* @param {string} url - The URL to redirect to.
|
|
136
|
+
* @param {number} [statusCode=302] - HTTP status code for the redirect.
|
|
137
|
+
*/
|
|
138
|
+
constructor(url: string, statusCode?: number);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Creates a standard success response.
|
|
142
|
+
*
|
|
143
|
+
* @typeParam T - The shape of the data returned in the response.
|
|
144
|
+
* @param {T} data - The data to include in the response.
|
|
145
|
+
* @param {string} [message="Success"] - Optional custom message.
|
|
146
|
+
* @returns {SuccessResponse<T>} A properly formatted success response.
|
|
147
|
+
*/
|
|
148
|
+
declare function createSuccessResponse<T>(data: T, message?: string): SuccessResponse<T>;
|
|
149
|
+
/**
|
|
150
|
+
* Creates a standard error response.
|
|
151
|
+
*
|
|
152
|
+
* @param {string} message - The error message.
|
|
153
|
+
* @param {number} [statusCode=500] - HTTP status code for the error.
|
|
154
|
+
* @returns {ErrorResponse} A properly formatted error response.
|
|
155
|
+
*/
|
|
156
|
+
declare function createErrorResponse(message: string, statusCode?: number): ErrorResponse;
|
|
157
|
+
/**
|
|
158
|
+
* Creates a final error response with request information.
|
|
159
|
+
*
|
|
160
|
+
* @param req - The original request object.
|
|
161
|
+
* @param status - The HTTP status code.
|
|
162
|
+
* @param message - The error message.
|
|
163
|
+
* @param error - The original error object (optional).
|
|
164
|
+
* @param options - Additional options for the response (optional).
|
|
165
|
+
* @returns A properly formatted final error response.
|
|
166
|
+
*/
|
|
167
|
+
declare function createFinalErrorResponse(req: Request, status: number, message: string, error?: any, options?: {
|
|
168
|
+
includeDetails?: boolean;
|
|
169
|
+
}): ApiErrorResponse$1;
|
|
170
|
+
/**
|
|
171
|
+
* Creates a paginated response from array data.
|
|
172
|
+
*
|
|
173
|
+
* @typeParam T - The shape of each item in the collection.
|
|
174
|
+
* @param {T[]} allItems - The complete array of items to paginate.
|
|
175
|
+
* @param {number} page - The requested page (1-based).
|
|
176
|
+
* @param {number} pageSize - The number of items per page.
|
|
177
|
+
* @param {string} [message="Success"] - Optional custom message.
|
|
178
|
+
* @returns {PaginatedResponse<T>} A properly formatted paginated response.
|
|
179
|
+
*/
|
|
180
|
+
declare function createPaginatedResponse<T>(allItems: T[], page: number, pageSize: number, message?: string): PaginatedResponse<T>;
|
|
181
|
+
/**
|
|
182
|
+
* Adapter to convert API responses to Express.js response format.
|
|
183
|
+
*
|
|
184
|
+
* @param {any} res - Express response object.
|
|
185
|
+
* @param {SuccessResponse<any> | ErrorResponse | RedirectResponse} apiResponse - API response instance.
|
|
186
|
+
*/
|
|
187
|
+
declare function sendResponse(res: any, apiResponse: SuccessResponse<any> | ErrorResponse | RedirectResponse): void;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Generic API response format.
|
|
191
|
+
* Used to wrap any successful or failed response from the server.
|
|
192
|
+
*/
|
|
193
|
+
interface ApiResponse<T = any> {
|
|
194
|
+
/** Payload returned from the API. Can be any shape depending on the endpoint. */
|
|
195
|
+
data: T | null;
|
|
196
|
+
/** Indicates whether an error occurred (true = error, false = success). */
|
|
197
|
+
error: boolean;
|
|
198
|
+
/** Success message describing the result of the operation. */
|
|
199
|
+
message: string;
|
|
200
|
+
/** Unique request ID for traceability in logs (e.g., from a middleware). */
|
|
201
|
+
requestId: string;
|
|
202
|
+
/** ISO timestamp when the response was generated. */
|
|
203
|
+
timestamp: string;
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Generic pagination structure used for paged lists (e.g., /users?page=1).
|
|
207
|
+
*/
|
|
208
|
+
interface Pagination<T = any> {
|
|
209
|
+
/** List of records for the current page. */
|
|
210
|
+
content: T[];
|
|
211
|
+
/** Metadata about the pagination state. */
|
|
212
|
+
pagination: {
|
|
213
|
+
/** Total number of records across all pages. */
|
|
214
|
+
totalRecords: number;
|
|
215
|
+
/** Total number of pages available. */
|
|
216
|
+
totalPages: number;
|
|
217
|
+
/** Current page number (1-based index). */
|
|
218
|
+
page: number;
|
|
219
|
+
/** Number of records per page. */
|
|
220
|
+
limit: number;
|
|
221
|
+
/** Field by which the data is sorted. */
|
|
222
|
+
sortBy: string;
|
|
223
|
+
/** Sort order: ascending or descending. */
|
|
224
|
+
sortOrder: 'asc' | 'desc';
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Alias for paginated API response.
|
|
229
|
+
* Allows semantic naming like `PaginationResponse<User>` or `PaginationResponse<Post>`.
|
|
230
|
+
*/
|
|
231
|
+
type PaginationResponse<T = any> = Pagination<T>;
|
|
232
|
+
/**
|
|
233
|
+
* Error response structure with additional metadata.
|
|
234
|
+
* Used for providing richer error information to clients.
|
|
235
|
+
*/
|
|
236
|
+
interface ApiErrorResponse extends Omit<ApiResponse<never>, 'data'> {
|
|
237
|
+
/** Error always true for error responses */
|
|
238
|
+
error: true;
|
|
239
|
+
/** HTTP status code */
|
|
240
|
+
status: number;
|
|
241
|
+
/** Path to the resource that caused the error */
|
|
242
|
+
path: string;
|
|
243
|
+
/** Stack trace of the error (if available) */
|
|
244
|
+
stack?: string[];
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Success response structure with strongly typed data.
|
|
248
|
+
* Used for providing successful responses to clients.
|
|
249
|
+
*/
|
|
250
|
+
interface ApiSuccessResponse<T = any> extends ApiResponse<T> {
|
|
251
|
+
/** Error always false for success responses */
|
|
252
|
+
error: false;
|
|
253
|
+
/** HTTP status code (usually 200) */
|
|
254
|
+
status?: number;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Response structure for batch operations.
|
|
258
|
+
* Used when multiple operations are performed in a single request.
|
|
259
|
+
*/
|
|
260
|
+
interface BatchResponse<T = any> {
|
|
261
|
+
/** Overall success/failure indicator */
|
|
262
|
+
success: boolean;
|
|
263
|
+
/** Total number of operations */
|
|
264
|
+
total: number;
|
|
265
|
+
/** Number of successful operations */
|
|
266
|
+
successful: number;
|
|
267
|
+
/** Number of failed operations */
|
|
268
|
+
failed: number;
|
|
269
|
+
/** Results of individual operations */
|
|
270
|
+
results: Array<{
|
|
271
|
+
/** Identifier for this operation */
|
|
272
|
+
id: string | number;
|
|
273
|
+
/** Success/failure indicator for this operation */
|
|
274
|
+
success: boolean;
|
|
275
|
+
/** Response data for this operation */
|
|
276
|
+
data?: T;
|
|
277
|
+
/** Error information if this operation failed */
|
|
278
|
+
error?: {
|
|
279
|
+
message: string;
|
|
280
|
+
code?: string;
|
|
281
|
+
};
|
|
282
|
+
}>;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* Response structure for asynchronous operations.
|
|
286
|
+
* Used when the operation will complete in the future.
|
|
287
|
+
*/
|
|
288
|
+
interface AsyncOperationResponse {
|
|
289
|
+
/** Always true for async operations */
|
|
290
|
+
async: true;
|
|
291
|
+
/** Job or task ID to check status later */
|
|
292
|
+
jobId: string;
|
|
293
|
+
/** Estimated completion time in seconds (if known) */
|
|
294
|
+
estimatedTime?: number;
|
|
295
|
+
/** URL to check status */
|
|
296
|
+
statusUrl: string;
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Response structure for streaming operations.
|
|
300
|
+
* Used when data is returned as a stream rather than all at once.
|
|
301
|
+
*/
|
|
302
|
+
interface StreamResponse {
|
|
303
|
+
/** Stream identifier */
|
|
304
|
+
streamId: string;
|
|
305
|
+
/** Stream type (e.g., 'json', 'binary') */
|
|
306
|
+
streamType: string;
|
|
307
|
+
/** Total size in bytes (if known) */
|
|
308
|
+
totalSize?: number;
|
|
309
|
+
/** Chunk size in bytes */
|
|
310
|
+
chunkSize: number;
|
|
311
|
+
}
|
|
312
|
+
/**
|
|
313
|
+
* Sort direction enumeration.
|
|
314
|
+
*/
|
|
315
|
+
declare enum SortDirection {
|
|
316
|
+
/** Ascending sort order */
|
|
317
|
+
ASC = "asc",
|
|
318
|
+
/** Descending sort order */
|
|
319
|
+
DESC = "desc"
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* Pagination parameters for API requests.
|
|
323
|
+
*/
|
|
324
|
+
interface PaginationParams {
|
|
325
|
+
/** Current page number (1-based index) */
|
|
326
|
+
page: number;
|
|
327
|
+
/** Number of records per page */
|
|
328
|
+
limit: number;
|
|
329
|
+
/** Field by which the data is sorted */
|
|
330
|
+
sortBy: string;
|
|
331
|
+
/** Sort order: ascending or descending */
|
|
332
|
+
sortOrder: SortDirection;
|
|
333
|
+
/** Optional search query */
|
|
334
|
+
search?: string;
|
|
335
|
+
}
|
|
336
|
+
/**
|
|
337
|
+
* Type that combines pagination parameters with additional data.
|
|
338
|
+
*/
|
|
339
|
+
type WithPagination<T = {}> = PaginationParams & T;
|
|
340
|
+
|
|
341
|
+
export { ErrorResponse, NoContentResponse, PaginatedResponse, RedirectResponse, SortDirection, SuccessResponse, createErrorResponse, createFinalErrorResponse, createPaginatedResponse, createSuccessResponse, sendResponse };
|
|
342
|
+
export type { ApiErrorResponse, ApiResponse, ApiSuccessResponse, AsyncOperationResponse, BatchResponse, Pagination, PaginationParams, PaginationResponse, StreamResponse, WithPagination };
|