@xrystal/dto 26.6.27-v12
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +724 -0
- package/dist/adapters/elysia.cjs +820 -0
- package/dist/adapters/elysia.d.ts +138 -0
- package/dist/adapters/elysia.js +816 -0
- package/dist/adapters/express.cjs +789 -0
- package/dist/adapters/express.d.ts +49 -0
- package/dist/adapters/express.js +785 -0
- package/dist/adapters/index.d.ts +7 -0
- package/dist/adapters/node.cjs +790 -0
- package/dist/adapters/node.d.ts +27 -0
- package/dist/adapters/node.js +786 -0
- package/dist/adapters/shared.d.ts +11 -0
- package/dist/adapters/socketio.cjs +767 -0
- package/dist/adapters/socketio.d.ts +31 -0
- package/dist/adapters/socketio.js +763 -0
- package/dist/adapters/websocket.cjs +759 -0
- package/dist/adapters/websocket.d.ts +31 -0
- package/dist/adapters/websocket.js +754 -0
- package/dist/core/assemble-log.d.ts +1 -0
- package/dist/core/assemble-queue.d.ts +1 -0
- package/dist/core/assemble.d.ts +9 -0
- package/dist/core/builder.d.ts +49 -0
- package/dist/core/config.d.ts +10 -0
- package/dist/core/context.d.ts +5 -0
- package/dist/core/hooks.d.ts +8 -0
- package/dist/core/index.cjs +1002 -0
- package/dist/core/index.d.ts +14 -0
- package/dist/core/index.js +986 -0
- package/dist/core/log-dto.d.ts +1 -0
- package/dist/core/metadata.d.ts +6 -0
- package/dist/core/plugins.d.ts +10 -0
- package/dist/core/presets.d.ts +32 -0
- package/dist/core/queue-dto.d.ts +1 -0
- package/dist/core/response-dto.d.ts +1 -0
- package/dist/core/schema-utils.d.ts +1 -0
- package/dist/dto/index.cjs +909 -0
- package/dist/dto/index.d.ts +6 -0
- package/dist/dto/index.js +898 -0
- package/dist/dto/log/assemble.d.ts +3 -0
- package/dist/dto/log/builder.d.ts +41 -0
- package/dist/dto/log/index.cjs +152 -0
- package/dist/dto/log/index.d.ts +3 -0
- package/dist/dto/log/index.js +149 -0
- package/dist/dto/log/types.d.ts +26 -0
- package/dist/dto/queue/assemble.d.ts +2 -0
- package/dist/dto/queue/builder.d.ts +35 -0
- package/dist/dto/queue/index.cjs +154 -0
- package/dist/dto/queue/index.d.ts +3 -0
- package/dist/dto/queue/index.js +151 -0
- package/dist/dto/queue/types.d.ts +34 -0
- package/dist/dto/response/assemble.d.ts +1 -0
- package/dist/dto/response/dto.d.ts +39 -0
- package/dist/dto/response/index.cjs +179 -0
- package/dist/dto/response/index.d.ts +2 -0
- package/dist/dto/response/index.js +176 -0
- package/dist/dto/runtime/dto.cjs +1318 -0
- package/dist/dto/runtime/dto.d.ts +64 -0
- package/dist/dto/runtime/dto.js +1315 -0
- package/dist/dto/shared/base-dto.d.ts +12 -0
- package/dist/dto/shared/schema-utils.d.ts +1 -0
- package/dist/enums/node-env.d.ts +5 -0
- package/dist/errors/codes.d.ts +15 -0
- package/dist/errors/error.d.ts +32 -0
- package/dist/errors/index.d.ts +2 -0
- package/dist/index.cjs +1482 -0
- package/dist/index.d.ts +22 -0
- package/dist/index.js +1433 -0
- package/dist/types/builder.d.ts +15 -0
- package/dist/types/codes.d.ts +7 -0
- package/dist/types/context.d.ts +16 -0
- package/dist/types/hooks.d.ts +7 -0
- package/dist/types/index.d.ts +10 -0
- package/dist/types/log.d.ts +1 -0
- package/dist/types/metadata.d.ts +7 -0
- package/dist/types/options.d.ts +24 -0
- package/dist/types/plugin.d.ts +35 -0
- package/dist/types/queue.d.ts +1 -0
- package/dist/types/response.d.ts +43 -0
- package/dist/utils/constants/http.d.ts +2 -0
- package/dist/utils/constants/index.d.ts +3 -0
- package/dist/utils/constants/security-headers.d.ts +1 -0
- package/dist/utils/constants/status-codes.d.ts +35 -0
- package/dist/utils/helpers/index.d.ts +2 -0
- package/dist/utils/helpers/request-id.d.ts +3 -0
- package/dist/utils/helpers/uuid.d.ts +2 -0
- package/dist/utils/index.d.ts +2 -0
- package/package.json +130 -0
|
@@ -0,0 +1,909 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/dto/log/assemble.ts
|
|
4
|
+
function assembleLogBody(input) {
|
|
5
|
+
const ordered = {
|
|
6
|
+
level: input.level,
|
|
7
|
+
service: input.service
|
|
8
|
+
};
|
|
9
|
+
if (input.message !== void 0) {
|
|
10
|
+
ordered.message = input.message;
|
|
11
|
+
}
|
|
12
|
+
if (input.payload !== void 0 && Object.keys(input.payload).length > 0) {
|
|
13
|
+
ordered.payload = input.payload;
|
|
14
|
+
}
|
|
15
|
+
if (input.code !== void 0) {
|
|
16
|
+
ordered.code = input.code;
|
|
17
|
+
}
|
|
18
|
+
ordered.timestamp = input.timestamp ?? (/* @__PURE__ */ new Date()).toISOString();
|
|
19
|
+
if (input.id !== void 0) {
|
|
20
|
+
ordered.id = input.id;
|
|
21
|
+
}
|
|
22
|
+
ordered.env = input.env;
|
|
23
|
+
return ordered;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
// src/dto/shared/schema-utils.ts
|
|
27
|
+
function assertRequiredFields(input, fields, label = "Missing required fields") {
|
|
28
|
+
const missing = fields.filter((field) => {
|
|
29
|
+
const value = input[field];
|
|
30
|
+
return value === void 0 || value === null || value === "";
|
|
31
|
+
});
|
|
32
|
+
if (missing.length > 0) {
|
|
33
|
+
throw new Error(`${label}: ${missing.join(", ")}`);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// src/dto/shared/base-dto.ts
|
|
38
|
+
var BaseDTO = class {
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// src/dto/log/builder.ts
|
|
42
|
+
var LogDTO = class _LogDTO extends BaseDTO {
|
|
43
|
+
_level;
|
|
44
|
+
_service;
|
|
45
|
+
_env;
|
|
46
|
+
_message;
|
|
47
|
+
_payload = {};
|
|
48
|
+
_code;
|
|
49
|
+
_timestamp;
|
|
50
|
+
_id;
|
|
51
|
+
static level(value) {
|
|
52
|
+
return new _LogDTO().level(value);
|
|
53
|
+
}
|
|
54
|
+
static service(value) {
|
|
55
|
+
return new _LogDTO().service(value);
|
|
56
|
+
}
|
|
57
|
+
static env(value) {
|
|
58
|
+
return new _LogDTO().env(value);
|
|
59
|
+
}
|
|
60
|
+
static message(value) {
|
|
61
|
+
return new _LogDTO().message(value);
|
|
62
|
+
}
|
|
63
|
+
static code(value) {
|
|
64
|
+
return new _LogDTO().code(value);
|
|
65
|
+
}
|
|
66
|
+
static timestamp(value) {
|
|
67
|
+
return new _LogDTO().timestamp(value);
|
|
68
|
+
}
|
|
69
|
+
static id(value) {
|
|
70
|
+
return new _LogDTO().id(value);
|
|
71
|
+
}
|
|
72
|
+
static payload(partial) {
|
|
73
|
+
return new _LogDTO().payload(partial);
|
|
74
|
+
}
|
|
75
|
+
/** One-shot body input — same last-wins rules as the instance `.schema()`. */
|
|
76
|
+
static schema(input = {}) {
|
|
77
|
+
return new _LogDTO().schema(input);
|
|
78
|
+
}
|
|
79
|
+
level(value) {
|
|
80
|
+
this._level = value;
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
service(value) {
|
|
84
|
+
this._service = value;
|
|
85
|
+
return this;
|
|
86
|
+
}
|
|
87
|
+
env(value) {
|
|
88
|
+
this._env = value;
|
|
89
|
+
return this;
|
|
90
|
+
}
|
|
91
|
+
message(value) {
|
|
92
|
+
this._message = value;
|
|
93
|
+
return this;
|
|
94
|
+
}
|
|
95
|
+
code(value) {
|
|
96
|
+
this._code = value;
|
|
97
|
+
return this;
|
|
98
|
+
}
|
|
99
|
+
timestamp(value) {
|
|
100
|
+
this._timestamp = value;
|
|
101
|
+
return this;
|
|
102
|
+
}
|
|
103
|
+
id(value) {
|
|
104
|
+
this._id = value;
|
|
105
|
+
return this;
|
|
106
|
+
}
|
|
107
|
+
payload(partial) {
|
|
108
|
+
this._payload = { ...this._payload, ...partial };
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
schema(input = {}) {
|
|
112
|
+
const { level, service, message, env, code, timestamp, id, payload } = input;
|
|
113
|
+
if (level !== void 0) this.level(level);
|
|
114
|
+
if (service !== void 0) this.service(service);
|
|
115
|
+
if (env !== void 0) this.env(env);
|
|
116
|
+
if (message !== void 0) this.message(message);
|
|
117
|
+
if (code !== void 0) this.code(code);
|
|
118
|
+
if (timestamp !== void 0) this.timestamp(timestamp);
|
|
119
|
+
if (id !== void 0) this.id(id);
|
|
120
|
+
if (payload !== void 0) {
|
|
121
|
+
this._payload = { ...payload };
|
|
122
|
+
}
|
|
123
|
+
return this;
|
|
124
|
+
}
|
|
125
|
+
build() {
|
|
126
|
+
assertRequiredFields(
|
|
127
|
+
{
|
|
128
|
+
level: this._level,
|
|
129
|
+
service: this._service,
|
|
130
|
+
env: this._env
|
|
131
|
+
},
|
|
132
|
+
["level", "service", "env"],
|
|
133
|
+
"Missing log fields"
|
|
134
|
+
);
|
|
135
|
+
const input = {
|
|
136
|
+
level: this._level,
|
|
137
|
+
service: this._service,
|
|
138
|
+
env: this._env,
|
|
139
|
+
message: this._message,
|
|
140
|
+
code: this._code,
|
|
141
|
+
timestamp: this._timestamp,
|
|
142
|
+
id: this._id
|
|
143
|
+
};
|
|
144
|
+
if (Object.keys(this._payload).length > 0) {
|
|
145
|
+
input.payload = this._payload;
|
|
146
|
+
}
|
|
147
|
+
return assembleLogBody(input);
|
|
148
|
+
}
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
// src/dto/queue/assemble.ts
|
|
152
|
+
function assembleQueueMessage(metadata, payload) {
|
|
153
|
+
const orderedMetadata = {
|
|
154
|
+
source: metadata.source,
|
|
155
|
+
eventType: metadata.eventType,
|
|
156
|
+
timestamp: metadata.timestamp,
|
|
157
|
+
correlationId: metadata.correlationId
|
|
158
|
+
};
|
|
159
|
+
if (metadata.userId !== void 0) {
|
|
160
|
+
orderedMetadata.userId = metadata.userId;
|
|
161
|
+
}
|
|
162
|
+
if (metadata.extraData !== void 0) {
|
|
163
|
+
orderedMetadata.extraData = metadata.extraData;
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
metadata: orderedMetadata,
|
|
167
|
+
payload
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
// src/utils/helpers/uuid.ts
|
|
172
|
+
function generateUUID() {
|
|
173
|
+
if (typeof crypto !== "undefined" && typeof crypto.randomUUID === "function") {
|
|
174
|
+
return crypto.randomUUID();
|
|
175
|
+
}
|
|
176
|
+
const bytes = new Uint8Array(16);
|
|
177
|
+
if (typeof crypto !== "undefined" && typeof crypto.getRandomValues === "function") {
|
|
178
|
+
crypto.getRandomValues(bytes);
|
|
179
|
+
} else {
|
|
180
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
181
|
+
bytes[i] = Math.floor(Math.random() * 256);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
bytes[6] = bytes[6] & 15 | 64;
|
|
185
|
+
bytes[8] = bytes[8] & 63 | 128;
|
|
186
|
+
const hex = Array.from(bytes, (b) => b.toString(16).padStart(2, "0")).join("");
|
|
187
|
+
return `${hex.slice(0, 8)}-${hex.slice(8, 12)}-${hex.slice(12, 16)}-${hex.slice(16, 20)}-${hex.slice(20)}`;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
// src/dto/queue/builder.ts
|
|
191
|
+
var QueueDTO = class _QueueDTO extends BaseDTO {
|
|
192
|
+
_source;
|
|
193
|
+
_eventType;
|
|
194
|
+
_userId;
|
|
195
|
+
_correlationId;
|
|
196
|
+
_extraData;
|
|
197
|
+
_payload;
|
|
198
|
+
_timestamp;
|
|
199
|
+
static source(value) {
|
|
200
|
+
return new _QueueDTO().source(value);
|
|
201
|
+
}
|
|
202
|
+
static eventType(value) {
|
|
203
|
+
return new _QueueDTO().eventType(value);
|
|
204
|
+
}
|
|
205
|
+
static userId(value) {
|
|
206
|
+
return new _QueueDTO().userId(value);
|
|
207
|
+
}
|
|
208
|
+
static correlationId(value) {
|
|
209
|
+
return new _QueueDTO().correlationId(value);
|
|
210
|
+
}
|
|
211
|
+
static extraData(value) {
|
|
212
|
+
return new _QueueDTO().extraData(value);
|
|
213
|
+
}
|
|
214
|
+
static timestamp(value) {
|
|
215
|
+
return new _QueueDTO().timestamp(value);
|
|
216
|
+
}
|
|
217
|
+
static payload(value) {
|
|
218
|
+
return new _QueueDTO().payload(value);
|
|
219
|
+
}
|
|
220
|
+
static schema(input = {}) {
|
|
221
|
+
return new _QueueDTO().schema(input);
|
|
222
|
+
}
|
|
223
|
+
source(value) {
|
|
224
|
+
this._source = value;
|
|
225
|
+
return this;
|
|
226
|
+
}
|
|
227
|
+
eventType(value) {
|
|
228
|
+
this._eventType = value;
|
|
229
|
+
return this;
|
|
230
|
+
}
|
|
231
|
+
userId(value) {
|
|
232
|
+
this._userId = value;
|
|
233
|
+
return this;
|
|
234
|
+
}
|
|
235
|
+
correlationId(value) {
|
|
236
|
+
this._correlationId = value;
|
|
237
|
+
return this;
|
|
238
|
+
}
|
|
239
|
+
extraData(value) {
|
|
240
|
+
this._extraData = value;
|
|
241
|
+
return this;
|
|
242
|
+
}
|
|
243
|
+
timestamp(value) {
|
|
244
|
+
this._timestamp = value;
|
|
245
|
+
return this;
|
|
246
|
+
}
|
|
247
|
+
payload(value) {
|
|
248
|
+
this._payload = value;
|
|
249
|
+
return this;
|
|
250
|
+
}
|
|
251
|
+
schema(input = {}) {
|
|
252
|
+
const { metadata, payload } = input;
|
|
253
|
+
if (metadata?.source !== void 0) this.source(metadata.source);
|
|
254
|
+
if (metadata?.eventType !== void 0) this.eventType(metadata.eventType);
|
|
255
|
+
if (metadata?.userId !== void 0) this.userId(metadata.userId);
|
|
256
|
+
if (metadata?.correlationId !== void 0) this.correlationId(metadata.correlationId);
|
|
257
|
+
if (metadata?.extraData !== void 0) this.extraData(metadata.extraData);
|
|
258
|
+
if (payload !== void 0) {
|
|
259
|
+
this._payload = payload;
|
|
260
|
+
}
|
|
261
|
+
return this;
|
|
262
|
+
}
|
|
263
|
+
build() {
|
|
264
|
+
assertRequiredFields(
|
|
265
|
+
{
|
|
266
|
+
source: this._source,
|
|
267
|
+
eventType: this._eventType
|
|
268
|
+
},
|
|
269
|
+
["source", "eventType"],
|
|
270
|
+
"Missing queue metadata fields"
|
|
271
|
+
);
|
|
272
|
+
return assembleQueueMessage(
|
|
273
|
+
{
|
|
274
|
+
source: this._source,
|
|
275
|
+
eventType: this._eventType,
|
|
276
|
+
timestamp: this._timestamp ?? Date.now(),
|
|
277
|
+
correlationId: this._correlationId ?? generateUUID(),
|
|
278
|
+
userId: this._userId,
|
|
279
|
+
extraData: this._extraData
|
|
280
|
+
},
|
|
281
|
+
this._payload
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
// src/core/assemble.ts
|
|
287
|
+
function assembleResponseBody(input) {
|
|
288
|
+
const ordered = {
|
|
289
|
+
status: input.status,
|
|
290
|
+
message: input.message
|
|
291
|
+
};
|
|
292
|
+
if (input.payload !== void 0 && Object.keys(input.payload).length > 0) {
|
|
293
|
+
ordered.payload = input.payload;
|
|
294
|
+
}
|
|
295
|
+
ordered.code = input.code;
|
|
296
|
+
return ordered;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
// src/utils/constants/status-codes.ts
|
|
300
|
+
var StatusCodes = {
|
|
301
|
+
SUCCESSFUL: {
|
|
302
|
+
OK: 200,
|
|
303
|
+
CREATED: 201,
|
|
304
|
+
ACCEPTED: 202,
|
|
305
|
+
NO_CONTENT: 204
|
|
306
|
+
},
|
|
307
|
+
CLIENT_ERROR: {
|
|
308
|
+
BAD_REQUEST: 400,
|
|
309
|
+
UNAUTHORIZED: 401,
|
|
310
|
+
FORBIDDEN: 403,
|
|
311
|
+
NOT_FOUND: 404,
|
|
312
|
+
CONFLICT: 409,
|
|
313
|
+
UNPROCESSABLE_ENTITY: 422,
|
|
314
|
+
TOO_MANY_REQUESTS: 429
|
|
315
|
+
},
|
|
316
|
+
SERVER_ERROR: {
|
|
317
|
+
INTERNAL_SERVER_ERROR: 500,
|
|
318
|
+
SERVICE_UNAVAILABLE: 503,
|
|
319
|
+
GATEWAY_TIMEOUT: 504
|
|
320
|
+
}
|
|
321
|
+
};
|
|
322
|
+
|
|
323
|
+
// src/errors/codes.ts
|
|
324
|
+
var DefaultCodes = {
|
|
325
|
+
SUCCESS: 0,
|
|
326
|
+
CLIENT: 1,
|
|
327
|
+
SERVER: 2,
|
|
328
|
+
VALIDATION: 3
|
|
329
|
+
};
|
|
330
|
+
function createCodeRegistry(overrides = {}) {
|
|
331
|
+
return { ...DefaultCodes, ...overrides };
|
|
332
|
+
}
|
|
333
|
+
function isSuccessCode(code, codes) {
|
|
334
|
+
return code === codes.SUCCESS;
|
|
335
|
+
}
|
|
336
|
+
function resolveCodeFromHttpStatus(statusCode, codes) {
|
|
337
|
+
if (statusCode >= 200 && statusCode < 300) return codes.SUCCESS;
|
|
338
|
+
if (statusCode === 422) return codes.VALIDATION;
|
|
339
|
+
if (statusCode >= 400 && statusCode < 500) return codes.CLIENT;
|
|
340
|
+
if (statusCode >= 500) return codes.SERVER;
|
|
341
|
+
if (statusCode >= 300 && statusCode < 400) return codes.SUCCESS;
|
|
342
|
+
return codes.CLIENT;
|
|
343
|
+
}
|
|
344
|
+
function defaultMessageForCode(code, codes) {
|
|
345
|
+
if (code === codes.SUCCESS) return "Operation successful";
|
|
346
|
+
if (code === codes.CLIENT) return "Client error";
|
|
347
|
+
if (code === codes.SERVER) return "Internal server error";
|
|
348
|
+
if (code === codes.VALIDATION) return "Validation failed";
|
|
349
|
+
return "Operation failed";
|
|
350
|
+
}
|
|
351
|
+
function resolveStatusFromCode(code, codes) {
|
|
352
|
+
return isSuccessCode(code, codes);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
// src/dto/response/dto.ts
|
|
356
|
+
var ResponseDTO = class _ResponseDTO extends BaseDTO {
|
|
357
|
+
_status;
|
|
358
|
+
_message;
|
|
359
|
+
_code;
|
|
360
|
+
_payload = {};
|
|
361
|
+
static status(value) {
|
|
362
|
+
return new _ResponseDTO().status(value);
|
|
363
|
+
}
|
|
364
|
+
static message(value) {
|
|
365
|
+
return new _ResponseDTO().message(value);
|
|
366
|
+
}
|
|
367
|
+
static code(value) {
|
|
368
|
+
return new _ResponseDTO().code(value);
|
|
369
|
+
}
|
|
370
|
+
static data(data) {
|
|
371
|
+
return new _ResponseDTO().data(data);
|
|
372
|
+
}
|
|
373
|
+
static payload(partial) {
|
|
374
|
+
return new _ResponseDTO().payload(partial);
|
|
375
|
+
}
|
|
376
|
+
static schema(input = {}, codes = DefaultCodes) {
|
|
377
|
+
return new _ResponseDTO().schema(input, codes);
|
|
378
|
+
}
|
|
379
|
+
status(value) {
|
|
380
|
+
this._status = value;
|
|
381
|
+
return this;
|
|
382
|
+
}
|
|
383
|
+
message(msg) {
|
|
384
|
+
this._message = msg;
|
|
385
|
+
return this;
|
|
386
|
+
}
|
|
387
|
+
code(code) {
|
|
388
|
+
this._code = code;
|
|
389
|
+
return this;
|
|
390
|
+
}
|
|
391
|
+
data(data) {
|
|
392
|
+
if (data === void 0) {
|
|
393
|
+
delete this._payload.data;
|
|
394
|
+
} else {
|
|
395
|
+
this._payload.data = data;
|
|
396
|
+
}
|
|
397
|
+
return this;
|
|
398
|
+
}
|
|
399
|
+
payload(partial) {
|
|
400
|
+
this._payload = { ...this._payload, ...partial };
|
|
401
|
+
return this;
|
|
402
|
+
}
|
|
403
|
+
setMeta(meta) {
|
|
404
|
+
this._payload.meta = { ...this._payload.meta ?? {}, ...meta };
|
|
405
|
+
return this;
|
|
406
|
+
}
|
|
407
|
+
paginate(total, page, limit) {
|
|
408
|
+
if (limit <= 0) {
|
|
409
|
+
throw new Error("ResponseDTO.paginate: limit must be greater than 0");
|
|
410
|
+
}
|
|
411
|
+
this._payload.pagination = {
|
|
412
|
+
total,
|
|
413
|
+
page,
|
|
414
|
+
limit,
|
|
415
|
+
totalPages: Math.ceil(total / limit),
|
|
416
|
+
hasNext: page * limit < total
|
|
417
|
+
};
|
|
418
|
+
return this;
|
|
419
|
+
}
|
|
420
|
+
schema(input = {}, codes = DefaultCodes) {
|
|
421
|
+
const { status, message, code, payload } = input;
|
|
422
|
+
if (code !== void 0) {
|
|
423
|
+
this.code(code);
|
|
424
|
+
}
|
|
425
|
+
if (status !== void 0) {
|
|
426
|
+
this.status(status);
|
|
427
|
+
} else if (code !== void 0) {
|
|
428
|
+
this.status(resolveStatusFromCode(code, codes));
|
|
429
|
+
}
|
|
430
|
+
if (message !== void 0) {
|
|
431
|
+
this.message(message);
|
|
432
|
+
}
|
|
433
|
+
if (payload !== void 0) {
|
|
434
|
+
this._payload = { ...payload };
|
|
435
|
+
}
|
|
436
|
+
return this;
|
|
437
|
+
}
|
|
438
|
+
getCode() {
|
|
439
|
+
return this._code;
|
|
440
|
+
}
|
|
441
|
+
getMessage() {
|
|
442
|
+
return this._message;
|
|
443
|
+
}
|
|
444
|
+
setCodeFromHttpStatus(httpStatusCode, codes) {
|
|
445
|
+
if (this._code === void 0) {
|
|
446
|
+
this._code = resolveCodeFromHttpStatus(httpStatusCode, codes);
|
|
447
|
+
}
|
|
448
|
+
return this;
|
|
449
|
+
}
|
|
450
|
+
transformData(fn) {
|
|
451
|
+
const next = new _ResponseDTO();
|
|
452
|
+
next._status = this._status;
|
|
453
|
+
next._message = this._message;
|
|
454
|
+
next._code = this._code;
|
|
455
|
+
next._payload = { ...this._payload };
|
|
456
|
+
if (this._payload.data !== void 0) {
|
|
457
|
+
next._payload.data = fn(this._payload.data);
|
|
458
|
+
}
|
|
459
|
+
return next;
|
|
460
|
+
}
|
|
461
|
+
build(options = {}) {
|
|
462
|
+
const codes = options.codes ?? DefaultCodes;
|
|
463
|
+
const code = this._code ?? (options.httpStatusCode !== void 0 ? resolveCodeFromHttpStatus(options.httpStatusCode, codes) : codes.SUCCESS);
|
|
464
|
+
const status = this._status ?? isSuccessCode(code, codes);
|
|
465
|
+
const message = this._message ?? defaultMessageForCode(code, codes);
|
|
466
|
+
if (options.withDefaultBody === false) {
|
|
467
|
+
return assembleResponseBody({ status, message, code });
|
|
468
|
+
}
|
|
469
|
+
const payload = this.buildPayload();
|
|
470
|
+
return assembleResponseBody({
|
|
471
|
+
status,
|
|
472
|
+
message,
|
|
473
|
+
payload,
|
|
474
|
+
code
|
|
475
|
+
});
|
|
476
|
+
}
|
|
477
|
+
buildPayload() {
|
|
478
|
+
const entries = Object.entries(this._payload).filter(([, value]) => value !== void 0);
|
|
479
|
+
if (entries.length === 0) return void 0;
|
|
480
|
+
return Object.fromEntries(entries);
|
|
481
|
+
}
|
|
482
|
+
};
|
|
483
|
+
|
|
484
|
+
// src/core/metadata.ts
|
|
485
|
+
var MetadataFactory = class {
|
|
486
|
+
static create(options = {}, context) {
|
|
487
|
+
return {
|
|
488
|
+
requestId: context?.requestId ?? generateUUID(),
|
|
489
|
+
serverTime: (/* @__PURE__ */ new Date()).toISOString(),
|
|
490
|
+
statusCode: 200,
|
|
491
|
+
...options
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
};
|
|
495
|
+
|
|
496
|
+
// src/core/plugins.ts
|
|
497
|
+
function sortPlugins(plugins) {
|
|
498
|
+
return [...plugins].sort((a, b) => (a.priority ?? 0) - (b.priority ?? 0));
|
|
499
|
+
}
|
|
500
|
+
var PluginRegistry = class {
|
|
501
|
+
plugins;
|
|
502
|
+
constructor(plugins = []) {
|
|
503
|
+
this.plugins = sortPlugins(plugins);
|
|
504
|
+
}
|
|
505
|
+
runBeforeBuild(ctx) {
|
|
506
|
+
for (const plugin of this.plugins) {
|
|
507
|
+
plugin.beforeBuild?.(ctx);
|
|
508
|
+
}
|
|
509
|
+
}
|
|
510
|
+
runAfterBuild(result, ctx) {
|
|
511
|
+
let current = result;
|
|
512
|
+
for (const plugin of this.plugins) {
|
|
513
|
+
const next = plugin.afterBuild?.(current, ctx);
|
|
514
|
+
if (next) current = next;
|
|
515
|
+
}
|
|
516
|
+
return current;
|
|
517
|
+
}
|
|
518
|
+
runBeforeDispatch(result, ctx) {
|
|
519
|
+
let current = result;
|
|
520
|
+
for (const plugin of this.plugins) {
|
|
521
|
+
const next = plugin.beforeDispatch?.(current, ctx);
|
|
522
|
+
if (next) current = next;
|
|
523
|
+
}
|
|
524
|
+
return current;
|
|
525
|
+
}
|
|
526
|
+
runAfterDispatch(result, ctx) {
|
|
527
|
+
for (const plugin of this.plugins) {
|
|
528
|
+
plugin.afterDispatch?.(result, ctx);
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
};
|
|
532
|
+
|
|
533
|
+
// src/core/presets.ts
|
|
534
|
+
var ResponsePresets = class {
|
|
535
|
+
builder;
|
|
536
|
+
codes;
|
|
537
|
+
constructor(builder, codes) {
|
|
538
|
+
this.builder = builder;
|
|
539
|
+
this.codes = codes;
|
|
540
|
+
}
|
|
541
|
+
applyExtras(extras) {
|
|
542
|
+
if (extras && Object.keys(extras).length > 0) {
|
|
543
|
+
this.builder.payload(extras);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
dispatch() {
|
|
547
|
+
return this.builder.build();
|
|
548
|
+
}
|
|
549
|
+
resolveFailureArgs(dataOrMessage, messageOrExtras, extras) {
|
|
550
|
+
if (typeof dataOrMessage === "string") {
|
|
551
|
+
return {
|
|
552
|
+
message: dataOrMessage,
|
|
553
|
+
extras: typeof messageOrExtras === "string" ? extras : messageOrExtras
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
return {
|
|
557
|
+
data: dataOrMessage,
|
|
558
|
+
message: typeof messageOrExtras === "string" ? messageOrExtras : void 0,
|
|
559
|
+
extras: typeof messageOrExtras === "string" ? extras : messageOrExtras
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
failure(httpStatus, code, dataOrMessage, messageOrExtras, extras) {
|
|
563
|
+
const resolved = this.resolveFailureArgs(dataOrMessage, messageOrExtras, extras);
|
|
564
|
+
this.builder.httpStatus(httpStatus).code(code).status(false).message(resolved.message ?? defaultMessageForCode(code, this.codes)).data(resolved.data);
|
|
565
|
+
this.applyExtras(resolved.extras);
|
|
566
|
+
return this.dispatch();
|
|
567
|
+
}
|
|
568
|
+
ok(data, message, extras) {
|
|
569
|
+
this.builder.httpStatus(StatusCodes.SUCCESSFUL.OK).code(this.codes.SUCCESS).status(true).message(message ?? defaultMessageForCode(this.codes.SUCCESS, this.codes)).data(data);
|
|
570
|
+
this.applyExtras(extras);
|
|
571
|
+
return this.dispatch();
|
|
572
|
+
}
|
|
573
|
+
created(data, message, extras) {
|
|
574
|
+
this.builder.httpStatus(StatusCodes.SUCCESSFUL.CREATED).code(this.codes.SUCCESS).status(true).message(message ?? defaultMessageForCode(this.codes.SUCCESS, this.codes)).data(data);
|
|
575
|
+
this.applyExtras(extras);
|
|
576
|
+
return this.dispatch();
|
|
577
|
+
}
|
|
578
|
+
accepted(message, extras) {
|
|
579
|
+
this.builder.httpStatus(StatusCodes.SUCCESSFUL.ACCEPTED).code(this.codes.SUCCESS).status(true).message(message ?? defaultMessageForCode(this.codes.SUCCESS, this.codes));
|
|
580
|
+
this.applyExtras(extras);
|
|
581
|
+
return this.dispatch();
|
|
582
|
+
}
|
|
583
|
+
noContent(extras) {
|
|
584
|
+
this.builder.httpStatus(StatusCodes.SUCCESSFUL.NO_CONTENT).code(this.codes.SUCCESS).status(true).message("").data(void 0);
|
|
585
|
+
this.applyExtras(extras);
|
|
586
|
+
return this.dispatch();
|
|
587
|
+
}
|
|
588
|
+
/** Generic client failure — HTTP 400, CLIENT code */
|
|
589
|
+
fail(dataOrMessage, messageOrExtras, extras) {
|
|
590
|
+
return this.failure(StatusCodes.CLIENT_ERROR.BAD_REQUEST, this.codes.CLIENT, dataOrMessage, messageOrExtras, extras);
|
|
591
|
+
}
|
|
592
|
+
/** Generic server failure — HTTP 500, SERVER code */
|
|
593
|
+
error(dataOrMessage, messageOrExtras, extras) {
|
|
594
|
+
return this.failure(
|
|
595
|
+
StatusCodes.SERVER_ERROR.INTERNAL_SERVER_ERROR,
|
|
596
|
+
this.codes.SERVER,
|
|
597
|
+
dataOrMessage,
|
|
598
|
+
messageOrExtras,
|
|
599
|
+
extras
|
|
600
|
+
);
|
|
601
|
+
}
|
|
602
|
+
badRequest(dataOrMessage, messageOrExtras, extras) {
|
|
603
|
+
return this.failure(StatusCodes.CLIENT_ERROR.BAD_REQUEST, this.codes.CLIENT, dataOrMessage, messageOrExtras, extras);
|
|
604
|
+
}
|
|
605
|
+
unauthorized(dataOrMessage, messageOrExtras, extras) {
|
|
606
|
+
return this.failure(StatusCodes.CLIENT_ERROR.UNAUTHORIZED, this.codes.CLIENT, dataOrMessage, messageOrExtras, extras);
|
|
607
|
+
}
|
|
608
|
+
forbidden(dataOrMessage, messageOrExtras, extras) {
|
|
609
|
+
return this.failure(StatusCodes.CLIENT_ERROR.FORBIDDEN, this.codes.CLIENT, dataOrMessage, messageOrExtras, extras);
|
|
610
|
+
}
|
|
611
|
+
notFound(dataOrMessage, messageOrExtras, extras) {
|
|
612
|
+
return this.failure(StatusCodes.CLIENT_ERROR.NOT_FOUND, this.codes.CLIENT, dataOrMessage, messageOrExtras, extras);
|
|
613
|
+
}
|
|
614
|
+
conflict(dataOrMessage, messageOrExtras, extras) {
|
|
615
|
+
return this.failure(StatusCodes.CLIENT_ERROR.CONFLICT, this.codes.CLIENT, dataOrMessage, messageOrExtras, extras);
|
|
616
|
+
}
|
|
617
|
+
unprocessable(dataOrMessage, messageOrExtras, extras) {
|
|
618
|
+
return this.failure(
|
|
619
|
+
StatusCodes.CLIENT_ERROR.UNPROCESSABLE_ENTITY,
|
|
620
|
+
this.codes.VALIDATION,
|
|
621
|
+
dataOrMessage,
|
|
622
|
+
messageOrExtras,
|
|
623
|
+
extras
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
tooManyRequests(dataOrMessage, messageOrExtras, extras) {
|
|
627
|
+
return this.failure(StatusCodes.CLIENT_ERROR.TOO_MANY_REQUESTS, this.codes.CLIENT, dataOrMessage, messageOrExtras, extras);
|
|
628
|
+
}
|
|
629
|
+
internalServerError(dataOrMessage, messageOrExtras, extras) {
|
|
630
|
+
return this.failure(
|
|
631
|
+
StatusCodes.SERVER_ERROR.INTERNAL_SERVER_ERROR,
|
|
632
|
+
this.codes.SERVER,
|
|
633
|
+
dataOrMessage,
|
|
634
|
+
messageOrExtras,
|
|
635
|
+
extras
|
|
636
|
+
);
|
|
637
|
+
}
|
|
638
|
+
serviceUnavailable(dataOrMessage, messageOrExtras, extras) {
|
|
639
|
+
return this.failure(
|
|
640
|
+
StatusCodes.SERVER_ERROR.SERVICE_UNAVAILABLE,
|
|
641
|
+
this.codes.SERVER,
|
|
642
|
+
dataOrMessage,
|
|
643
|
+
messageOrExtras,
|
|
644
|
+
extras
|
|
645
|
+
);
|
|
646
|
+
}
|
|
647
|
+
gatewayTimeout(dataOrMessage, messageOrExtras, extras) {
|
|
648
|
+
return this.failure(
|
|
649
|
+
StatusCodes.SERVER_ERROR.GATEWAY_TIMEOUT,
|
|
650
|
+
this.codes.SERVER,
|
|
651
|
+
dataOrMessage,
|
|
652
|
+
messageOrExtras,
|
|
653
|
+
extras
|
|
654
|
+
);
|
|
655
|
+
}
|
|
656
|
+
};
|
|
657
|
+
|
|
658
|
+
// src/core/config.ts
|
|
659
|
+
function resolveBuilderConfig(options = {}) {
|
|
660
|
+
return {
|
|
661
|
+
verbose: options.verbose === true,
|
|
662
|
+
includeStack: options.includeStack,
|
|
663
|
+
withDefaultBody: options.withDefaultBody !== false,
|
|
664
|
+
codes: createCodeRegistry(options.codes),
|
|
665
|
+
hooks: options.hooks,
|
|
666
|
+
plugins: options.plugins ?? []
|
|
667
|
+
};
|
|
668
|
+
}
|
|
669
|
+
|
|
670
|
+
// src/errors/error.ts
|
|
671
|
+
var ResponseError = class extends Error {
|
|
672
|
+
code;
|
|
673
|
+
httpStatus;
|
|
674
|
+
payload;
|
|
675
|
+
constructor(options = {}) {
|
|
676
|
+
const codes = createCodeRegistry(options.codes);
|
|
677
|
+
const code = options.code ?? codes.SERVER;
|
|
678
|
+
const message = options.message ?? defaultMessageForCode(code, codes);
|
|
679
|
+
super(message, { cause: options.cause });
|
|
680
|
+
this.name = "ResponseError";
|
|
681
|
+
this.code = code;
|
|
682
|
+
this.httpStatus = options.httpStatus ?? codeToDefaultHttpStatus(code, codes);
|
|
683
|
+
if (options.payload !== void 0) {
|
|
684
|
+
this.payload = options.payload;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
toJSON() {
|
|
688
|
+
return {
|
|
689
|
+
name: this.name,
|
|
690
|
+
message: this.message,
|
|
691
|
+
code: this.code,
|
|
692
|
+
httpStatus: this.httpStatus,
|
|
693
|
+
payload: this.payload
|
|
694
|
+
};
|
|
695
|
+
}
|
|
696
|
+
};
|
|
697
|
+
function isResponseError(value) {
|
|
698
|
+
return value instanceof ResponseError;
|
|
699
|
+
}
|
|
700
|
+
function codeToDefaultHttpStatus(code, codes) {
|
|
701
|
+
if (code === codes.SUCCESS) return 200;
|
|
702
|
+
if (code === codes.CLIENT) return 400;
|
|
703
|
+
if (code === codes.SERVER) return 500;
|
|
704
|
+
if (code === codes.VALIDATION) return 422;
|
|
705
|
+
return 500;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
// src/core/builder.ts
|
|
709
|
+
var Builder = class {
|
|
710
|
+
dispatcher;
|
|
711
|
+
options;
|
|
712
|
+
context;
|
|
713
|
+
plugins;
|
|
714
|
+
body;
|
|
715
|
+
_statusCode = StatusCodes.SUCCESSFUL.OK;
|
|
716
|
+
_headers = {};
|
|
717
|
+
_cookies = [];
|
|
718
|
+
_presets;
|
|
719
|
+
constructor(dispatcher, options = {}, context) {
|
|
720
|
+
this.dispatcher = dispatcher;
|
|
721
|
+
this.options = resolveBuilderConfig(options);
|
|
722
|
+
this.context = context;
|
|
723
|
+
this.plugins = new PluginRegistry(this.options.plugins);
|
|
724
|
+
this.body = new ResponseDTO();
|
|
725
|
+
}
|
|
726
|
+
get dto() {
|
|
727
|
+
return this.body;
|
|
728
|
+
}
|
|
729
|
+
httpStatus(code) {
|
|
730
|
+
this._statusCode = code;
|
|
731
|
+
this.body.setCodeFromHttpStatus(code, this.options.codes);
|
|
732
|
+
return this;
|
|
733
|
+
}
|
|
734
|
+
code(code) {
|
|
735
|
+
this.body.code(code);
|
|
736
|
+
return this;
|
|
737
|
+
}
|
|
738
|
+
status(value) {
|
|
739
|
+
this.body.status(value);
|
|
740
|
+
return this;
|
|
741
|
+
}
|
|
742
|
+
message(msg) {
|
|
743
|
+
this.body.message(msg);
|
|
744
|
+
return this;
|
|
745
|
+
}
|
|
746
|
+
data(data) {
|
|
747
|
+
this.body.data(data);
|
|
748
|
+
return this;
|
|
749
|
+
}
|
|
750
|
+
payload(partial) {
|
|
751
|
+
this.body.payload(partial);
|
|
752
|
+
return this;
|
|
753
|
+
}
|
|
754
|
+
schema(input = {}) {
|
|
755
|
+
this.body.schema(input, this.options.codes);
|
|
756
|
+
return this;
|
|
757
|
+
}
|
|
758
|
+
setMeta(meta) {
|
|
759
|
+
this.body.setMeta(meta);
|
|
760
|
+
return this;
|
|
761
|
+
}
|
|
762
|
+
withMetadata(options = {}) {
|
|
763
|
+
const generated = MetadataFactory.create(
|
|
764
|
+
{ statusCode: this._statusCode, ...options },
|
|
765
|
+
{ requestId: this.context?.requestId }
|
|
766
|
+
);
|
|
767
|
+
return this.setMeta(generated);
|
|
768
|
+
}
|
|
769
|
+
paginate(total, page, limit) {
|
|
770
|
+
this.body.paginate(total, page, limit);
|
|
771
|
+
return this;
|
|
772
|
+
}
|
|
773
|
+
setHeaders(keyOrHeaders, value) {
|
|
774
|
+
if (typeof keyOrHeaders === "string") {
|
|
775
|
+
if (value !== void 0) {
|
|
776
|
+
this._headers[keyOrHeaders] = value;
|
|
777
|
+
}
|
|
778
|
+
} else {
|
|
779
|
+
this._headers = { ...this._headers, ...keyOrHeaders };
|
|
780
|
+
}
|
|
781
|
+
return this;
|
|
782
|
+
}
|
|
783
|
+
header(key, value) {
|
|
784
|
+
return this.setHeaders(key, value);
|
|
785
|
+
}
|
|
786
|
+
setCookies(nameOrCookies, valueOrOptions, options) {
|
|
787
|
+
if (typeof nameOrCookies === "string") {
|
|
788
|
+
if (typeof valueOrOptions === "string") {
|
|
789
|
+
this._cookies.push({ name: nameOrCookies, value: valueOrOptions, options });
|
|
790
|
+
}
|
|
791
|
+
} else {
|
|
792
|
+
const sharedOptions = typeof valueOrOptions === "object" ? valueOrOptions : void 0;
|
|
793
|
+
for (const [name, value] of Object.entries(nameOrCookies)) {
|
|
794
|
+
this._cookies.push({ name, value, options: sharedOptions });
|
|
795
|
+
}
|
|
796
|
+
}
|
|
797
|
+
return this;
|
|
798
|
+
}
|
|
799
|
+
fromError(error) {
|
|
800
|
+
if (isResponseError(error)) {
|
|
801
|
+
return this.httpStatus(error.httpStatus).code(error.code).message(error.message).payload(error.payload ? { ...error.payload } : {}).data(void 0);
|
|
802
|
+
}
|
|
803
|
+
return this.httpStatus(StatusCodes.SERVER_ERROR.INTERNAL_SERVER_ERROR).code(this.options.codes.SERVER).message(this.resolvePublicErrorMessage(error)).data(void 0);
|
|
804
|
+
}
|
|
805
|
+
async wrap(promise) {
|
|
806
|
+
try {
|
|
807
|
+
this.data(await promise);
|
|
808
|
+
return this;
|
|
809
|
+
} catch (error) {
|
|
810
|
+
this.fromError(error);
|
|
811
|
+
this.options.hooks?.emit("error", {
|
|
812
|
+
error,
|
|
813
|
+
message: this.body.getMessage(),
|
|
814
|
+
statusCode: this._statusCode,
|
|
815
|
+
code: this.body.getCode() ?? resolveCodeFromHttpStatus(this._statusCode, this.options.codes)
|
|
816
|
+
});
|
|
817
|
+
return this;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
transform(fn) {
|
|
821
|
+
const next = this;
|
|
822
|
+
next.body = this.body.transformData(fn);
|
|
823
|
+
return next;
|
|
824
|
+
}
|
|
825
|
+
when(condition, fn) {
|
|
826
|
+
if (condition) fn(this);
|
|
827
|
+
return this;
|
|
828
|
+
}
|
|
829
|
+
get as() {
|
|
830
|
+
if (!this._presets) {
|
|
831
|
+
this._presets = new ResponsePresets(this, this.options.codes);
|
|
832
|
+
}
|
|
833
|
+
return this._presets;
|
|
834
|
+
}
|
|
835
|
+
build() {
|
|
836
|
+
const pluginCtx = this.createPluginContext();
|
|
837
|
+
this.options.hooks?.emit("before_build", { statusCode: this._statusCode });
|
|
838
|
+
this.plugins.runBeforeBuild(pluginCtx);
|
|
839
|
+
const body = this.body.build({
|
|
840
|
+
codes: this.options.codes,
|
|
841
|
+
withDefaultBody: this.options.withDefaultBody,
|
|
842
|
+
httpStatusCode: this._statusCode
|
|
843
|
+
});
|
|
844
|
+
let result = {
|
|
845
|
+
body,
|
|
846
|
+
statusCode: this._statusCode,
|
|
847
|
+
headers: { ...this._headers },
|
|
848
|
+
cookies: [...this._cookies]
|
|
849
|
+
};
|
|
850
|
+
result = this.plugins.runAfterBuild(result, pluginCtx);
|
|
851
|
+
this.emitHook("after_build", result);
|
|
852
|
+
if (this.dispatcher) {
|
|
853
|
+
result = this.plugins.runBeforeDispatch(result, pluginCtx);
|
|
854
|
+
this.emitHook("before_dispatch", result);
|
|
855
|
+
const dispatched = this.dispatcher.dispatch(result);
|
|
856
|
+
this.plugins.runAfterDispatch(result, pluginCtx);
|
|
857
|
+
this.emitHook("after_dispatch", result);
|
|
858
|
+
return dispatched ?? result;
|
|
859
|
+
}
|
|
860
|
+
return result;
|
|
861
|
+
}
|
|
862
|
+
resolvePublicErrorMessage(error) {
|
|
863
|
+
if (!this.options.verbose && !isResponseError(error)) {
|
|
864
|
+
return defaultMessageForCode(this.options.codes.SERVER, this.options.codes);
|
|
865
|
+
}
|
|
866
|
+
if (error instanceof Error) {
|
|
867
|
+
if (this.options.includeStack && error.stack) return error.stack;
|
|
868
|
+
return error.message || defaultMessageForCode(this.options.codes.SERVER, this.options.codes);
|
|
869
|
+
}
|
|
870
|
+
if (typeof error === "string") return error;
|
|
871
|
+
return defaultMessageForCode(this.options.codes.SERVER, this.options.codes);
|
|
872
|
+
}
|
|
873
|
+
createPluginContext() {
|
|
874
|
+
return {
|
|
875
|
+
builder: this,
|
|
876
|
+
context: this.context,
|
|
877
|
+
options: this.options,
|
|
878
|
+
codes: this.options.codes
|
|
879
|
+
};
|
|
880
|
+
}
|
|
881
|
+
emitHook(event, result) {
|
|
882
|
+
const hooks = this.options.hooks;
|
|
883
|
+
if (!hooks?.hasListeners(event)) return;
|
|
884
|
+
hooks.emit(event, structuredClone(result));
|
|
885
|
+
}
|
|
886
|
+
};
|
|
887
|
+
function createBuilderProxy(dispatcher, options = {}, context) {
|
|
888
|
+
const instance = new Builder(dispatcher, options, context);
|
|
889
|
+
return new Proxy(instance, {
|
|
890
|
+
get(target, prop, receiver) {
|
|
891
|
+
const value = Reflect.get(target, prop, receiver);
|
|
892
|
+
if (typeof value === "function") {
|
|
893
|
+
return value.bind(target);
|
|
894
|
+
}
|
|
895
|
+
return value;
|
|
896
|
+
}
|
|
897
|
+
});
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
exports.BaseDTO = BaseDTO;
|
|
901
|
+
exports.Builder = Builder;
|
|
902
|
+
exports.LogDTO = LogDTO;
|
|
903
|
+
exports.QueueDTO = QueueDTO;
|
|
904
|
+
exports.ResponseDTO = ResponseDTO;
|
|
905
|
+
exports.ResponsePresets = ResponsePresets;
|
|
906
|
+
exports.assembleLogBody = assembleLogBody;
|
|
907
|
+
exports.assembleQueueMessage = assembleQueueMessage;
|
|
908
|
+
exports.assembleResponseBody = assembleResponseBody;
|
|
909
|
+
exports.createBuilderProxy = createBuilderProxy;
|