@catbee/utils 1.1.0 → 2.0.0-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +27 -27
- package/array/array.utils.d.ts +191 -0
- package/array/index.cjs +246 -0
- package/array/index.d.ts +25 -0
- package/array/index.mjs +228 -0
- package/async/async.utils.d.ts +296 -0
- package/async/index.cjs +428 -0
- package/async/index.d.ts +25 -0
- package/async/index.mjs +407 -0
- package/cache/cache.utils.d.ts +176 -0
- package/cache/index.cjs +292 -0
- package/cache/index.d.ts +25 -0
- package/cache/index.mjs +290 -0
- package/config/config.d.ts +57 -0
- package/config/index.cjs +136 -0
- package/config/index.d.ts +26 -0
- package/config/index.mjs +131 -0
- package/context-store/context-store.utils.d.ts +212 -0
- package/context-store/index.cjs +267 -0
- package/context-store/index.d.ts +25 -0
- package/context-store/index.mjs +261 -0
- package/crypto/crypto.utils.d.ts +183 -0
- package/crypto/index.cjs +182 -0
- package/crypto/index.d.ts +25 -0
- package/crypto/index.mjs +166 -0
- package/date/date.utils.d.ts +190 -0
- package/date/index.cjs +295 -0
- package/date/index.d.ts +25 -0
- package/date/index.mjs +283 -0
- package/decorators/decorators.utils.d.ts +705 -0
- package/decorators/index.cjs +913 -0
- package/decorators/index.d.ts +25 -0
- package/decorators/index.mjs +872 -0
- package/dir/dir.utils.d.ts +216 -0
- package/dir/index.cjs +416 -0
- package/dir/index.d.ts +25 -0
- package/dir/index.mjs +389 -0
- package/env/env.utils.d.ts +400 -0
- package/env/index.cjs +761 -0
- package/env/index.d.ts +25 -0
- package/env/index.mjs +758 -0
- package/exception/exception.utils.d.ts +253 -0
- package/exception/index.cjs +362 -0
- package/exception/index.d.ts +25 -0
- package/exception/index.mjs +338 -0
- package/fs/fs.utils.d.ts +196 -0
- package/fs/index.cjs +253 -0
- package/fs/index.d.ts +25 -0
- package/fs/index.mjs +228 -0
- package/http-status-codes/http-status-codes.d.ts +289 -0
- package/http-status-codes/index.cjs +96 -0
- package/http-status-codes/index.d.ts +25 -0
- package/http-status-codes/index.mjs +94 -0
- package/id/id.utils.d.ts +59 -0
- package/id/index.cjs +62 -0
- package/id/index.d.ts +25 -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 +25 -0
- package/logger/index.mjs +313 -0
- package/logger/logger.utils.d.ts +210 -0
- package/middleware/index.cjs +177 -0
- package/middleware/index.d.ts +25 -0
- package/middleware/index.mjs +170 -0
- package/middleware/middleware.utils.d.ts +123 -0
- package/obj/index.cjs +317 -0
- package/obj/index.d.ts +25 -0
- package/obj/index.mjs +301 -0
- package/obj/obj.utils.d.ts +156 -0
- package/package.json +171 -19
- package/performance/index.cjs +231 -0
- package/performance/index.d.ts +25 -0
- package/performance/index.mjs +225 -0
- package/performance/performance.utils.d.ts +159 -0
- package/request/index.cjs +202 -0
- package/request/index.d.ts +26 -0
- package/request/index.mjs +194 -0
- package/request/request.utils.d.ts +109 -0
- package/response/index.cjs +234 -0
- package/response/index.d.ts +26 -0
- package/response/index.mjs +222 -0
- package/response/response.utils.d.ts +186 -0
- package/server/index.cjs +1627 -0
- package/server/index.d.ts +28 -0
- package/server/index.mjs +1617 -0
- package/server/server.builder.d.ts +531 -0
- package/server/server.d.ts +303 -0
- package/stream/index.cjs +151 -0
- package/stream/index.d.ts +25 -0
- package/stream/index.mjs +144 -0
- package/stream/stream.utils.d.ts +111 -0
- package/string/index.cjs +109 -0
- package/string/index.d.ts +25 -0
- package/string/index.mjs +95 -0
- package/string/string.utils.d.ts +124 -0
- package/type/index.cjs +129 -0
- package/type/index.d.ts +25 -0
- package/type/index.mjs +119 -0
- package/type/type.utils.d.ts +129 -0
- package/types/api-response.d.ts +175 -0
- package/types/common.d.ts +148 -0
- package/types/config.d.ts +88 -0
- package/types/index.cjs +34 -0
- package/types/index.d.ts +28 -0
- package/types/index.mjs +32 -0
- package/types/server.d.ts +291 -0
- package/url/index.cjs +201 -0
- package/url/index.d.ts +25 -0
- package/url/index.mjs +189 -0
- package/url/url.utils.d.ts +164 -0
- package/validate/index.cjs +212 -0
- package/validate/index.d.ts +25 -0
- package/validate/index.mjs +188 -0
- package/validate/validate.utils.d.ts +200 -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,26 @@
|
|
|
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
|
+
export * from './response.utils';
|
|
26
|
+
export * from '../types/api-response';
|
|
@@ -0,0 +1,222 @@
|
|
|
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 { getRequestId } from '@catbee/utils/context-store';
|
|
26
|
+
import { Env } from '@catbee/utils/env';
|
|
27
|
+
import { uuid } from '@catbee/utils/id';
|
|
28
|
+
|
|
29
|
+
var __defProp = Object.defineProperty;
|
|
30
|
+
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
31
|
+
var SuccessResponse = class {
|
|
32
|
+
static {
|
|
33
|
+
__name(this, "SuccessResponse");
|
|
34
|
+
}
|
|
35
|
+
/** Message describing the result of the operation. */
|
|
36
|
+
message = "Success";
|
|
37
|
+
/** Whether the response is an error. Always false in success responses. */
|
|
38
|
+
error = false;
|
|
39
|
+
/** The payload returned from the API. */
|
|
40
|
+
data = null;
|
|
41
|
+
/** Timestamp when the response was generated, in ISO format. */
|
|
42
|
+
timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
43
|
+
/** Unique identifier for this response, useful for request tracing. */
|
|
44
|
+
requestId = getRequestId() ?? uuid();
|
|
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, data) {
|
|
52
|
+
if (message) this.message = message;
|
|
53
|
+
this.data = data ?? null;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
var ErrorResponse = class extends Error {
|
|
57
|
+
static {
|
|
58
|
+
__name(this, "ErrorResponse");
|
|
59
|
+
}
|
|
60
|
+
/** HTTP status code associated with the error (e.g., 404, 500). */
|
|
61
|
+
status;
|
|
62
|
+
/** Indicates that this is an error. Always true. */
|
|
63
|
+
error = true;
|
|
64
|
+
/** Timestamp when the error occurred, in ISO format. */
|
|
65
|
+
timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
66
|
+
/** Unique identifier for this error instance. */
|
|
67
|
+
requestId = getRequestId() ?? uuid();
|
|
68
|
+
/**
|
|
69
|
+
* Constructs a new error response.
|
|
70
|
+
*
|
|
71
|
+
* @param {string} message - The error message to display or log.
|
|
72
|
+
* @param {number} [status=500] - Optional HTTP status code (defaults to 500).
|
|
73
|
+
*/
|
|
74
|
+
constructor(message, status = 500) {
|
|
75
|
+
super(message);
|
|
76
|
+
this.status = status;
|
|
77
|
+
this.name = this.constructor.name;
|
|
78
|
+
Error.captureStackTrace(this, this.constructor);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
var PaginatedResponse = class extends SuccessResponse {
|
|
82
|
+
static {
|
|
83
|
+
__name(this, "PaginatedResponse");
|
|
84
|
+
}
|
|
85
|
+
/** Total number of items across all pages */
|
|
86
|
+
total;
|
|
87
|
+
/** Current page number (1-based) */
|
|
88
|
+
page;
|
|
89
|
+
/** Number of items per page */
|
|
90
|
+
pageSize;
|
|
91
|
+
/** Total number of pages */
|
|
92
|
+
totalPages;
|
|
93
|
+
/** Whether there's a next page available */
|
|
94
|
+
hasNext;
|
|
95
|
+
/** Whether there's a previous page available */
|
|
96
|
+
hasPrevious;
|
|
97
|
+
/**
|
|
98
|
+
* Constructs a new paginated response.
|
|
99
|
+
*
|
|
100
|
+
* @param {T[]} items - The current page of items.
|
|
101
|
+
* @param {Object} pagination - Pagination information.
|
|
102
|
+
* @param {number} pagination.total - Total number of items across all pages.
|
|
103
|
+
* @param {number} pagination.page - Current page number (1-based).
|
|
104
|
+
* @param {number} pagination.pageSize - Number of items per page.
|
|
105
|
+
* @param {string} [message="Success"] - Optional custom message.
|
|
106
|
+
*/
|
|
107
|
+
constructor(items, pagination, message = "Success") {
|
|
108
|
+
super(message, items);
|
|
109
|
+
this.total = pagination.total;
|
|
110
|
+
this.page = pagination.page;
|
|
111
|
+
this.pageSize = pagination.pageSize;
|
|
112
|
+
this.totalPages = Math.ceil(this.total / this.pageSize);
|
|
113
|
+
this.hasNext = this.page < this.totalPages;
|
|
114
|
+
this.hasPrevious = this.page > 1;
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
var NoContentResponse = class extends SuccessResponse {
|
|
118
|
+
static {
|
|
119
|
+
__name(this, "NoContentResponse");
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* Constructs a new no-content response.
|
|
123
|
+
*
|
|
124
|
+
* @param {string} [message="Operation completed successfully"] - Optional custom message.
|
|
125
|
+
*/
|
|
126
|
+
constructor(message = "Operation completed successfully") {
|
|
127
|
+
super(message, null);
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
var RedirectResponse = class {
|
|
131
|
+
static {
|
|
132
|
+
__name(this, "RedirectResponse");
|
|
133
|
+
}
|
|
134
|
+
/** The URL to redirect to */
|
|
135
|
+
redirectUrl;
|
|
136
|
+
/** HTTP status code for the redirect (301, 302, 303, 307, 308) */
|
|
137
|
+
statusCode;
|
|
138
|
+
/** Whether the response is a redirect */
|
|
139
|
+
isRedirect = true;
|
|
140
|
+
/** Unique identifier for this response */
|
|
141
|
+
requestId = getRequestId() ?? uuid();
|
|
142
|
+
/**
|
|
143
|
+
* Constructs a new redirect response.
|
|
144
|
+
*
|
|
145
|
+
* @param {string} url - The URL to redirect to.
|
|
146
|
+
* @param {number} [statusCode=302] - HTTP status code for the redirect.
|
|
147
|
+
*/
|
|
148
|
+
constructor(url, statusCode = 302) {
|
|
149
|
+
this.redirectUrl = url;
|
|
150
|
+
this.statusCode = statusCode;
|
|
151
|
+
}
|
|
152
|
+
};
|
|
153
|
+
function createSuccessResponse(data, message = "Success") {
|
|
154
|
+
return new SuccessResponse(message, data);
|
|
155
|
+
}
|
|
156
|
+
__name(createSuccessResponse, "createSuccessResponse");
|
|
157
|
+
function createErrorResponse(message, statusCode = 500) {
|
|
158
|
+
return new ErrorResponse(message, statusCode);
|
|
159
|
+
}
|
|
160
|
+
__name(createErrorResponse, "createErrorResponse");
|
|
161
|
+
function createFinalErrorResponse(req, status, message, error, options) {
|
|
162
|
+
const isDev = Env.isDev();
|
|
163
|
+
const includeDetails = options?.includeDetails && isDev;
|
|
164
|
+
const response = {
|
|
165
|
+
error: true,
|
|
166
|
+
message,
|
|
167
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString(),
|
|
168
|
+
requestId: error?.requestId || req?.id || uuid(),
|
|
169
|
+
status,
|
|
170
|
+
path: req.originalUrl || req.url
|
|
171
|
+
};
|
|
172
|
+
if (includeDetails && error?.stack) {
|
|
173
|
+
response.stack = error.stack.split("\n").map((line) => line.trim());
|
|
174
|
+
}
|
|
175
|
+
return response;
|
|
176
|
+
}
|
|
177
|
+
__name(createFinalErrorResponse, "createFinalErrorResponse");
|
|
178
|
+
function createPaginatedResponse(allItems, page, pageSize, message = "Success") {
|
|
179
|
+
const validPage = Math.max(1, page);
|
|
180
|
+
const validPageSize = Math.max(1, pageSize);
|
|
181
|
+
const startIndex = (validPage - 1) * validPageSize;
|
|
182
|
+
const endIndex = startIndex + validPageSize;
|
|
183
|
+
const pageItems = allItems.slice(startIndex, endIndex);
|
|
184
|
+
return new PaginatedResponse(pageItems, {
|
|
185
|
+
total: allItems.length,
|
|
186
|
+
page: validPage,
|
|
187
|
+
pageSize: validPageSize
|
|
188
|
+
}, message);
|
|
189
|
+
}
|
|
190
|
+
__name(createPaginatedResponse, "createPaginatedResponse");
|
|
191
|
+
function sendResponse(res, apiResponse) {
|
|
192
|
+
if ("redirectUrl" in apiResponse) {
|
|
193
|
+
res.redirect(apiResponse.statusCode, apiResponse.redirectUrl);
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
if ("error" in apiResponse && apiResponse.error) {
|
|
197
|
+
const errorResponse = apiResponse;
|
|
198
|
+
res.status(errorResponse.status).json({
|
|
199
|
+
error: true,
|
|
200
|
+
message: errorResponse.message,
|
|
201
|
+
timestamp: errorResponse.timestamp,
|
|
202
|
+
requestId: errorResponse.requestId
|
|
203
|
+
});
|
|
204
|
+
return;
|
|
205
|
+
}
|
|
206
|
+
const successResponse = apiResponse;
|
|
207
|
+
if (successResponse instanceof NoContentResponse) {
|
|
208
|
+
res.status(204).end();
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
res.status(200).json(successResponse);
|
|
212
|
+
}
|
|
213
|
+
__name(sendResponse, "sendResponse");
|
|
214
|
+
|
|
215
|
+
// src/types/api-response.ts
|
|
216
|
+
var SortDirection = /* @__PURE__ */ (function(SortDirection2) {
|
|
217
|
+
SortDirection2["ASC"] = "asc";
|
|
218
|
+
SortDirection2["DESC"] = "desc";
|
|
219
|
+
return SortDirection2;
|
|
220
|
+
})({});
|
|
221
|
+
|
|
222
|
+
export { ErrorResponse, NoContentResponse, PaginatedResponse, RedirectResponse, SortDirection, SuccessResponse, createErrorResponse, createFinalErrorResponse, createPaginatedResponse, createSuccessResponse, sendResponse };
|
|
@@ -0,0 +1,186 @@
|
|
|
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 type { Request } from 'express';
|
|
26
|
+
import { ApiErrorResponse, ApiResponse } from '@catbee/utils/response';
|
|
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 declare class SuccessResponse<T> implements ApiResponse<T> {
|
|
34
|
+
/** Message describing the result of the operation. */
|
|
35
|
+
message: string;
|
|
36
|
+
/** Whether the response is an error. Always false in success responses. */
|
|
37
|
+
error: boolean;
|
|
38
|
+
/** The payload returned from the API. */
|
|
39
|
+
data: T | null;
|
|
40
|
+
/** Timestamp when the response was generated, in ISO format. */
|
|
41
|
+
timestamp: string;
|
|
42
|
+
/** Unique identifier for this response, useful for request tracing. */
|
|
43
|
+
requestId: string;
|
|
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: string, data?: T);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Wrapper for error responses that extends the native `Error` object.
|
|
54
|
+
* Implements `ApiResponse` but omits the `data` field (which should not be present in errors).
|
|
55
|
+
*/
|
|
56
|
+
export declare class ErrorResponse extends Error implements Omit<ApiResponse<never>, 'data'> {
|
|
57
|
+
/** HTTP status code associated with the error (e.g., 404, 500). */
|
|
58
|
+
status: number;
|
|
59
|
+
/** Indicates that this is an error. Always true. */
|
|
60
|
+
error: boolean;
|
|
61
|
+
/** Timestamp when the error occurred, in ISO format. */
|
|
62
|
+
timestamp: string;
|
|
63
|
+
/** Unique identifier for this error instance. */
|
|
64
|
+
requestId: string;
|
|
65
|
+
/**
|
|
66
|
+
* Constructs a new error response.
|
|
67
|
+
*
|
|
68
|
+
* @param {string} message - The error message to display or log.
|
|
69
|
+
* @param {number} [status=500] - Optional HTTP status code (defaults to 500).
|
|
70
|
+
*/
|
|
71
|
+
constructor(message: string, status?: number);
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Response with paginated data extending the standard success response.
|
|
75
|
+
* Useful for APIs that return large collections of data.
|
|
76
|
+
*
|
|
77
|
+
* @typeParam T - The shape of each item in the paginated collection.
|
|
78
|
+
*/
|
|
79
|
+
export declare class PaginatedResponse<T> extends SuccessResponse<T[]> {
|
|
80
|
+
/** Total number of items across all pages */
|
|
81
|
+
total: number;
|
|
82
|
+
/** Current page number (1-based) */
|
|
83
|
+
page: number;
|
|
84
|
+
/** Number of items per page */
|
|
85
|
+
pageSize: number;
|
|
86
|
+
/** Total number of pages */
|
|
87
|
+
totalPages: number;
|
|
88
|
+
/** Whether there's a next page available */
|
|
89
|
+
hasNext: boolean;
|
|
90
|
+
/** Whether there's a previous page available */
|
|
91
|
+
hasPrevious: boolean;
|
|
92
|
+
/**
|
|
93
|
+
* Constructs a new paginated response.
|
|
94
|
+
*
|
|
95
|
+
* @param {T[]} items - The current page of items.
|
|
96
|
+
* @param {Object} pagination - Pagination information.
|
|
97
|
+
* @param {number} pagination.total - Total number of items across all pages.
|
|
98
|
+
* @param {number} pagination.page - Current page number (1-based).
|
|
99
|
+
* @param {number} pagination.pageSize - Number of items per page.
|
|
100
|
+
* @param {string} [message="Success"] - Optional custom message.
|
|
101
|
+
*/
|
|
102
|
+
constructor(items: T[], pagination: {
|
|
103
|
+
total: number;
|
|
104
|
+
page: number;
|
|
105
|
+
pageSize: number;
|
|
106
|
+
}, message?: string);
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Specialized response for operations that don't return data (HTTP 204).
|
|
110
|
+
*/
|
|
111
|
+
export declare class NoContentResponse extends SuccessResponse<null> {
|
|
112
|
+
/**
|
|
113
|
+
* Constructs a new no-content response.
|
|
114
|
+
*
|
|
115
|
+
* @param {string} [message="Operation completed successfully"] - Optional custom message.
|
|
116
|
+
*/
|
|
117
|
+
constructor(message?: string);
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* Specialized response for redirects.
|
|
121
|
+
*/
|
|
122
|
+
export declare class RedirectResponse {
|
|
123
|
+
/** The URL to redirect to */
|
|
124
|
+
redirectUrl: string;
|
|
125
|
+
/** HTTP status code for the redirect (301, 302, 303, 307, 308) */
|
|
126
|
+
statusCode: number;
|
|
127
|
+
/** Whether the response is a redirect */
|
|
128
|
+
isRedirect: boolean;
|
|
129
|
+
/** Unique identifier for this response */
|
|
130
|
+
requestId: string;
|
|
131
|
+
/**
|
|
132
|
+
* Constructs a new redirect response.
|
|
133
|
+
*
|
|
134
|
+
* @param {string} url - The URL to redirect to.
|
|
135
|
+
* @param {number} [statusCode=302] - HTTP status code for the redirect.
|
|
136
|
+
*/
|
|
137
|
+
constructor(url: string, statusCode?: number);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Creates a standard success response.
|
|
141
|
+
*
|
|
142
|
+
* @typeParam T - The shape of the data returned in the response.
|
|
143
|
+
* @param {T} data - The data to include in the response.
|
|
144
|
+
* @param {string} [message="Success"] - Optional custom message.
|
|
145
|
+
* @returns {SuccessResponse<T>} A properly formatted success response.
|
|
146
|
+
*/
|
|
147
|
+
export declare function createSuccessResponse<T>(data: T, message?: string): SuccessResponse<T>;
|
|
148
|
+
/**
|
|
149
|
+
* Creates a standard error response.
|
|
150
|
+
*
|
|
151
|
+
* @param {string} message - The error message.
|
|
152
|
+
* @param {number} [statusCode=500] - HTTP status code for the error.
|
|
153
|
+
* @returns {ErrorResponse} A properly formatted error response.
|
|
154
|
+
*/
|
|
155
|
+
export declare function createErrorResponse(message: string, statusCode?: number): ErrorResponse;
|
|
156
|
+
/**
|
|
157
|
+
* Creates a final error response with request information.
|
|
158
|
+
*
|
|
159
|
+
* @param req - The original request object.
|
|
160
|
+
* @param status - The HTTP status code.
|
|
161
|
+
* @param message - The error message.
|
|
162
|
+
* @param error - The original error object (optional).
|
|
163
|
+
* @param options - Additional options for the response (optional).
|
|
164
|
+
* @returns A properly formatted final error response.
|
|
165
|
+
*/
|
|
166
|
+
export declare function createFinalErrorResponse(req: Request, status: number, message: string, error?: any, options?: {
|
|
167
|
+
includeDetails?: boolean;
|
|
168
|
+
}): ApiErrorResponse;
|
|
169
|
+
/**
|
|
170
|
+
* Creates a paginated response from array data.
|
|
171
|
+
*
|
|
172
|
+
* @typeParam T - The shape of each item in the collection.
|
|
173
|
+
* @param {T[]} allItems - The complete array of items to paginate.
|
|
174
|
+
* @param {number} page - The requested page (1-based).
|
|
175
|
+
* @param {number} pageSize - The number of items per page.
|
|
176
|
+
* @param {string} [message="Success"] - Optional custom message.
|
|
177
|
+
* @returns {PaginatedResponse<T>} A properly formatted paginated response.
|
|
178
|
+
*/
|
|
179
|
+
export declare function createPaginatedResponse<T>(allItems: T[], page: number, pageSize: number, message?: string): PaginatedResponse<T>;
|
|
180
|
+
/**
|
|
181
|
+
* Adapter to convert API responses to Express.js response format.
|
|
182
|
+
*
|
|
183
|
+
* @param {any} res - Express response object.
|
|
184
|
+
* @param {SuccessResponse<any> | ErrorResponse | RedirectResponse} apiResponse - API response instance.
|
|
185
|
+
*/
|
|
186
|
+
export declare function sendResponse(res: any, apiResponse: SuccessResponse<any> | ErrorResponse | RedirectResponse): void;
|