@xrystal/dto 26.6.27-v15 → 26.11.14
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 +21 -0
- package/README.md +181 -65
- package/dist/adapters/elysia.cjs +125 -125
- package/dist/adapters/elysia.js +125 -125
- package/dist/adapters/express.cjs +149 -128
- package/dist/adapters/express.js +149 -128
- package/dist/adapters/fetch.cjs +122 -126
- package/dist/adapters/fetch.js +122 -126
- package/dist/adapters/node.cjs +122 -126
- package/dist/adapters/node.js +122 -126
- package/dist/adapters/socketio.cjs +121 -123
- package/dist/adapters/socketio.js +121 -123
- package/dist/adapters/websocket.cjs +121 -123
- package/dist/adapters/websocket.js +121 -123
- package/dist/dto/adapters/bind.d.ts +2 -2
- package/dist/dto/adapters/elysia-adapter.d.ts +79 -0
- package/dist/dto/adapters/elysia.d.ts +1 -1
- package/dist/dto/adapters/http.d.ts +1 -76
- package/dist/dto/index.cjs +46 -27
- package/dist/dto/index.js +46 -27
- package/dist/dto/response/index.cjs +12 -2
- package/dist/dto/response/index.js +12 -2
- package/dist/dto/runtime/builder-runtime.d.ts +11 -0
- package/dist/dto/runtime/index.cjs +57 -38
- package/dist/dto/runtime/index.d.ts +7 -15
- package/dist/dto/runtime/index.js +57 -38
- package/dist/index.cjs +46 -27
- package/dist/index.js +46 -27
- package/package.json +5 -2
|
@@ -1,9 +1,5 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
require('elysia');
|
|
4
|
-
|
|
5
|
-
// src/dto/adapters/http.ts
|
|
6
|
-
|
|
7
3
|
// src/utils/constants/security-headers.ts
|
|
8
4
|
var DEFAULT_SECURITY_HEADERS = {
|
|
9
5
|
"X-Content-Type-Options": "nosniff",
|
|
@@ -16,6 +12,89 @@ var DEFAULT_SECURITY_HEADERS = {
|
|
|
16
12
|
"X-DNS-Prefetch-Control": "off"
|
|
17
13
|
};
|
|
18
14
|
|
|
15
|
+
// src/utils/helpers/uuid.ts
|
|
16
|
+
function generateUUID() {
|
|
17
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
18
|
+
return crypto.randomUUID();
|
|
19
|
+
}
|
|
20
|
+
const bytes = new Uint8Array(16);
|
|
21
|
+
if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
|
|
22
|
+
crypto.getRandomValues(bytes);
|
|
23
|
+
} else {
|
|
24
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
25
|
+
bytes[i] = Math.floor(Math.random() * 256);
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
29
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
30
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
31
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
32
|
+
}
|
|
33
|
+
function isValidUUID(id) {
|
|
34
|
+
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(id);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/utils/helpers/request-id.ts
|
|
38
|
+
var DEFAULT_REQUEST_ID_HEADERS = [
|
|
39
|
+
"x-request-id",
|
|
40
|
+
"x-correlation-id",
|
|
41
|
+
"traceparent"
|
|
42
|
+
];
|
|
43
|
+
function readHeader(req, name) {
|
|
44
|
+
const fromGet = req.get?.(name);
|
|
45
|
+
if (fromGet) return fromGet;
|
|
46
|
+
const headers = req.headers;
|
|
47
|
+
if (!headers) return void 0;
|
|
48
|
+
const direct = headers[name] ?? headers[name.toLowerCase()];
|
|
49
|
+
if (direct === void 0) return void 0;
|
|
50
|
+
return Array.isArray(direct) ? direct[0] : direct;
|
|
51
|
+
}
|
|
52
|
+
function traceIdFromTraceparent(traceparent) {
|
|
53
|
+
const parts = traceparent.trim().split("-");
|
|
54
|
+
if (parts.length < 2) return void 0;
|
|
55
|
+
const traceId = parts[1];
|
|
56
|
+
if (!traceId || !/^[0-9a-f]{32}$/i.test(traceId)) return void 0;
|
|
57
|
+
return `${traceId.slice(0, 8)}-${traceId.slice(8, 12)}-${traceId.slice(12, 16)}-${traceId.slice(16, 20)}-${traceId.slice(20)}`;
|
|
58
|
+
}
|
|
59
|
+
function resolveRequestId(req, headerNames = DEFAULT_REQUEST_ID_HEADERS) {
|
|
60
|
+
for (const name of headerNames) {
|
|
61
|
+
const raw = readHeader(req, name);
|
|
62
|
+
if (!raw?.trim()) continue;
|
|
63
|
+
if (name === "traceparent") {
|
|
64
|
+
const traceId = traceIdFromTraceparent(raw);
|
|
65
|
+
if (traceId) return traceId;
|
|
66
|
+
continue;
|
|
67
|
+
}
|
|
68
|
+
const id = raw.trim();
|
|
69
|
+
if (isValidUUID(id)) return id;
|
|
70
|
+
return id;
|
|
71
|
+
}
|
|
72
|
+
return generateUUID();
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// src/dto/factories.ts
|
|
76
|
+
var ContextFactory = class {
|
|
77
|
+
static create(req, options = {}) {
|
|
78
|
+
return {
|
|
79
|
+
requestId: resolveRequestId(req, options.requestIdHeaders),
|
|
80
|
+
startTime: Date.now()
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
static fromId(requestId) {
|
|
84
|
+
return { requestId, startTime: Date.now() };
|
|
85
|
+
}
|
|
86
|
+
};
|
|
87
|
+
var MetadataFactory = class {
|
|
88
|
+
static create(options = {}, context) {
|
|
89
|
+
return {
|
|
90
|
+
requestId: context?.requestId ?? generateUUID(),
|
|
91
|
+
serverTime: (/* @__PURE__ */ new Date()).toISOString(),
|
|
92
|
+
statusCode: 200,
|
|
93
|
+
...options
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
};
|
|
97
|
+
|
|
19
98
|
// src/utils/constants/status-codes.ts
|
|
20
99
|
var StatusCodes = {
|
|
21
100
|
SUCCESSFUL: {
|
|
@@ -72,6 +151,44 @@ function resolveStatusFromCode(code, codes) {
|
|
|
72
151
|
return isSuccessCode(code, codes);
|
|
73
152
|
}
|
|
74
153
|
|
|
154
|
+
// src/errors/error.ts
|
|
155
|
+
var ResponseError = class extends Error {
|
|
156
|
+
code;
|
|
157
|
+
httpStatus;
|
|
158
|
+
payload;
|
|
159
|
+
constructor(options = {}) {
|
|
160
|
+
const codes = createCodeRegistry(options.codes);
|
|
161
|
+
const code = options.code ?? codes.SERVER;
|
|
162
|
+
const message = options.message ?? defaultMessageForCode(code, codes);
|
|
163
|
+
super(message, { cause: options.cause });
|
|
164
|
+
this.name = "ResponseError";
|
|
165
|
+
this.code = code;
|
|
166
|
+
this.httpStatus = options.httpStatus ?? codeToDefaultHttpStatus(code, codes);
|
|
167
|
+
if (options.payload !== void 0) {
|
|
168
|
+
this.payload = options.payload;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
toJSON() {
|
|
172
|
+
return {
|
|
173
|
+
name: this.name,
|
|
174
|
+
message: this.message,
|
|
175
|
+
code: this.code,
|
|
176
|
+
httpStatus: this.httpStatus,
|
|
177
|
+
payload: this.payload
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
function isResponseError(value) {
|
|
182
|
+
return value instanceof ResponseError;
|
|
183
|
+
}
|
|
184
|
+
function codeToDefaultHttpStatus(code, codes) {
|
|
185
|
+
if (code === codes.SUCCESS) return 200;
|
|
186
|
+
if (code === codes.CLIENT) return 400;
|
|
187
|
+
if (code === codes.SERVER) return 500;
|
|
188
|
+
if (code === codes.VALIDATION) return 422;
|
|
189
|
+
return 500;
|
|
190
|
+
}
|
|
191
|
+
|
|
75
192
|
// src/dto/shared.ts
|
|
76
193
|
var BaseDTO = class {
|
|
77
194
|
};
|
|
@@ -86,28 +203,6 @@ function resolveBuilderConfig(options = {}) {
|
|
|
86
203
|
};
|
|
87
204
|
}
|
|
88
205
|
|
|
89
|
-
// src/utils/helpers/uuid.ts
|
|
90
|
-
function generateUUID() {
|
|
91
|
-
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
92
|
-
return crypto.randomUUID();
|
|
93
|
-
}
|
|
94
|
-
const bytes = new Uint8Array(16);
|
|
95
|
-
if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
|
|
96
|
-
crypto.getRandomValues(bytes);
|
|
97
|
-
} else {
|
|
98
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
99
|
-
bytes[i] = Math.floor(Math.random() * 256);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
bytes[6] = bytes[6] & 15 | 64;
|
|
103
|
-
bytes[8] = bytes[8] & 63 | 128;
|
|
104
|
-
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
105
|
-
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
106
|
-
}
|
|
107
|
-
function isValidUUID(id) {
|
|
108
|
-
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(id);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
206
|
// src/dto/shapes/response.ts
|
|
112
207
|
function assembleResponseBody(input) {
|
|
113
208
|
const ordered = {
|
|
@@ -227,105 +322,6 @@ var ResponseDTO = class _ResponseDTO extends BaseDTO {
|
|
|
227
322
|
}
|
|
228
323
|
};
|
|
229
324
|
|
|
230
|
-
// src/utils/helpers/request-id.ts
|
|
231
|
-
var DEFAULT_REQUEST_ID_HEADERS = [
|
|
232
|
-
"x-request-id",
|
|
233
|
-
"x-correlation-id",
|
|
234
|
-
"traceparent"
|
|
235
|
-
];
|
|
236
|
-
function readHeader(req, name) {
|
|
237
|
-
const fromGet = req.get?.(name);
|
|
238
|
-
if (fromGet) return fromGet;
|
|
239
|
-
const headers = req.headers;
|
|
240
|
-
if (!headers) return void 0;
|
|
241
|
-
const direct = headers[name] ?? headers[name.toLowerCase()];
|
|
242
|
-
if (direct === void 0) return void 0;
|
|
243
|
-
return Array.isArray(direct) ? direct[0] : direct;
|
|
244
|
-
}
|
|
245
|
-
function traceIdFromTraceparent(traceparent) {
|
|
246
|
-
const parts = traceparent.trim().split("-");
|
|
247
|
-
if (parts.length < 2) return void 0;
|
|
248
|
-
const traceId = parts[1];
|
|
249
|
-
if (!traceId || !/^[0-9a-f]{32}$/i.test(traceId)) return void 0;
|
|
250
|
-
return `${traceId.slice(0, 8)}-${traceId.slice(8, 12)}-${traceId.slice(12, 16)}-${traceId.slice(16, 20)}-${traceId.slice(20)}`;
|
|
251
|
-
}
|
|
252
|
-
function resolveRequestId(req, headerNames = DEFAULT_REQUEST_ID_HEADERS) {
|
|
253
|
-
for (const name of headerNames) {
|
|
254
|
-
const raw = readHeader(req, name);
|
|
255
|
-
if (!raw?.trim()) continue;
|
|
256
|
-
if (name === "traceparent") {
|
|
257
|
-
const traceId = traceIdFromTraceparent(raw);
|
|
258
|
-
if (traceId) return traceId;
|
|
259
|
-
continue;
|
|
260
|
-
}
|
|
261
|
-
const id = raw.trim();
|
|
262
|
-
if (isValidUUID(id)) return id;
|
|
263
|
-
return id;
|
|
264
|
-
}
|
|
265
|
-
return generateUUID();
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
// src/dto/factories.ts
|
|
269
|
-
var ContextFactory = class {
|
|
270
|
-
static create(req, options = {}) {
|
|
271
|
-
return {
|
|
272
|
-
requestId: resolveRequestId(req, options.requestIdHeaders),
|
|
273
|
-
startTime: Date.now()
|
|
274
|
-
};
|
|
275
|
-
}
|
|
276
|
-
static fromId(requestId) {
|
|
277
|
-
return { requestId, startTime: Date.now() };
|
|
278
|
-
}
|
|
279
|
-
};
|
|
280
|
-
var MetadataFactory = class {
|
|
281
|
-
static create(options = {}, context) {
|
|
282
|
-
return {
|
|
283
|
-
requestId: context?.requestId ?? generateUUID(),
|
|
284
|
-
serverTime: (/* @__PURE__ */ new Date()).toISOString(),
|
|
285
|
-
statusCode: 200,
|
|
286
|
-
...options
|
|
287
|
-
};
|
|
288
|
-
}
|
|
289
|
-
};
|
|
290
|
-
|
|
291
|
-
// src/errors/error.ts
|
|
292
|
-
var ResponseError = class extends Error {
|
|
293
|
-
code;
|
|
294
|
-
httpStatus;
|
|
295
|
-
payload;
|
|
296
|
-
constructor(options = {}) {
|
|
297
|
-
const codes = createCodeRegistry(options.codes);
|
|
298
|
-
const code = options.code ?? codes.SERVER;
|
|
299
|
-
const message = options.message ?? defaultMessageForCode(code, codes);
|
|
300
|
-
super(message, { cause: options.cause });
|
|
301
|
-
this.name = "ResponseError";
|
|
302
|
-
this.code = code;
|
|
303
|
-
this.httpStatus = options.httpStatus ?? codeToDefaultHttpStatus(code, codes);
|
|
304
|
-
if (options.payload !== void 0) {
|
|
305
|
-
this.payload = options.payload;
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
toJSON() {
|
|
309
|
-
return {
|
|
310
|
-
name: this.name,
|
|
311
|
-
message: this.message,
|
|
312
|
-
code: this.code,
|
|
313
|
-
httpStatus: this.httpStatus,
|
|
314
|
-
payload: this.payload
|
|
315
|
-
};
|
|
316
|
-
}
|
|
317
|
-
};
|
|
318
|
-
function isResponseError(value) {
|
|
319
|
-
return value instanceof ResponseError;
|
|
320
|
-
}
|
|
321
|
-
function codeToDefaultHttpStatus(code, codes) {
|
|
322
|
-
if (code === codes.SUCCESS) return 200;
|
|
323
|
-
if (code === codes.CLIENT) return 400;
|
|
324
|
-
if (code === codes.SERVER) return 500;
|
|
325
|
-
if (code === codes.VALIDATION) return 422;
|
|
326
|
-
return 500;
|
|
327
|
-
}
|
|
328
|
-
|
|
329
325
|
// src/dto/send/plugins.ts
|
|
330
326
|
function sortPlugins(plugins) {
|
|
331
327
|
return [...plugins].sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));
|
|
@@ -370,6 +366,21 @@ function shouldSendResponseBody(statusCode) {
|
|
|
370
366
|
}
|
|
371
367
|
|
|
372
368
|
// src/dto/send/transport.ts
|
|
369
|
+
function serializeSetCookie(cookie) {
|
|
370
|
+
let header = `${cookie.name}=${cookie.value}`;
|
|
371
|
+
const options = cookie.options ?? {};
|
|
372
|
+
if (options.maxAge !== void 0) header += `; Max-Age=${options.maxAge}`;
|
|
373
|
+
if (typeof options.domain === "string") header += `; Domain=${options.domain}`;
|
|
374
|
+
if (typeof options.path === "string") header += `; Path=${options.path}`;
|
|
375
|
+
if (options.expires instanceof Date) header += `; Expires=${options.expires.toUTCString()}`;
|
|
376
|
+
if (options.httpOnly === true) header += "; HttpOnly";
|
|
377
|
+
if (options.secure === true) header += "; Secure";
|
|
378
|
+
if (typeof options.sameSite === "string") header += `; SameSite=${options.sameSite}`;
|
|
379
|
+
return header;
|
|
380
|
+
}
|
|
381
|
+
function buildSetCookieHeaders(cookies) {
|
|
382
|
+
return cookies.map(serializeSetCookie);
|
|
383
|
+
}
|
|
373
384
|
function applyRecordHeaders(setHeader, headers) {
|
|
374
385
|
for (const [key, value] of Object.entries(headers)) {
|
|
375
386
|
setHeader(key, value);
|
|
@@ -385,8 +396,18 @@ function dispatchExpressResponse(result, res) {
|
|
|
385
396
|
return result;
|
|
386
397
|
}
|
|
387
398
|
applyRecordHeaders((name, value) => res.setHeader(name, value), result.headers);
|
|
388
|
-
if (result.cookies.length > 0
|
|
389
|
-
|
|
399
|
+
if (result.cookies.length > 0) {
|
|
400
|
+
if (res.cookie) {
|
|
401
|
+
applyExpressCookies(res.cookie.bind(res), result.cookies);
|
|
402
|
+
} else {
|
|
403
|
+
const cookieHeaders = buildSetCookieHeaders(result.cookies);
|
|
404
|
+
if (cookieHeaders.length > 0) {
|
|
405
|
+
res.setHeader(
|
|
406
|
+
"Set-Cookie",
|
|
407
|
+
cookieHeaders.length === 1 ? cookieHeaders[0] : cookieHeaders
|
|
408
|
+
);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
390
411
|
}
|
|
391
412
|
res.status(result.statusCode);
|
|
392
413
|
if (shouldSendResponseBody(result.statusCode)) {
|
|
@@ -738,7 +759,7 @@ function createBuilderProxy(sink, options = {}, context) {
|
|
|
738
759
|
});
|
|
739
760
|
}
|
|
740
761
|
|
|
741
|
-
// src/dto/runtime/
|
|
762
|
+
// src/dto/runtime/builder-runtime.ts
|
|
742
763
|
function createBuilderRuntime(sink, options, context) {
|
|
743
764
|
const builderOptions = {
|
|
744
765
|
verbose: options.verbose,
|
package/dist/adapters/express.js
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
import 'elysia';
|
|
2
|
-
|
|
3
|
-
// src/dto/adapters/http.ts
|
|
4
|
-
|
|
5
1
|
// src/utils/constants/security-headers.ts
|
|
6
2
|
var DEFAULT_SECURITY_HEADERS = {
|
|
7
3
|
"X-Content-Type-Options": "nosniff",
|
|
@@ -14,6 +10,89 @@ var DEFAULT_SECURITY_HEADERS = {
|
|
|
14
10
|
"X-DNS-Prefetch-Control": "off"
|
|
15
11
|
};
|
|
16
12
|
|
|
13
|
+
// src/utils/helpers/uuid.ts
|
|
14
|
+
function generateUUID() {
|
|
15
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
16
|
+
return crypto.randomUUID();
|
|
17
|
+
}
|
|
18
|
+
const bytes = new Uint8Array(16);
|
|
19
|
+
if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
|
|
20
|
+
crypto.getRandomValues(bytes);
|
|
21
|
+
} else {
|
|
22
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
23
|
+
bytes[i] = Math.floor(Math.random() * 256);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
27
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
28
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
29
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
30
|
+
}
|
|
31
|
+
function isValidUUID(id) {
|
|
32
|
+
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(id);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// src/utils/helpers/request-id.ts
|
|
36
|
+
var DEFAULT_REQUEST_ID_HEADERS = [
|
|
37
|
+
"x-request-id",
|
|
38
|
+
"x-correlation-id",
|
|
39
|
+
"traceparent"
|
|
40
|
+
];
|
|
41
|
+
function readHeader(req, name) {
|
|
42
|
+
const fromGet = req.get?.(name);
|
|
43
|
+
if (fromGet) return fromGet;
|
|
44
|
+
const headers = req.headers;
|
|
45
|
+
if (!headers) return void 0;
|
|
46
|
+
const direct = headers[name] ?? headers[name.toLowerCase()];
|
|
47
|
+
if (direct === void 0) return void 0;
|
|
48
|
+
return Array.isArray(direct) ? direct[0] : direct;
|
|
49
|
+
}
|
|
50
|
+
function traceIdFromTraceparent(traceparent) {
|
|
51
|
+
const parts = traceparent.trim().split("-");
|
|
52
|
+
if (parts.length < 2) return void 0;
|
|
53
|
+
const traceId = parts[1];
|
|
54
|
+
if (!traceId || !/^[0-9a-f]{32}$/i.test(traceId)) return void 0;
|
|
55
|
+
return `${traceId.slice(0, 8)}-${traceId.slice(8, 12)}-${traceId.slice(12, 16)}-${traceId.slice(16, 20)}-${traceId.slice(20)}`;
|
|
56
|
+
}
|
|
57
|
+
function resolveRequestId(req, headerNames = DEFAULT_REQUEST_ID_HEADERS) {
|
|
58
|
+
for (const name of headerNames) {
|
|
59
|
+
const raw = readHeader(req, name);
|
|
60
|
+
if (!raw?.trim()) continue;
|
|
61
|
+
if (name === "traceparent") {
|
|
62
|
+
const traceId = traceIdFromTraceparent(raw);
|
|
63
|
+
if (traceId) return traceId;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
const id = raw.trim();
|
|
67
|
+
if (isValidUUID(id)) return id;
|
|
68
|
+
return id;
|
|
69
|
+
}
|
|
70
|
+
return generateUUID();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
// src/dto/factories.ts
|
|
74
|
+
var ContextFactory = class {
|
|
75
|
+
static create(req, options = {}) {
|
|
76
|
+
return {
|
|
77
|
+
requestId: resolveRequestId(req, options.requestIdHeaders),
|
|
78
|
+
startTime: Date.now()
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
static fromId(requestId) {
|
|
82
|
+
return { requestId, startTime: Date.now() };
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
var MetadataFactory = class {
|
|
86
|
+
static create(options = {}, context) {
|
|
87
|
+
return {
|
|
88
|
+
requestId: context?.requestId ?? generateUUID(),
|
|
89
|
+
serverTime: (/* @__PURE__ */ new Date()).toISOString(),
|
|
90
|
+
statusCode: 200,
|
|
91
|
+
...options
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
17
96
|
// src/utils/constants/status-codes.ts
|
|
18
97
|
var StatusCodes = {
|
|
19
98
|
SUCCESSFUL: {
|
|
@@ -70,6 +149,44 @@ function resolveStatusFromCode(code, codes) {
|
|
|
70
149
|
return isSuccessCode(code, codes);
|
|
71
150
|
}
|
|
72
151
|
|
|
152
|
+
// src/errors/error.ts
|
|
153
|
+
var ResponseError = class extends Error {
|
|
154
|
+
code;
|
|
155
|
+
httpStatus;
|
|
156
|
+
payload;
|
|
157
|
+
constructor(options = {}) {
|
|
158
|
+
const codes = createCodeRegistry(options.codes);
|
|
159
|
+
const code = options.code ?? codes.SERVER;
|
|
160
|
+
const message = options.message ?? defaultMessageForCode(code, codes);
|
|
161
|
+
super(message, { cause: options.cause });
|
|
162
|
+
this.name = "ResponseError";
|
|
163
|
+
this.code = code;
|
|
164
|
+
this.httpStatus = options.httpStatus ?? codeToDefaultHttpStatus(code, codes);
|
|
165
|
+
if (options.payload !== void 0) {
|
|
166
|
+
this.payload = options.payload;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
toJSON() {
|
|
170
|
+
return {
|
|
171
|
+
name: this.name,
|
|
172
|
+
message: this.message,
|
|
173
|
+
code: this.code,
|
|
174
|
+
httpStatus: this.httpStatus,
|
|
175
|
+
payload: this.payload
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
function isResponseError(value) {
|
|
180
|
+
return value instanceof ResponseError;
|
|
181
|
+
}
|
|
182
|
+
function codeToDefaultHttpStatus(code, codes) {
|
|
183
|
+
if (code === codes.SUCCESS) return 200;
|
|
184
|
+
if (code === codes.CLIENT) return 400;
|
|
185
|
+
if (code === codes.SERVER) return 500;
|
|
186
|
+
if (code === codes.VALIDATION) return 422;
|
|
187
|
+
return 500;
|
|
188
|
+
}
|
|
189
|
+
|
|
73
190
|
// src/dto/shared.ts
|
|
74
191
|
var BaseDTO = class {
|
|
75
192
|
};
|
|
@@ -84,28 +201,6 @@ function resolveBuilderConfig(options = {}) {
|
|
|
84
201
|
};
|
|
85
202
|
}
|
|
86
203
|
|
|
87
|
-
// src/utils/helpers/uuid.ts
|
|
88
|
-
function generateUUID() {
|
|
89
|
-
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
90
|
-
return crypto.randomUUID();
|
|
91
|
-
}
|
|
92
|
-
const bytes = new Uint8Array(16);
|
|
93
|
-
if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
|
|
94
|
-
crypto.getRandomValues(bytes);
|
|
95
|
-
} else {
|
|
96
|
-
for (let i = 0; i < bytes.length; i++) {
|
|
97
|
-
bytes[i] = Math.floor(Math.random() * 256);
|
|
98
|
-
}
|
|
99
|
-
}
|
|
100
|
-
bytes[6] = bytes[6] & 15 | 64;
|
|
101
|
-
bytes[8] = bytes[8] & 63 | 128;
|
|
102
|
-
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
103
|
-
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
104
|
-
}
|
|
105
|
-
function isValidUUID(id) {
|
|
106
|
-
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(id);
|
|
107
|
-
}
|
|
108
|
-
|
|
109
204
|
// src/dto/shapes/response.ts
|
|
110
205
|
function assembleResponseBody(input) {
|
|
111
206
|
const ordered = {
|
|
@@ -225,105 +320,6 @@ var ResponseDTO = class _ResponseDTO extends BaseDTO {
|
|
|
225
320
|
}
|
|
226
321
|
};
|
|
227
322
|
|
|
228
|
-
// src/utils/helpers/request-id.ts
|
|
229
|
-
var DEFAULT_REQUEST_ID_HEADERS = [
|
|
230
|
-
"x-request-id",
|
|
231
|
-
"x-correlation-id",
|
|
232
|
-
"traceparent"
|
|
233
|
-
];
|
|
234
|
-
function readHeader(req, name) {
|
|
235
|
-
const fromGet = req.get?.(name);
|
|
236
|
-
if (fromGet) return fromGet;
|
|
237
|
-
const headers = req.headers;
|
|
238
|
-
if (!headers) return void 0;
|
|
239
|
-
const direct = headers[name] ?? headers[name.toLowerCase()];
|
|
240
|
-
if (direct === void 0) return void 0;
|
|
241
|
-
return Array.isArray(direct) ? direct[0] : direct;
|
|
242
|
-
}
|
|
243
|
-
function traceIdFromTraceparent(traceparent) {
|
|
244
|
-
const parts = traceparent.trim().split("-");
|
|
245
|
-
if (parts.length < 2) return void 0;
|
|
246
|
-
const traceId = parts[1];
|
|
247
|
-
if (!traceId || !/^[0-9a-f]{32}$/i.test(traceId)) return void 0;
|
|
248
|
-
return `${traceId.slice(0, 8)}-${traceId.slice(8, 12)}-${traceId.slice(12, 16)}-${traceId.slice(16, 20)}-${traceId.slice(20)}`;
|
|
249
|
-
}
|
|
250
|
-
function resolveRequestId(req, headerNames = DEFAULT_REQUEST_ID_HEADERS) {
|
|
251
|
-
for (const name of headerNames) {
|
|
252
|
-
const raw = readHeader(req, name);
|
|
253
|
-
if (!raw?.trim()) continue;
|
|
254
|
-
if (name === "traceparent") {
|
|
255
|
-
const traceId = traceIdFromTraceparent(raw);
|
|
256
|
-
if (traceId) return traceId;
|
|
257
|
-
continue;
|
|
258
|
-
}
|
|
259
|
-
const id = raw.trim();
|
|
260
|
-
if (isValidUUID(id)) return id;
|
|
261
|
-
return id;
|
|
262
|
-
}
|
|
263
|
-
return generateUUID();
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
// src/dto/factories.ts
|
|
267
|
-
var ContextFactory = class {
|
|
268
|
-
static create(req, options = {}) {
|
|
269
|
-
return {
|
|
270
|
-
requestId: resolveRequestId(req, options.requestIdHeaders),
|
|
271
|
-
startTime: Date.now()
|
|
272
|
-
};
|
|
273
|
-
}
|
|
274
|
-
static fromId(requestId) {
|
|
275
|
-
return { requestId, startTime: Date.now() };
|
|
276
|
-
}
|
|
277
|
-
};
|
|
278
|
-
var MetadataFactory = class {
|
|
279
|
-
static create(options = {}, context) {
|
|
280
|
-
return {
|
|
281
|
-
requestId: context?.requestId ?? generateUUID(),
|
|
282
|
-
serverTime: (/* @__PURE__ */ new Date()).toISOString(),
|
|
283
|
-
statusCode: 200,
|
|
284
|
-
...options
|
|
285
|
-
};
|
|
286
|
-
}
|
|
287
|
-
};
|
|
288
|
-
|
|
289
|
-
// src/errors/error.ts
|
|
290
|
-
var ResponseError = class extends Error {
|
|
291
|
-
code;
|
|
292
|
-
httpStatus;
|
|
293
|
-
payload;
|
|
294
|
-
constructor(options = {}) {
|
|
295
|
-
const codes = createCodeRegistry(options.codes);
|
|
296
|
-
const code = options.code ?? codes.SERVER;
|
|
297
|
-
const message = options.message ?? defaultMessageForCode(code, codes);
|
|
298
|
-
super(message, { cause: options.cause });
|
|
299
|
-
this.name = "ResponseError";
|
|
300
|
-
this.code = code;
|
|
301
|
-
this.httpStatus = options.httpStatus ?? codeToDefaultHttpStatus(code, codes);
|
|
302
|
-
if (options.payload !== void 0) {
|
|
303
|
-
this.payload = options.payload;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
toJSON() {
|
|
307
|
-
return {
|
|
308
|
-
name: this.name,
|
|
309
|
-
message: this.message,
|
|
310
|
-
code: this.code,
|
|
311
|
-
httpStatus: this.httpStatus,
|
|
312
|
-
payload: this.payload
|
|
313
|
-
};
|
|
314
|
-
}
|
|
315
|
-
};
|
|
316
|
-
function isResponseError(value) {
|
|
317
|
-
return value instanceof ResponseError;
|
|
318
|
-
}
|
|
319
|
-
function codeToDefaultHttpStatus(code, codes) {
|
|
320
|
-
if (code === codes.SUCCESS) return 200;
|
|
321
|
-
if (code === codes.CLIENT) return 400;
|
|
322
|
-
if (code === codes.SERVER) return 500;
|
|
323
|
-
if (code === codes.VALIDATION) return 422;
|
|
324
|
-
return 500;
|
|
325
|
-
}
|
|
326
|
-
|
|
327
323
|
// src/dto/send/plugins.ts
|
|
328
324
|
function sortPlugins(plugins) {
|
|
329
325
|
return [...plugins].sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));
|
|
@@ -368,6 +364,21 @@ function shouldSendResponseBody(statusCode) {
|
|
|
368
364
|
}
|
|
369
365
|
|
|
370
366
|
// src/dto/send/transport.ts
|
|
367
|
+
function serializeSetCookie(cookie) {
|
|
368
|
+
let header = `${cookie.name}=${cookie.value}`;
|
|
369
|
+
const options = cookie.options ?? {};
|
|
370
|
+
if (options.maxAge !== void 0) header += `; Max-Age=${options.maxAge}`;
|
|
371
|
+
if (typeof options.domain === "string") header += `; Domain=${options.domain}`;
|
|
372
|
+
if (typeof options.path === "string") header += `; Path=${options.path}`;
|
|
373
|
+
if (options.expires instanceof Date) header += `; Expires=${options.expires.toUTCString()}`;
|
|
374
|
+
if (options.httpOnly === true) header += "; HttpOnly";
|
|
375
|
+
if (options.secure === true) header += "; Secure";
|
|
376
|
+
if (typeof options.sameSite === "string") header += `; SameSite=${options.sameSite}`;
|
|
377
|
+
return header;
|
|
378
|
+
}
|
|
379
|
+
function buildSetCookieHeaders(cookies) {
|
|
380
|
+
return cookies.map(serializeSetCookie);
|
|
381
|
+
}
|
|
371
382
|
function applyRecordHeaders(setHeader, headers) {
|
|
372
383
|
for (const [key, value] of Object.entries(headers)) {
|
|
373
384
|
setHeader(key, value);
|
|
@@ -383,8 +394,18 @@ function dispatchExpressResponse(result, res) {
|
|
|
383
394
|
return result;
|
|
384
395
|
}
|
|
385
396
|
applyRecordHeaders((name, value) => res.setHeader(name, value), result.headers);
|
|
386
|
-
if (result.cookies.length > 0
|
|
387
|
-
|
|
397
|
+
if (result.cookies.length > 0) {
|
|
398
|
+
if (res.cookie) {
|
|
399
|
+
applyExpressCookies(res.cookie.bind(res), result.cookies);
|
|
400
|
+
} else {
|
|
401
|
+
const cookieHeaders = buildSetCookieHeaders(result.cookies);
|
|
402
|
+
if (cookieHeaders.length > 0) {
|
|
403
|
+
res.setHeader(
|
|
404
|
+
"Set-Cookie",
|
|
405
|
+
cookieHeaders.length === 1 ? cookieHeaders[0] : cookieHeaders
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
388
409
|
}
|
|
389
410
|
res.status(result.statusCode);
|
|
390
411
|
if (shouldSendResponseBody(result.statusCode)) {
|
|
@@ -736,7 +757,7 @@ function createBuilderProxy(sink, options = {}, context) {
|
|
|
736
757
|
});
|
|
737
758
|
}
|
|
738
759
|
|
|
739
|
-
// src/dto/runtime/
|
|
760
|
+
// src/dto/runtime/builder-runtime.ts
|
|
740
761
|
function createBuilderRuntime(sink, options, context) {
|
|
741
762
|
const builderOptions = {
|
|
742
763
|
verbose: options.verbose,
|