@arkyn/server 3.0.1-beta.58 → 3.0.1-beta.59

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/bundle.js ADDED
@@ -0,0 +1,1090 @@
1
+ var k = Object.defineProperty;
2
+ var I = (e, s, t) => s in e ? k(e, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[s] = t;
3
+ var o = (e, s, t) => I(e, typeof s != "symbol" ? s + "" : s, t);
4
+ const j = {};
5
+ function R() {
6
+ const e = process.cwd(), n = (new Error().stack || "").split(`
7
+ `).map((u) => u.trim()), r = g.ignoreFile;
8
+ let a = 2;
9
+ for (; a < n.length && (n[a].includes("node:internal") || n[a].includes("/node_modules/")); )
10
+ a++;
11
+ if (r)
12
+ for (; a < n.length && n[a].includes(r); )
13
+ a++;
14
+ const y = n[a] || "";
15
+ let i = "Unknown function", c = "Unknown caller";
16
+ const l = y.match(/at\s+([^(\s]+)\s+\(([^)]+)\)/);
17
+ if (l)
18
+ i = l[1], c = l[2];
19
+ else {
20
+ const u = y.match(/at\s+(.+)/);
21
+ if (u) {
22
+ c = u[1];
23
+ const f = c.match(/at\s+([^(\s]+)\s+/);
24
+ f && f[1] !== "new" && (i = f[1]);
25
+ }
26
+ }
27
+ c.includes("(") && (c = c.substring(
28
+ c.indexOf("(") + 1,
29
+ c.lastIndexOf(")")
30
+ )), c = c.split(":").slice(0, -2).join(":");
31
+ try {
32
+ c = j.relative(e, c);
33
+ } catch {
34
+ }
35
+ return { functionName: i, callerInfo: c };
36
+ }
37
+ class g {
38
+ /**
39
+ * Sets the file name to be ignored during stack trace analysis.
40
+ *
41
+ * This method configures the debug service to skip specific files when
42
+ * determining the actual caller of a function. This is useful when you have
43
+ * adapter or wrapper functions that you want to be transparent in the debug output.
44
+ *
45
+ * @param file - The name of the file to ignore in stack traces (e.g., "httpAdapter.ts")
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * // Ignore the HTTP adapter file so debug shows the actual business logic caller
50
+ * HttpDebugService.setIgnoreFile("httpAdapter.ts");
51
+ * ```
52
+ */
53
+ static setIgnoreFile(s) {
54
+ this.ignoreFile = s;
55
+ }
56
+ }
57
+ /**
58
+ * The name of the file to ignore when analyzing the stack trace.
59
+ * When set, the `getCaller` function will skip stack frames containing this file name.
60
+ */
61
+ o(g, "ignoreFile");
62
+ function h(e, s, t) {
63
+ var r;
64
+ if (process.env.NODE_ENV === "development" || ((r = process.env) == null ? void 0 : r.SHOW_ERRORS_IN_CONSOLE) === "true") {
65
+ const i = "\x1B[36m[ARKYN-DEBUG]\x1B[0m", { callerInfo: c, functionName: l } = R();
66
+ let u = `${i} ${e} initialized
67
+ `;
68
+ u += `${i} Caller Function: ${l}
69
+ `, u += `${i} Caller Location: ${c}
70
+ `, u += `${i} Body: ${JSON.stringify(s, null, 2)}
71
+ `, t && (u += `${i} Cause: ${JSON.stringify(t, null, 2)}
72
+ `), console.log(u);
73
+ }
74
+ }
75
+ class O {
76
+ /**
77
+ * Creates an instance of the `BadGateway` class.
78
+ *
79
+ * @param message - A descriptive message explaining the cause of the error.
80
+ * @param cause - Optional additional information about the cause of the error.
81
+ */
82
+ constructor(s, t) {
83
+ o(this, "body");
84
+ o(this, "cause");
85
+ o(this, "status", 502);
86
+ o(this, "statusText");
87
+ this.body = { name: "BadGateway", message: s }, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, h("BadGateway", this.body, this.cause);
88
+ }
89
+ /**
90
+ * Converts the `BadGateway` instance into a `Response` object with a JSON body.
91
+ * This method ensures the response has the appropriate headers, status, and status text.
92
+ *
93
+ * @returns A `Response` object with the serialized JSON body and response metadata.
94
+ */
95
+ toResponse() {
96
+ const s = {
97
+ headers: { "Content-Type": "application/json" },
98
+ status: this.status,
99
+ statusText: this.statusText
100
+ };
101
+ return new Response(JSON.stringify(this.body), s);
102
+ }
103
+ /**
104
+ * Converts the `BadGateway` instance into a `Response` object using the `Response.json` method.
105
+ * This method is an alternative to `toResponse` for generating JSON error responses.
106
+ *
107
+ * @returns A `Response` object with the JSON body and response metadata.
108
+ */
109
+ toJson() {
110
+ const s = {
111
+ status: this.status,
112
+ statusText: this.statusText
113
+ };
114
+ return Response.json(this.body, s);
115
+ }
116
+ }
117
+ class T {
118
+ /**
119
+ * Creates an instance of the `BadRequest` class.
120
+ *
121
+ * @param message - A descriptive message explaining the cause of the error.
122
+ * @param cause - Optional additional information about the cause of the error.
123
+ */
124
+ constructor(s, t) {
125
+ o(this, "body");
126
+ o(this, "cause");
127
+ o(this, "status", 400);
128
+ o(this, "statusText");
129
+ this.body = { name: "BadRequest", message: s }, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, h("BadRequest", this.body, this.cause);
130
+ }
131
+ /**
132
+ * Converts the `BadRequest` instance into a `Response` object with a JSON body.
133
+ * This method ensures the response has the appropriate headers, status, and status text.
134
+ *
135
+ * @returns A `Response` object with the serialized JSON body and response metadata.
136
+ */
137
+ toResponse() {
138
+ const s = {
139
+ headers: { "Content-Type": "application/json" },
140
+ status: this.status,
141
+ statusText: this.statusText
142
+ };
143
+ return new Response(JSON.stringify(this.body), s);
144
+ }
145
+ /**
146
+ * Converts the `BadRequest` instance into a `Response` object using the `Response.json` method.
147
+ * This method is an alternative to `toResponse` for generating JSON error responses.
148
+ *
149
+ * @returns A `Response` object with the JSON body and response metadata.
150
+ */
151
+ toJson() {
152
+ const s = {
153
+ status: this.status,
154
+ statusText: this.statusText
155
+ };
156
+ return Response.json(this.body, s);
157
+ }
158
+ }
159
+ class U {
160
+ /**
161
+ * Creates an instance of the `Conflict` class.
162
+ *
163
+ * @param message - A descriptive message explaining the cause of the conflict.
164
+ * @param cause - Optional additional information about the cause of the conflict.
165
+ */
166
+ constructor(s, t) {
167
+ o(this, "body");
168
+ o(this, "cause");
169
+ o(this, "status", 409);
170
+ o(this, "statusText");
171
+ this.body = { name: "Conflict", message: s }, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, h("Conflict", this.body, this.cause);
172
+ }
173
+ /**
174
+ * Converts the `Conflict` instance into a `Response` object with a JSON body.
175
+ * This method ensures the response has the appropriate headers, status, and status text.
176
+ *
177
+ * @returns A `Response` object with the serialized JSON body and response metadata.
178
+ */
179
+ toResponse() {
180
+ const s = {
181
+ headers: { "Content-Type": "application/json" },
182
+ status: this.status,
183
+ statusText: this.statusText
184
+ };
185
+ return new Response(JSON.stringify(this.body), s);
186
+ }
187
+ /**
188
+ * Converts the `Conflict` instance into a `Response` object using the `Response.json` method.
189
+ * This method is an alternative to `toResponse` for generating JSON error responses.
190
+ *
191
+ * @returns A `Response` object with the JSON body and response metadata.
192
+ */
193
+ toJson() {
194
+ const s = {
195
+ status: this.status,
196
+ statusText: this.statusText
197
+ };
198
+ return Response.json(this.body, s);
199
+ }
200
+ }
201
+ class E {
202
+ /**
203
+ * Creates an instance of the `Forbidden` class.
204
+ *
205
+ * @param message - A descriptive message explaining why access is forbidden.
206
+ * @param cause - Optional additional information about the cause of the error.
207
+ */
208
+ constructor(s, t) {
209
+ o(this, "body");
210
+ o(this, "cause");
211
+ o(this, "status", 403);
212
+ o(this, "statusText");
213
+ this.body = { name: "Forbidden", message: s }, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, h("Forbidden", this.body, this.cause);
214
+ }
215
+ /**
216
+ * Converts the `Forbidden` instance into a `Response` object with a JSON body.
217
+ * This method ensures the response has the appropriate headers, status, and status text.
218
+ *
219
+ * @returns A `Response` object with the serialized JSON body and response metadata.
220
+ */
221
+ toResponse() {
222
+ const s = {
223
+ headers: { "Content-Type": "application/json" },
224
+ status: this.status,
225
+ statusText: this.statusText
226
+ };
227
+ return new Response(JSON.stringify(this.body), s);
228
+ }
229
+ /**
230
+ * Converts the `Forbidden` instance into a `Response` object using the `Response.json` method.
231
+ * This method is an alternative to `toResponse` for generating JSON error responses.
232
+ *
233
+ * @returns A `Response` object with the JSON body and response metadata.
234
+ */
235
+ toJson() {
236
+ const s = {
237
+ status: this.status,
238
+ statusText: this.statusText
239
+ };
240
+ return Response.json(this.body, s);
241
+ }
242
+ }
243
+ class J {
244
+ /**
245
+ * Creates an instance of the `NotFound` class.
246
+ *
247
+ * @param message - A descriptive message explaining the reason the resource was not found.
248
+ * @param cause - Optional additional information about the cause of the error.
249
+ */
250
+ constructor(s, t) {
251
+ o(this, "body");
252
+ o(this, "cause");
253
+ o(this, "status", 404);
254
+ o(this, "statusText");
255
+ this.body = { name: "NotFound", message: s }, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, h("NotFound", this.body, this.cause);
256
+ }
257
+ /**
258
+ * Converts the `NotFound` instance into a `Response` object with a JSON body.
259
+ * This method ensures the response has the appropriate headers, status, and status text.
260
+ *
261
+ * @returns A `Response` object with the serialized JSON body and response metadata.
262
+ */
263
+ toResponse() {
264
+ const s = {
265
+ headers: { "Content-Type": "application/json" },
266
+ status: this.status,
267
+ statusText: this.statusText
268
+ };
269
+ return new Response(JSON.stringify(this.body), s);
270
+ }
271
+ /**
272
+ * Converts the `NotFound` instance into a `Response` object using the `Response.json` method.
273
+ * This method is an alternative to `toResponse` for generating JSON error responses.
274
+ *
275
+ * @returns A `Response` object with the JSON body and response metadata.
276
+ */
277
+ toJson() {
278
+ const s = {
279
+ status: this.status,
280
+ statusText: this.statusText
281
+ };
282
+ return Response.json(this.body, s);
283
+ }
284
+ }
285
+ class C {
286
+ /**
287
+ * Creates an instance of the `NotImplemented` class.
288
+ *
289
+ * @param message - A descriptive message explaining why the functionality is not implemented.
290
+ * @param cause - Optional additional information about the cause of the error.
291
+ */
292
+ constructor(s, t) {
293
+ o(this, "body");
294
+ o(this, "cause");
295
+ o(this, "status", 501);
296
+ o(this, "statusText");
297
+ this.body = { name: "NotImplemented", message: s }, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, h("NotImplemented", this.body, this.cause);
298
+ }
299
+ /**
300
+ * Converts the `NotImplemented` instance into a `Response` object with a JSON body.
301
+ * This method ensures the response has the appropriate headers, status, and status text.
302
+ *
303
+ * @returns A `Response` object with the serialized JSON body and response metadata.
304
+ */
305
+ toResponse() {
306
+ const s = {
307
+ headers: { "Content-Type": "application/json" },
308
+ status: this.status,
309
+ statusText: this.statusText
310
+ };
311
+ return new Response(JSON.stringify(this.body), s);
312
+ }
313
+ /**
314
+ * Converts the `NotImplemented` instance into a `Response` object using the `Response.json` method.
315
+ * This method is an alternative to `toResponse` for generating JSON error responses.
316
+ *
317
+ * @returns A `Response` object with the JSON body and response metadata.
318
+ */
319
+ toJson() {
320
+ const s = {
321
+ status: this.status,
322
+ statusText: this.statusText
323
+ };
324
+ return Response.json(this.body, s);
325
+ }
326
+ }
327
+ class b {
328
+ /**
329
+ * Creates an instance of the `ServerError` class.
330
+ *
331
+ * @param message - A descriptive message explaining the cause of the server error.
332
+ * @param cause - Optional additional information about the cause of the error.
333
+ */
334
+ constructor(s, t) {
335
+ o(this, "body");
336
+ o(this, "cause");
337
+ o(this, "status", 500);
338
+ o(this, "statusText");
339
+ this.body = { name: "ServerError", message: s }, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, h("ServerError", this.body, this.cause);
340
+ }
341
+ /**
342
+ * Converts the `ServerError` instance into a `Response` object with a JSON body.
343
+ * This method ensures the response has the appropriate headers, status, and status text.
344
+ *
345
+ * @returns A `Response` object with the serialized JSON body and response metadata.
346
+ */
347
+ toResponse() {
348
+ const s = {
349
+ headers: { "Content-Type": "application/json" },
350
+ status: this.status,
351
+ statusText: this.statusText
352
+ };
353
+ return new Response(JSON.stringify(this.body), s);
354
+ }
355
+ /**
356
+ * Converts the `ServerError` instance into a `Response` object using the `Response.json` method.
357
+ * This method is an alternative to `toResponse` for generating JSON error responses.
358
+ *
359
+ * @returns A `Response` object with the JSON body and response metadata.
360
+ */
361
+ toJson() {
362
+ const s = {
363
+ status: this.status,
364
+ statusText: this.statusText
365
+ };
366
+ return Response.json(this.body, s);
367
+ }
368
+ }
369
+ class q {
370
+ /**
371
+ * Creates an instance of the `Unauthorized` class.
372
+ *
373
+ * @param message - A descriptive message explaining why the request is unauthorized.
374
+ * @param cause - Optional additional information about the cause of the error.
375
+ */
376
+ constructor(s, t) {
377
+ o(this, "body");
378
+ o(this, "cause");
379
+ o(this, "status", 401);
380
+ o(this, "statusText");
381
+ this.body = { name: "Unauthorized", message: s }, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, h("Unauthorized", this.body, this.cause);
382
+ }
383
+ /**
384
+ * Converts the `Unauthorized` instance into a `Response` object with a JSON body.
385
+ * This method ensures the response has the appropriate headers, status, and status text.
386
+ *
387
+ * @returns A `Response` object with the serialized JSON body and response metadata.
388
+ */
389
+ toResponse() {
390
+ const s = {
391
+ headers: { "Content-Type": "application/json" },
392
+ status: this.status,
393
+ statusText: this.statusText
394
+ };
395
+ return new Response(JSON.stringify(this.body), s);
396
+ }
397
+ /**
398
+ * Converts the `Unauthorized` instance into a `Response` object using the `Response.json` method.
399
+ * This method is an alternative to `toResponse` for generating JSON error responses.
400
+ *
401
+ * @returns A `Response` object with the JSON body and response metadata.
402
+ */
403
+ toJson() {
404
+ const s = {
405
+ status: this.status,
406
+ statusText: this.statusText
407
+ };
408
+ return Response.json(this.body, s);
409
+ }
410
+ }
411
+ class m {
412
+ /**
413
+ * Creates an instance of the `UnprocessableEntity` class.
414
+ *
415
+ * @param props - An object containing details about the error, such as:
416
+ * - `data`: Additional data related to the error.
417
+ * - `fieldErrors`: A record of field-specific error messages.
418
+ * - `fields`: A record of field values that caused the error.
419
+ * - `message`: A descriptive message explaining the error.
420
+ * @param enableDebug - A boolean indicating whether to enable debug logging for this error.
421
+ */
422
+ constructor(s, t = !1) {
423
+ o(this, "body");
424
+ o(this, "status", 422);
425
+ o(this, "statusText");
426
+ this.statusText = s.message || "Unprocessable Entity", this.body = {
427
+ name: "UnprocessableEntity",
428
+ message: s.message || null,
429
+ data: s.data,
430
+ fieldErrors: s.fieldErrors,
431
+ fields: s.fields
432
+ }, t && h("UnprocessableEntity", this.body);
433
+ }
434
+ /**
435
+ * Converts the `UnprocessableEntity` instance into a `Response` object with a JSON body.
436
+ * This method ensures the response has the appropriate headers, status, and status text.
437
+ *
438
+ * @returns A `Response` object with the serialized JSON body and response metadata.
439
+ */
440
+ toResponse() {
441
+ const s = {
442
+ headers: { "Content-Type": "application/json" },
443
+ status: this.status,
444
+ statusText: this.statusText
445
+ };
446
+ return new Response(JSON.stringify(this.body), s);
447
+ }
448
+ /**
449
+ * Converts the `UnprocessableEntity` instance into a `Response` object using the `Response.json` method.
450
+ * This method is an alternative to `toResponse` for generating JSON error responses.
451
+ *
452
+ * @returns A `Response` object with the JSON body and response metadata.
453
+ */
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 H {
463
+ /**
464
+ * Creates an instance of the `Created` class.
465
+ *
466
+ * @param body - The response body to be included in the HTTP response.
467
+ * @param init - Optional initialization object for customizing headers, status, and status text.
468
+ */
469
+ constructor(s, t) {
470
+ o(this, "body");
471
+ o(this, "headers");
472
+ o(this, "status");
473
+ o(this, "statusText");
474
+ this.body = s, this.headers = (t == null ? void 0 : t.headers) || {}, this.status = (t == null ? void 0 : t.status) || 201, this.statusText = (t == null ? void 0 : t.statusText) || "Resource created successfully";
475
+ }
476
+ /**
477
+ * Converts the `Created` instance into a `Response` object with a JSON body.
478
+ * This method ensures the response has the appropriate headers, status, and status text.
479
+ *
480
+ * @returns A `Response` object with the serialized JSON body and response metadata.
481
+ */
482
+ toResponse() {
483
+ const s = {
484
+ headers: { "Content-Type": "application/json", ...this.headers },
485
+ status: this.status,
486
+ statusText: this.statusText
487
+ };
488
+ return new Response(JSON.stringify(this.body), s);
489
+ }
490
+ /**
491
+ * Converts the `Created` instance into a `Response` object using the `Response.json` method.
492
+ * This method is an alternative to `toResponse` for generating JSON responses.
493
+ *
494
+ * @returns A `Response` object with the JSON body and response metadata.
495
+ */
496
+ toJson() {
497
+ const s = {
498
+ headers: this.headers,
499
+ status: this.status,
500
+ statusText: this.statusText
501
+ };
502
+ return Response.json(this.body, s);
503
+ }
504
+ }
505
+ class P {
506
+ /**
507
+ * Creates an instance of the `Found` class.
508
+ *
509
+ * @param body - The body of the response.
510
+ * @param init - Optional initialization object to set headers, status, and statusText.
511
+ * - `headers`: Additional headers to include in the response.
512
+ * - `status`: HTTP status code (default is 302).
513
+ * - `statusText`: HTTP status text (default is "Found").
514
+ */
515
+ constructor(s, t) {
516
+ o(this, "body");
517
+ o(this, "headers");
518
+ o(this, "status");
519
+ o(this, "statusText");
520
+ this.body = s, this.headers = (t == null ? void 0 : t.headers) || {}, this.status = (t == null ? void 0 : t.status) || 302, this.statusText = (t == null ? void 0 : t.statusText) || "Found";
521
+ }
522
+ /**
523
+ * Converts the `Found` instance into a `Response` object.
524
+ * This method serializes the response body as JSON and includes the appropriate headers.
525
+ *
526
+ * @returns A `Response` object with the serialized JSON body and the specified headers, status, and statusText.
527
+ */
528
+ toResponse() {
529
+ const s = {
530
+ headers: { "Content-Type": "application/json", ...this.headers },
531
+ status: this.status,
532
+ statusText: this.statusText
533
+ };
534
+ return new Response(JSON.stringify(this.body), s);
535
+ }
536
+ /**
537
+ * Converts the `Found` instance into a JSON response using `Response.json`.
538
+ * This method is an alternative to `toResponse` for creating JSON responses.
539
+ *
540
+ * @returns A `Response` object with the JSON body and the specified headers, status, and statusText.
541
+ */
542
+ toJson() {
543
+ const s = {
544
+ headers: this.headers,
545
+ status: this.status,
546
+ statusText: this.statusText
547
+ };
548
+ return Response.json(this.body, s);
549
+ }
550
+ }
551
+ class B {
552
+ /**
553
+ * Creates an instance of the `NoContent` class.
554
+ *
555
+ * @param init - Optional initialization object for customizing headers, status, and status text.
556
+ */
557
+ constructor(s) {
558
+ o(this, "headers");
559
+ o(this, "status");
560
+ o(this, "statusText");
561
+ this.headers = (s == null ? void 0 : s.headers) || {}, this.status = (s == null ? void 0 : s.status) || 204, this.statusText = (s == null ? void 0 : s.statusText) ?? "No content";
562
+ }
563
+ /**
564
+ * Converts the `NoContent` instance into a `Response` object.
565
+ * This method ensures the response has the appropriate headers, status, and status text.
566
+ *
567
+ * @returns A `Response` object with no body and response metadata.
568
+ */
569
+ toResponse() {
570
+ const s = {
571
+ headers: this.headers,
572
+ status: this.status,
573
+ statusText: this.statusText
574
+ };
575
+ return new Response(null, s);
576
+ }
577
+ }
578
+ class L {
579
+ /**
580
+ * Creates an instance of the `Success` class.
581
+ *
582
+ * @param body - The response body to be included in the HTTP response.
583
+ * @param init - Optional initialization object for customizing headers, status, and status text.
584
+ */
585
+ constructor(s, t) {
586
+ o(this, "body");
587
+ o(this, "headers");
588
+ o(this, "status");
589
+ o(this, "statusText");
590
+ this.body = s, this.headers = (t == null ? void 0 : t.headers) || {}, this.status = (t == null ? void 0 : t.status) || 200, this.statusText = (t == null ? void 0 : t.statusText) ?? "OK";
591
+ }
592
+ /**
593
+ * Converts the `Success` instance into a `Response` object with a JSON body.
594
+ * This method ensures the response has the appropriate headers, status, and status text.
595
+ *
596
+ * @returns A `Response` object with the serialized JSON body and response metadata.
597
+ */
598
+ toResponse() {
599
+ const s = {
600
+ headers: { "Content-Type": "application/json", ...this.headers },
601
+ status: this.status,
602
+ statusText: this.statusText
603
+ };
604
+ return new Response(JSON.stringify(this.body), s);
605
+ }
606
+ /**
607
+ * Converts the `Success` instance into a `Response` object using the `Response.json` method.
608
+ * This method is an alternative to `toResponse` for generating JSON responses.
609
+ *
610
+ * @returns A `Response` object with the JSON body and response metadata.
611
+ */
612
+ toJson() {
613
+ const s = {
614
+ headers: this.headers,
615
+ status: this.status,
616
+ statusText: this.statusText
617
+ };
618
+ return Response.json(this.body, s);
619
+ }
620
+ }
621
+ class v {
622
+ /**
623
+ * Creates an instance of the `Updated` class.
624
+ *
625
+ * @param body - The response body to be included in the HTTP response.
626
+ * @param init - Optional initialization object for customizing headers, status, and status text.
627
+ */
628
+ constructor(s, t) {
629
+ o(this, "body");
630
+ o(this, "headers");
631
+ o(this, "status");
632
+ o(this, "statusText");
633
+ this.body = s, this.headers = (t == null ? void 0 : t.headers) || {}, this.status = (t == null ? void 0 : t.status) || 200, this.statusText = (t == null ? void 0 : t.statusText) || "Resource updated successfully";
634
+ }
635
+ /**
636
+ * Converts the `Updated` instance into a `Response` object with a JSON body.
637
+ * This method ensures the response has the appropriate headers, status, and status text.
638
+ *
639
+ * @returns A `Response` object with the serialized JSON body and response metadata.
640
+ */
641
+ toResponse() {
642
+ const s = {
643
+ headers: { "Content-Type": "application/json", ...this.headers },
644
+ status: this.status,
645
+ statusText: this.statusText
646
+ };
647
+ return new Response(JSON.stringify(this.body), s);
648
+ }
649
+ /**
650
+ * Converts the `Updated` instance into a `Response` object using the `Response.json` method.
651
+ * This method is an alternative to `toResponse` for generating JSON responses.
652
+ *
653
+ * @returns A `Response` object with the JSON body and response metadata.
654
+ */
655
+ toJson() {
656
+ const s = {
657
+ headers: this.headers,
658
+ status: this.status,
659
+ statusText: this.statusText
660
+ };
661
+ return Response.json(this.body, s);
662
+ }
663
+ }
664
+ class A {
665
+ static mapHeaders(s) {
666
+ return s instanceof Headers ? Object.fromEntries(s.entries()) : typeof s == "object" ? Object.entries(s).reduce((t, [n, r]) => (typeof r == "string" ? t[n] = r : Array.isArray(r) ? t[n] = r.join(", ") : t[n] = JSON.stringify(r), t), {}) : {};
667
+ }
668
+ static mapQueryParams(s) {
669
+ const t = {};
670
+ return s.forEach((n, r) => {
671
+ t[r] = n;
672
+ }), t;
673
+ }
674
+ static handle(s) {
675
+ return {
676
+ rawUrl: s.url,
677
+ status: s.status,
678
+ method: s.method,
679
+ token: null,
680
+ elapsedTime: s.elapsedTime,
681
+ requestHeaders: this.mapHeaders(s.requestHeaders),
682
+ requestBody: s.requestBody || null,
683
+ queryParams: this.mapQueryParams(s.queryParams),
684
+ responseHeaders: this.mapHeaders(s.responseHeaders),
685
+ responseBody: s.responseBody || null
686
+ };
687
+ }
688
+ }
689
+ class w {
690
+ /**
691
+ * Sets the configuration for the arkyn. This method initializes the arkyn configuration
692
+ * with the provided `arkynConfig` values. If the configuration has already been set,
693
+ * the method will return early without making any changes.
694
+ *
695
+ * @param arkynConfig - An object containing the arkyn configuration properties.
696
+ * @param arkynConfig.arkynTrafficSourceId - The key used to identify the arkyn.
697
+ * @param arkynConfig.arkynUserToken - The user token for authenticating with the arkyn.
698
+ * @param arkynConfig.arkynLogBaseApiUrl - (Optional) The API URL for the arkyn. If not provided,
699
+ * a default URL will be used.
700
+ */
701
+ static setArkynConfig(s) {
702
+ if (this.arkynConfig) return;
703
+ let n = s.arkynLogBaseApiUrl || "https://logs-arkyn-flow-logs.vw6wo7.easypanel.host";
704
+ n = n + "/http-traffic-records/:trafficSourceId", this.arkynConfig = {
705
+ arkynTrafficSourceId: s.arkynTrafficSourceId,
706
+ arkynUserToken: s.arkynUserToken,
707
+ arkynApiUrl: n
708
+ };
709
+ }
710
+ /**
711
+ * Retrieves the current arkyn configuration for the ArkynLogService.
712
+ *
713
+ * @returns {ArkynConfigProps | undefined} The current arkyn configuration if set,
714
+ * or `undefined` if no configuration has been initialized.
715
+ */
716
+ static getArkynConfig() {
717
+ return this.arkynConfig;
718
+ }
719
+ /**
720
+ * Resets the arkyn configuration to `undefined`.
721
+ * This method can be used to clear the current configuration.
722
+ */
723
+ static resetArkynConfig() {
724
+ this.arkynConfig = void 0;
725
+ }
726
+ }
727
+ o(w, "arkynConfig");
728
+ async function $(e) {
729
+ const s = w.getArkynConfig();
730
+ if (!s) return;
731
+ const { arkynUserToken: t, arkynApiUrl: n } = s, {
732
+ elapsedTime: r,
733
+ method: a,
734
+ queryParams: y,
735
+ requestBody: i,
736
+ requestHeaders: c,
737
+ responseBody: l,
738
+ responseHeaders: u,
739
+ status: f,
740
+ token: K,
741
+ rawUrl: S
742
+ } = e;
743
+ try {
744
+ const d = new URL(S);
745
+ let x = "HTTPS";
746
+ d.protocol === "http:" && (x = "HTTP");
747
+ const N = JSON.stringify({
748
+ domainUrl: d.protocol + "//" + d.host,
749
+ pathnameUrl: d.pathname,
750
+ status: f,
751
+ protocol: x,
752
+ method: a,
753
+ trafficUserId: null,
754
+ elapsedTime: r,
755
+ requestHeaders: c,
756
+ requestBody: i,
757
+ queryParams: y,
758
+ responseHeaders: u,
759
+ responseBody: l
760
+ });
761
+ await fetch(
762
+ n.replace(
763
+ ":trafficSourceId",
764
+ s.arkynTrafficSourceId
765
+ ),
766
+ {
767
+ method: "POST",
768
+ body: N,
769
+ headers: {
770
+ "Content-Type": "application/json",
771
+ Authorization: `Bearer ${t}`
772
+ }
773
+ }
774
+ );
775
+ } catch (d) {
776
+ h("arkyn log error", "Error sending request", d);
777
+ }
778
+ }
779
+ async function p(e, s, t = {}, n) {
780
+ const r = {
781
+ POST: "Resource created successfully",
782
+ PUT: "Resource updated successfully",
783
+ DELETE: "Resource deleted successfully",
784
+ PATCH: "Resource patched successfully",
785
+ GET: "Request successful"
786
+ };
787
+ try {
788
+ const a = performance.now(), y = { ...t, "Content-Type": "application/json" }, i = await fetch(s, {
789
+ method: e,
790
+ headers: y,
791
+ body: n ? JSON.stringify(n) : void 0
792
+ }), c = performance.now() - a, l = i.status;
793
+ let u = null;
794
+ try {
795
+ u = await i.json();
796
+ } catch {
797
+ u = null;
798
+ }
799
+ const f = A.handle({
800
+ elapsedTime: c,
801
+ method: e,
802
+ queryParams: new URL(s).searchParams,
803
+ requestHeaders: y,
804
+ requestBody: n,
805
+ responseBody: u,
806
+ responseHeaders: i.headers,
807
+ status: l,
808
+ url: s
809
+ });
810
+ return $(f), i.ok ? {
811
+ success: !0,
812
+ status: l,
813
+ message: (u == null ? void 0 : u.message) || r[e],
814
+ response: u,
815
+ cause: null
816
+ } : {
817
+ success: !1,
818
+ status: l,
819
+ message: (u == null ? void 0 : u.message) || i.statusText || "Request failed",
820
+ response: u,
821
+ cause: null
822
+ };
823
+ } catch (a) {
824
+ return h("Network error or request failed", null, a), {
825
+ success: !1,
826
+ status: 0,
827
+ message: "Network error or request failed",
828
+ response: null,
829
+ cause: a instanceof Error ? a.message : String(a)
830
+ };
831
+ }
832
+ }
833
+ async function F(e, s = {}, t) {
834
+ return p("DELETE", e, s, t);
835
+ }
836
+ async function D(e, s = {}) {
837
+ return p("GET", e, s);
838
+ }
839
+ async function M(e, s = {}, t) {
840
+ return p("PATCH", e, s, t);
841
+ }
842
+ async function z(e, s = {}, t) {
843
+ return p("POST", e, s, t);
844
+ }
845
+ async function G(e, s = {}, t) {
846
+ return p("PUT", e, s, t);
847
+ }
848
+ class W {
849
+ /**
850
+ * Creates an instance of ApiService.
851
+ * @param props - The configuration properties for the API instance.
852
+ * @param props.baseUrl - The base URL for the API.
853
+ * @param props.baseHeaders - Optional base headers to include in all requests.
854
+ * @param props.baseToken - Optional base token for authorization.
855
+ */
856
+ constructor(s) {
857
+ o(this, "baseUrl");
858
+ o(this, "baseHeaders");
859
+ o(this, "baseToken");
860
+ this.baseUrl = s.baseUrl, this.baseHeaders = s.baseHeaders || void 0, this.baseToken = s.baseToken || void 0;
861
+ }
862
+ /**
863
+ * Generates the full URL by appending the route to the base URL.
864
+ * @param route - The route to append to the base URL.
865
+ * @returns The full URL as a string.
866
+ */
867
+ generateURL(s) {
868
+ return this.baseUrl + s;
869
+ }
870
+ /**
871
+ * Generates the headers for a request by merging base headers, provided headers, and tokens.
872
+ * @param initHeaders - Initial headers to include in the request.
873
+ * @param token - Optional token to override the base token.
874
+ * @returns The merged headers as a HeadersInit object.
875
+ */
876
+ generateHeaders(s, t) {
877
+ let n = {};
878
+ 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;
879
+ }
880
+ /**
881
+ * Sends a get request to the specified route.
882
+ * @param route - The API route to send the get request to.
883
+ * @param data - The request data, including optional headers and token.
884
+ * @returns The API response data.
885
+ */
886
+ async get(s, t) {
887
+ const n = this.generateURL(s), r = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token);
888
+ return await D(n, r);
889
+ }
890
+ /**
891
+ * Sends a post request to the specified route.
892
+ * @param route - The API route to send the post request to.
893
+ * @param data - The request data, including body, optional headers, and token.
894
+ * @returns The API response data.
895
+ */
896
+ async post(s, t) {
897
+ const n = this.generateURL(s), r = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token), a = t == null ? void 0 : t.body;
898
+ return await z(n, r, a);
899
+ }
900
+ /**
901
+ * Sends a put request to the specified route.
902
+ * @param route - The API route to send the put request to.
903
+ * @param data - The request data, including body, optional headers, and token.
904
+ * @returns The API response data.
905
+ */
906
+ async put(s, t) {
907
+ const n = this.generateURL(s), r = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token), a = t == null ? void 0 : t.body;
908
+ return await G(n, r, a);
909
+ }
910
+ /**
911
+ * Sends a patch request to the specified route.
912
+ * @param route - The API route to send the patch request to.
913
+ * @param data - The request data, including body, optional headers, and token.
914
+ * @returns The API response data.
915
+ */
916
+ async patch(s, t) {
917
+ const n = this.generateURL(s), r = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token), a = t == null ? void 0 : t.body;
918
+ return await M(n, r, a);
919
+ }
920
+ /**
921
+ * Sends a delete request to the specified route.
922
+ * @param route - The API route to send the delete request to.
923
+ * @param data - The request data, including body, optional headers, and token.
924
+ * @returns The API response data.
925
+ */
926
+ async delete(s, t) {
927
+ const n = this.generateURL(s), r = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token), a = t == null ? void 0 : t.body;
928
+ return await F(n, r, a);
929
+ }
930
+ }
931
+ function Y(e, s) {
932
+ var t, n, r;
933
+ return e != null && e.message && typeof (e == null ? void 0 : e.message) == "string" ? e == null ? void 0 : e.message : e != null && e.error && typeof (e == null ? void 0 : e.error) == "string" ? e == null ? void 0 : e.error : (t = e == null ? void 0 : e.error) != null && t.message && typeof ((n = e == null ? void 0 : e.error) == null ? void 0 : n.message) == "string" ? (r = e == null ? void 0 : e.error) == null ? void 0 : r.message : s != null && s.statusText && typeof (s == null ? void 0 : s.statusText) == "string" ? s == null ? void 0 : s.statusText : "Missing error message";
934
+ }
935
+ const X = async (e) => {
936
+ let s;
937
+ const t = await e.arrayBuffer(), n = new TextDecoder().decode(t);
938
+ try {
939
+ s = JSON.parse(n);
940
+ } catch {
941
+ try {
942
+ if (n.includes("=")) {
943
+ const a = new URLSearchParams(n);
944
+ s = Object.fromEntries(a.entries());
945
+ } else
946
+ throw new T("Invalid URLSearchParams format");
947
+ } catch {
948
+ throw new T("Failed to extract data from request");
949
+ }
950
+ }
951
+ return s;
952
+ };
953
+ function Z(e) {
954
+ switch (!0) {
955
+ case e instanceof Response:
956
+ return e;
957
+ case e instanceof P:
958
+ return e.toResponse();
959
+ case e instanceof H:
960
+ return e.toResponse();
961
+ case e instanceof v:
962
+ return e.toResponse();
963
+ case e instanceof L:
964
+ return e.toResponse();
965
+ case e instanceof B:
966
+ return e.toResponse();
967
+ }
968
+ switch (!0) {
969
+ case e instanceof O:
970
+ return e.toResponse();
971
+ case e instanceof T:
972
+ return e.toResponse();
973
+ case e instanceof U:
974
+ return e.toResponse();
975
+ case e instanceof E:
976
+ return e.toResponse();
977
+ case e instanceof J:
978
+ return e.toResponse();
979
+ case e instanceof C:
980
+ return e.toResponse();
981
+ case e instanceof b:
982
+ return e.toResponse();
983
+ case e instanceof q:
984
+ return e.toResponse();
985
+ case e instanceof m:
986
+ return e.toResponse();
987
+ }
988
+ return new b("Server error", e).toResponse();
989
+ }
990
+ function V([
991
+ e,
992
+ s
993
+ ]) {
994
+ const t = s.safeParse(e);
995
+ if (t.success === !1) {
996
+ const n = Object.fromEntries(
997
+ t.error.issues.map((r) => (console.log(r), [r.path.join("."), r.message]))
998
+ );
999
+ return {
1000
+ success: t.success,
1001
+ fieldErrors: n,
1002
+ fields: e
1003
+ };
1004
+ } else
1005
+ return {
1006
+ success: t.success,
1007
+ data: t.data
1008
+ };
1009
+ }
1010
+ const ss = (e, s = "") => {
1011
+ const t = new URL(e.url);
1012
+ if (s === "") return t.searchParams;
1013
+ const n = Array.from(
1014
+ t.searchParams.entries()
1015
+ ).filter(([r]) => r.startsWith(`${s}:`)).map(([r, a]) => [r.replace(`${s}:`, ""), a]);
1016
+ return new URLSearchParams(n);
1017
+ };
1018
+ function _(e) {
1019
+ const s = "Error validating:", t = e.issues.map(
1020
+ ({ path: n, message: r }) => `-> ${n.join(".")}: ${r}`
1021
+ );
1022
+ return [s, ...t].join(`
1023
+ `);
1024
+ }
1025
+ class ts {
1026
+ constructor(s) {
1027
+ o(this, "functionName");
1028
+ o(this, "callerInfo");
1029
+ this.schema = s;
1030
+ const { callerInfo: t, functionName: n } = R();
1031
+ this.callerInfo = t, this.functionName = n;
1032
+ }
1033
+ isValid(s) {
1034
+ return this.schema.safeParse(s).success;
1035
+ }
1036
+ safeValidate(s) {
1037
+ return this.schema.safeParse(s);
1038
+ }
1039
+ validate(s) {
1040
+ try {
1041
+ return this.schema.parse(s);
1042
+ } catch (t) {
1043
+ throw new b(_(t));
1044
+ }
1045
+ }
1046
+ formValidate(s, t) {
1047
+ const n = V([s, this.schema]);
1048
+ if (!n.success) {
1049
+ h("UnprocessableEntity", n);
1050
+ const r = Object.keys(n.fieldErrors)[0];
1051
+ throw new m(
1052
+ {
1053
+ fields: n.fields,
1054
+ fieldErrors: n.fieldErrors,
1055
+ data: { scrollTo: r },
1056
+ message: t
1057
+ },
1058
+ !1
1059
+ );
1060
+ }
1061
+ return n.data;
1062
+ }
1063
+ }
1064
+ export {
1065
+ W as ApiService,
1066
+ w as ArkynLogService,
1067
+ O as BadGateway,
1068
+ T as BadRequest,
1069
+ U as Conflict,
1070
+ H as Created,
1071
+ E as Forbidden,
1072
+ P as Found,
1073
+ g as HttpDebugService,
1074
+ B as NoContent,
1075
+ J as NotFound,
1076
+ C as NotImplemented,
1077
+ ts as SchemaValidator,
1078
+ b as ServerError,
1079
+ L as Success,
1080
+ q as Unauthorized,
1081
+ m as UnprocessableEntity,
1082
+ v as Updated,
1083
+ Y as decodeErrorMessageFromRequest,
1084
+ X as decodeRequestBody,
1085
+ Z as errorHandler,
1086
+ V as formParse,
1087
+ R as getCaller,
1088
+ ss as getScopedParams,
1089
+ h as httpDebug
1090
+ };
@@ -0,0 +1,8 @@
1
+ (function(a,l){typeof exports=="object"&&typeof module<"u"?l(exports):typeof define=="function"&&define.amd?define(["exports"],l):(a=typeof globalThis<"u"?globalThis:a||self,l(a["@arkyn/server"]={}))})(this,function(a){"use strict";var Z=Object.defineProperty;var ss=(a,l,f)=>l in a?Z(a,l,{enumerable:!0,configurable:!0,writable:!0,value:f}):a[l]=f;var o=(a,l,f)=>ss(a,typeof l!="symbol"?l+"":l,f);const l={};function f(){const t=process.cwd(),n=(new Error().stack||"").split(`
2
+ `).map(c=>c.trim()),r=x.ignoreFile;let u=2;for(;u<n.length&&(n[u].includes("node:internal")||n[u].includes("/node_modules/"));)u++;if(r)for(;u<n.length&&n[u].includes(r);)u++;const p=n[u]||"";let h="Unknown function",i="Unknown caller";const d=p.match(/at\s+([^(\s]+)\s+\(([^)]+)\)/);if(d)h=d[1],i=d[2];else{const c=p.match(/at\s+(.+)/);if(c){i=c[1];const T=i.match(/at\s+([^(\s]+)\s+/);T&&T[1]!=="new"&&(h=T[1])}}i.includes("(")&&(i=i.substring(i.indexOf("(")+1,i.lastIndexOf(")"))),i=i.split(":").slice(0,-2).join(":");try{i=l.relative(t,i)}catch{}return{functionName:h,callerInfo:i}}class x{static setIgnoreFile(s){this.ignoreFile=s}}o(x,"ignoreFile");function y(t,s,e){var r;if(process.env.NODE_ENV==="development"||((r=process.env)==null?void 0:r.SHOW_ERRORS_IN_CONSOLE)==="true"){const h="\x1B[36m[ARKYN-DEBUG]\x1B[0m",{callerInfo:i,functionName:d}=f();let c=`${h} ${t} initialized
3
+ `;c+=`${h} Caller Function: ${d}
4
+ `,c+=`${h} Caller Location: ${i}
5
+ `,c+=`${h} Body: ${JSON.stringify(s,null,2)}
6
+ `,e&&(c+=`${h} Cause: ${JSON.stringify(e,null,2)}
7
+ `),console.log(c)}}class N{constructor(s,e){o(this,"body");o(this,"cause");o(this,"status",502);o(this,"statusText");this.body={name:"BadGateway",message:s},this.statusText=s,this.cause=e?JSON.stringify(e):void 0,y("BadGateway",this.body,this.cause)}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.body),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.body,s)}}class m{constructor(s,e){o(this,"body");o(this,"cause");o(this,"status",400);o(this,"statusText");this.body={name:"BadRequest",message:s},this.statusText=s,this.cause=e?JSON.stringify(e):void 0,y("BadRequest",this.body,this.cause)}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.body),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.body,s)}}class k{constructor(s,e){o(this,"body");o(this,"cause");o(this,"status",409);o(this,"statusText");this.body={name:"Conflict",message:s},this.statusText=s,this.cause=e?JSON.stringify(e):void 0,y("Conflict",this.body,this.cause)}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.body),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.body,s)}}class I{constructor(s,e){o(this,"body");o(this,"cause");o(this,"status",403);o(this,"statusText");this.body={name:"Forbidden",message:s},this.statusText=s,this.cause=e?JSON.stringify(e):void 0,y("Forbidden",this.body,this.cause)}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.body),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.body,s)}}class U{constructor(s,e){o(this,"body");o(this,"cause");o(this,"status",404);o(this,"statusText");this.body={name:"NotFound",message:s},this.statusText=s,this.cause=e?JSON.stringify(e):void 0,y("NotFound",this.body,this.cause)}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.body),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.body,s)}}class j{constructor(s,e){o(this,"body");o(this,"cause");o(this,"status",501);o(this,"statusText");this.body={name:"NotImplemented",message:s},this.statusText=s,this.cause=e?JSON.stringify(e):void 0,y("NotImplemented",this.body,this.cause)}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.body),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.body,s)}}class R{constructor(s,e){o(this,"body");o(this,"cause");o(this,"status",500);o(this,"statusText");this.body={name:"ServerError",message:s},this.statusText=s,this.cause=e?JSON.stringify(e):void 0,y("ServerError",this.body,this.cause)}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.body),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.body,s)}}class O{constructor(s,e){o(this,"body");o(this,"cause");o(this,"status",401);o(this,"statusText");this.body={name:"Unauthorized",message:s},this.statusText=s,this.cause=e?JSON.stringify(e):void 0,y("Unauthorized",this.body,this.cause)}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.body),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.body,s)}}class S{constructor(s,e=!1){o(this,"body");o(this,"status",422);o(this,"statusText");this.statusText=s.message||"Unprocessable Entity",this.body={name:"UnprocessableEntity",message:s.message||null,data:s.data,fieldErrors:s.fieldErrors,fields:s.fields},e&&y("UnprocessableEntity",this.body)}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.body),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.body,s)}}class E{constructor(s,e){o(this,"body");o(this,"headers");o(this,"status");o(this,"statusText");this.body=s,this.headers=(e==null?void 0:e.headers)||{},this.status=(e==null?void 0:e.status)||201,this.statusText=(e==null?void 0:e.statusText)||"Resource created successfully"}toResponse(){const s={headers:{"Content-Type":"application/json",...this.headers},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.body),s)}toJson(){const s={headers:this.headers,status:this.status,statusText:this.statusText};return Response.json(this.body,s)}}class J{constructor(s,e){o(this,"body");o(this,"headers");o(this,"status");o(this,"statusText");this.body=s,this.headers=(e==null?void 0:e.headers)||{},this.status=(e==null?void 0:e.status)||302,this.statusText=(e==null?void 0:e.statusText)||"Found"}toResponse(){const s={headers:{"Content-Type":"application/json",...this.headers},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.body),s)}toJson(){const s={headers:this.headers,status:this.status,statusText:this.statusText};return Response.json(this.body,s)}}class C{constructor(s){o(this,"headers");o(this,"status");o(this,"statusText");this.headers=(s==null?void 0:s.headers)||{},this.status=(s==null?void 0:s.status)||204,this.statusText=(s==null?void 0:s.statusText)??"No content"}toResponse(){const s={headers:this.headers,status:this.status,statusText:this.statusText};return new Response(null,s)}}class q{constructor(s,e){o(this,"body");o(this,"headers");o(this,"status");o(this,"statusText");this.body=s,this.headers=(e==null?void 0:e.headers)||{},this.status=(e==null?void 0:e.status)||200,this.statusText=(e==null?void 0:e.statusText)??"OK"}toResponse(){const s={headers:{"Content-Type":"application/json",...this.headers},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.body),s)}toJson(){const s={headers:this.headers,status:this.status,statusText:this.statusText};return Response.json(this.body,s)}}class P{constructor(s,e){o(this,"body");o(this,"headers");o(this,"status");o(this,"statusText");this.body=s,this.headers=(e==null?void 0:e.headers)||{},this.status=(e==null?void 0:e.status)||200,this.statusText=(e==null?void 0:e.statusText)||"Resource updated successfully"}toResponse(){const s={headers:{"Content-Type":"application/json",...this.headers},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.body),s)}toJson(){const s={headers:this.headers,status:this.status,statusText:this.statusText};return Response.json(this.body,s)}}class v{static mapHeaders(s){return s instanceof Headers?Object.fromEntries(s.entries()):typeof s=="object"?Object.entries(s).reduce((e,[n,r])=>(typeof r=="string"?e[n]=r:Array.isArray(r)?e[n]=r.join(", "):e[n]=JSON.stringify(r),e),{}):{}}static mapQueryParams(s){const e={};return s.forEach((n,r)=>{e[r]=n}),e}static handle(s){return{rawUrl:s.url,status:s.status,method:s.method,token:null,elapsedTime:s.elapsedTime,requestHeaders:this.mapHeaders(s.requestHeaders),requestBody:s.requestBody||null,queryParams:this.mapQueryParams(s.queryParams),responseHeaders:this.mapHeaders(s.responseHeaders),responseBody:s.responseBody||null}}}class w{static setArkynConfig(s){if(this.arkynConfig)return;let n=s.arkynLogBaseApiUrl||"https://logs-arkyn-flow-logs.vw6wo7.easypanel.host";n=n+"/http-traffic-records/:trafficSourceId",this.arkynConfig={arkynTrafficSourceId:s.arkynTrafficSourceId,arkynUserToken:s.arkynUserToken,arkynApiUrl:n}}static getArkynConfig(){return this.arkynConfig}static resetArkynConfig(){this.arkynConfig=void 0}}o(w,"arkynConfig");async function L(t){const s=w.getArkynConfig();if(!s)return;const{arkynUserToken:e,arkynApiUrl:n}=s,{elapsedTime:r,method:u,queryParams:p,requestBody:h,requestHeaders:i,responseBody:d,responseHeaders:c,status:T,token:es,rawUrl:Y}=t;try{const b=new URL(Y);let B="HTTPS";b.protocol==="http:"&&(B="HTTP");const X=JSON.stringify({domainUrl:b.protocol+"//"+b.host,pathnameUrl:b.pathname,status:T,protocol:B,method:u,trafficUserId:null,elapsedTime:r,requestHeaders:i,requestBody:h,queryParams:p,responseHeaders:c,responseBody:d});await fetch(n.replace(":trafficSourceId",s.arkynTrafficSourceId),{method:"POST",body:X,headers:{"Content-Type":"application/json",Authorization:`Bearer ${e}`}})}catch(b){y("arkyn log error","Error sending request",b)}}async function g(t,s,e={},n){const r={POST:"Resource created successfully",PUT:"Resource updated successfully",DELETE:"Resource deleted successfully",PATCH:"Resource patched successfully",GET:"Request successful"};try{const u=performance.now(),p={...e,"Content-Type":"application/json"},h=await fetch(s,{method:t,headers:p,body:n?JSON.stringify(n):void 0}),i=performance.now()-u,d=h.status;let c=null;try{c=await h.json()}catch{c=null}const T=v.handle({elapsedTime:i,method:t,queryParams:new URL(s).searchParams,requestHeaders:p,requestBody:n,responseBody:c,responseHeaders:h.headers,status:d,url:s});return L(T),h.ok?{success:!0,status:d,message:(c==null?void 0:c.message)||r[t],response:c,cause:null}:{success:!1,status:d,message:(c==null?void 0:c.message)||h.statusText||"Request failed",response:c,cause:null}}catch(u){return y("Network error or request failed",null,u),{success:!1,status:0,message:"Network error or request failed",response:null,cause:u instanceof Error?u.message:String(u)}}}async function A(t,s={},e){return g("DELETE",t,s,e)}async function F(t,s={}){return g("GET",t,s)}async function $(t,s={},e){return g("PATCH",t,s,e)}async function D(t,s={},e){return g("POST",t,s,e)}async function M(t,s={},e){return g("PUT",t,s,e)}class z{constructor(s){o(this,"baseUrl");o(this,"baseHeaders");o(this,"baseToken");this.baseUrl=s.baseUrl,this.baseHeaders=s.baseHeaders||void 0,this.baseToken=s.baseToken||void 0}generateURL(s){return this.baseUrl+s}generateHeaders(s,e){let n={};return this.baseToken&&(n={Authorization:`Bearer ${this.baseToken}`}),this.baseHeaders&&(n={...n,...this.baseHeaders}),s&&(n={...n,...s}),e&&(n={...n,Authorization:`Bearer ${e}`}),n}async get(s,e){const n=this.generateURL(s),r=this.generateHeaders((e==null?void 0:e.headers)||{},e==null?void 0:e.token);return await F(n,r)}async post(s,e){const n=this.generateURL(s),r=this.generateHeaders((e==null?void 0:e.headers)||{},e==null?void 0:e.token),u=e==null?void 0:e.body;return await D(n,r,u)}async put(s,e){const n=this.generateURL(s),r=this.generateHeaders((e==null?void 0:e.headers)||{},e==null?void 0:e.token),u=e==null?void 0:e.body;return await M(n,r,u)}async patch(s,e){const n=this.generateURL(s),r=this.generateHeaders((e==null?void 0:e.headers)||{},e==null?void 0:e.token),u=e==null?void 0:e.body;return await $(n,r,u)}async delete(s,e){const n=this.generateURL(s),r=this.generateHeaders((e==null?void 0:e.headers)||{},e==null?void 0:e.token),u=e==null?void 0:e.body;return await A(n,r,u)}}function G(t,s){var e,n,r;return t!=null&&t.message&&typeof(t==null?void 0:t.message)=="string"?t==null?void 0:t.message:t!=null&&t.error&&typeof(t==null?void 0:t.error)=="string"?t==null?void 0:t.error:(e=t==null?void 0:t.error)!=null&&e.message&&typeof((n=t==null?void 0:t.error)==null?void 0:n.message)=="string"?(r=t==null?void 0:t.error)==null?void 0:r.message:s!=null&&s.statusText&&typeof(s==null?void 0:s.statusText)=="string"?s==null?void 0:s.statusText:"Missing error message"}const V=async t=>{let s;const e=await t.arrayBuffer(),n=new TextDecoder().decode(e);try{s=JSON.parse(n)}catch{try{if(n.includes("=")){const u=new URLSearchParams(n);s=Object.fromEntries(u.entries())}else throw new m("Invalid URLSearchParams format")}catch{throw new m("Failed to extract data from request")}}return s};function _(t){switch(!0){case t instanceof Response:return t;case t instanceof J:return t.toResponse();case t instanceof E:return t.toResponse();case t instanceof P:return t.toResponse();case t instanceof q:return t.toResponse();case t instanceof C:return t.toResponse()}switch(!0){case t instanceof N:return t.toResponse();case t instanceof m:return t.toResponse();case t instanceof k:return t.toResponse();case t instanceof I:return t.toResponse();case t instanceof U:return t.toResponse();case t instanceof j:return t.toResponse();case t instanceof R:return t.toResponse();case t instanceof O:return t.toResponse();case t instanceof S:return t.toResponse()}return new R("Server error",t).toResponse()}function H([t,s]){const e=s.safeParse(t);if(e.success===!1){const n=Object.fromEntries(e.error.issues.map(r=>(console.log(r),[r.path.join("."),r.message])));return{success:e.success,fieldErrors:n,fields:t}}else return{success:e.success,data:e.data}}const K=(t,s="")=>{const e=new URL(t.url);if(s==="")return e.searchParams;const n=Array.from(e.searchParams.entries()).filter(([r])=>r.startsWith(`${s}:`)).map(([r,u])=>[r.replace(`${s}:`,""),u]);return new URLSearchParams(n)};function Q(t){const s="Error validating:",e=t.issues.map(({path:n,message:r})=>`-> ${n.join(".")}: ${r}`);return[s,...e].join(`
8
+ `)}class W{constructor(s){o(this,"functionName");o(this,"callerInfo");this.schema=s;const{callerInfo:e,functionName:n}=f();this.callerInfo=e,this.functionName=n}isValid(s){return this.schema.safeParse(s).success}safeValidate(s){return this.schema.safeParse(s)}validate(s){try{return this.schema.parse(s)}catch(e){throw new R(Q(e))}}formValidate(s,e){const n=H([s,this.schema]);if(!n.success){y("UnprocessableEntity",n);const r=Object.keys(n.fieldErrors)[0];throw new S({fields:n.fields,fieldErrors:n.fieldErrors,data:{scrollTo:r},message:e},!1)}return n.data}}a.ApiService=z,a.ArkynLogService=w,a.BadGateway=N,a.BadRequest=m,a.Conflict=k,a.Created=E,a.Forbidden=I,a.Found=J,a.HttpDebugService=x,a.NoContent=C,a.NotFound=U,a.NotImplemented=j,a.SchemaValidator=W,a.ServerError=R,a.Success=q,a.Unauthorized=O,a.UnprocessableEntity=S,a.Updated=P,a.decodeErrorMessageFromRequest=G,a.decodeRequestBody=V,a.errorHandler=_,a.formParse=H,a.getCaller=f,a.getScopedParams=K,a.httpDebug=y,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@arkyn/server",
3
- "version": "3.0.1-beta.58",
3
+ "version": "3.0.1-beta.59",
4
4
  "author": "Arkyn | Lucas Gonçalves",
5
- "main": "./dist/index.js",
6
- "module": "./dist/index.js",
5
+ "main": "./dist/bundle.js",
6
+ "module": "./dist/bundle.js",
7
7
  "type": "module",
8
8
  "types": "./dist/index.d.ts",
9
9
  "license": "Apache-2.0",
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "scripts": {
29
29
  "clean": "rm -rf dist",
30
- "build": "bun run clean && bunx tsc --project tsconfig.json",
30
+ "build": "bunx vite build && tsc",
31
31
  "test": "vitest --config vitest.config.ts",
32
32
  "typecheck": "bunx tsc --project tsconfig.json --noEmit"
33
33
  },
@@ -38,6 +38,7 @@
38
38
  "devDependencies": {
39
39
  "bun-types": "latest",
40
40
  "vitest": "^3.2.4",
41
- "typescript": "^5.9.2"
41
+ "typescript": "^5.9.2",
42
+ "vite": "^5.4.19"
42
43
  }
43
44
  }