@arkyn/server 3.0.1-beta.159 → 3.0.1-beta.166
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/dist/index.js +1026 -1262
- package/dist/modules/http/api/_logRequest.js +55 -68
- package/dist/modules/http/api/_makeRequest.js +69 -68
- package/dist/modules/http/api/deleteRequest.js +12 -12
- package/dist/modules/http/api/getRequest.js +11 -11
- package/dist/modules/http/api/patchRequest.js +11 -11
- package/dist/modules/http/api/postRequest.js +11 -11
- package/dist/modules/http/api/putRequest.js +11 -11
- package/dist/modules/http/badResponses/_badResponse.js +62 -58
- package/dist/modules/http/badResponses/badGateway.js +22 -28
- package/dist/modules/http/badResponses/badRequest.js +22 -28
- package/dist/modules/http/badResponses/conflict.js +22 -28
- package/dist/modules/http/badResponses/forbidden.js +22 -28
- package/dist/modules/http/badResponses/notFound.js +22 -28
- package/dist/modules/http/badResponses/notImplemented.js +22 -28
- package/dist/modules/http/badResponses/serverError.js +22 -28
- package/dist/modules/http/badResponses/unauthorized.js +22 -28
- package/dist/modules/http/badResponses/unprocessableEntity.js +27 -35
- package/dist/modules/http/successResponses/_successResponse.js +61 -68
- package/dist/modules/http/successResponses/created.js +22 -28
- package/dist/modules/http/successResponses/found.js +22 -28
- package/dist/modules/http/successResponses/noContent.js +16 -20
- package/dist/modules/http/successResponses/success.js +22 -28
- package/dist/modules/http/successResponses/updated.js +22 -28
- package/dist/modules/index.js +33 -67
- package/dist/modules/services/apiService.js +108 -128
- package/dist/modules/services/debugService.js +35 -65
- package/dist/modules/services/logMapperService.js +28 -51
- package/dist/modules/services/logService.js +20 -30
- package/dist/modules/utilities/decodeRequestBody.js +19 -21
- package/dist/modules/utilities/decodeRequestErrorMessage.js +5 -6
- package/dist/modules/utilities/errorHandler.js +36 -51
- package/dist/modules/utilities/flushDebugLogs.js +15 -19
- package/dist/modules/utilities/formAsyncParse.js +13 -18
- package/dist/modules/utilities/formParse.js +13 -18
- package/dist/modules/utilities/getScopedParams.js +8 -10
- package/dist/modules/utilities/schemaValidator.js +53 -104
- package/dist/modules/validations/validateCep.js +8 -8
- package/dist/modules/validations/validateCnpj.js +49 -26
- package/dist/modules/validations/validateCpf.js +22 -23
- package/dist/modules/validations/validateDate.js +25 -26
- package/dist/modules/validations/validateEmail.js +54 -53
- package/dist/modules/validations/validatePassword.js +11 -15
- package/dist/modules/validations/validatePhone.js +8 -8
- package/dist/modules/validations/validateRg.js +7 -7
- package/generate-version.ts +74 -0
- package/package.json +17 -9
package/dist/index.js
CHANGED
|
@@ -1,1301 +1,1065 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
i.indexOf("(") + 1,
|
|
54
|
-
i.lastIndexOf(")")
|
|
55
|
-
)), i = i.split(":").slice(0, -2).join(":");
|
|
56
|
-
try {
|
|
57
|
-
i = H.relative(s, i);
|
|
58
|
-
} catch {
|
|
59
|
-
}
|
|
60
|
-
return { functionName: u, callerInfo: i };
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
/**
|
|
64
|
-
* The name of the file to ignore when analyzing the stack trace.
|
|
65
|
-
* When set, the `getCaller` function will skip stack frames containing this file name.
|
|
66
|
-
*/
|
|
67
|
-
c(N, "ignoreFiles", []);
|
|
68
|
-
function g(e) {
|
|
69
|
-
var t;
|
|
70
|
-
if (process.env.NODE_ENV === "development" || ((t = process.env) == null ? void 0 : t.DEBUG_MODE) === "true") {
|
|
71
|
-
const o = `${{
|
|
72
|
-
yellow: "\x1B[33m",
|
|
73
|
-
cyan: "\x1B[36m",
|
|
74
|
-
red: "\x1B[31m",
|
|
75
|
-
green: "\x1B[32m"
|
|
76
|
-
}[e.scheme]}[${e.name}]\x1B[0m`;
|
|
77
|
-
let a = `
|
|
78
|
-
`;
|
|
79
|
-
e.debugs.forEach((u, i) => {
|
|
80
|
-
a += `${o} ${u.trim()}`, i < e.debugs.length - 1 && (a += `
|
|
81
|
-
`);
|
|
82
|
-
}), console.log(a);
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
class f {
|
|
86
|
-
constructor() {
|
|
87
|
-
c(this, "_cause");
|
|
88
|
-
c(this, "_name", "BadResponse");
|
|
89
|
-
c(this, "_status", 500);
|
|
90
|
-
c(this, "_statusText", "Unknown error");
|
|
91
|
-
c(this, "_debugColor", "red");
|
|
92
|
-
}
|
|
93
|
-
get cause() {
|
|
94
|
-
return this._cause;
|
|
95
|
-
}
|
|
96
|
-
set cause(s) {
|
|
97
|
-
this._cause = s;
|
|
98
|
-
}
|
|
99
|
-
get name() {
|
|
100
|
-
return this._name;
|
|
101
|
-
}
|
|
102
|
-
set name(s) {
|
|
103
|
-
this._name = s;
|
|
104
|
-
}
|
|
105
|
-
get status() {
|
|
106
|
-
return this._status;
|
|
107
|
-
}
|
|
108
|
-
set status(s) {
|
|
109
|
-
this._status = s;
|
|
110
|
-
}
|
|
111
|
-
get statusText() {
|
|
112
|
-
return this._statusText;
|
|
113
|
-
}
|
|
114
|
-
set statusText(s) {
|
|
115
|
-
this._statusText = s;
|
|
116
|
-
}
|
|
117
|
-
get debugColor() {
|
|
118
|
-
return this._debugColor;
|
|
119
|
-
}
|
|
120
|
-
set debugColor(s) {
|
|
121
|
-
["green", "yellow", "cyan", "red"].includes(s) && (this._debugColor = s);
|
|
122
|
-
}
|
|
123
|
-
onDebug() {
|
|
124
|
-
const s = [], { callerInfo: t, functionName: n } = N.getCaller();
|
|
125
|
-
s.push(`Caller Function: ${n}`), s.push(`Caller Location: ${t}`), this._statusText && s.push(`Message: ${this._statusText}`), this._cause && s.push(`Cause: ${U(JSON.stringify(this._cause))}`), g({ scheme: "red", name: this._name, debugs: s });
|
|
126
|
-
}
|
|
127
|
-
makeBody() {
|
|
128
|
-
return {
|
|
129
|
-
name: this._name,
|
|
130
|
-
message: this._statusText,
|
|
131
|
-
cause: this._cause
|
|
132
|
-
};
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
class V extends f {
|
|
136
|
-
/**
|
|
137
|
-
* @param message - Error description sent in the response body and logged for debugging.
|
|
138
|
-
* @param cause - Optional extra context (serialized to JSON in the response).
|
|
139
|
-
*/
|
|
140
|
-
constructor(s, t) {
|
|
141
|
-
super(), this.name = "BadGateway", this.status = 502, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
142
|
-
}
|
|
143
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
144
|
-
toResponse() {
|
|
145
|
-
const s = {
|
|
146
|
-
headers: { "Content-Type": "application/json" },
|
|
147
|
-
status: this.status,
|
|
148
|
-
statusText: this.statusText
|
|
149
|
-
};
|
|
150
|
-
return new Response(JSON.stringify(this.makeBody()), s);
|
|
151
|
-
}
|
|
152
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
153
|
-
toJson() {
|
|
154
|
-
const s = {
|
|
155
|
-
status: this.status,
|
|
156
|
-
statusText: this.statusText
|
|
157
|
-
};
|
|
158
|
-
return Response.json(this.makeBody(), s);
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
class T extends f {
|
|
162
|
-
/**
|
|
163
|
-
* @param message - Error description.
|
|
164
|
-
* @param cause - Optional extra context (serialized to JSON).
|
|
165
|
-
*/
|
|
166
|
-
constructor(s, t) {
|
|
167
|
-
super(), this.name = "BadRequest", this.status = 400, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
168
|
-
}
|
|
169
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
170
|
-
toResponse() {
|
|
171
|
-
const s = {
|
|
172
|
-
headers: { "Content-Type": "application/json" },
|
|
173
|
-
status: this.status,
|
|
174
|
-
statusText: this.statusText
|
|
175
|
-
};
|
|
176
|
-
return new Response(JSON.stringify(this.makeBody()), s);
|
|
177
|
-
}
|
|
178
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
179
|
-
toJson() {
|
|
180
|
-
const s = {
|
|
181
|
-
status: this.status,
|
|
182
|
-
statusText: this.statusText
|
|
183
|
-
};
|
|
184
|
-
return Response.json(this.makeBody(), s);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
class Z extends f {
|
|
188
|
-
/**
|
|
189
|
-
* @param message - Error description.
|
|
190
|
-
* @param cause - Optional extra context (serialized to JSON).
|
|
191
|
-
*/
|
|
192
|
-
constructor(s, t) {
|
|
193
|
-
super(), this.name = "Conflict", this.status = 409, this.statusText = s, this.debugColor = "yellow", this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
194
|
-
}
|
|
195
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
196
|
-
toResponse() {
|
|
197
|
-
const s = {
|
|
198
|
-
headers: { "Content-Type": "application/json" },
|
|
199
|
-
status: this.status,
|
|
200
|
-
statusText: this.statusText
|
|
201
|
-
};
|
|
202
|
-
return new Response(JSON.stringify(this.makeBody()), s);
|
|
203
|
-
}
|
|
204
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
205
|
-
toJson() {
|
|
206
|
-
const s = {
|
|
207
|
-
status: this.status,
|
|
208
|
-
statusText: this.statusText
|
|
209
|
-
};
|
|
210
|
-
return Response.json(this.makeBody(), s);
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
class G extends f {
|
|
214
|
-
/**
|
|
215
|
-
* @param message - Error description.
|
|
216
|
-
* @param cause - Optional extra context (serialized to JSON).
|
|
217
|
-
*/
|
|
218
|
-
constructor(s, t) {
|
|
219
|
-
super(), this.name = "Forbidden", this.status = 403, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
220
|
-
}
|
|
221
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
222
|
-
toResponse() {
|
|
223
|
-
const s = {
|
|
224
|
-
headers: { "Content-Type": "application/json" },
|
|
225
|
-
status: this.status,
|
|
226
|
-
statusText: this.statusText
|
|
227
|
-
};
|
|
228
|
-
return new Response(JSON.stringify(this.makeBody()), s);
|
|
229
|
-
}
|
|
230
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
231
|
-
toJson() {
|
|
232
|
-
const s = {
|
|
233
|
-
status: this.status,
|
|
234
|
-
statusText: this.statusText
|
|
235
|
-
};
|
|
236
|
-
return Response.json(this.makeBody(), s);
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
|
-
class W extends f {
|
|
240
|
-
/**
|
|
241
|
-
* @param message - Error description.
|
|
242
|
-
* @param cause - Optional extra context (serialized to JSON).
|
|
243
|
-
*/
|
|
244
|
-
constructor(s, t) {
|
|
245
|
-
super(), this.name = "NotFound", this.status = 404, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
246
|
-
}
|
|
247
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
248
|
-
toResponse() {
|
|
249
|
-
const s = {
|
|
250
|
-
headers: { "Content-Type": "application/json" },
|
|
251
|
-
status: this.status,
|
|
252
|
-
statusText: this.statusText
|
|
253
|
-
};
|
|
254
|
-
return new Response(JSON.stringify(this.makeBody()), s);
|
|
255
|
-
}
|
|
256
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
257
|
-
toJson() {
|
|
258
|
-
const s = {
|
|
259
|
-
status: this.status,
|
|
260
|
-
statusText: this.statusText
|
|
261
|
-
};
|
|
262
|
-
return Response.json(this.makeBody(), s);
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
class Y extends f {
|
|
266
|
-
/**
|
|
267
|
-
* @param message - Error description.
|
|
268
|
-
* @param cause - Optional extra context (serialized to JSON).
|
|
269
|
-
*/
|
|
270
|
-
constructor(s, t) {
|
|
271
|
-
super(), this.name = "NotImplemented", this.status = 501, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
272
|
-
}
|
|
273
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
274
|
-
toResponse() {
|
|
275
|
-
const s = {
|
|
276
|
-
headers: { "Content-Type": "application/json" },
|
|
277
|
-
status: this.status,
|
|
278
|
-
statusText: this.statusText
|
|
279
|
-
};
|
|
280
|
-
return new Response(JSON.stringify(this.makeBody()), s);
|
|
281
|
-
}
|
|
282
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
283
|
-
toJson() {
|
|
284
|
-
const s = {
|
|
285
|
-
status: this.status,
|
|
286
|
-
statusText: this.statusText
|
|
287
|
-
};
|
|
288
|
-
return Response.json(this.makeBody(), s);
|
|
289
|
-
}
|
|
290
|
-
}
|
|
291
|
-
class x extends f {
|
|
292
|
-
/**
|
|
293
|
-
* @param message - Error description.
|
|
294
|
-
* @param cause - Optional extra context (serialized to JSON).
|
|
295
|
-
*/
|
|
296
|
-
constructor(s, t) {
|
|
297
|
-
super(), this.name = "ServerError", this.status = 500, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
298
|
-
}
|
|
299
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
300
|
-
toResponse() {
|
|
301
|
-
const s = {
|
|
302
|
-
headers: { "Content-Type": "application/json" },
|
|
303
|
-
status: this.status,
|
|
304
|
-
statusText: this.statusText
|
|
305
|
-
};
|
|
306
|
-
return new Response(JSON.stringify(this.makeBody()), s);
|
|
307
|
-
}
|
|
308
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
309
|
-
toJson() {
|
|
310
|
-
const s = {
|
|
311
|
-
status: this.status,
|
|
312
|
-
statusText: this.statusText
|
|
313
|
-
};
|
|
314
|
-
return Response.json(this.makeBody(), s);
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
class K extends f {
|
|
318
|
-
/**
|
|
319
|
-
* @param message - Error description.
|
|
320
|
-
* @param cause - Optional extra context (serialized to JSON).
|
|
321
|
-
*/
|
|
322
|
-
constructor(s, t) {
|
|
323
|
-
super(), this.name = "Unauthorized", this.status = 401, this.statusText = s, this.debugColor = "yellow", this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
324
|
-
}
|
|
325
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
326
|
-
toResponse() {
|
|
327
|
-
const s = {
|
|
328
|
-
headers: { "Content-Type": "application/json" },
|
|
329
|
-
status: this.status,
|
|
330
|
-
statusText: this.statusText
|
|
331
|
-
};
|
|
332
|
-
return new Response(JSON.stringify(this.makeBody()), s);
|
|
333
|
-
}
|
|
334
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
335
|
-
toJson() {
|
|
336
|
-
const s = {
|
|
337
|
-
status: this.status,
|
|
338
|
-
statusText: this.statusText
|
|
339
|
-
};
|
|
340
|
-
return Response.json(this.makeBody(), s);
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
class R extends f {
|
|
344
|
-
/**
|
|
345
|
-
* @param props.message - Human-readable description of the error.
|
|
346
|
-
* @param props.fieldErrors - Per-field validation messages keyed by field name.
|
|
347
|
-
* @param props.fields - Original submitted field values (useful for repopulating forms).
|
|
348
|
-
* @param props.data - Any extra data to include in the response body.
|
|
349
|
-
*/
|
|
350
|
-
constructor(s) {
|
|
351
|
-
super(), this.name = "UnprocessableEntity", this.status = 422, this.statusText = s.message || "Unprocessable entity", this.debugColor = "yellow", this.cause = {
|
|
352
|
-
data: s.data,
|
|
353
|
-
fieldErrors: s.fieldErrors,
|
|
354
|
-
fields: s.fields
|
|
355
|
-
}, this.onDebug();
|
|
356
|
-
}
|
|
357
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
358
|
-
toResponse() {
|
|
359
|
-
const s = {
|
|
360
|
-
headers: { "Content-Type": "application/json" },
|
|
361
|
-
status: this.status,
|
|
362
|
-
statusText: this.statusText
|
|
363
|
-
};
|
|
364
|
-
return new Response(JSON.stringify(this.makeBody()), s);
|
|
365
|
-
}
|
|
366
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
367
|
-
toJson() {
|
|
368
|
-
const s = {
|
|
369
|
-
status: this.status,
|
|
370
|
-
statusText: this.statusText
|
|
371
|
-
};
|
|
372
|
-
return Response.json(this.makeBody(), s);
|
|
373
|
-
}
|
|
374
|
-
}
|
|
375
|
-
class y {
|
|
376
|
-
constructor() {
|
|
377
|
-
c(this, "_body", null);
|
|
378
|
-
c(this, "_name", "SuccessResponse");
|
|
379
|
-
c(this, "_status", 200);
|
|
380
|
-
c(this, "_statusText", "OK");
|
|
381
|
-
c(this, "_debugColor", "green");
|
|
382
|
-
}
|
|
383
|
-
get body() {
|
|
384
|
-
return this._body;
|
|
385
|
-
}
|
|
386
|
-
set body(s) {
|
|
387
|
-
this._body = s ?? null;
|
|
388
|
-
}
|
|
389
|
-
get name() {
|
|
390
|
-
return this._name;
|
|
391
|
-
}
|
|
392
|
-
set name(s) {
|
|
393
|
-
this._name = s;
|
|
394
|
-
}
|
|
395
|
-
get status() {
|
|
396
|
-
return this._status;
|
|
397
|
-
}
|
|
398
|
-
set status(s) {
|
|
399
|
-
this._status = s;
|
|
400
|
-
}
|
|
401
|
-
get statusText() {
|
|
402
|
-
return this._statusText;
|
|
403
|
-
}
|
|
404
|
-
set statusText(s) {
|
|
405
|
-
this._statusText = s;
|
|
406
|
-
}
|
|
407
|
-
get debugColor() {
|
|
408
|
-
return this._debugColor;
|
|
409
|
-
}
|
|
410
|
-
set debugColor(s) {
|
|
411
|
-
["green", "yellow", "cyan", "red"].includes(s) && (this._debugColor = s);
|
|
412
|
-
}
|
|
413
|
-
/**
|
|
414
|
-
* Logs debug information for success responses including caller context and response details.
|
|
415
|
-
*
|
|
416
|
-
* @param {any} body - The response body or success data to be logged
|
|
417
|
-
*
|
|
418
|
-
* @example
|
|
419
|
-
* ```typescript
|
|
420
|
-
* const SuccessResponse = new SuccessResponse();
|
|
421
|
-
* SuccessResponse.onDebug({ data: "Operation completed successfully" });
|
|
422
|
-
* ```
|
|
423
|
-
*/
|
|
424
|
-
onDebug(s) {
|
|
425
|
-
const t = [], { callerInfo: n, functionName: r } = N.getCaller();
|
|
426
|
-
t.push(`Caller Function: ${r}`), t.push(`Caller Location: ${n}`), this.statusText && t.push(`Message: ${this.statusText}`), s && t.push(`Body: ${JSON.stringify(s)}`), g({ scheme: this._debugColor, name: this.name, debugs: t });
|
|
427
|
-
}
|
|
428
|
-
makeBody() {
|
|
429
|
-
return {
|
|
430
|
-
name: this.name,
|
|
431
|
-
message: this.statusText,
|
|
432
|
-
body: this.body
|
|
433
|
-
};
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
class Q extends y {
|
|
437
|
-
/**
|
|
438
|
-
* @param message - Description included in the response status text.
|
|
439
|
-
* @param body - Data to include in the response body.
|
|
440
|
-
*/
|
|
441
|
-
constructor(s, t) {
|
|
442
|
-
super(), this.name = "Created", this.status = 201, this.statusText = s, this.body = t || void 0, this.onDebug();
|
|
443
|
-
}
|
|
444
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
445
|
-
toResponse() {
|
|
446
|
-
const s = {
|
|
447
|
-
headers: { "Content-Type": "application/json" },
|
|
448
|
-
status: this.status,
|
|
449
|
-
statusText: this.statusText
|
|
450
|
-
};
|
|
451
|
-
return new Response(JSON.stringify(this.body), s);
|
|
452
|
-
}
|
|
453
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
454
|
-
toJson() {
|
|
455
|
-
const s = {
|
|
456
|
-
status: this.status,
|
|
457
|
-
statusText: this.statusText
|
|
458
|
-
};
|
|
459
|
-
return Response.json(this.body, s);
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
class X extends y {
|
|
463
|
-
/**
|
|
464
|
-
* @param message - Description included in the response status text.
|
|
465
|
-
* @param body - Data to include in the response body.
|
|
466
|
-
*/
|
|
467
|
-
constructor(s, t) {
|
|
468
|
-
super(), this.name = "Found", this.status = 302, this.statusText = s, this.body = t || void 0, this.onDebug();
|
|
469
|
-
}
|
|
470
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
471
|
-
toResponse() {
|
|
472
|
-
const s = {
|
|
473
|
-
headers: { "Content-Type": "application/json" },
|
|
474
|
-
status: this.status,
|
|
475
|
-
statusText: this.statusText
|
|
476
|
-
};
|
|
477
|
-
return new Response(JSON.stringify(this.body), s);
|
|
478
|
-
}
|
|
479
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
480
|
-
toJson() {
|
|
481
|
-
const s = {
|
|
482
|
-
status: this.status,
|
|
483
|
-
statusText: this.statusText
|
|
484
|
-
};
|
|
485
|
-
return Response.json(this.body, s);
|
|
486
|
-
}
|
|
487
|
-
}
|
|
488
|
-
class ss extends y {
|
|
489
|
-
/**
|
|
490
|
-
* @param message - Description included in the response status text.
|
|
491
|
-
*/
|
|
492
|
-
constructor(s) {
|
|
493
|
-
super(), this.name = "NoContent", this.status = 204, this.statusText = s, this.onDebug();
|
|
494
|
-
}
|
|
495
|
-
/** Converts to a `Response` with `Content-Type: application/json` header and no body. */
|
|
496
|
-
toResponse() {
|
|
497
|
-
const s = {
|
|
498
|
-
headers: { "Content-Type": "application/json" },
|
|
499
|
-
status: this.status,
|
|
500
|
-
statusText: this.statusText
|
|
501
|
-
};
|
|
502
|
-
return new Response(null, s);
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
class es extends y {
|
|
506
|
-
/**
|
|
507
|
-
* @param message - Description included in the response status text.
|
|
508
|
-
* @param body - Data to include in the response body.
|
|
509
|
-
*/
|
|
510
|
-
constructor(s, t) {
|
|
511
|
-
super(), this.name = "Success", this.status = 200, this.statusText = s, this.body = t || void 0, this.onDebug();
|
|
512
|
-
}
|
|
513
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
514
|
-
toResponse() {
|
|
515
|
-
const s = {
|
|
516
|
-
headers: { "Content-Type": "application/json" },
|
|
517
|
-
status: this.status,
|
|
518
|
-
statusText: this.statusText
|
|
519
|
-
};
|
|
520
|
-
return new Response(JSON.stringify(this.body), s);
|
|
521
|
-
}
|
|
522
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
523
|
-
toJson() {
|
|
524
|
-
const s = {
|
|
525
|
-
status: this.status,
|
|
526
|
-
statusText: this.statusText
|
|
527
|
-
};
|
|
528
|
-
return Response.json(this.body, s);
|
|
529
|
-
}
|
|
530
|
-
}
|
|
531
|
-
class ts extends y {
|
|
532
|
-
/**
|
|
533
|
-
* @param message - Description included in the response status text.
|
|
534
|
-
* @param body - Data to include in the response body.
|
|
535
|
-
*/
|
|
536
|
-
constructor(s, t) {
|
|
537
|
-
super(), this.name = "Updated", this.status = 200, this.statusText = s, this.body = t || void 0, this.onDebug();
|
|
538
|
-
}
|
|
539
|
-
/** Converts to a `Response` with `Content-Type: application/json` header. */
|
|
540
|
-
toResponse() {
|
|
541
|
-
const s = {
|
|
542
|
-
headers: { "Content-Type": "application/json" },
|
|
543
|
-
status: this.status,
|
|
544
|
-
statusText: this.statusText
|
|
545
|
-
};
|
|
546
|
-
return new Response(JSON.stringify(this.body), s);
|
|
547
|
-
}
|
|
548
|
-
/** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
|
|
549
|
-
toJson() {
|
|
550
|
-
const s = {
|
|
551
|
-
status: this.status,
|
|
552
|
-
statusText: this.statusText
|
|
553
|
-
};
|
|
554
|
-
return Response.json(this.body, s);
|
|
555
|
-
}
|
|
556
|
-
}
|
|
557
|
-
class ns {
|
|
558
|
-
/**
|
|
559
|
-
* Converts various header formats into a plain key-value object.
|
|
560
|
-
*
|
|
561
|
-
* @param {HeadersInit} headers - The headers to map.
|
|
562
|
-
* @returns {Record<string, string>} A plain object with header key-value pairs.
|
|
563
|
-
* @private
|
|
564
|
-
*/
|
|
565
|
-
static mapHeaders(s) {
|
|
566
|
-
return s instanceof Headers ? Object.fromEntries(s.entries()) : typeof s == "object" ? Object.entries(s).reduce(
|
|
567
|
-
(t, [n, r]) => (typeof r == "string" ? t[n] = r : Array.isArray(r) ? t[n] = r.join(", ") : t[n] = JSON.stringify(r), t),
|
|
568
|
-
{}
|
|
569
|
-
) : {};
|
|
570
|
-
}
|
|
571
|
-
/**
|
|
572
|
-
* Converts URLSearchParams into a plain key-value object.
|
|
573
|
-
*
|
|
574
|
-
* @param {URLSearchParams} queryParams - The query parameters to map.
|
|
575
|
-
* @returns {Record<string, string>} A plain object with query parameter key-value pairs.
|
|
576
|
-
* @private
|
|
577
|
-
*/
|
|
578
|
-
static mapQueryParams(s) {
|
|
579
|
-
const t = {};
|
|
580
|
-
return s.forEach((n, r) => t[r] = n), t;
|
|
581
|
-
}
|
|
582
|
-
/**
|
|
583
|
-
* Transforms raw HTTP request/response data into a standardized log output format.
|
|
584
|
-
*
|
|
585
|
-
* @param {InputProps} props - The input properties containing request/response data.
|
|
586
|
-
* @returns {OutputProps} The mapped output object ready for logging.
|
|
587
|
-
*/
|
|
588
|
-
static handle(s) {
|
|
589
|
-
return {
|
|
590
|
-
rawUrl: s.rawUrl,
|
|
591
|
-
status: s.status,
|
|
592
|
-
method: s.method,
|
|
593
|
-
token: null,
|
|
594
|
-
elapsedTime: s.elapsedTime,
|
|
595
|
-
requestHeaders: this.mapHeaders(s.requestHeaders),
|
|
596
|
-
requestBody: s.requestBody || null,
|
|
597
|
-
queryParams: {
|
|
598
|
-
...this.mapQueryParams(s.queryParams),
|
|
599
|
-
...s.urlParams
|
|
600
|
-
},
|
|
601
|
-
responseHeaders: this.mapHeaders(s.responseHeaders),
|
|
602
|
-
responseBody: s.responseBody || null
|
|
603
|
-
};
|
|
604
|
-
}
|
|
605
|
-
}
|
|
606
|
-
class v {
|
|
607
|
-
/**
|
|
608
|
-
* Sets the log service configuration once. Subsequent calls are ignored.
|
|
609
|
-
*
|
|
610
|
-
* @param config.trafficSourceId - Traffic source identifier.
|
|
611
|
-
* @param config.userToken - User token for authentication.
|
|
612
|
-
* @param config.logBaseApiUrl - Override the default log ingestion base URL.
|
|
613
|
-
*/
|
|
614
|
-
static setConfig(s) {
|
|
615
|
-
if (this.config) return;
|
|
616
|
-
const { trafficSourceId: t, userToken: n, logBaseApiUrl: r } = s, a = `${r || "http://62.238.8.44:8081"}/ingest-log`;
|
|
617
|
-
this.config = { trafficSourceId: t, userToken: n, apiUrl: a };
|
|
618
|
-
}
|
|
619
|
-
/** Returns the stored configuration, or `undefined` if `setConfig` has not been called. */
|
|
620
|
-
static getConfig() {
|
|
621
|
-
return this.config;
|
|
622
|
-
}
|
|
623
|
-
/**
|
|
624
|
-
* Resets the stored configuration, allowing a new initialization.
|
|
625
|
-
*/
|
|
626
|
-
static resetConfig() {
|
|
627
|
-
this.config = void 0;
|
|
628
|
-
}
|
|
629
|
-
}
|
|
630
|
-
c(v, "config");
|
|
631
|
-
async function rs(e) {
|
|
632
|
-
const s = v.getConfig();
|
|
633
|
-
if (!s) return;
|
|
634
|
-
const { userToken: t, apiUrl: n, trafficSourceId: r } = s, {
|
|
635
|
-
elapsedTime: o,
|
|
636
|
-
method: a,
|
|
637
|
-
queryParams: u,
|
|
638
|
-
requestBody: i,
|
|
639
|
-
requestHeaders: l,
|
|
640
|
-
responseBody: h,
|
|
641
|
-
responseHeaders: d,
|
|
642
|
-
status: P,
|
|
643
|
-
token: vs,
|
|
644
|
-
rawUrl: $
|
|
645
|
-
} = e;
|
|
646
|
-
if (process.env.NODE_ENV !== "development")
|
|
647
|
-
try {
|
|
648
|
-
const m = new URL($);
|
|
649
|
-
let D = "https";
|
|
650
|
-
m.protocol === "http:" && (D = "http");
|
|
651
|
-
const w = JSON.stringify({
|
|
652
|
-
domainUrl: m.protocol + "//" + m.host,
|
|
653
|
-
pathnameUrl: m.pathname,
|
|
654
|
-
trafficSourceId: r,
|
|
655
|
-
status: P,
|
|
656
|
-
protocol: D,
|
|
657
|
-
method: a.toLowerCase(),
|
|
658
|
-
trafficUserId: null,
|
|
659
|
-
elapsedTime: o,
|
|
660
|
-
requestHeaders: JSON.stringify(l),
|
|
661
|
-
requestBody: JSON.stringify(i),
|
|
662
|
-
queryParams: JSON.stringify(u),
|
|
663
|
-
responseHeaders: JSON.stringify(d),
|
|
664
|
-
responseBody: JSON.stringify(h)
|
|
665
|
-
}), C = {
|
|
666
|
-
"Content-Type": "application/json",
|
|
667
|
-
Authorization: `Bearer ${t}`
|
|
668
|
-
}, p = await fetch(n, {
|
|
669
|
-
method: "POST",
|
|
670
|
-
body: w,
|
|
671
|
-
headers: C
|
|
672
|
-
});
|
|
673
|
-
if (!p.ok) {
|
|
674
|
-
const j = await p.text(), B = p.status, J = p.statusText, E = await p.json().catch(() => null);
|
|
675
|
-
g({
|
|
676
|
-
name: "LogError",
|
|
677
|
-
scheme: "red",
|
|
678
|
-
debugs: [
|
|
679
|
-
"Failed to log request.",
|
|
680
|
-
`Status: ${B} ${J}.`,
|
|
681
|
-
`Status text: ${j}.`,
|
|
682
|
-
`JSON Response: ${E ? q(E, 0) : "No JSON response"}`
|
|
683
|
-
]
|
|
684
|
-
});
|
|
685
|
-
}
|
|
686
|
-
} catch (m) {
|
|
687
|
-
g({
|
|
688
|
-
debugs: [`Error sending request: ${m}`],
|
|
689
|
-
name: "LogError",
|
|
690
|
-
scheme: "red"
|
|
691
|
-
});
|
|
692
|
-
}
|
|
693
|
-
}
|
|
694
|
-
async function b(e) {
|
|
695
|
-
let s = e.url;
|
|
696
|
-
e.urlParams && Object.entries(e.urlParams).forEach(([n, r]) => {
|
|
697
|
-
s = s.replaceAll(`:${n}`, r);
|
|
698
|
-
});
|
|
699
|
-
const t = {
|
|
700
|
-
POST: "Resource created successfully",
|
|
701
|
-
PUT: "Resource updated successfully",
|
|
702
|
-
DELETE: "Resource deleted successfully",
|
|
703
|
-
PATCH: "Resource patched successfully",
|
|
704
|
-
GET: "Request successful"
|
|
705
|
-
};
|
|
706
|
-
try {
|
|
707
|
-
const n = performance.now(), r = { ...e.headers, "Content-Type": "application/json" }, o = await fetch(s, {
|
|
708
|
-
headers: r,
|
|
709
|
-
method: e.method,
|
|
710
|
-
body: e.body ? JSON.stringify(e.body) : void 0
|
|
711
|
-
}), a = performance.now() - n, u = o.status;
|
|
712
|
-
let i = null;
|
|
713
|
-
try {
|
|
714
|
-
i = await o.json();
|
|
715
|
-
} catch {
|
|
716
|
-
i = null;
|
|
717
|
-
}
|
|
718
|
-
const l = ns.handle({
|
|
719
|
-
elapsedTime: a,
|
|
720
|
-
method: e.method,
|
|
721
|
-
queryParams: new URL(s).searchParams,
|
|
722
|
-
requestHeaders: r,
|
|
723
|
-
requestBody: e.body,
|
|
724
|
-
responseBody: i,
|
|
725
|
-
responseHeaders: o.headers,
|
|
726
|
-
status: u,
|
|
727
|
-
rawUrl: e.url,
|
|
728
|
-
urlParams: e.urlParams
|
|
729
|
-
});
|
|
730
|
-
return rs(l), o.ok ? {
|
|
731
|
-
success: !0,
|
|
732
|
-
status: u,
|
|
733
|
-
message: (i == null ? void 0 : i.message) || t[e.method],
|
|
734
|
-
response: i,
|
|
735
|
-
cause: null
|
|
736
|
-
} : {
|
|
737
|
-
success: !1,
|
|
738
|
-
status: u,
|
|
739
|
-
message: (i == null ? void 0 : i.message) || o.statusText || "Request failed",
|
|
740
|
-
response: i,
|
|
741
|
-
cause: null
|
|
742
|
-
};
|
|
743
|
-
} catch (n) {
|
|
744
|
-
return g({
|
|
745
|
-
debugs: [`Network error or request failed: ${n}`],
|
|
746
|
-
name: "MakeRequestError",
|
|
747
|
-
scheme: "red"
|
|
748
|
-
}), {
|
|
749
|
-
success: !1,
|
|
750
|
-
status: 0,
|
|
751
|
-
message: "Network error or request failed",
|
|
752
|
-
response: null,
|
|
753
|
-
cause: n instanceof Error ? n.message : String(n)
|
|
754
|
-
};
|
|
755
|
-
}
|
|
1
|
+
import { ValidateDateService as e, formatJsonObject as t, formatJsonString as n, removeNonNumeric as r } from "@arkyn/shared";
|
|
2
|
+
import i from "node:path";
|
|
3
|
+
import a from "node:dns";
|
|
4
|
+
import { countries as o } from "@arkyn/templates";
|
|
5
|
+
import { isValidPhoneNumber as s, parsePhoneNumberWithError as c } from "libphonenumber-js";
|
|
6
|
+
//#region src/services/debugService.ts
|
|
7
|
+
var l = class {
|
|
8
|
+
static ignoreFiles = [];
|
|
9
|
+
static setIgnoreFile(e) {
|
|
10
|
+
this.ignoreFiles.push(e);
|
|
11
|
+
}
|
|
12
|
+
static clearIgnoreFiles() {
|
|
13
|
+
this.ignoreFiles = [];
|
|
14
|
+
}
|
|
15
|
+
static getCaller() {
|
|
16
|
+
let e = process.cwd(), t = ((/* @__PURE__ */ Error()).stack || "").split("\n").map((e) => e.trim()), n = 2;
|
|
17
|
+
for (; n < t.length && (t[n].includes("node:internal") || t[n].includes("/node_modules/"));) n++;
|
|
18
|
+
if (this.ignoreFiles.length > 0) for (; n < t.length && this.ignoreFiles.some((e) => t[n].includes(e));) n++;
|
|
19
|
+
let r = t[n] || "", a = "Unknown function", o = "Unknown caller", s = r.match(/at\s+([^(\s]+)\s+\(([^)]+)\)/);
|
|
20
|
+
if (s) a = s[1], o = s[2];
|
|
21
|
+
else {
|
|
22
|
+
let e = r.match(/at\s+(.+)/);
|
|
23
|
+
if (e) {
|
|
24
|
+
o = e[1];
|
|
25
|
+
let t = o.match(/at\s+([^(\s]+)\s+/);
|
|
26
|
+
t && t[1] !== "new" && (a = t[1]);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
o.includes("(") && (o = o.substring(o.indexOf("(") + 1, o.lastIndexOf(")"))), o = o.split(":").slice(0, -2).join(":");
|
|
30
|
+
try {
|
|
31
|
+
o = i.relative(e, o);
|
|
32
|
+
} catch {}
|
|
33
|
+
return {
|
|
34
|
+
functionName: a,
|
|
35
|
+
callerInfo: o
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/utilities/flushDebugLogs.ts
|
|
41
|
+
function u(e) {
|
|
42
|
+
if (process.env.NODE_ENV === "development" || process.env?.DEBUG_MODE === "true") {
|
|
43
|
+
let t = `${{
|
|
44
|
+
yellow: "\x1B[33m",
|
|
45
|
+
cyan: "\x1B[36m",
|
|
46
|
+
red: "\x1B[31m",
|
|
47
|
+
green: "\x1B[32m"
|
|
48
|
+
}[e.scheme]}[${e.name}][0m`, n = "\n";
|
|
49
|
+
e.debugs.forEach((r, i) => {
|
|
50
|
+
n += `${t} ${r.trim()}`, i < e.debugs.length - 1 && (n += "\n");
|
|
51
|
+
}), console.log(n);
|
|
52
|
+
}
|
|
756
53
|
}
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
54
|
+
//#endregion
|
|
55
|
+
//#region src/http/badResponses/_badResponse.ts
|
|
56
|
+
var d = class {
|
|
57
|
+
_cause;
|
|
58
|
+
_name = "BadResponse";
|
|
59
|
+
_status = 500;
|
|
60
|
+
_statusText = "Unknown error";
|
|
61
|
+
_debugColor = "red";
|
|
62
|
+
get cause() {
|
|
63
|
+
return this._cause;
|
|
64
|
+
}
|
|
65
|
+
set cause(e) {
|
|
66
|
+
this._cause = e;
|
|
67
|
+
}
|
|
68
|
+
get name() {
|
|
69
|
+
return this._name;
|
|
70
|
+
}
|
|
71
|
+
set name(e) {
|
|
72
|
+
this._name = e;
|
|
73
|
+
}
|
|
74
|
+
get status() {
|
|
75
|
+
return this._status;
|
|
76
|
+
}
|
|
77
|
+
set status(e) {
|
|
78
|
+
this._status = e;
|
|
79
|
+
}
|
|
80
|
+
get statusText() {
|
|
81
|
+
return this._statusText;
|
|
82
|
+
}
|
|
83
|
+
set statusText(e) {
|
|
84
|
+
this._statusText = e;
|
|
85
|
+
}
|
|
86
|
+
get debugColor() {
|
|
87
|
+
return this._debugColor;
|
|
88
|
+
}
|
|
89
|
+
set debugColor(e) {
|
|
90
|
+
[
|
|
91
|
+
"green",
|
|
92
|
+
"yellow",
|
|
93
|
+
"cyan",
|
|
94
|
+
"red"
|
|
95
|
+
].includes(e) && (this._debugColor = e);
|
|
96
|
+
}
|
|
97
|
+
onDebug() {
|
|
98
|
+
let e = [], { callerInfo: t, functionName: r } = l.getCaller();
|
|
99
|
+
e.push(`Caller Function: ${r}`), e.push(`Caller Location: ${t}`), this._statusText && e.push(`Message: ${this._statusText}`), this._cause && e.push(`Cause: ${n(JSON.stringify(this._cause))}`), u({
|
|
100
|
+
scheme: "red",
|
|
101
|
+
name: this._name,
|
|
102
|
+
debugs: e
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
makeBody() {
|
|
106
|
+
return {
|
|
107
|
+
name: this._name,
|
|
108
|
+
message: this._statusText,
|
|
109
|
+
cause: this._cause
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
}, f = class extends d {
|
|
113
|
+
constructor(e, t) {
|
|
114
|
+
super(), this.name = "BadGateway", this.status = 502, this.statusText = e, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
115
|
+
}
|
|
116
|
+
toResponse() {
|
|
117
|
+
let e = {
|
|
118
|
+
headers: { "Content-Type": "application/json" },
|
|
119
|
+
status: this.status,
|
|
120
|
+
statusText: this.statusText
|
|
121
|
+
};
|
|
122
|
+
return new Response(JSON.stringify(this.makeBody()), e);
|
|
123
|
+
}
|
|
124
|
+
toJson() {
|
|
125
|
+
let e = {
|
|
126
|
+
status: this.status,
|
|
127
|
+
statusText: this.statusText
|
|
128
|
+
};
|
|
129
|
+
return Response.json(this.makeBody(), e);
|
|
130
|
+
}
|
|
131
|
+
}, p = class extends d {
|
|
132
|
+
constructor(e, t) {
|
|
133
|
+
super(), this.name = "BadRequest", this.status = 400, this.statusText = e, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
134
|
+
}
|
|
135
|
+
toResponse() {
|
|
136
|
+
let e = {
|
|
137
|
+
headers: { "Content-Type": "application/json" },
|
|
138
|
+
status: this.status,
|
|
139
|
+
statusText: this.statusText
|
|
140
|
+
};
|
|
141
|
+
return new Response(JSON.stringify(this.makeBody()), e);
|
|
142
|
+
}
|
|
143
|
+
toJson() {
|
|
144
|
+
let e = {
|
|
145
|
+
status: this.status,
|
|
146
|
+
statusText: this.statusText
|
|
147
|
+
};
|
|
148
|
+
return Response.json(this.makeBody(), e);
|
|
149
|
+
}
|
|
150
|
+
}, m = class extends d {
|
|
151
|
+
constructor(e, t) {
|
|
152
|
+
super(), this.name = "Conflict", this.status = 409, this.statusText = e, this.debugColor = "yellow", this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
153
|
+
}
|
|
154
|
+
toResponse() {
|
|
155
|
+
let e = {
|
|
156
|
+
headers: { "Content-Type": "application/json" },
|
|
157
|
+
status: this.status,
|
|
158
|
+
statusText: this.statusText
|
|
159
|
+
};
|
|
160
|
+
return new Response(JSON.stringify(this.makeBody()), e);
|
|
161
|
+
}
|
|
162
|
+
toJson() {
|
|
163
|
+
let e = {
|
|
164
|
+
status: this.status,
|
|
165
|
+
statusText: this.statusText
|
|
166
|
+
};
|
|
167
|
+
return Response.json(this.makeBody(), e);
|
|
168
|
+
}
|
|
169
|
+
}, h = class extends d {
|
|
170
|
+
constructor(e, t) {
|
|
171
|
+
super(), this.name = "Forbidden", this.status = 403, this.statusText = e, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
172
|
+
}
|
|
173
|
+
toResponse() {
|
|
174
|
+
let e = {
|
|
175
|
+
headers: { "Content-Type": "application/json" },
|
|
176
|
+
status: this.status,
|
|
177
|
+
statusText: this.statusText
|
|
178
|
+
};
|
|
179
|
+
return new Response(JSON.stringify(this.makeBody()), e);
|
|
180
|
+
}
|
|
181
|
+
toJson() {
|
|
182
|
+
let e = {
|
|
183
|
+
status: this.status,
|
|
184
|
+
statusText: this.statusText
|
|
185
|
+
};
|
|
186
|
+
return Response.json(this.makeBody(), e);
|
|
187
|
+
}
|
|
188
|
+
}, g = class extends d {
|
|
189
|
+
constructor(e, t) {
|
|
190
|
+
super(), this.name = "NotFound", this.status = 404, this.statusText = e, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
191
|
+
}
|
|
192
|
+
toResponse() {
|
|
193
|
+
let e = {
|
|
194
|
+
headers: { "Content-Type": "application/json" },
|
|
195
|
+
status: this.status,
|
|
196
|
+
statusText: this.statusText
|
|
197
|
+
};
|
|
198
|
+
return new Response(JSON.stringify(this.makeBody()), e);
|
|
199
|
+
}
|
|
200
|
+
toJson() {
|
|
201
|
+
let e = {
|
|
202
|
+
status: this.status,
|
|
203
|
+
statusText: this.statusText
|
|
204
|
+
};
|
|
205
|
+
return Response.json(this.makeBody(), e);
|
|
206
|
+
}
|
|
207
|
+
}, _ = class extends d {
|
|
208
|
+
constructor(e, t) {
|
|
209
|
+
super(), this.name = "NotImplemented", this.status = 501, this.statusText = e, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
210
|
+
}
|
|
211
|
+
toResponse() {
|
|
212
|
+
let e = {
|
|
213
|
+
headers: { "Content-Type": "application/json" },
|
|
214
|
+
status: this.status,
|
|
215
|
+
statusText: this.statusText
|
|
216
|
+
};
|
|
217
|
+
return new Response(JSON.stringify(this.makeBody()), e);
|
|
218
|
+
}
|
|
219
|
+
toJson() {
|
|
220
|
+
let e = {
|
|
221
|
+
status: this.status,
|
|
222
|
+
statusText: this.statusText
|
|
223
|
+
};
|
|
224
|
+
return Response.json(this.makeBody(), e);
|
|
225
|
+
}
|
|
226
|
+
}, v = class extends d {
|
|
227
|
+
constructor(e, t) {
|
|
228
|
+
super(), this.name = "ServerError", this.status = 500, this.statusText = e, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
229
|
+
}
|
|
230
|
+
toResponse() {
|
|
231
|
+
let e = {
|
|
232
|
+
headers: { "Content-Type": "application/json" },
|
|
233
|
+
status: this.status,
|
|
234
|
+
statusText: this.statusText
|
|
235
|
+
};
|
|
236
|
+
return new Response(JSON.stringify(this.makeBody()), e);
|
|
237
|
+
}
|
|
238
|
+
toJson() {
|
|
239
|
+
let e = {
|
|
240
|
+
status: this.status,
|
|
241
|
+
statusText: this.statusText
|
|
242
|
+
};
|
|
243
|
+
return Response.json(this.makeBody(), e);
|
|
244
|
+
}
|
|
245
|
+
}, y = class extends d {
|
|
246
|
+
constructor(e, t) {
|
|
247
|
+
super(), this.name = "Unauthorized", this.status = 401, this.statusText = e, this.debugColor = "yellow", this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
|
|
248
|
+
}
|
|
249
|
+
toResponse() {
|
|
250
|
+
let e = {
|
|
251
|
+
headers: { "Content-Type": "application/json" },
|
|
252
|
+
status: this.status,
|
|
253
|
+
statusText: this.statusText
|
|
254
|
+
};
|
|
255
|
+
return new Response(JSON.stringify(this.makeBody()), e);
|
|
256
|
+
}
|
|
257
|
+
toJson() {
|
|
258
|
+
let e = {
|
|
259
|
+
status: this.status,
|
|
260
|
+
statusText: this.statusText
|
|
261
|
+
};
|
|
262
|
+
return Response.json(this.makeBody(), e);
|
|
263
|
+
}
|
|
264
|
+
}, b = class extends d {
|
|
265
|
+
constructor(e) {
|
|
266
|
+
super(), this.name = "UnprocessableEntity", this.status = 422, this.statusText = e.message || "Unprocessable entity", this.debugColor = "yellow", this.cause = {
|
|
267
|
+
data: e.data,
|
|
268
|
+
fieldErrors: e.fieldErrors,
|
|
269
|
+
fields: e.fields
|
|
270
|
+
}, this.onDebug();
|
|
271
|
+
}
|
|
272
|
+
toResponse() {
|
|
273
|
+
let e = {
|
|
274
|
+
headers: { "Content-Type": "application/json" },
|
|
275
|
+
status: this.status,
|
|
276
|
+
statusText: this.statusText
|
|
277
|
+
};
|
|
278
|
+
return new Response(JSON.stringify(this.makeBody()), e);
|
|
279
|
+
}
|
|
280
|
+
toJson() {
|
|
281
|
+
let e = {
|
|
282
|
+
status: this.status,
|
|
283
|
+
statusText: this.statusText
|
|
284
|
+
};
|
|
285
|
+
return Response.json(this.makeBody(), e);
|
|
286
|
+
}
|
|
287
|
+
}, x = class {
|
|
288
|
+
_body = null;
|
|
289
|
+
_name = "SuccessResponse";
|
|
290
|
+
_status = 200;
|
|
291
|
+
_statusText = "OK";
|
|
292
|
+
_debugColor = "green";
|
|
293
|
+
get body() {
|
|
294
|
+
return this._body;
|
|
295
|
+
}
|
|
296
|
+
set body(e) {
|
|
297
|
+
this._body = e ?? null;
|
|
298
|
+
}
|
|
299
|
+
get name() {
|
|
300
|
+
return this._name;
|
|
301
|
+
}
|
|
302
|
+
set name(e) {
|
|
303
|
+
this._name = e;
|
|
304
|
+
}
|
|
305
|
+
get status() {
|
|
306
|
+
return this._status;
|
|
307
|
+
}
|
|
308
|
+
set status(e) {
|
|
309
|
+
this._status = e;
|
|
310
|
+
}
|
|
311
|
+
get statusText() {
|
|
312
|
+
return this._statusText;
|
|
313
|
+
}
|
|
314
|
+
set statusText(e) {
|
|
315
|
+
this._statusText = e;
|
|
316
|
+
}
|
|
317
|
+
get debugColor() {
|
|
318
|
+
return this._debugColor;
|
|
319
|
+
}
|
|
320
|
+
set debugColor(e) {
|
|
321
|
+
[
|
|
322
|
+
"green",
|
|
323
|
+
"yellow",
|
|
324
|
+
"cyan",
|
|
325
|
+
"red"
|
|
326
|
+
].includes(e) && (this._debugColor = e);
|
|
327
|
+
}
|
|
328
|
+
onDebug(e) {
|
|
329
|
+
let t = [], { callerInfo: n, functionName: r } = l.getCaller();
|
|
330
|
+
t.push(`Caller Function: ${r}`), t.push(`Caller Location: ${n}`), this.statusText && t.push(`Message: ${this.statusText}`), e && t.push(`Body: ${JSON.stringify(e)}`), u({
|
|
331
|
+
scheme: this._debugColor,
|
|
332
|
+
name: this.name,
|
|
333
|
+
debugs: t
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
makeBody() {
|
|
337
|
+
return {
|
|
338
|
+
name: this.name,
|
|
339
|
+
message: this.statusText,
|
|
340
|
+
body: this.body
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
}, S = class extends x {
|
|
344
|
+
constructor(e, t) {
|
|
345
|
+
super(), this.name = "Created", this.status = 201, this.statusText = e, this.body = t || void 0, this.onDebug();
|
|
346
|
+
}
|
|
347
|
+
toResponse() {
|
|
348
|
+
let e = {
|
|
349
|
+
headers: { "Content-Type": "application/json" },
|
|
350
|
+
status: this.status,
|
|
351
|
+
statusText: this.statusText
|
|
352
|
+
};
|
|
353
|
+
return new Response(JSON.stringify(this.body), e);
|
|
354
|
+
}
|
|
355
|
+
toJson() {
|
|
356
|
+
let e = {
|
|
357
|
+
status: this.status,
|
|
358
|
+
statusText: this.statusText
|
|
359
|
+
};
|
|
360
|
+
return Response.json(this.body, e);
|
|
361
|
+
}
|
|
362
|
+
}, C = class extends x {
|
|
363
|
+
constructor(e, t) {
|
|
364
|
+
super(), this.name = "Found", this.status = 302, this.statusText = e, this.body = t || void 0, this.onDebug();
|
|
365
|
+
}
|
|
366
|
+
toResponse() {
|
|
367
|
+
let e = {
|
|
368
|
+
headers: { "Content-Type": "application/json" },
|
|
369
|
+
status: this.status,
|
|
370
|
+
statusText: this.statusText
|
|
371
|
+
};
|
|
372
|
+
return new Response(JSON.stringify(this.body), e);
|
|
373
|
+
}
|
|
374
|
+
toJson() {
|
|
375
|
+
let e = {
|
|
376
|
+
status: this.status,
|
|
377
|
+
statusText: this.statusText
|
|
378
|
+
};
|
|
379
|
+
return Response.json(this.body, e);
|
|
380
|
+
}
|
|
381
|
+
}, w = class extends x {
|
|
382
|
+
constructor(e) {
|
|
383
|
+
super(), this.name = "NoContent", this.status = 204, this.statusText = e, this.onDebug();
|
|
384
|
+
}
|
|
385
|
+
toResponse() {
|
|
386
|
+
let e = {
|
|
387
|
+
headers: { "Content-Type": "application/json" },
|
|
388
|
+
status: this.status,
|
|
389
|
+
statusText: this.statusText
|
|
390
|
+
};
|
|
391
|
+
return new Response(null, e);
|
|
392
|
+
}
|
|
393
|
+
}, T = class extends x {
|
|
394
|
+
constructor(e, t) {
|
|
395
|
+
super(), this.name = "Success", this.status = 200, this.statusText = e, this.body = t || void 0, this.onDebug();
|
|
396
|
+
}
|
|
397
|
+
toResponse() {
|
|
398
|
+
let e = {
|
|
399
|
+
headers: { "Content-Type": "application/json" },
|
|
400
|
+
status: this.status,
|
|
401
|
+
statusText: this.statusText
|
|
402
|
+
};
|
|
403
|
+
return new Response(JSON.stringify(this.body), e);
|
|
404
|
+
}
|
|
405
|
+
toJson() {
|
|
406
|
+
let e = {
|
|
407
|
+
status: this.status,
|
|
408
|
+
statusText: this.statusText
|
|
409
|
+
};
|
|
410
|
+
return Response.json(this.body, e);
|
|
411
|
+
}
|
|
412
|
+
}, E = class extends x {
|
|
413
|
+
constructor(e, t) {
|
|
414
|
+
super(), this.name = "Updated", this.status = 200, this.statusText = e, this.body = t || void 0, this.onDebug();
|
|
415
|
+
}
|
|
416
|
+
toResponse() {
|
|
417
|
+
let e = {
|
|
418
|
+
headers: { "Content-Type": "application/json" },
|
|
419
|
+
status: this.status,
|
|
420
|
+
statusText: this.statusText
|
|
421
|
+
};
|
|
422
|
+
return new Response(JSON.stringify(this.body), e);
|
|
423
|
+
}
|
|
424
|
+
toJson() {
|
|
425
|
+
let e = {
|
|
426
|
+
status: this.status,
|
|
427
|
+
statusText: this.statusText
|
|
428
|
+
};
|
|
429
|
+
return Response.json(this.body, e);
|
|
430
|
+
}
|
|
431
|
+
}, D = class {
|
|
432
|
+
static mapHeaders(e) {
|
|
433
|
+
return e instanceof Headers ? Object.fromEntries(e.entries()) : typeof e == "object" ? Object.entries(e).reduce((e, [t, n]) => (typeof n == "string" ? e[t] = n : Array.isArray(n) ? e[t] = n.join(", ") : e[t] = JSON.stringify(n), e), {}) : {};
|
|
434
|
+
}
|
|
435
|
+
static mapQueryParams(e) {
|
|
436
|
+
let t = {};
|
|
437
|
+
return e.forEach((e, n) => t[n] = e), t;
|
|
438
|
+
}
|
|
439
|
+
static handle(e) {
|
|
440
|
+
return {
|
|
441
|
+
rawUrl: e.rawUrl,
|
|
442
|
+
status: e.status,
|
|
443
|
+
method: e.method,
|
|
444
|
+
token: null,
|
|
445
|
+
elapsedTime: e.elapsedTime,
|
|
446
|
+
requestHeaders: this.mapHeaders(e.requestHeaders),
|
|
447
|
+
requestBody: e.requestBody || null,
|
|
448
|
+
queryParams: {
|
|
449
|
+
...this.mapQueryParams(e.queryParams),
|
|
450
|
+
...e.urlParams
|
|
451
|
+
},
|
|
452
|
+
responseHeaders: this.mapHeaders(e.responseHeaders),
|
|
453
|
+
responseBody: e.responseBody || null
|
|
454
|
+
};
|
|
455
|
+
}
|
|
456
|
+
}, O = class {
|
|
457
|
+
static config;
|
|
458
|
+
static setConfig(e) {
|
|
459
|
+
if (this.config) return;
|
|
460
|
+
let { trafficSourceId: t, userToken: n, logBaseApiUrl: r } = e, i = `${r || "http://62.238.8.44:8081"}/ingest-log`;
|
|
461
|
+
this.config = {
|
|
462
|
+
trafficSourceId: t,
|
|
463
|
+
userToken: n,
|
|
464
|
+
apiUrl: i
|
|
465
|
+
};
|
|
466
|
+
}
|
|
467
|
+
static getConfig() {
|
|
468
|
+
return this.config;
|
|
469
|
+
}
|
|
470
|
+
static resetConfig() {
|
|
471
|
+
this.config = void 0;
|
|
472
|
+
}
|
|
473
|
+
};
|
|
474
|
+
//#endregion
|
|
475
|
+
//#region src/http/api/_logRequest.ts
|
|
476
|
+
async function k(e) {
|
|
477
|
+
let n = O.getConfig();
|
|
478
|
+
if (!n) return;
|
|
479
|
+
let { userToken: r, apiUrl: i, trafficSourceId: a } = n, { elapsedTime: o, method: s, queryParams: c, requestBody: l, requestHeaders: d, responseBody: f, responseHeaders: p, status: m, token: h, rawUrl: g } = e;
|
|
480
|
+
if (process.env.NODE_ENV !== "development") try {
|
|
481
|
+
let e = new URL(g), n = "https";
|
|
482
|
+
e.protocol === "http:" && (n = "http");
|
|
483
|
+
let h = JSON.stringify({
|
|
484
|
+
domainUrl: e.protocol + "//" + e.host,
|
|
485
|
+
pathnameUrl: e.pathname,
|
|
486
|
+
trafficSourceId: a,
|
|
487
|
+
status: m,
|
|
488
|
+
protocol: n,
|
|
489
|
+
method: s.toLowerCase(),
|
|
490
|
+
trafficUserId: null,
|
|
491
|
+
elapsedTime: o,
|
|
492
|
+
requestHeaders: JSON.stringify(d),
|
|
493
|
+
requestBody: JSON.stringify(l),
|
|
494
|
+
queryParams: JSON.stringify(c),
|
|
495
|
+
responseHeaders: JSON.stringify(p),
|
|
496
|
+
responseBody: JSON.stringify(f)
|
|
497
|
+
}), _ = {
|
|
498
|
+
"Content-Type": "application/json",
|
|
499
|
+
Authorization: `Bearer ${r}`
|
|
500
|
+
}, v = await fetch(i, {
|
|
501
|
+
method: "POST",
|
|
502
|
+
body: h,
|
|
503
|
+
headers: _
|
|
504
|
+
});
|
|
505
|
+
if (!v.ok) {
|
|
506
|
+
let e = await v.text(), n = v.status, r = v.statusText, i = await v.json().catch(() => null);
|
|
507
|
+
u({
|
|
508
|
+
name: "LogError",
|
|
509
|
+
scheme: "red",
|
|
510
|
+
debugs: [
|
|
511
|
+
"Failed to log request.",
|
|
512
|
+
`Status: ${n} ${r}.`,
|
|
513
|
+
`Status text: ${e}.`,
|
|
514
|
+
`JSON Response: ${i ? t(i, 0) : "No JSON response"}`
|
|
515
|
+
]
|
|
516
|
+
});
|
|
517
|
+
}
|
|
518
|
+
} catch (e) {
|
|
519
|
+
u({
|
|
520
|
+
debugs: [`Error sending request: ${e}`],
|
|
521
|
+
name: "LogError",
|
|
522
|
+
scheme: "red"
|
|
523
|
+
});
|
|
524
|
+
}
|
|
765
525
|
}
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
526
|
+
//#endregion
|
|
527
|
+
//#region src/http/api/_makeRequest.ts
|
|
528
|
+
async function A(e) {
|
|
529
|
+
let t = e.url;
|
|
530
|
+
e.urlParams && Object.entries(e.urlParams).forEach(([e, n]) => {
|
|
531
|
+
t = t.replaceAll(`:${e}`, n);
|
|
532
|
+
});
|
|
533
|
+
let n = {
|
|
534
|
+
POST: "Resource created successfully",
|
|
535
|
+
PUT: "Resource updated successfully",
|
|
536
|
+
DELETE: "Resource deleted successfully",
|
|
537
|
+
PATCH: "Resource patched successfully",
|
|
538
|
+
GET: "Request successful"
|
|
539
|
+
};
|
|
540
|
+
try {
|
|
541
|
+
let r = performance.now(), i = {
|
|
542
|
+
...e.headers,
|
|
543
|
+
"Content-Type": "application/json"
|
|
544
|
+
}, a = await fetch(t, {
|
|
545
|
+
headers: i,
|
|
546
|
+
method: e.method,
|
|
547
|
+
body: e.body ? JSON.stringify(e.body) : void 0
|
|
548
|
+
}), o = performance.now() - r, s = a.status, c = null;
|
|
549
|
+
try {
|
|
550
|
+
c = await a.json();
|
|
551
|
+
} catch {
|
|
552
|
+
c = null;
|
|
553
|
+
}
|
|
554
|
+
return k(D.handle({
|
|
555
|
+
elapsedTime: o,
|
|
556
|
+
method: e.method,
|
|
557
|
+
queryParams: new URL(t).searchParams,
|
|
558
|
+
requestHeaders: i,
|
|
559
|
+
requestBody: e.body,
|
|
560
|
+
responseBody: c,
|
|
561
|
+
responseHeaders: a.headers,
|
|
562
|
+
status: s,
|
|
563
|
+
rawUrl: e.url,
|
|
564
|
+
urlParams: e.urlParams
|
|
565
|
+
})), a.ok ? {
|
|
566
|
+
success: !0,
|
|
567
|
+
status: s,
|
|
568
|
+
message: c?.message || n[e.method],
|
|
569
|
+
response: c,
|
|
570
|
+
cause: null
|
|
571
|
+
} : {
|
|
572
|
+
success: !1,
|
|
573
|
+
status: s,
|
|
574
|
+
message: c?.message || a.statusText || "Request failed",
|
|
575
|
+
response: c,
|
|
576
|
+
cause: null
|
|
577
|
+
};
|
|
578
|
+
} catch (e) {
|
|
579
|
+
return u({
|
|
580
|
+
debugs: [`Network error or request failed: ${e}`],
|
|
581
|
+
name: "MakeRequestError",
|
|
582
|
+
scheme: "red"
|
|
583
|
+
}), {
|
|
584
|
+
success: !1,
|
|
585
|
+
status: 0,
|
|
586
|
+
message: "Network error or request failed",
|
|
587
|
+
response: null,
|
|
588
|
+
cause: e instanceof Error ? e.message : String(e)
|
|
589
|
+
};
|
|
590
|
+
}
|
|
773
591
|
}
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
592
|
+
//#endregion
|
|
593
|
+
//#region src/http/api/deleteRequest.ts
|
|
594
|
+
async function j(e) {
|
|
595
|
+
return A({
|
|
596
|
+
method: "DELETE",
|
|
597
|
+
url: e.url,
|
|
598
|
+
urlParams: e.urlParams,
|
|
599
|
+
headers: e.headers,
|
|
600
|
+
body: e.body
|
|
601
|
+
});
|
|
782
602
|
}
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
|
|
603
|
+
//#endregion
|
|
604
|
+
//#region src/http/api/getRequest.ts
|
|
605
|
+
async function M(e) {
|
|
606
|
+
return A({
|
|
607
|
+
method: "GET",
|
|
608
|
+
url: e.url,
|
|
609
|
+
urlParams: e.urlParams,
|
|
610
|
+
headers: e.headers
|
|
611
|
+
});
|
|
791
612
|
}
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
798
|
-
|
|
799
|
-
|
|
613
|
+
//#endregion
|
|
614
|
+
//#region src/http/api/patchRequest.ts
|
|
615
|
+
async function ee(e) {
|
|
616
|
+
return A({
|
|
617
|
+
method: "PATCH",
|
|
618
|
+
url: e.url,
|
|
619
|
+
urlParams: e.urlParams,
|
|
620
|
+
headers: e.headers,
|
|
621
|
+
body: e.body
|
|
622
|
+
});
|
|
800
623
|
}
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
const r = [], o = (a) => JSON.stringify(a, null, 2);
|
|
812
|
-
r.push(`Base URL: ${this.baseUrl}`), r.push(`Endpoint: ${s}`), r.push(`Status/Method: ${t} => ${n.status}`), r.push(`Message: ${n.message}`), n.headers && r.push(`Headers: ${o(n.headers)}`), n.body && r.push(`Body: ${o(n.body)}`), g({ debugs: r, name: "ApiDebug", scheme: "yellow" });
|
|
813
|
-
}
|
|
814
|
-
}
|
|
815
|
-
generateHeaders(s, t) {
|
|
816
|
-
let n = {};
|
|
817
|
-
return this.baseToken && (n = { Authorization: `Bearer ${this.baseToken}` }), this.baseHeaders && (n = { ...n, ...this.baseHeaders }), s && (n = { ...n, ...s }), t && (n = { ...n, Authorization: `Bearer ${t}` }), n;
|
|
818
|
-
}
|
|
819
|
-
/**
|
|
820
|
-
* Sends a get request to the specified endpoint.
|
|
821
|
-
* @param endpoint - The API endpoint to send the get request to.
|
|
822
|
-
* @param data - The request data, including optional headers and token.
|
|
823
|
-
* @returns The API response data.
|
|
824
|
-
*/
|
|
825
|
-
async get(s, t) {
|
|
826
|
-
const n = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token), r = await is({
|
|
827
|
-
url: this.baseUrl + s,
|
|
828
|
-
urlParams: (t == null ? void 0 : t.urlParams) || {},
|
|
829
|
-
headers: n
|
|
830
|
-
});
|
|
831
|
-
return this.onDebug(s, "get", {
|
|
832
|
-
headers: n,
|
|
833
|
-
message: r.message,
|
|
834
|
-
status: r.status
|
|
835
|
-
}), r;
|
|
836
|
-
}
|
|
837
|
-
/**
|
|
838
|
-
* Sends a post request to the specified endpoint.
|
|
839
|
-
* @param endpoint - The API endpoint to send the post request to.
|
|
840
|
-
* @param data - The request data, including body, optional headers, and token.
|
|
841
|
-
* @returns The API response data.
|
|
842
|
-
*/
|
|
843
|
-
async post(s, t) {
|
|
844
|
-
const n = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token), r = t == null ? void 0 : t.body, o = await us({
|
|
845
|
-
url: this.baseUrl + s,
|
|
846
|
-
urlParams: (t == null ? void 0 : t.urlParams) || {},
|
|
847
|
-
headers: n,
|
|
848
|
-
body: r
|
|
849
|
-
});
|
|
850
|
-
return this.onDebug(s, "post", {
|
|
851
|
-
headers: n,
|
|
852
|
-
body: r,
|
|
853
|
-
message: o.message,
|
|
854
|
-
status: o.status
|
|
855
|
-
}), o;
|
|
856
|
-
}
|
|
857
|
-
/**
|
|
858
|
-
* Sends a put request to the specified endpoint.
|
|
859
|
-
* @param endpoint - The API endpoint to send the put request to.
|
|
860
|
-
* @param data - The request data, including body, optional headers, and token.
|
|
861
|
-
* @returns The API response data.
|
|
862
|
-
*/
|
|
863
|
-
async put(s, t) {
|
|
864
|
-
const n = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token), r = t == null ? void 0 : t.body, o = await cs({
|
|
865
|
-
url: this.baseUrl + s,
|
|
866
|
-
urlParams: (t == null ? void 0 : t.urlParams) || {},
|
|
867
|
-
headers: n,
|
|
868
|
-
body: r
|
|
869
|
-
});
|
|
870
|
-
return this.onDebug(s, "put", {
|
|
871
|
-
headers: n,
|
|
872
|
-
body: r,
|
|
873
|
-
message: o.message,
|
|
874
|
-
status: o.status
|
|
875
|
-
}), o;
|
|
876
|
-
}
|
|
877
|
-
/**
|
|
878
|
-
* Sends a patch request to the specified endpoint.
|
|
879
|
-
* @param endpoint - The API endpoint to send the patch request to.
|
|
880
|
-
* @param data - The request data, including body, optional headers, and token.
|
|
881
|
-
* @returns The API response data.
|
|
882
|
-
*/
|
|
883
|
-
async patch(s, t) {
|
|
884
|
-
const n = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token), r = t == null ? void 0 : t.body, o = await as({
|
|
885
|
-
url: this.baseUrl + s,
|
|
886
|
-
urlParams: (t == null ? void 0 : t.urlParams) || {},
|
|
887
|
-
headers: n,
|
|
888
|
-
body: r
|
|
889
|
-
});
|
|
890
|
-
return this.onDebug(s, "patch", {
|
|
891
|
-
headers: n,
|
|
892
|
-
body: r,
|
|
893
|
-
message: o.message,
|
|
894
|
-
status: o.status
|
|
895
|
-
}), o;
|
|
896
|
-
}
|
|
897
|
-
/**
|
|
898
|
-
* Sends a delete request to the specified endpoint.
|
|
899
|
-
* @param endpoint - The API endpoint to send the delete request to.
|
|
900
|
-
* @param data - The request data, including body, optional headers, and token.
|
|
901
|
-
* @returns The API response data.
|
|
902
|
-
*/
|
|
903
|
-
async delete(s, t) {
|
|
904
|
-
const n = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token), r = t == null ? void 0 : t.body, o = await os({
|
|
905
|
-
url: this.baseUrl + s,
|
|
906
|
-
urlParams: (t == null ? void 0 : t.urlParams) || {},
|
|
907
|
-
headers: n,
|
|
908
|
-
body: r
|
|
909
|
-
});
|
|
910
|
-
return this.onDebug(s, "delete", {
|
|
911
|
-
headers: n,
|
|
912
|
-
body: r,
|
|
913
|
-
message: o.message,
|
|
914
|
-
status: o.status
|
|
915
|
-
}), o;
|
|
916
|
-
}
|
|
624
|
+
//#endregion
|
|
625
|
+
//#region src/http/api/postRequest.ts
|
|
626
|
+
async function N(e) {
|
|
627
|
+
return A({
|
|
628
|
+
method: "POST",
|
|
629
|
+
url: e.url,
|
|
630
|
+
urlParams: e.urlParams,
|
|
631
|
+
headers: e.headers,
|
|
632
|
+
body: e.body
|
|
633
|
+
});
|
|
917
634
|
}
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
} else
|
|
929
|
-
throw new T("Invalid URLSearchParams format");
|
|
930
|
-
} catch {
|
|
931
|
-
throw new T("Failed to extract data from request");
|
|
932
|
-
}
|
|
933
|
-
}
|
|
934
|
-
return s;
|
|
635
|
+
//#endregion
|
|
636
|
+
//#region src/http/api/putRequest.ts
|
|
637
|
+
async function P(e) {
|
|
638
|
+
return A({
|
|
639
|
+
method: "PUT",
|
|
640
|
+
url: e.url,
|
|
641
|
+
urlParams: e.urlParams,
|
|
642
|
+
headers: e.headers,
|
|
643
|
+
body: e.body
|
|
644
|
+
});
|
|
935
645
|
}
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
646
|
+
//#endregion
|
|
647
|
+
//#region src/services/apiService.ts
|
|
648
|
+
var F = class {
|
|
649
|
+
baseUrl;
|
|
650
|
+
baseHeaders;
|
|
651
|
+
baseToken;
|
|
652
|
+
enableDebug;
|
|
653
|
+
constructor(e) {
|
|
654
|
+
this.baseUrl = e.baseUrl, this.baseHeaders = e.baseHeaders || void 0, this.baseToken = e.baseToken || void 0, this.enableDebug = e.enableDebug || !1;
|
|
655
|
+
}
|
|
656
|
+
onDebug(e, t, n) {
|
|
657
|
+
if (this.enableDebug) {
|
|
658
|
+
let r = [], i = (e) => JSON.stringify(e, null, 2);
|
|
659
|
+
r.push(`Base URL: ${this.baseUrl}`), r.push(`Endpoint: ${e}`), r.push(`Status/Method: ${t} => ${n.status}`), r.push(`Message: ${n.message}`), n.headers && r.push(`Headers: ${i(n.headers)}`), n.body && r.push(`Body: ${i(n.body)}`), u({
|
|
660
|
+
debugs: r,
|
|
661
|
+
name: "ApiDebug",
|
|
662
|
+
scheme: "yellow"
|
|
663
|
+
});
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
generateHeaders(e, t) {
|
|
667
|
+
let n = {};
|
|
668
|
+
return this.baseToken && (n = { Authorization: `Bearer ${this.baseToken}` }), this.baseHeaders && (n = {
|
|
669
|
+
...n,
|
|
670
|
+
...this.baseHeaders
|
|
671
|
+
}), e && (n = {
|
|
672
|
+
...n,
|
|
673
|
+
...e
|
|
674
|
+
}), t && (n = {
|
|
675
|
+
...n,
|
|
676
|
+
Authorization: `Bearer ${t}`
|
|
677
|
+
}), n;
|
|
678
|
+
}
|
|
679
|
+
async get(e, t) {
|
|
680
|
+
let n = this.generateHeaders(t?.headers || {}, t?.token), r = await M({
|
|
681
|
+
url: this.baseUrl + e,
|
|
682
|
+
urlParams: t?.urlParams || {},
|
|
683
|
+
headers: n
|
|
684
|
+
});
|
|
685
|
+
return this.onDebug(e, "get", {
|
|
686
|
+
headers: n,
|
|
687
|
+
message: r.message,
|
|
688
|
+
status: r.status
|
|
689
|
+
}), r;
|
|
690
|
+
}
|
|
691
|
+
async post(e, t) {
|
|
692
|
+
let n = this.generateHeaders(t?.headers || {}, t?.token), r = t?.body, i = await N({
|
|
693
|
+
url: this.baseUrl + e,
|
|
694
|
+
urlParams: t?.urlParams || {},
|
|
695
|
+
headers: n,
|
|
696
|
+
body: r
|
|
697
|
+
});
|
|
698
|
+
return this.onDebug(e, "post", {
|
|
699
|
+
headers: n,
|
|
700
|
+
body: r,
|
|
701
|
+
message: i.message,
|
|
702
|
+
status: i.status
|
|
703
|
+
}), i;
|
|
704
|
+
}
|
|
705
|
+
async put(e, t) {
|
|
706
|
+
let n = this.generateHeaders(t?.headers || {}, t?.token), r = t?.body, i = await P({
|
|
707
|
+
url: this.baseUrl + e,
|
|
708
|
+
urlParams: t?.urlParams || {},
|
|
709
|
+
headers: n,
|
|
710
|
+
body: r
|
|
711
|
+
});
|
|
712
|
+
return this.onDebug(e, "put", {
|
|
713
|
+
headers: n,
|
|
714
|
+
body: r,
|
|
715
|
+
message: i.message,
|
|
716
|
+
status: i.status
|
|
717
|
+
}), i;
|
|
718
|
+
}
|
|
719
|
+
async patch(e, t) {
|
|
720
|
+
let n = this.generateHeaders(t?.headers || {}, t?.token), r = t?.body, i = await ee({
|
|
721
|
+
url: this.baseUrl + e,
|
|
722
|
+
urlParams: t?.urlParams || {},
|
|
723
|
+
headers: n,
|
|
724
|
+
body: r
|
|
725
|
+
});
|
|
726
|
+
return this.onDebug(e, "patch", {
|
|
727
|
+
headers: n,
|
|
728
|
+
body: r,
|
|
729
|
+
message: i.message,
|
|
730
|
+
status: i.status
|
|
731
|
+
}), i;
|
|
732
|
+
}
|
|
733
|
+
async delete(e, t) {
|
|
734
|
+
let n = this.generateHeaders(t?.headers || {}, t?.token), r = t?.body, i = await j({
|
|
735
|
+
url: this.baseUrl + e,
|
|
736
|
+
urlParams: t?.urlParams || {},
|
|
737
|
+
headers: n,
|
|
738
|
+
body: r
|
|
739
|
+
});
|
|
740
|
+
return this.onDebug(e, "delete", {
|
|
741
|
+
headers: n,
|
|
742
|
+
body: r,
|
|
743
|
+
message: i.message,
|
|
744
|
+
status: i.status
|
|
745
|
+
}), i;
|
|
746
|
+
}
|
|
747
|
+
};
|
|
748
|
+
//#endregion
|
|
749
|
+
//#region src/utilities/decodeRequestBody.ts
|
|
750
|
+
async function I(e) {
|
|
751
|
+
let t, n = await e.arrayBuffer(), r = new TextDecoder().decode(n);
|
|
752
|
+
try {
|
|
753
|
+
t = JSON.parse(r);
|
|
754
|
+
} catch {
|
|
755
|
+
try {
|
|
756
|
+
if (r.includes("=")) {
|
|
757
|
+
let e = new URLSearchParams(r);
|
|
758
|
+
t = Object.fromEntries(e.entries());
|
|
759
|
+
} else throw new p("Invalid URLSearchParams format");
|
|
760
|
+
} catch {
|
|
761
|
+
throw new p("Failed to extract data from request");
|
|
762
|
+
}
|
|
763
|
+
}
|
|
764
|
+
return t;
|
|
939
765
|
}
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
case e instanceof X:
|
|
945
|
-
return e.toResponse();
|
|
946
|
-
case e instanceof Q:
|
|
947
|
-
return e.toResponse();
|
|
948
|
-
case e instanceof ts:
|
|
949
|
-
return e.toResponse();
|
|
950
|
-
case e instanceof es:
|
|
951
|
-
return e.toResponse();
|
|
952
|
-
case e instanceof ss:
|
|
953
|
-
return e.toResponse();
|
|
954
|
-
}
|
|
955
|
-
switch (!0) {
|
|
956
|
-
case e instanceof V:
|
|
957
|
-
return e.toResponse();
|
|
958
|
-
case e instanceof T:
|
|
959
|
-
return e.toResponse();
|
|
960
|
-
case e instanceof Z:
|
|
961
|
-
return e.toResponse();
|
|
962
|
-
case e instanceof G:
|
|
963
|
-
return e.toResponse();
|
|
964
|
-
case e instanceof W:
|
|
965
|
-
return e.toResponse();
|
|
966
|
-
case e instanceof Y:
|
|
967
|
-
return e.toResponse();
|
|
968
|
-
case e instanceof x:
|
|
969
|
-
return e.toResponse();
|
|
970
|
-
case e instanceof K:
|
|
971
|
-
return e.toResponse();
|
|
972
|
-
case e instanceof R:
|
|
973
|
-
return e.toResponse();
|
|
974
|
-
}
|
|
975
|
-
return new x("Server error", e).toResponse();
|
|
766
|
+
//#endregion
|
|
767
|
+
//#region src/utilities/decodeRequestErrorMessage.ts
|
|
768
|
+
function L(e, t) {
|
|
769
|
+
return e?.message && typeof e?.message == "string" ? e?.message : e?.operator_erro_message && typeof e?.operator_erro_message == "string" ? e?.operator_erro_message : e?.error && typeof e?.error == "string" ? e?.error : e?.error?.message && typeof e?.error?.message == "string" ? e?.error?.message : t?.statusText && typeof t?.statusText == "string" ? t?.statusText : "Missing error message";
|
|
976
770
|
}
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
771
|
+
//#endregion
|
|
772
|
+
//#region src/utilities/errorHandler.ts
|
|
773
|
+
function R(e) {
|
|
774
|
+
switch (!0) {
|
|
775
|
+
case e instanceof Response: return e;
|
|
776
|
+
case e instanceof C: return e.toResponse();
|
|
777
|
+
case e instanceof S: return e.toResponse();
|
|
778
|
+
case e instanceof E: return e.toResponse();
|
|
779
|
+
case e instanceof T: return e.toResponse();
|
|
780
|
+
case e instanceof w: return e.toResponse();
|
|
781
|
+
}
|
|
782
|
+
switch (!0) {
|
|
783
|
+
case e instanceof f: return e.toResponse();
|
|
784
|
+
case e instanceof p: return e.toResponse();
|
|
785
|
+
case e instanceof m: return e.toResponse();
|
|
786
|
+
case e instanceof h: return e.toResponse();
|
|
787
|
+
case e instanceof g: return e.toResponse();
|
|
788
|
+
case e instanceof _: return e.toResponse();
|
|
789
|
+
case e instanceof v: return e.toResponse();
|
|
790
|
+
case e instanceof y: return e.toResponse();
|
|
791
|
+
case e instanceof b: return e.toResponse();
|
|
792
|
+
}
|
|
793
|
+
return new v("Server error", e).toResponse();
|
|
992
794
|
}
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
success: !0,
|
|
1006
|
-
data: t.data
|
|
1007
|
-
};
|
|
795
|
+
//#endregion
|
|
796
|
+
//#region src/utilities/formAsyncParse.ts
|
|
797
|
+
async function z([e, t]) {
|
|
798
|
+
let n = await t.safeParseAsync(e);
|
|
799
|
+
return n.success === !1 ? {
|
|
800
|
+
success: !1,
|
|
801
|
+
fieldErrors: Object.fromEntries(n.error.issues.map((e) => [e.path.join("."), e.message])),
|
|
802
|
+
fields: e
|
|
803
|
+
} : {
|
|
804
|
+
success: !0,
|
|
805
|
+
data: n.data
|
|
806
|
+
};
|
|
1008
807
|
}
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
808
|
+
//#endregion
|
|
809
|
+
//#region src/utilities/formParse.ts
|
|
810
|
+
function B([e, t]) {
|
|
811
|
+
let n = t.safeParse(e);
|
|
812
|
+
return n.success === !1 ? {
|
|
813
|
+
success: !1,
|
|
814
|
+
fieldErrors: Object.fromEntries(n.error.issues.map((e) => [e.path.join("."), e.message])),
|
|
815
|
+
fields: e
|
|
816
|
+
} : {
|
|
817
|
+
success: !0,
|
|
818
|
+
data: n.data
|
|
819
|
+
};
|
|
1016
820
|
}
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
821
|
+
//#endregion
|
|
822
|
+
//#region src/utilities/getScopedParams.ts
|
|
823
|
+
function V(e, t = "") {
|
|
824
|
+
let n = new URL(e.url);
|
|
825
|
+
if (t === "") return n.searchParams;
|
|
826
|
+
let r = Array.from(n.searchParams.entries()).filter(([e]) => e.startsWith(`${t}:`)).map(([e, n]) => [e.replace(`${t}:`, ""), n]);
|
|
827
|
+
return new URLSearchParams(r);
|
|
1023
828
|
}
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
constructor(s) {
|
|
1029
|
-
this.schema = s;
|
|
1030
|
-
}
|
|
1031
|
-
/**
|
|
1032
|
-
* Returns `true` if the data satisfies the schema, `false` otherwise. Never throws.
|
|
1033
|
-
*
|
|
1034
|
-
* @param data - The value to check.
|
|
1035
|
-
*/
|
|
1036
|
-
isValid(s) {
|
|
1037
|
-
return this.schema.safeParse(s).success;
|
|
1038
|
-
}
|
|
1039
|
-
/**
|
|
1040
|
-
* Validates data and returns the raw Zod `safeParseResult` without throwing.
|
|
1041
|
-
* Useful when you need access to the full error details.
|
|
1042
|
-
*
|
|
1043
|
-
* @param data - The value to validate.
|
|
1044
|
-
*/
|
|
1045
|
-
safeValidate(s) {
|
|
1046
|
-
return this.schema.safeParse(s);
|
|
1047
|
-
}
|
|
1048
|
-
/**
|
|
1049
|
-
* Validates data and returns the typed result, throwing `ServerError` on failure.
|
|
1050
|
-
* Use for validating internal/trusted data (e.g. env vars, config objects).
|
|
1051
|
-
*
|
|
1052
|
-
* @param data - The value to validate.
|
|
1053
|
-
* @throws `ServerError` with a formatted field-by-field error message.
|
|
1054
|
-
*/
|
|
1055
|
-
validate(s) {
|
|
1056
|
-
try {
|
|
1057
|
-
return this.schema.parse(s);
|
|
1058
|
-
} catch (t) {
|
|
1059
|
-
throw new x(fs(t));
|
|
1060
|
-
}
|
|
1061
|
-
}
|
|
1062
|
-
/**
|
|
1063
|
-
* Validates form data and returns the typed result, throwing `UnprocessableEntity` on failure.
|
|
1064
|
-
* The error includes `fieldErrors`, `fields`, and `data.scrollTo` (first failing field name).
|
|
1065
|
-
*
|
|
1066
|
-
* @param data - The raw form data to validate.
|
|
1067
|
-
* @param message - Optional human-readable error message for the 422 response.
|
|
1068
|
-
* @throws `UnprocessableEntity` with structured field errors for client-side form handling.
|
|
1069
|
-
*
|
|
1070
|
-
* @example
|
|
1071
|
-
* ```typescript
|
|
1072
|
-
* const validator = new SchemaValidator(registerSchema);
|
|
1073
|
-
* const body = validator.formValidate(await decodeRequestBody(request));
|
|
1074
|
-
* ```
|
|
1075
|
-
*/
|
|
1076
|
-
formValidate(s, t) {
|
|
1077
|
-
const n = hs([s, this.schema]);
|
|
1078
|
-
if ("fieldErrors" in n) {
|
|
1079
|
-
const r = Object.keys(n.fieldErrors)[0];
|
|
1080
|
-
throw new R({
|
|
1081
|
-
fields: n.fields,
|
|
1082
|
-
fieldErrors: n.fieldErrors,
|
|
1083
|
-
data: { scrollTo: r },
|
|
1084
|
-
message: t
|
|
1085
|
-
});
|
|
1086
|
-
}
|
|
1087
|
-
return n.data;
|
|
1088
|
-
}
|
|
1089
|
-
/**
|
|
1090
|
-
* Async version of `formValidate` for schemas with async refinements (e.g. uniqueness checks).
|
|
1091
|
-
*
|
|
1092
|
-
* @param data - The raw form data to validate.
|
|
1093
|
-
* @param message - Optional human-readable error message for the 422 response.
|
|
1094
|
-
* @throws `UnprocessableEntity` with structured field errors for client-side form handling.
|
|
1095
|
-
*
|
|
1096
|
-
* @example
|
|
1097
|
-
* ```typescript
|
|
1098
|
-
* const validator = new SchemaValidator(registerSchema);
|
|
1099
|
-
* const body = await validator.formAsyncValidate(await decodeRequestBody(request));
|
|
1100
|
-
* ```
|
|
1101
|
-
*/
|
|
1102
|
-
async formAsyncValidate(s, t) {
|
|
1103
|
-
const n = await ls([s, this.schema]);
|
|
1104
|
-
if ("fieldErrors" in n) {
|
|
1105
|
-
const r = Object.keys(n.fieldErrors)[0];
|
|
1106
|
-
throw new R({
|
|
1107
|
-
fields: n.fields,
|
|
1108
|
-
fieldErrors: n.fieldErrors,
|
|
1109
|
-
data: { scrollTo: r },
|
|
1110
|
-
message: t
|
|
1111
|
-
});
|
|
1112
|
-
}
|
|
1113
|
-
return n.data;
|
|
1114
|
-
}
|
|
829
|
+
//#endregion
|
|
830
|
+
//#region src/utilities/schemaValidator.ts
|
|
831
|
+
function H(e) {
|
|
832
|
+
return ["Error validating:", ...e.issues.map(({ path: e, message: t }) => `-> ${e.join(".")}: ${t}`)].join("\n");
|
|
1115
833
|
}
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
834
|
+
var U = class {
|
|
835
|
+
schema;
|
|
836
|
+
constructor(e) {
|
|
837
|
+
this.schema = e;
|
|
838
|
+
}
|
|
839
|
+
isValid(e) {
|
|
840
|
+
return this.schema.safeParse(e).success;
|
|
841
|
+
}
|
|
842
|
+
safeValidate(e) {
|
|
843
|
+
return this.schema.safeParse(e);
|
|
844
|
+
}
|
|
845
|
+
validate(e) {
|
|
846
|
+
try {
|
|
847
|
+
return this.schema.parse(e);
|
|
848
|
+
} catch (e) {
|
|
849
|
+
throw new v(H(e));
|
|
850
|
+
}
|
|
851
|
+
}
|
|
852
|
+
formValidate(e, t) {
|
|
853
|
+
let n = B([e, this.schema]);
|
|
854
|
+
if ("fieldErrors" in n) {
|
|
855
|
+
let e = Object.keys(n.fieldErrors)[0];
|
|
856
|
+
throw new b({
|
|
857
|
+
fields: n.fields,
|
|
858
|
+
fieldErrors: n.fieldErrors,
|
|
859
|
+
data: { scrollTo: e },
|
|
860
|
+
message: t
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
return n.data;
|
|
864
|
+
}
|
|
865
|
+
async formAsyncValidate(e, t) {
|
|
866
|
+
let n = await z([e, this.schema]);
|
|
867
|
+
if ("fieldErrors" in n) {
|
|
868
|
+
let e = Object.keys(n.fieldErrors)[0];
|
|
869
|
+
throw new b({
|
|
870
|
+
fields: n.fields,
|
|
871
|
+
fieldErrors: n.fieldErrors,
|
|
872
|
+
data: { scrollTo: e },
|
|
873
|
+
message: t
|
|
874
|
+
});
|
|
875
|
+
}
|
|
876
|
+
return n.data;
|
|
877
|
+
}
|
|
878
|
+
};
|
|
879
|
+
//#endregion
|
|
880
|
+
//#region src/validations/validateCep.ts
|
|
881
|
+
function W(e) {
|
|
882
|
+
if (!(/^\d{5}-\d{3}$/.test(e) || /^\d{8}$/.test(e))) return !1;
|
|
883
|
+
let t = r(e), n = /^\d{8}$/.test(t);
|
|
884
|
+
return t.length === 8 && n;
|
|
1120
885
|
}
|
|
1121
|
-
|
|
1122
|
-
|
|
886
|
+
//#endregion
|
|
887
|
+
//#region src/validations/validateCnpj.ts
|
|
888
|
+
function G(e) {
|
|
889
|
+
return e.length !== 14;
|
|
1123
890
|
}
|
|
1124
|
-
function
|
|
1125
|
-
|
|
1126
|
-
|
|
891
|
+
function K(e) {
|
|
892
|
+
let [t] = e;
|
|
893
|
+
return [...e].every((e) => e === t);
|
|
1127
894
|
}
|
|
1128
|
-
function
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
return n < 2 ? 0 : 11 - n;
|
|
895
|
+
function q(e, t) {
|
|
896
|
+
let n = 0;
|
|
897
|
+
for (let r = 0; r < t.length; r++) n += parseInt(e[r]) * t[r];
|
|
898
|
+
let r = n % 11;
|
|
899
|
+
return r < 2 ? 0 : 11 - r;
|
|
1134
900
|
}
|
|
1135
|
-
function
|
|
1136
|
-
|
|
901
|
+
function J(e) {
|
|
902
|
+
return e.slice(12);
|
|
1137
903
|
}
|
|
1138
|
-
function
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
904
|
+
function te(e) {
|
|
905
|
+
if (!e || e.length > 18 || e.length < 14 || /\s/.test(e)) return !1;
|
|
906
|
+
let t = r(e);
|
|
907
|
+
if (G(t) || K(t)) return !1;
|
|
908
|
+
let n = t.slice(0, 12), i = q(n, [
|
|
909
|
+
5,
|
|
910
|
+
4,
|
|
911
|
+
3,
|
|
912
|
+
2,
|
|
913
|
+
9,
|
|
914
|
+
8,
|
|
915
|
+
7,
|
|
916
|
+
6,
|
|
917
|
+
5,
|
|
918
|
+
4,
|
|
919
|
+
3,
|
|
920
|
+
2
|
|
921
|
+
]), a = q(n + i, [
|
|
922
|
+
6,
|
|
923
|
+
5,
|
|
924
|
+
4,
|
|
925
|
+
3,
|
|
926
|
+
2,
|
|
927
|
+
9,
|
|
928
|
+
8,
|
|
929
|
+
7,
|
|
930
|
+
6,
|
|
931
|
+
5,
|
|
932
|
+
4,
|
|
933
|
+
3,
|
|
934
|
+
2
|
|
935
|
+
]);
|
|
936
|
+
return J(t) === `${i}${a}`;
|
|
1147
937
|
}
|
|
1148
|
-
|
|
1149
|
-
|
|
938
|
+
//#endregion
|
|
939
|
+
//#region src/validations/validateCpf.ts
|
|
940
|
+
function Y(e) {
|
|
941
|
+
return e.length !== 11;
|
|
1150
942
|
}
|
|
1151
|
-
function
|
|
1152
|
-
|
|
1153
|
-
|
|
943
|
+
function X(e) {
|
|
944
|
+
let [t] = e;
|
|
945
|
+
return [...e].every((e) => e === t);
|
|
1154
946
|
}
|
|
1155
|
-
function
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
return n < 2 ? 0 : 11 - n;
|
|
947
|
+
function Z(e, t) {
|
|
948
|
+
let n = 0;
|
|
949
|
+
for (let r of e) t > 1 && (n += parseInt(r) * t--);
|
|
950
|
+
let r = n % 11;
|
|
951
|
+
return r < 2 ? 0 : 11 - r;
|
|
1161
952
|
}
|
|
1162
|
-
function
|
|
1163
|
-
|
|
953
|
+
function Q(e) {
|
|
954
|
+
return e.slice(9);
|
|
1164
955
|
}
|
|
1165
|
-
function
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
956
|
+
function ne(e) {
|
|
957
|
+
if (!e || e.length > 14 || e.length < 11 || /\s/.test(e)) return !1;
|
|
958
|
+
let t = r(e);
|
|
959
|
+
if (Y(t) || X(t)) return !1;
|
|
960
|
+
let n = Z(t, 10), i = Z(t, 11);
|
|
961
|
+
return Q(t) === `${n}${i}`;
|
|
1171
962
|
}
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
963
|
+
//#endregion
|
|
964
|
+
//#region src/validations/validateDate.ts
|
|
965
|
+
function re(t, n) {
|
|
966
|
+
let r = n?.inputFormat || "brazilianDate", i = n?.minYear || 1900, a = n?.maxYear || 3e3, o = new e();
|
|
967
|
+
o.validateInputFormat(r);
|
|
968
|
+
let s, c, l, u = t.split(/[-/]/).map(Number);
|
|
969
|
+
if (u.length !== 3) return !1;
|
|
970
|
+
try {
|
|
971
|
+
switch (r) {
|
|
972
|
+
case "brazilianDate":
|
|
973
|
+
[s, c, l] = u, o.validateDateParts(l, c, s);
|
|
974
|
+
break;
|
|
975
|
+
case "isoDate":
|
|
976
|
+
[c, s, l] = u, o.validateDateParts(l, c, s);
|
|
977
|
+
break;
|
|
978
|
+
case "timestamp":
|
|
979
|
+
[l, c, s] = u, o.validateDateParts(l, c, s);
|
|
980
|
+
break;
|
|
981
|
+
}
|
|
982
|
+
return !(l < i || l > a);
|
|
983
|
+
} catch {
|
|
984
|
+
return !1;
|
|
985
|
+
}
|
|
1194
986
|
}
|
|
1195
|
-
|
|
1196
|
-
|
|
987
|
+
//#endregion
|
|
988
|
+
//#region src/validations/validateEmail.ts
|
|
989
|
+
function ie(e) {
|
|
990
|
+
return /^[a-zA-Z0-9.!$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*$/.test(e);
|
|
1197
991
|
}
|
|
1198
|
-
function
|
|
1199
|
-
|
|
992
|
+
function ae(e) {
|
|
993
|
+
return !(e.length === 0 || e.length > 64 || e.startsWith(".") || e.endsWith(".") || e.includes("..") || !/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+$/.test(e));
|
|
1200
994
|
}
|
|
1201
|
-
function
|
|
1202
|
-
|
|
995
|
+
function oe(e) {
|
|
996
|
+
return !(e.length === 0 || e.length > 63 || e.startsWith("-") || e.endsWith("-") || !/^[a-zA-Z0-9-]+$/.test(e));
|
|
1203
997
|
}
|
|
1204
|
-
function
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
return !(t.length < 2 || !/^[a-zA-Z]+$/.test(t));
|
|
998
|
+
function $(e) {
|
|
999
|
+
if (e.length === 0 || e.length > 253 || e.startsWith(".") || e.endsWith(".") || e.startsWith("-") || e.endsWith("-")) return !1;
|
|
1000
|
+
let t = e.split(".");
|
|
1001
|
+
if (t.length < 2) return !1;
|
|
1002
|
+
for (let e of t) if (!oe(e)) return !1;
|
|
1003
|
+
let n = t[t.length - 1];
|
|
1004
|
+
return !(n.length < 2 || !/^[a-zA-Z]+$/.test(n));
|
|
1212
1005
|
}
|
|
1213
|
-
function
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1006
|
+
function se(e) {
|
|
1007
|
+
let t = e.split("@");
|
|
1008
|
+
if (t.length !== 2) return !1;
|
|
1009
|
+
let [n, r] = t;
|
|
1010
|
+
return !(!ae(n) || !$(r));
|
|
1218
1011
|
}
|
|
1219
|
-
function
|
|
1220
|
-
|
|
1221
|
-
|
|
1012
|
+
function ce(e) {
|
|
1013
|
+
let t = e.split("@");
|
|
1014
|
+
return t.length === 2 ? t[1].toLowerCase() : null;
|
|
1222
1015
|
}
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1016
|
+
var le = [
|
|
1017
|
+
"MX",
|
|
1018
|
+
"A",
|
|
1019
|
+
"AAAA"
|
|
1020
|
+
];
|
|
1021
|
+
async function ue(e, t) {
|
|
1022
|
+
try {
|
|
1023
|
+
return await a?.promises?.resolve(e, t), !0;
|
|
1024
|
+
} catch {
|
|
1025
|
+
return !1;
|
|
1026
|
+
}
|
|
1231
1027
|
}
|
|
1232
|
-
async function
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
return !1;
|
|
1028
|
+
async function de(e) {
|
|
1029
|
+
for (let t of le) if (await ue(e, t)) return !0;
|
|
1030
|
+
return !1;
|
|
1236
1031
|
}
|
|
1237
|
-
async function
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1032
|
+
async function fe(e) {
|
|
1033
|
+
if (!e || typeof e != "string") return !1;
|
|
1034
|
+
let t = e.trim();
|
|
1035
|
+
if (!ie(t) || !se(t)) return !1;
|
|
1036
|
+
let n = ce(t);
|
|
1037
|
+
return n ? await de(n) : !1;
|
|
1243
1038
|
}
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
o
|
|
1255
|
-
].every((a) => a);
|
|
1039
|
+
//#endregion
|
|
1040
|
+
//#region src/validations/validatePassword.ts
|
|
1041
|
+
function pe(e) {
|
|
1042
|
+
return e ? [
|
|
1043
|
+
e.length >= 8,
|
|
1044
|
+
/[A-Z]/.test(e),
|
|
1045
|
+
/[a-z]/.test(e),
|
|
1046
|
+
/\d/.test(e),
|
|
1047
|
+
/[!@#$%^&*(),.?":;{}|<>_\-+=~`[\]\\\/]/.test(e)
|
|
1048
|
+
].every((e) => e) : !1;
|
|
1256
1049
|
}
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1050
|
+
//#endregion
|
|
1051
|
+
//#region src/validations/validatePhone.ts
|
|
1052
|
+
function me(e) {
|
|
1053
|
+
if (!s(e)) return !1;
|
|
1054
|
+
let t = c(e)?.country;
|
|
1055
|
+
return !(!t || !o.find((e) => e.iso === t));
|
|
1261
1056
|
}
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1057
|
+
//#endregion
|
|
1058
|
+
//#region src/validations/validateRg.ts
|
|
1059
|
+
function he(e) {
|
|
1060
|
+
if (!e || !/^[0-9a-zA-Z.-]+$/.test(e)) return !1;
|
|
1061
|
+
let t = e.replace(/[^a-zA-Z0-9]/g, "");
|
|
1062
|
+
return t.length < 7 || t.length > 9 ? !1 : /^[0-9]{7,8}[0-9Xx]?$/.test(t);
|
|
1266
1063
|
}
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
V as BadGateway,
|
|
1270
|
-
T as BadRequest,
|
|
1271
|
-
Z as Conflict,
|
|
1272
|
-
Q as Created,
|
|
1273
|
-
N as DebugService,
|
|
1274
|
-
G as Forbidden,
|
|
1275
|
-
X as Found,
|
|
1276
|
-
v as LogService,
|
|
1277
|
-
ss as NoContent,
|
|
1278
|
-
W as NotFound,
|
|
1279
|
-
Y as NotImplemented,
|
|
1280
|
-
As as SchemaValidator,
|
|
1281
|
-
x as ServerError,
|
|
1282
|
-
es as Success,
|
|
1283
|
-
K as Unauthorized,
|
|
1284
|
-
R as UnprocessableEntity,
|
|
1285
|
-
ts as Updated,
|
|
1286
|
-
ks as decodeRequestBody,
|
|
1287
|
-
Is as decodeRequestErrorMessage,
|
|
1288
|
-
Us as errorHandler,
|
|
1289
|
-
g as flushDebugLogs,
|
|
1290
|
-
ls as formAsyncParse,
|
|
1291
|
-
hs as formParse,
|
|
1292
|
-
qs as getScopedParams,
|
|
1293
|
-
Hs as validateCep,
|
|
1294
|
-
Ls as validateCnpj,
|
|
1295
|
-
Fs as validateCpf,
|
|
1296
|
-
zs as validateDate,
|
|
1297
|
-
Ms as validateEmail,
|
|
1298
|
-
Vs as validatePassword,
|
|
1299
|
-
Zs as validatePhone,
|
|
1300
|
-
Gs as validateRg
|
|
1301
|
-
};
|
|
1064
|
+
//#endregion
|
|
1065
|
+
export { F as ApiService, f as BadGateway, p as BadRequest, m as Conflict, S as Created, l as DebugService, h as Forbidden, C as Found, O as LogService, w as NoContent, g as NotFound, _ as NotImplemented, U as SchemaValidator, v as ServerError, T as Success, y as Unauthorized, b as UnprocessableEntity, E as Updated, I as decodeRequestBody, L as decodeRequestErrorMessage, R as errorHandler, u as flushDebugLogs, z as formAsyncParse, B as formParse, V as getScopedParams, W as validateCep, te as validateCnpj, ne as validateCpf, re as validateDate, fe as validateEmail, pe as validatePassword, me as validatePhone, he as validateRg };
|