@arkyn/server 3.0.1-beta.119 → 3.0.1-beta.120
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 +144 -115
- package/dist/bundle.umd.cjs +5 -5
- package/dist/services/apiService.d.ts.map +1 -1
- package/dist/services/apiService.js +50 -14
- package/package.json +1 -1
package/dist/bundle.js
CHANGED
|
@@ -2,8 +2,8 @@ var k = Object.defineProperty;
|
|
|
2
2
|
var w = (e, s, t) => s in e ? k(e, s, { enumerable: !0, configurable: !0, writable: !0, value: t }) : e[s] = t;
|
|
3
3
|
var c = (e, s, t) => w(e, typeof s != "symbol" ? s + "" : s, t);
|
|
4
4
|
import "zod";
|
|
5
|
-
import { removeNonNumeric as R, ValidateDateService as
|
|
6
|
-
import
|
|
5
|
+
import { removeNonNumeric as R, ValidateDateService as j } from "@arkyn/shared";
|
|
6
|
+
import J from "node:dns";
|
|
7
7
|
import { countries as _ } from "@arkyn/templates";
|
|
8
8
|
const I = {};
|
|
9
9
|
class S {
|
|
@@ -54,36 +54,36 @@ class S {
|
|
|
54
54
|
static getCaller() {
|
|
55
55
|
const s = process.cwd(), r = (new Error().stack || "").split(`
|
|
56
56
|
`).map((l) => l.trim());
|
|
57
|
-
let
|
|
58
|
-
for (;
|
|
59
|
-
|
|
57
|
+
let o = 2;
|
|
58
|
+
for (; o < r.length && (r[o].includes("node:internal") || r[o].includes("/node_modules/")); )
|
|
59
|
+
o++;
|
|
60
60
|
if (this.ignoreFiles.length > 0)
|
|
61
|
-
for (;
|
|
62
|
-
(l) => r[
|
|
61
|
+
for (; o < r.length && this.ignoreFiles.some(
|
|
62
|
+
(l) => r[o].includes(l)
|
|
63
63
|
); )
|
|
64
|
-
|
|
65
|
-
const a = r[
|
|
66
|
-
let u = "Unknown function",
|
|
64
|
+
o++;
|
|
65
|
+
const a = r[o] || "";
|
|
66
|
+
let u = "Unknown function", i = "Unknown caller";
|
|
67
67
|
const h = a.match(/at\s+([^(\s]+)\s+\(([^)]+)\)/);
|
|
68
68
|
if (h)
|
|
69
|
-
u = h[1],
|
|
69
|
+
u = h[1], i = h[2];
|
|
70
70
|
else {
|
|
71
71
|
const l = a.match(/at\s+(.+)/);
|
|
72
72
|
if (l) {
|
|
73
|
-
|
|
74
|
-
const
|
|
75
|
-
|
|
73
|
+
i = l[1];
|
|
74
|
+
const p = i.match(/at\s+([^(\s]+)\s+/);
|
|
75
|
+
p && p[1] !== "new" && (u = p[1]);
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
)),
|
|
78
|
+
i.includes("(") && (i = i.substring(
|
|
79
|
+
i.indexOf("(") + 1,
|
|
80
|
+
i.lastIndexOf(")")
|
|
81
|
+
)), i = i.split(":").slice(0, -2).join(":");
|
|
82
82
|
try {
|
|
83
|
-
|
|
83
|
+
i = I.relative(s, i);
|
|
84
84
|
} catch {
|
|
85
85
|
}
|
|
86
|
-
return { functionName: u, callerInfo:
|
|
86
|
+
return { functionName: u, callerInfo: i };
|
|
87
87
|
}
|
|
88
88
|
}
|
|
89
89
|
/**
|
|
@@ -91,10 +91,10 @@ class S {
|
|
|
91
91
|
* When set, the `getCaller` function will skip stack frames containing this file name.
|
|
92
92
|
*/
|
|
93
93
|
c(S, "ignoreFiles", []);
|
|
94
|
-
function
|
|
94
|
+
function d(e) {
|
|
95
95
|
var t;
|
|
96
96
|
if (process.env.NODE_ENV === "development" || ((t = process.env) == null ? void 0 : t.DEBUG_MODE) === "true") {
|
|
97
|
-
const
|
|
97
|
+
const o = `${{
|
|
98
98
|
yellow: "\x1B[33m",
|
|
99
99
|
cyan: "\x1B[36m",
|
|
100
100
|
red: "\x1B[31m",
|
|
@@ -103,7 +103,7 @@ function p(e) {
|
|
|
103
103
|
let a = `
|
|
104
104
|
`;
|
|
105
105
|
e.debugs.forEach((u) => {
|
|
106
|
-
a += `${
|
|
106
|
+
a += `${o} ${u.trim()}
|
|
107
107
|
`;
|
|
108
108
|
}), console.log(a);
|
|
109
109
|
}
|
|
@@ -141,7 +141,7 @@ class f {
|
|
|
141
141
|
}
|
|
142
142
|
onDebug() {
|
|
143
143
|
const s = [], { callerInfo: t, functionName: n } = S.getCaller();
|
|
144
|
-
s.push(`Caller Function: ${n}`), s.push(`Caller Location: ${t}`), this._statusText && s.push(`Message: ${this._statusText}`), this._cause && s.push(`Cause: ${JSON.stringify(this._cause)}`),
|
|
144
|
+
s.push(`Caller Function: ${n}`), s.push(`Caller Location: ${t}`), this._statusText && s.push(`Message: ${this._statusText}`), this._cause && s.push(`Cause: ${JSON.stringify(this._cause)}`), d({ scheme: "red", name: this._name, debugs: s });
|
|
145
145
|
}
|
|
146
146
|
makeBody() {
|
|
147
147
|
return {
|
|
@@ -500,7 +500,7 @@ class b extends f {
|
|
|
500
500
|
return Response.json(this.makeBody(), s);
|
|
501
501
|
}
|
|
502
502
|
}
|
|
503
|
-
class
|
|
503
|
+
class g {
|
|
504
504
|
constructor() {
|
|
505
505
|
c(this, "_body", null);
|
|
506
506
|
c(this, "_name", "SuccessResponse");
|
|
@@ -544,7 +544,7 @@ class y {
|
|
|
544
544
|
*/
|
|
545
545
|
onDebug(s) {
|
|
546
546
|
const t = [], { callerInfo: n, functionName: r } = S.getCaller();
|
|
547
|
-
t.push(`Caller Function: ${r}`), t.push(`Caller Location: ${n}`), this.statusText && t.push(`Message: ${this.statusText}`), s && t.push(`Body: ${JSON.stringify(s)}`),
|
|
547
|
+
t.push(`Caller Function: ${r}`), t.push(`Caller Location: ${n}`), this.statusText && t.push(`Message: ${this.statusText}`), s && t.push(`Body: ${JSON.stringify(s)}`), d({ scheme: "red", name: this.name, debugs: t });
|
|
548
548
|
}
|
|
549
549
|
makeBody() {
|
|
550
550
|
return {
|
|
@@ -554,7 +554,7 @@ class y {
|
|
|
554
554
|
};
|
|
555
555
|
}
|
|
556
556
|
}
|
|
557
|
-
class F extends
|
|
557
|
+
class F extends g {
|
|
558
558
|
/**
|
|
559
559
|
* Creates an instance of the `Created` class.
|
|
560
560
|
*
|
|
@@ -589,7 +589,7 @@ class F extends y {
|
|
|
589
589
|
return Response.json(this.body, s);
|
|
590
590
|
}
|
|
591
591
|
}
|
|
592
|
-
class z extends
|
|
592
|
+
class z extends g {
|
|
593
593
|
/**
|
|
594
594
|
* Creates an instance of the `Found` class.
|
|
595
595
|
*
|
|
@@ -624,7 +624,7 @@ class z extends y {
|
|
|
624
624
|
return Response.json(this.body, s);
|
|
625
625
|
}
|
|
626
626
|
}
|
|
627
|
-
class M extends
|
|
627
|
+
class M extends g {
|
|
628
628
|
/**
|
|
629
629
|
* Creates an instance of the `NoContent` class.
|
|
630
630
|
* @param {string} message - A message describing the creation status.
|
|
@@ -645,7 +645,7 @@ class M extends y {
|
|
|
645
645
|
return new Response(null, s);
|
|
646
646
|
}
|
|
647
647
|
}
|
|
648
|
-
class V extends
|
|
648
|
+
class V extends g {
|
|
649
649
|
/**
|
|
650
650
|
* Creates an instance of the `Success` class.
|
|
651
651
|
*
|
|
@@ -680,7 +680,7 @@ class V extends y {
|
|
|
680
680
|
return Response.json(this.body, s);
|
|
681
681
|
}
|
|
682
682
|
}
|
|
683
|
-
class Z extends
|
|
683
|
+
class Z extends g {
|
|
684
684
|
/**
|
|
685
685
|
* Creates an instance of the `Updated` class.
|
|
686
686
|
*
|
|
@@ -764,7 +764,7 @@ class G {
|
|
|
764
764
|
};
|
|
765
765
|
}
|
|
766
766
|
}
|
|
767
|
-
class
|
|
767
|
+
class $ {
|
|
768
768
|
/**
|
|
769
769
|
* Sets the log service configuration only once.
|
|
770
770
|
* If the configuration is already set, the call is ignored.
|
|
@@ -793,55 +793,55 @@ class v {
|
|
|
793
793
|
this.config = void 0;
|
|
794
794
|
}
|
|
795
795
|
}
|
|
796
|
-
c(
|
|
796
|
+
c($, "config");
|
|
797
797
|
async function W(e) {
|
|
798
|
-
const s =
|
|
798
|
+
const s = $.getConfig();
|
|
799
799
|
if (!s) return;
|
|
800
800
|
const { userToken: t, apiUrl: n, trafficSourceId: r } = s, {
|
|
801
|
-
elapsedTime:
|
|
801
|
+
elapsedTime: o,
|
|
802
802
|
method: a,
|
|
803
803
|
queryParams: u,
|
|
804
|
-
requestBody:
|
|
804
|
+
requestBody: i,
|
|
805
805
|
requestHeaders: h,
|
|
806
806
|
responseBody: l,
|
|
807
|
-
responseHeaders:
|
|
808
|
-
status:
|
|
807
|
+
responseHeaders: p,
|
|
808
|
+
status: v,
|
|
809
809
|
token: xs,
|
|
810
|
-
rawUrl:
|
|
810
|
+
rawUrl: B
|
|
811
811
|
} = e;
|
|
812
812
|
if (process.env.NODE_ENV !== "development")
|
|
813
813
|
try {
|
|
814
|
-
const m = new URL(
|
|
814
|
+
const m = new URL(B);
|
|
815
815
|
let D = "https";
|
|
816
816
|
m.protocol === "http:" && (D = "http");
|
|
817
|
-
const
|
|
817
|
+
const O = JSON.stringify({
|
|
818
818
|
domainUrl: m.protocol + "//" + m.host,
|
|
819
819
|
pathnameUrl: m.pathname,
|
|
820
820
|
trafficSourceId: r,
|
|
821
|
-
status:
|
|
821
|
+
status: v,
|
|
822
822
|
protocol: D,
|
|
823
823
|
method: a.toLowerCase(),
|
|
824
824
|
trafficUserId: null,
|
|
825
|
-
elapsedTime:
|
|
825
|
+
elapsedTime: o,
|
|
826
826
|
requestHeaders: JSON.stringify(h),
|
|
827
|
-
requestBody: JSON.stringify(
|
|
827
|
+
requestBody: JSON.stringify(i),
|
|
828
828
|
queryParams: JSON.stringify(u),
|
|
829
|
-
responseHeaders: JSON.stringify(
|
|
829
|
+
responseHeaders: JSON.stringify(p),
|
|
830
830
|
responseBody: JSON.stringify(l)
|
|
831
831
|
}), P = {
|
|
832
832
|
"Content-Type": "application/json",
|
|
833
833
|
Authorization: `Bearer ${t}`
|
|
834
834
|
};
|
|
835
|
-
await fetch(n, { method: "POST", body:
|
|
835
|
+
await fetch(n, { method: "POST", body: O, headers: P });
|
|
836
836
|
} catch (m) {
|
|
837
|
-
|
|
837
|
+
d({
|
|
838
838
|
debugs: [`Error sending request: ${m}`],
|
|
839
839
|
name: "LogError",
|
|
840
840
|
scheme: "red"
|
|
841
841
|
});
|
|
842
842
|
}
|
|
843
843
|
}
|
|
844
|
-
async function
|
|
844
|
+
async function y(e) {
|
|
845
845
|
let s = e.url;
|
|
846
846
|
e.urlParams && Object.entries(e.urlParams).forEach(([n, r]) => {
|
|
847
847
|
s = s.replaceAll(`:${n}`, r);
|
|
@@ -854,16 +854,16 @@ async function g(e) {
|
|
|
854
854
|
GET: "Request successful"
|
|
855
855
|
};
|
|
856
856
|
try {
|
|
857
|
-
const n = performance.now(), r = { ...e.headers, "Content-Type": "application/json" },
|
|
857
|
+
const n = performance.now(), r = { ...e.headers, "Content-Type": "application/json" }, o = await fetch(s, {
|
|
858
858
|
headers: r,
|
|
859
859
|
method: e.method,
|
|
860
860
|
body: e.body ? JSON.stringify(e.body) : void 0
|
|
861
|
-
}), a = performance.now() - n, u =
|
|
862
|
-
let
|
|
861
|
+
}), a = performance.now() - n, u = o.status;
|
|
862
|
+
let i = null;
|
|
863
863
|
try {
|
|
864
|
-
|
|
864
|
+
i = await o.json();
|
|
865
865
|
} catch {
|
|
866
|
-
|
|
866
|
+
i = null;
|
|
867
867
|
}
|
|
868
868
|
const h = G.handle({
|
|
869
869
|
elapsedTime: a,
|
|
@@ -871,27 +871,27 @@ async function g(e) {
|
|
|
871
871
|
queryParams: new URL(s).searchParams,
|
|
872
872
|
requestHeaders: r,
|
|
873
873
|
requestBody: e.body,
|
|
874
|
-
responseBody:
|
|
875
|
-
responseHeaders:
|
|
874
|
+
responseBody: i,
|
|
875
|
+
responseHeaders: o.headers,
|
|
876
876
|
status: u,
|
|
877
877
|
rawUrl: e.url,
|
|
878
878
|
urlParams: e.urlParams
|
|
879
879
|
});
|
|
880
|
-
return W(h),
|
|
880
|
+
return W(h), o.ok ? {
|
|
881
881
|
success: !0,
|
|
882
882
|
status: u,
|
|
883
|
-
message: (
|
|
884
|
-
response:
|
|
883
|
+
message: (i == null ? void 0 : i.message) || t[e.method],
|
|
884
|
+
response: i,
|
|
885
885
|
cause: null
|
|
886
886
|
} : {
|
|
887
887
|
success: !1,
|
|
888
888
|
status: u,
|
|
889
|
-
message: (
|
|
890
|
-
response:
|
|
889
|
+
message: (i == null ? void 0 : i.message) || o.statusText || "Request failed",
|
|
890
|
+
response: i,
|
|
891
891
|
cause: null
|
|
892
892
|
};
|
|
893
893
|
} catch (n) {
|
|
894
|
-
return
|
|
894
|
+
return d({
|
|
895
895
|
debugs: [`Network error or request failed: ${n}`],
|
|
896
896
|
name: "MakeRequestError",
|
|
897
897
|
scheme: "red"
|
|
@@ -905,7 +905,7 @@ async function g(e) {
|
|
|
905
905
|
}
|
|
906
906
|
}
|
|
907
907
|
async function Y(e) {
|
|
908
|
-
return
|
|
908
|
+
return y({
|
|
909
909
|
method: "DELETE",
|
|
910
910
|
url: e.url,
|
|
911
911
|
urlParams: e.urlParams,
|
|
@@ -914,7 +914,7 @@ async function Y(e) {
|
|
|
914
914
|
});
|
|
915
915
|
}
|
|
916
916
|
async function K(e) {
|
|
917
|
-
return
|
|
917
|
+
return y({
|
|
918
918
|
method: "GET",
|
|
919
919
|
url: e.url,
|
|
920
920
|
urlParams: e.urlParams,
|
|
@@ -922,7 +922,7 @@ async function K(e) {
|
|
|
922
922
|
});
|
|
923
923
|
}
|
|
924
924
|
async function Q(e) {
|
|
925
|
-
return
|
|
925
|
+
return y({
|
|
926
926
|
method: "PATCH",
|
|
927
927
|
url: e.url,
|
|
928
928
|
urlParams: e.urlParams,
|
|
@@ -931,7 +931,7 @@ async function Q(e) {
|
|
|
931
931
|
});
|
|
932
932
|
}
|
|
933
933
|
async function X(e) {
|
|
934
|
-
return
|
|
934
|
+
return y({
|
|
935
935
|
method: "POST",
|
|
936
936
|
url: e.url,
|
|
937
937
|
urlParams: e.urlParams,
|
|
@@ -940,7 +940,7 @@ async function X(e) {
|
|
|
940
940
|
});
|
|
941
941
|
}
|
|
942
942
|
async function ss(e) {
|
|
943
|
-
return
|
|
943
|
+
return y({
|
|
944
944
|
method: "PUT",
|
|
945
945
|
url: e.url,
|
|
946
946
|
urlParams: e.urlParams,
|
|
@@ -958,8 +958,8 @@ class Es {
|
|
|
958
958
|
}
|
|
959
959
|
onDebug(s, t, n) {
|
|
960
960
|
if (this.enableDebug) {
|
|
961
|
-
const r = [];
|
|
962
|
-
r.push(`Base URL: ${this.baseUrl}`), r.push(`Endpoint: ${s}`), r.push(`Method: ${t}`), n
|
|
961
|
+
const r = [], o = (a) => a ? JSON.stringify(a, null, 2) : null;
|
|
962
|
+
r.push(`Base URL: ${this.baseUrl}`), r.push(`Endpoint: ${s}`), r.push(`Method: ${t}`), r.push(`Status: ${n.status}`), r.push(`Message: ${n.message}`), r.push(`Headers: ${o(n.headers)}`), r.push(`Body: ${o(n.body)}`), r.push(`Response: ${o(n.response)}`), d({ debugs: r, name: "ApiDebug", scheme: "yellow" });
|
|
963
963
|
}
|
|
964
964
|
}
|
|
965
965
|
generateHeaders(s, t) {
|
|
@@ -973,12 +973,17 @@ class Es {
|
|
|
973
973
|
* @returns The API response data.
|
|
974
974
|
*/
|
|
975
975
|
async get(s, t) {
|
|
976
|
-
const n = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token)
|
|
977
|
-
return this.onDebug(s, "get", [n]), await K({
|
|
976
|
+
const n = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token), r = await K({
|
|
978
977
|
url: this.baseUrl + s,
|
|
979
978
|
urlParams: (t == null ? void 0 : t.urlParams) || {},
|
|
980
979
|
headers: n
|
|
981
980
|
});
|
|
981
|
+
return this.onDebug(s, "get", {
|
|
982
|
+
headers: n,
|
|
983
|
+
message: r.message,
|
|
984
|
+
status: r.status,
|
|
985
|
+
response: r.response
|
|
986
|
+
}), r;
|
|
982
987
|
}
|
|
983
988
|
/**
|
|
984
989
|
* Sends a post request to the specified endpoint.
|
|
@@ -987,13 +992,19 @@ class Es {
|
|
|
987
992
|
* @returns The API response data.
|
|
988
993
|
*/
|
|
989
994
|
async post(s, t) {
|
|
990
|
-
const n = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token), r = t == null ? void 0 : t.body
|
|
991
|
-
return this.onDebug(s, "post", [n, r]), await X({
|
|
995
|
+
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 X({
|
|
992
996
|
url: this.baseUrl + s,
|
|
993
997
|
urlParams: (t == null ? void 0 : t.urlParams) || {},
|
|
994
998
|
headers: n,
|
|
995
999
|
body: r
|
|
996
1000
|
});
|
|
1001
|
+
return this.onDebug(s, "post", {
|
|
1002
|
+
headers: n,
|
|
1003
|
+
body: r,
|
|
1004
|
+
message: o.message,
|
|
1005
|
+
status: o.status,
|
|
1006
|
+
response: o.response
|
|
1007
|
+
}), o;
|
|
997
1008
|
}
|
|
998
1009
|
/**
|
|
999
1010
|
* Sends a put request to the specified endpoint.
|
|
@@ -1002,13 +1013,19 @@ class Es {
|
|
|
1002
1013
|
* @returns The API response data.
|
|
1003
1014
|
*/
|
|
1004
1015
|
async put(s, t) {
|
|
1005
|
-
const n = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token), r = t == null ? void 0 : t.body
|
|
1006
|
-
return this.onDebug(s, "put", [n, r]), await ss({
|
|
1016
|
+
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 ss({
|
|
1007
1017
|
url: this.baseUrl + s,
|
|
1008
1018
|
urlParams: (t == null ? void 0 : t.urlParams) || {},
|
|
1009
1019
|
headers: n,
|
|
1010
1020
|
body: r
|
|
1011
1021
|
});
|
|
1022
|
+
return this.onDebug(s, "put", {
|
|
1023
|
+
headers: n,
|
|
1024
|
+
body: r,
|
|
1025
|
+
message: o.message,
|
|
1026
|
+
status: o.status,
|
|
1027
|
+
response: o.response
|
|
1028
|
+
}), o;
|
|
1012
1029
|
}
|
|
1013
1030
|
/**
|
|
1014
1031
|
* Sends a patch request to the specified endpoint.
|
|
@@ -1017,13 +1034,19 @@ class Es {
|
|
|
1017
1034
|
* @returns The API response data.
|
|
1018
1035
|
*/
|
|
1019
1036
|
async patch(s, t) {
|
|
1020
|
-
const n = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token), r = t == null ? void 0 : t.body
|
|
1021
|
-
return this.onDebug(s, "patch", [n, r]), await Q({
|
|
1037
|
+
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 Q({
|
|
1022
1038
|
url: this.baseUrl + s,
|
|
1023
1039
|
urlParams: (t == null ? void 0 : t.urlParams) || {},
|
|
1024
1040
|
headers: n,
|
|
1025
1041
|
body: r
|
|
1026
1042
|
});
|
|
1043
|
+
return this.onDebug(s, "patch", {
|
|
1044
|
+
headers: n,
|
|
1045
|
+
body: r,
|
|
1046
|
+
message: o.message,
|
|
1047
|
+
status: o.status,
|
|
1048
|
+
response: o.response
|
|
1049
|
+
}), o;
|
|
1027
1050
|
}
|
|
1028
1051
|
/**
|
|
1029
1052
|
* Sends a delete request to the specified endpoint.
|
|
@@ -1032,16 +1055,22 @@ class Es {
|
|
|
1032
1055
|
* @returns The API response data.
|
|
1033
1056
|
*/
|
|
1034
1057
|
async delete(s, t) {
|
|
1035
|
-
const n = this.generateHeaders((t == null ? void 0 : t.headers) || {}, t == null ? void 0 : t.token), r = t == null ? void 0 : t.body
|
|
1036
|
-
return this.onDebug(s, "delete", [n, r]), await Y({
|
|
1058
|
+
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 Y({
|
|
1037
1059
|
url: this.baseUrl + s,
|
|
1038
1060
|
urlParams: (t == null ? void 0 : t.urlParams) || {},
|
|
1039
1061
|
headers: n,
|
|
1040
1062
|
body: r
|
|
1041
1063
|
});
|
|
1064
|
+
return this.onDebug(s, "delete", {
|
|
1065
|
+
headers: n,
|
|
1066
|
+
body: r,
|
|
1067
|
+
message: o.message,
|
|
1068
|
+
status: o.status,
|
|
1069
|
+
response: o.response
|
|
1070
|
+
}), o;
|
|
1042
1071
|
}
|
|
1043
1072
|
}
|
|
1044
|
-
async function
|
|
1073
|
+
async function $s(e) {
|
|
1045
1074
|
let s;
|
|
1046
1075
|
const t = await e.arrayBuffer(), n = new TextDecoder().decode(t);
|
|
1047
1076
|
try {
|
|
@@ -1049,8 +1078,8 @@ async function vs(e) {
|
|
|
1049
1078
|
} catch {
|
|
1050
1079
|
try {
|
|
1051
1080
|
if (n.includes("=")) {
|
|
1052
|
-
const
|
|
1053
|
-
s = Object.fromEntries(
|
|
1081
|
+
const o = new URLSearchParams(n);
|
|
1082
|
+
s = Object.fromEntries(o.entries());
|
|
1054
1083
|
} else
|
|
1055
1084
|
throw new T("Invalid URLSearchParams format");
|
|
1056
1085
|
} catch {
|
|
@@ -1059,11 +1088,11 @@ async function vs(e) {
|
|
|
1059
1088
|
}
|
|
1060
1089
|
return s;
|
|
1061
1090
|
}
|
|
1062
|
-
function
|
|
1091
|
+
function vs(e, s) {
|
|
1063
1092
|
var t, n, r;
|
|
1064
1093
|
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";
|
|
1065
1094
|
}
|
|
1066
|
-
function
|
|
1095
|
+
function Bs(e) {
|
|
1067
1096
|
switch (!0) {
|
|
1068
1097
|
case e instanceof Response:
|
|
1069
1098
|
return e;
|
|
@@ -1140,12 +1169,12 @@ function ts([
|
|
|
1140
1169
|
data: t.data
|
|
1141
1170
|
};
|
|
1142
1171
|
}
|
|
1143
|
-
function
|
|
1172
|
+
function Os(e, s = "") {
|
|
1144
1173
|
const t = new URL(e.url);
|
|
1145
1174
|
if (s === "") return t.searchParams;
|
|
1146
1175
|
const n = Array.from(
|
|
1147
1176
|
t.searchParams.entries()
|
|
1148
|
-
).filter(([r]) => r.startsWith(`${s}:`)).map(([r,
|
|
1177
|
+
).filter(([r]) => r.startsWith(`${s}:`)).map(([r, o]) => [r.replace(`${s}:`, ""), o]);
|
|
1149
1178
|
return new URLSearchParams(n);
|
|
1150
1179
|
}
|
|
1151
1180
|
function ns(e) {
|
|
@@ -1326,11 +1355,11 @@ function ws(e) {
|
|
|
1326
1355
|
if (!e || e.length > 18 || e.length < 14 || /\s/.test(e)) return !1;
|
|
1327
1356
|
const t = R(e);
|
|
1328
1357
|
if (rs(t) || os(t)) return !1;
|
|
1329
|
-
const n = t.slice(0, 12), r = N(n, [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]),
|
|
1358
|
+
const n = t.slice(0, 12), r = N(n, [5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]), o = N(
|
|
1330
1359
|
n + r,
|
|
1331
1360
|
[6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2]
|
|
1332
1361
|
);
|
|
1333
|
-
return is(t) === `${r}${
|
|
1362
|
+
return is(t) === `${r}${o}`;
|
|
1334
1363
|
}
|
|
1335
1364
|
function as(e) {
|
|
1336
1365
|
return e.length !== 11;
|
|
@@ -1349,32 +1378,32 @@ function E(e, s) {
|
|
|
1349
1378
|
function cs(e) {
|
|
1350
1379
|
return e.slice(9);
|
|
1351
1380
|
}
|
|
1352
|
-
function
|
|
1381
|
+
function js(e) {
|
|
1353
1382
|
if (!e || e.length > 14 || e.length < 11 || /\s/.test(e)) return !1;
|
|
1354
1383
|
const t = R(e);
|
|
1355
1384
|
if (as(t) || us(t)) return !1;
|
|
1356
1385
|
const n = E(t, 10), r = E(t, 11);
|
|
1357
1386
|
return cs(t) === `${n}${r}`;
|
|
1358
1387
|
}
|
|
1359
|
-
function
|
|
1360
|
-
const t = (s == null ? void 0 : s.inputFormat) || "brazilianDate", n = (s == null ? void 0 : s.minYear) || 1900, r = (s == null ? void 0 : s.maxYear) || 3e3,
|
|
1361
|
-
|
|
1362
|
-
let a, u,
|
|
1388
|
+
function Js(e, s) {
|
|
1389
|
+
const t = (s == null ? void 0 : s.inputFormat) || "brazilianDate", n = (s == null ? void 0 : s.minYear) || 1900, r = (s == null ? void 0 : s.maxYear) || 3e3, o = new j();
|
|
1390
|
+
o.validateInputFormat(t);
|
|
1391
|
+
let a, u, i;
|
|
1363
1392
|
const h = e.split(/[-/]/).map(Number);
|
|
1364
1393
|
if (h.length !== 3) return !1;
|
|
1365
1394
|
try {
|
|
1366
1395
|
switch (t) {
|
|
1367
1396
|
case "brazilianDate":
|
|
1368
|
-
[a, u,
|
|
1397
|
+
[a, u, i] = h, o.validateDateParts(i, u, a);
|
|
1369
1398
|
break;
|
|
1370
1399
|
case "isoDate":
|
|
1371
|
-
[u, a,
|
|
1400
|
+
[u, a, i] = h, o.validateDateParts(i, u, a);
|
|
1372
1401
|
break;
|
|
1373
1402
|
case "timestamp":
|
|
1374
|
-
[
|
|
1403
|
+
[i, u, a] = h, o.validateDateParts(i, u, a);
|
|
1375
1404
|
break;
|
|
1376
1405
|
}
|
|
1377
|
-
return !(
|
|
1406
|
+
return !(i < n || i > r);
|
|
1378
1407
|
} catch {
|
|
1379
1408
|
return !1;
|
|
1380
1409
|
}
|
|
@@ -1397,40 +1426,40 @@ function ms(e) {
|
|
|
1397
1426
|
const t = s[s.length - 1];
|
|
1398
1427
|
return !(t.length < 2 || !/^[a-zA-Z]+$/.test(t));
|
|
1399
1428
|
}
|
|
1400
|
-
function
|
|
1429
|
+
function ps(e) {
|
|
1401
1430
|
const s = e.split("@");
|
|
1402
1431
|
if (s.length !== 2) return !1;
|
|
1403
1432
|
const [t, n] = s;
|
|
1404
1433
|
return !(!ls(t) || !ms(n));
|
|
1405
1434
|
}
|
|
1406
|
-
function
|
|
1435
|
+
function ds(e) {
|
|
1407
1436
|
const s = e.split("@");
|
|
1408
1437
|
return s.length === 2 ? s[1].toLowerCase() : null;
|
|
1409
1438
|
}
|
|
1410
|
-
const
|
|
1411
|
-
async function
|
|
1439
|
+
const gs = ["MX", "A", "AAAA"];
|
|
1440
|
+
async function ys(e, s) {
|
|
1412
1441
|
var t, n;
|
|
1413
1442
|
try {
|
|
1414
|
-
return await ((n = (t =
|
|
1443
|
+
return await ((n = (t = J) == null ? void 0 : t.promises) == null ? void 0 : n.resolve(e, s)), !0;
|
|
1415
1444
|
} catch {
|
|
1416
1445
|
return !1;
|
|
1417
1446
|
}
|
|
1418
1447
|
}
|
|
1419
1448
|
async function Ts(e) {
|
|
1420
|
-
for (const s of
|
|
1421
|
-
if (await
|
|
1449
|
+
for (const s of gs)
|
|
1450
|
+
if (await ys(e, s)) return !0;
|
|
1422
1451
|
return !1;
|
|
1423
1452
|
}
|
|
1424
1453
|
async function _s(e) {
|
|
1425
1454
|
if (!e || typeof e != "string") return !1;
|
|
1426
1455
|
const s = e.trim();
|
|
1427
|
-
if (!hs(s) || !
|
|
1428
|
-
const t =
|
|
1456
|
+
if (!hs(s) || !ps(s)) return !1;
|
|
1457
|
+
const t = ds(s);
|
|
1429
1458
|
return t ? await Ts(t) : !1;
|
|
1430
1459
|
}
|
|
1431
1460
|
function Is(e) {
|
|
1432
1461
|
if (!e) return !1;
|
|
1433
|
-
const s = e.length >= 8, t = /[A-Z]/.test(e), n = /[a-z]/.test(e), r = /\d/.test(e),
|
|
1462
|
+
const s = e.length >= 8, t = /[A-Z]/.test(e), n = /[a-z]/.test(e), r = /\d/.test(e), o = /[!@#$%^&*(),.?":;{}|<>_\-+=~`[\]\\\/]/.test(
|
|
1434
1463
|
e
|
|
1435
1464
|
);
|
|
1436
1465
|
return [
|
|
@@ -1438,7 +1467,7 @@ function Is(e) {
|
|
|
1438
1467
|
t,
|
|
1439
1468
|
n,
|
|
1440
1469
|
r,
|
|
1441
|
-
|
|
1470
|
+
o
|
|
1442
1471
|
].every((a) => a);
|
|
1443
1472
|
}
|
|
1444
1473
|
function Us(e) {
|
|
@@ -1466,7 +1495,7 @@ export {
|
|
|
1466
1495
|
S as DebugService,
|
|
1467
1496
|
A as Forbidden,
|
|
1468
1497
|
z as Found,
|
|
1469
|
-
|
|
1498
|
+
$ as LogService,
|
|
1470
1499
|
M as NoContent,
|
|
1471
1500
|
q as NotFound,
|
|
1472
1501
|
H as NotImplemented,
|
|
@@ -1476,17 +1505,17 @@ export {
|
|
|
1476
1505
|
L as Unauthorized,
|
|
1477
1506
|
b as UnprocessableEntity,
|
|
1478
1507
|
Z as Updated,
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1508
|
+
$s as decodeRequestBody,
|
|
1509
|
+
vs as decodeRequestErrorMessage,
|
|
1510
|
+
Bs as errorHandler,
|
|
1511
|
+
d as flushDebugLogs,
|
|
1483
1512
|
es as formAsyncParse,
|
|
1484
1513
|
ts as formParse,
|
|
1485
|
-
|
|
1514
|
+
Os as getScopedParams,
|
|
1486
1515
|
ks as validateCep,
|
|
1487
1516
|
ws as validateCnpj,
|
|
1488
|
-
|
|
1489
|
-
|
|
1517
|
+
js as validateCpf,
|
|
1518
|
+
Js as validateDate,
|
|
1490
1519
|
_s as validateEmail,
|
|
1491
1520
|
Is as validatePassword,
|
|
1492
1521
|
Us as validatePhone,
|
package/dist/bundle.umd.cjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
(function(i,h){typeof exports=="object"&&typeof module<"u"?h(exports,require("zod"),require("@arkyn/shared"),require("node:dns"),require("@arkyn/templates")):typeof define=="function"&&define.amd?define(["exports","zod","@arkyn/shared","node:dns","@arkyn/templates"],h):(i=typeof globalThis<"u"?globalThis:i||self,h(i["@arkyn/server"]={},null,i.shared,i.dns,i.templates))})(this,function(i,h,d,D,L){"use strict";var
|
|
2
|
-
`).map(
|
|
3
|
-
`;e.debugs.forEach(c=>{u+=`${
|
|
4
|
-
`}),console.log(u)}}class m{constructor(){l(this,"_cause");l(this,"_name","BadResponse");l(this,"_status",500);l(this,"_statusText","Unknown error")}get cause(){return this._cause}set cause(s){this._cause=s}get name(){return this._name}set name(s){this._name=s}get status(){return this._status}set status(s){this._status=s}get statusText(){return this._statusText}set statusText(s){this._statusText=s}onDebug(){const s=[],{callerInfo:t,functionName:n}=x.getCaller();s.push(`Caller Function: ${n}`),s.push(`Caller Location: ${t}`),this._statusText&&s.push(`Message: ${this._statusText}`),this._cause&&s.push(`Cause: ${JSON.stringify(this._cause)}`),g({scheme:"red",name:this._name,debugs:s})}makeBody(){return{name:this._name,message:this._statusText,cause:this._cause}}}class P extends m{constructor(s,t){super(),this.name="BadGateway",this.status=502,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class S extends m{constructor(s,t){super(),this.name="BadRequest",this.status=400,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class B extends m{constructor(s,t){super(),this.name="Conflict",this.status=409,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class O extends m{constructor(s,t){super(),this.name="Forbidden",this.status=403,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class $ extends m{constructor(s,t){super(),this.name="NotFound",this.status=404,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class k extends m{constructor(s,t){super(),this.name="NotImplemented",this.status=501,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class v extends m{constructor(s,t){super(),this.name="ServerError",this.status=500,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class w extends m{constructor(s,t){super(),this.name="Unauthorized",this.status=401,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class N extends m{constructor(s){super(),this.name="UnprocessableEntity",this.status=422,this.statusText=s.message||"Unprocessable entity",this.cause={data:s.data,fieldErrors:s.fieldErrors,fields:s.fields},this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class T{constructor(){l(this,"_body",null);l(this,"_name","SuccessResponse");l(this,"_status",200);l(this,"_statusText","OK")}get body(){return this._body}set body(s){this._body=s??null}get name(){return this._name}set name(s){this._name=s}get status(){return this._status}set status(s){this._status=s}get statusText(){return this._statusText}set statusText(s){this._statusText=s}onDebug(s){const t=[],{callerInfo:n,functionName:r}=x.getCaller();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:"red",name:this.name,debugs:t})}makeBody(){return{name:this.name,message:this.statusText,body:this.body}}}class j extends T{constructor(s,t){super(),this.name="Created",this.status=201,this.statusText=s,this.body=t||void 0,this.onDebug()}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 extends T{constructor(s,t){super(),this.name="Found",this.status=302,this.statusText=s,this.body=t||void 0,this.onDebug()}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 C extends T{constructor(s){super(),this.name="NoContent",this.status=204,this.statusText=s,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(null,s)}}class _ extends T{constructor(s,t){super(),this.name="Success",this.status=200,this.statusText=s,this.body=t||void 0,this.onDebug()}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 extends T{constructor(s,t){super(),this.name="Updated",this.status=200,this.statusText=s,this.body=t||void 0,this.onDebug()}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{static mapHeaders(s){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),{}):{}}static mapQueryParams(s){const t={};return s.forEach((n,r)=>t[r]=n),t}static handle(s){return{rawUrl:s.rawUrl,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),...s.urlParams},responseHeaders:this.mapHeaders(s.responseHeaders),responseBody:s.responseBody||null}}}class E{static setConfig(s){if(this.config)return;const{trafficSourceId:t,userToken:n,logBaseApiUrl:r}=s,u=`${r||"http://95.216.190.158:8081"}/ingest-log`;this.config={trafficSourceId:t,userToken:n,apiUrl:u}}static getConfig(){return this.config}static resetConfig(){this.config=void 0}}l(E,"config");async function V(e){const s=E.getConfig();if(!s)return;const{userToken:t,apiUrl:n,trafficSourceId:r}=s,{elapsedTime:a,method:u,queryParams:c,requestBody:o,requestHeaders:f,responseBody:y,responseHeaders:R,status:Bs,token:Js,rawUrl:Os}=e;if(process.env.NODE_ENV!=="development")try{const p=new URL(Os);let F="https";p.protocol==="http:"&&(F="http");const $s=JSON.stringify({domainUrl:p.protocol+"//"+p.host,pathnameUrl:p.pathname,trafficSourceId:r,status:Bs,protocol:F,method:u.toLowerCase(),trafficUserId:null,elapsedTime:a,requestHeaders:JSON.stringify(f),requestBody:JSON.stringify(o),queryParams:JSON.stringify(c),responseHeaders:JSON.stringify(R),responseBody:JSON.stringify(y)}),ks={"Content-Type":"application/json",Authorization:`Bearer ${t}`};await fetch(n,{method:"POST",body:$s,headers:ks})}catch(p){g({debugs:[`Error sending request: ${p}`],name:"LogError",scheme:"red"})}}async function b(e){let s=e.url;e.urlParams&&Object.entries(e.urlParams).forEach(([n,r])=>{s=s.replaceAll(`:${n}`,r)});const t={POST:"Resource created successfully",PUT:"Resource updated successfully",DELETE:"Resource deleted successfully",PATCH:"Resource patched successfully",GET:"Request successful"};try{const n=performance.now(),r={...e.headers,"Content-Type":"application/json"},a=await fetch(s,{headers:r,method:e.method,body:e.body?JSON.stringify(e.body):void 0}),u=performance.now()-n,c=a.status;let o=null;try{o=await a.json()}catch{o=null}const f=M.handle({elapsedTime:u,method:e.method,queryParams:new URL(s).searchParams,requestHeaders:r,requestBody:e.body,responseBody:o,responseHeaders:a.headers,status:c,rawUrl:e.url,urlParams:e.urlParams});return V(f),a.ok?{success:!0,status:c,message:(o==null?void 0:o.message)||t[e.method],response:o,cause:null}:{success:!1,status:c,message:(o==null?void 0:o.message)||a.statusText||"Request failed",response:o,cause:null}}catch(n){return g({debugs:[`Network error or request failed: ${n}`],name:"MakeRequestError",scheme:"red"}),{success:!1,status:0,message:"Network error or request failed",response:null,cause:n instanceof Error?n.message:String(n)}}}async function Z(e){return b({method:"DELETE",url:e.url,urlParams:e.urlParams,headers:e.headers,body:e.body})}async function G(e){return b({method:"GET",url:e.url,urlParams:e.urlParams,headers:e.headers})}async function W(e){return b({method:"PATCH",url:e.url,urlParams:e.urlParams,headers:e.headers,body:e.body})}async function Y(e){return b({method:"POST",url:e.url,urlParams:e.urlParams,headers:e.headers,body:e.body})}async function K(e){return b({method:"PUT",url:e.url,urlParams:e.urlParams,headers:e.headers,body:e.body})}class Q{constructor(s){l(this,"baseUrl");l(this,"baseHeaders");l(this,"baseToken");l(this,"enableDebug");this.baseUrl=s.baseUrl,this.baseHeaders=s.baseHeaders||void 0,this.baseToken=s.baseToken||void 0,this.enableDebug=s.enableDebug||!1}onDebug(s,t,n){if(this.enableDebug){const r=[];r.push(`Base URL: ${this.baseUrl}`),r.push(`Endpoint: ${s}`),r.push(`Method: ${t}`),n[0]&&r.push(`Headers: ${JSON.stringify(n[0])}`),n[1]&&r.push(`Body: ${JSON.stringify(n[1])}`),g({debugs:r,name:"ApiDebug",scheme:"yellow"})}}generateHeaders(s,t){let n={};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}async get(s,t){const n=this.generateHeaders((t==null?void 0:t.headers)||{},t==null?void 0:t.token);return this.onDebug(s,"get",[n]),await G({url:this.baseUrl+s,urlParams:(t==null?void 0:t.urlParams)||{},headers:n})}async post(s,t){const n=this.generateHeaders((t==null?void 0:t.headers)||{},t==null?void 0:t.token),r=t==null?void 0:t.body;return this.onDebug(s,"post",[n,r]),await Y({url:this.baseUrl+s,urlParams:(t==null?void 0:t.urlParams)||{},headers:n,body:r})}async put(s,t){const n=this.generateHeaders((t==null?void 0:t.headers)||{},t==null?void 0:t.token),r=t==null?void 0:t.body;return this.onDebug(s,"put",[n,r]),await K({url:this.baseUrl+s,urlParams:(t==null?void 0:t.urlParams)||{},headers:n,body:r})}async patch(s,t){const n=this.generateHeaders((t==null?void 0:t.headers)||{},t==null?void 0:t.token),r=t==null?void 0:t.body;return this.onDebug(s,"patch",[n,r]),await W({url:this.baseUrl+s,urlParams:(t==null?void 0:t.urlParams)||{},headers:n,body:r})}async delete(s,t){const n=this.generateHeaders((t==null?void 0:t.headers)||{},t==null?void 0:t.token),r=t==null?void 0:t.body;return this.onDebug(s,"delete",[n,r]),await Z({url:this.baseUrl+s,urlParams:(t==null?void 0:t.urlParams)||{},headers:n,body:r})}}async function X(e){let s;const t=await e.arrayBuffer(),n=new TextDecoder().decode(t);try{s=JSON.parse(n)}catch{try{if(n.includes("=")){const a=new URLSearchParams(n);s=Object.fromEntries(a.entries())}else throw new S("Invalid URLSearchParams format")}catch{throw new S("Failed to extract data from request")}}return s}function ss(e,s){var t,n,r;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"}function es(e){switch(!0){case e instanceof Response:return e;case e instanceof J:return e.toResponse();case e instanceof j:return e.toResponse();case e instanceof I:return e.toResponse();case e instanceof _:return e.toResponse();case e instanceof C:return e.toResponse()}switch(!0){case e instanceof P:return e.toResponse();case e instanceof S:return e.toResponse();case e instanceof B:return e.toResponse();case e instanceof O:return e.toResponse();case e instanceof $:return e.toResponse();case e instanceof k:return e.toResponse();case e instanceof v:return e.toResponse();case e instanceof w:return e.toResponse();case e instanceof N:return e.toResponse()}return new v("Server error",e).toResponse()}async function U([e,s]){const t=await s.safeParseAsync(e);if(t.success===!1){const n=Object.fromEntries(t.error.issues.map(r=>[r.path.join("."),r.message]));return{success:t.success,fieldErrors:n,fields:e}}else return{success:t.success,data:t.data}}function q([e,s]){const t=s.safeParse(e);if(t.success===!1){const n=Object.fromEntries(t.error.issues.map(r=>[r.path.join("."),r.message]));return{success:t.success,fieldErrors:n,fields:e}}else return{success:t.success,data:t.data}}function ts(e,s=""){const t=new URL(e.url);if(s==="")return t.searchParams;const n=Array.from(t.searchParams.entries()).filter(([r])=>r.startsWith(`${s}:`)).map(([r,a])=>[r.replace(`${s}:`,""),a]);return new URLSearchParams(n)}function ns(e){const s="Error validating:",t=e.issues.map(({path:n,message:r})=>`-> ${n.join(".")}: ${r}`);return[s,...t].join(`
|
|
5
|
-
`)}class rs{constructor(s){this.schema=s}isValid(s){return this.schema.safeParse(s).success}safeValidate(s){return this.schema.safeParse(s)}validate(s){try{return this.schema.parse(s)}catch(t){throw new v(ns(t))}}formValidate(s,t){const n=q([s,this.schema]);if(!n.success){const r=Object.keys(n.fieldErrors)[0];throw new N({fields:n.fields,fieldErrors:n.fieldErrors,data:{scrollTo:r},message:t})}return n.data}async formAsyncValidate(s,t){const n=await U([s,this.schema]);if(!n.success){const r=Object.keys(n.fieldErrors)[0];throw new N({fields:n.fields,fieldErrors:n.fieldErrors,data:{scrollTo:r},message:t})}return n.data}}function
|
|
1
|
+
(function(i,h){typeof exports=="object"&&typeof module<"u"?h(exports,require("zod"),require("@arkyn/shared"),require("node:dns"),require("@arkyn/templates")):typeof define=="function"&&define.amd?define(["exports","zod","@arkyn/shared","node:dns","@arkyn/templates"],h):(i=typeof globalThis<"u"?globalThis:i||self,h(i["@arkyn/server"]={},null,i.shared,i.dns,i.templates))})(this,function(i,h,d,D,L){"use strict";var js=Object.defineProperty;var ws=(i,h,d)=>h in i?js(i,h,{enumerable:!0,configurable:!0,writable:!0,value:d}):i[h]=d;var l=(i,h,d)=>ws(i,typeof h!="symbol"?h+"":h,d);const z={};class x{static setIgnoreFile(s){this.ignoreFiles.push(s)}static clearIgnoreFiles(){this.ignoreFiles=[]}static getCaller(){const s=process.cwd(),r=(new Error().stack||"").split(`
|
|
2
|
+
`).map(g=>g.trim());let o=2;for(;o<r.length&&(r[o].includes("node:internal")||r[o].includes("/node_modules/"));)o++;if(this.ignoreFiles.length>0)for(;o<r.length&&this.ignoreFiles.some(g=>r[o].includes(g));)o++;const u=r[o]||"";let c="Unknown function",a="Unknown caller";const f=u.match(/at\s+([^(\s]+)\s+\(([^)]+)\)/);if(f)c=f[1],a=f[2];else{const g=u.match(/at\s+(.+)/);if(g){a=g[1];const R=a.match(/at\s+([^(\s]+)\s+/);R&&R[1]!=="new"&&(c=R[1])}}a.includes("(")&&(a=a.substring(a.indexOf("(")+1,a.lastIndexOf(")"))),a=a.split(":").slice(0,-2).join(":");try{a=z.relative(s,a)}catch{}return{functionName:c,callerInfo:a}}}l(x,"ignoreFiles",[]);function y(e){var t;if(process.env.NODE_ENV==="development"||((t=process.env)==null?void 0:t.DEBUG_MODE)==="true"){const o=`${{yellow:"\x1B[33m",cyan:"\x1B[36m",red:"\x1B[31m",green:"\x1B[32m"}[e.scheme]}[${e.name}]\x1B[0m`;let u=`
|
|
3
|
+
`;e.debugs.forEach(c=>{u+=`${o} ${c.trim()}
|
|
4
|
+
`}),console.log(u)}}class m{constructor(){l(this,"_cause");l(this,"_name","BadResponse");l(this,"_status",500);l(this,"_statusText","Unknown error")}get cause(){return this._cause}set cause(s){this._cause=s}get name(){return this._name}set name(s){this._name=s}get status(){return this._status}set status(s){this._status=s}get statusText(){return this._statusText}set statusText(s){this._statusText=s}onDebug(){const s=[],{callerInfo:t,functionName:n}=x.getCaller();s.push(`Caller Function: ${n}`),s.push(`Caller Location: ${t}`),this._statusText&&s.push(`Message: ${this._statusText}`),this._cause&&s.push(`Cause: ${JSON.stringify(this._cause)}`),y({scheme:"red",name:this._name,debugs:s})}makeBody(){return{name:this._name,message:this._statusText,cause:this._cause}}}class P extends m{constructor(s,t){super(),this.name="BadGateway",this.status=502,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class S extends m{constructor(s,t){super(),this.name="BadRequest",this.status=400,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class $ extends m{constructor(s,t){super(),this.name="Conflict",this.status=409,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class B extends m{constructor(s,t){super(),this.name="Forbidden",this.status=403,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class k extends m{constructor(s,t){super(),this.name="NotFound",this.status=404,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class O extends m{constructor(s,t){super(),this.name="NotImplemented",this.status=501,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class v extends m{constructor(s,t){super(),this.name="ServerError",this.status=500,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class j extends m{constructor(s,t){super(),this.name="Unauthorized",this.status=401,this.statusText=s,this.cause=t?JSON.stringify(t):void 0,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class N extends m{constructor(s){super(),this.name="UnprocessableEntity",this.status=422,this.statusText=s.message||"Unprocessable entity",this.cause={data:s.data,fieldErrors:s.fieldErrors,fields:s.fields},this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(JSON.stringify(this.makeBody()),s)}toJson(){const s={status:this.status,statusText:this.statusText};return Response.json(this.makeBody(),s)}}class T{constructor(){l(this,"_body",null);l(this,"_name","SuccessResponse");l(this,"_status",200);l(this,"_statusText","OK")}get body(){return this._body}set body(s){this._body=s??null}get name(){return this._name}set name(s){this._name=s}get status(){return this._status}set status(s){this._status=s}get statusText(){return this._statusText}set statusText(s){this._statusText=s}onDebug(s){const t=[],{callerInfo:n,functionName:r}=x.getCaller();t.push(`Caller Function: ${r}`),t.push(`Caller Location: ${n}`),this.statusText&&t.push(`Message: ${this.statusText}`),s&&t.push(`Body: ${JSON.stringify(s)}`),y({scheme:"red",name:this.name,debugs:t})}makeBody(){return{name:this.name,message:this.statusText,body:this.body}}}class w extends T{constructor(s,t){super(),this.name="Created",this.status=201,this.statusText=s,this.body=t||void 0,this.onDebug()}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 extends T{constructor(s,t){super(),this.name="Found",this.status=302,this.statusText=s,this.body=t||void 0,this.onDebug()}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 C extends T{constructor(s){super(),this.name="NoContent",this.status=204,this.statusText=s,this.onDebug()}toResponse(){const s={headers:{"Content-Type":"application/json"},status:this.status,statusText:this.statusText};return new Response(null,s)}}class _ extends T{constructor(s,t){super(),this.name="Success",this.status=200,this.statusText=s,this.body=t||void 0,this.onDebug()}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 extends T{constructor(s,t){super(),this.name="Updated",this.status=200,this.statusText=s,this.body=t||void 0,this.onDebug()}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{static mapHeaders(s){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),{}):{}}static mapQueryParams(s){const t={};return s.forEach((n,r)=>t[r]=n),t}static handle(s){return{rawUrl:s.rawUrl,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),...s.urlParams},responseHeaders:this.mapHeaders(s.responseHeaders),responseBody:s.responseBody||null}}}class E{static setConfig(s){if(this.config)return;const{trafficSourceId:t,userToken:n,logBaseApiUrl:r}=s,u=`${r||"http://95.216.190.158:8081"}/ingest-log`;this.config={trafficSourceId:t,userToken:n,apiUrl:u}}static getConfig(){return this.config}static resetConfig(){this.config=void 0}}l(E,"config");async function V(e){const s=E.getConfig();if(!s)return;const{userToken:t,apiUrl:n,trafficSourceId:r}=s,{elapsedTime:o,method:u,queryParams:c,requestBody:a,requestHeaders:f,responseBody:g,responseHeaders:R,status:$s,token:Js,rawUrl:Bs}=e;if(process.env.NODE_ENV!=="development")try{const p=new URL(Bs);let F="https";p.protocol==="http:"&&(F="http");const ks=JSON.stringify({domainUrl:p.protocol+"//"+p.host,pathnameUrl:p.pathname,trafficSourceId:r,status:$s,protocol:F,method:u.toLowerCase(),trafficUserId:null,elapsedTime:o,requestHeaders:JSON.stringify(f),requestBody:JSON.stringify(a),queryParams:JSON.stringify(c),responseHeaders:JSON.stringify(R),responseBody:JSON.stringify(g)}),Os={"Content-Type":"application/json",Authorization:`Bearer ${t}`};await fetch(n,{method:"POST",body:ks,headers:Os})}catch(p){y({debugs:[`Error sending request: ${p}`],name:"LogError",scheme:"red"})}}async function b(e){let s=e.url;e.urlParams&&Object.entries(e.urlParams).forEach(([n,r])=>{s=s.replaceAll(`:${n}`,r)});const t={POST:"Resource created successfully",PUT:"Resource updated successfully",DELETE:"Resource deleted successfully",PATCH:"Resource patched successfully",GET:"Request successful"};try{const n=performance.now(),r={...e.headers,"Content-Type":"application/json"},o=await fetch(s,{headers:r,method:e.method,body:e.body?JSON.stringify(e.body):void 0}),u=performance.now()-n,c=o.status;let a=null;try{a=await o.json()}catch{a=null}const f=M.handle({elapsedTime:u,method:e.method,queryParams:new URL(s).searchParams,requestHeaders:r,requestBody:e.body,responseBody:a,responseHeaders:o.headers,status:c,rawUrl:e.url,urlParams:e.urlParams});return V(f),o.ok?{success:!0,status:c,message:(a==null?void 0:a.message)||t[e.method],response:a,cause:null}:{success:!1,status:c,message:(a==null?void 0:a.message)||o.statusText||"Request failed",response:a,cause:null}}catch(n){return y({debugs:[`Network error or request failed: ${n}`],name:"MakeRequestError",scheme:"red"}),{success:!1,status:0,message:"Network error or request failed",response:null,cause:n instanceof Error?n.message:String(n)}}}async function Z(e){return b({method:"DELETE",url:e.url,urlParams:e.urlParams,headers:e.headers,body:e.body})}async function G(e){return b({method:"GET",url:e.url,urlParams:e.urlParams,headers:e.headers})}async function W(e){return b({method:"PATCH",url:e.url,urlParams:e.urlParams,headers:e.headers,body:e.body})}async function Y(e){return b({method:"POST",url:e.url,urlParams:e.urlParams,headers:e.headers,body:e.body})}async function K(e){return b({method:"PUT",url:e.url,urlParams:e.urlParams,headers:e.headers,body:e.body})}class Q{constructor(s){l(this,"baseUrl");l(this,"baseHeaders");l(this,"baseToken");l(this,"enableDebug");this.baseUrl=s.baseUrl,this.baseHeaders=s.baseHeaders||void 0,this.baseToken=s.baseToken||void 0,this.enableDebug=s.enableDebug||!1}onDebug(s,t,n){if(this.enableDebug){const r=[],o=u=>u?JSON.stringify(u,null,2):null;r.push(`Base URL: ${this.baseUrl}`),r.push(`Endpoint: ${s}`),r.push(`Method: ${t}`),r.push(`Status: ${n.status}`),r.push(`Message: ${n.message}`),r.push(`Headers: ${o(n.headers)}`),r.push(`Body: ${o(n.body)}`),r.push(`Response: ${o(n.response)}`),y({debugs:r,name:"ApiDebug",scheme:"yellow"})}}generateHeaders(s,t){let n={};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}async get(s,t){const n=this.generateHeaders((t==null?void 0:t.headers)||{},t==null?void 0:t.token),r=await G({url:this.baseUrl+s,urlParams:(t==null?void 0:t.urlParams)||{},headers:n});return this.onDebug(s,"get",{headers:n,message:r.message,status:r.status,response:r.response}),r}async post(s,t){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 Y({url:this.baseUrl+s,urlParams:(t==null?void 0:t.urlParams)||{},headers:n,body:r});return this.onDebug(s,"post",{headers:n,body:r,message:o.message,status:o.status,response:o.response}),o}async put(s,t){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 K({url:this.baseUrl+s,urlParams:(t==null?void 0:t.urlParams)||{},headers:n,body:r});return this.onDebug(s,"put",{headers:n,body:r,message:o.message,status:o.status,response:o.response}),o}async patch(s,t){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 W({url:this.baseUrl+s,urlParams:(t==null?void 0:t.urlParams)||{},headers:n,body:r});return this.onDebug(s,"patch",{headers:n,body:r,message:o.message,status:o.status,response:o.response}),o}async delete(s,t){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 Z({url:this.baseUrl+s,urlParams:(t==null?void 0:t.urlParams)||{},headers:n,body:r});return this.onDebug(s,"delete",{headers:n,body:r,message:o.message,status:o.status,response:o.response}),o}}async function X(e){let s;const t=await e.arrayBuffer(),n=new TextDecoder().decode(t);try{s=JSON.parse(n)}catch{try{if(n.includes("=")){const o=new URLSearchParams(n);s=Object.fromEntries(o.entries())}else throw new S("Invalid URLSearchParams format")}catch{throw new S("Failed to extract data from request")}}return s}function ss(e,s){var t,n,r;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"}function es(e){switch(!0){case e instanceof Response:return e;case e instanceof J:return e.toResponse();case e instanceof w:return e.toResponse();case e instanceof I:return e.toResponse();case e instanceof _:return e.toResponse();case e instanceof C:return e.toResponse()}switch(!0){case e instanceof P:return e.toResponse();case e instanceof S:return e.toResponse();case e instanceof $:return e.toResponse();case e instanceof B:return e.toResponse();case e instanceof k:return e.toResponse();case e instanceof O:return e.toResponse();case e instanceof v:return e.toResponse();case e instanceof j:return e.toResponse();case e instanceof N:return e.toResponse()}return new v("Server error",e).toResponse()}async function U([e,s]){const t=await s.safeParseAsync(e);if(t.success===!1){const n=Object.fromEntries(t.error.issues.map(r=>[r.path.join("."),r.message]));return{success:t.success,fieldErrors:n,fields:e}}else return{success:t.success,data:t.data}}function q([e,s]){const t=s.safeParse(e);if(t.success===!1){const n=Object.fromEntries(t.error.issues.map(r=>[r.path.join("."),r.message]));return{success:t.success,fieldErrors:n,fields:e}}else return{success:t.success,data:t.data}}function ts(e,s=""){const t=new URL(e.url);if(s==="")return t.searchParams;const n=Array.from(t.searchParams.entries()).filter(([r])=>r.startsWith(`${s}:`)).map(([r,o])=>[r.replace(`${s}:`,""),o]);return new URLSearchParams(n)}function ns(e){const s="Error validating:",t=e.issues.map(({path:n,message:r})=>`-> ${n.join(".")}: ${r}`);return[s,...t].join(`
|
|
5
|
+
`)}class rs{constructor(s){this.schema=s}isValid(s){return this.schema.safeParse(s).success}safeValidate(s){return this.schema.safeParse(s)}validate(s){try{return this.schema.parse(s)}catch(t){throw new v(ns(t))}}formValidate(s,t){const n=q([s,this.schema]);if(!n.success){const r=Object.keys(n.fieldErrors)[0];throw new N({fields:n.fields,fieldErrors:n.fieldErrors,data:{scrollTo:r},message:t})}return n.data}async formAsyncValidate(s,t){const n=await U([s,this.schema]);if(!n.success){const r=Object.keys(n.fieldErrors)[0];throw new N({fields:n.fields,fieldErrors:n.fieldErrors,data:{scrollTo:r},message:t})}return n.data}}function os(e){if(!(/^\d{5}-\d{3}$/.test(e)||/^\d{8}$/.test(e)))return!1;const t=d.removeNonNumeric(e),n=8,r=/^\d{8}$/.test(t);return t.length===n&&r}function is(e){return e.length!==14}function as(e){const[s]=e;return[...e].every(t=>t===s)}function A(e,s){let t=0;for(let r=0;r<s.length;r++)t+=parseInt(e[r])*s[r];const n=t%11;return n<2?0:11-n}function us(e){return e.slice(12)}function cs(e){if(!e||e.length>18||e.length<14||/\s/.test(e))return!1;const t=d.removeNonNumeric(e);if(is(t)||as(t))return!1;const n=t.slice(0,12),r=A(n,[5,4,3,2,9,8,7,6,5,4,3,2]),o=A(n+r,[6,5,4,3,2,9,8,7,6,5,4,3,2]);return us(t)===`${r}${o}`}function ls(e){return e.length!==11}function hs(e){const[s]=e;return[...e].every(t=>t===s)}function H(e,s){let t=0;for(const r of e)s>1&&(t+=parseInt(r)*s--);const n=t%11;return n<2?0:11-n}function fs(e){return e.slice(9)}function ds(e){if(!e||e.length>14||e.length<11||/\s/.test(e))return!1;const t=d.removeNonNumeric(e);if(ls(t)||hs(t))return!1;const n=H(t,10),r=H(t,11);return fs(t)===`${n}${r}`}function ms(e,s){const t=(s==null?void 0:s.inputFormat)||"brazilianDate",n=(s==null?void 0:s.minYear)||1900,r=(s==null?void 0:s.maxYear)||3e3,o=new d.ValidateDateService;o.validateInputFormat(t);let u,c,a;const f=e.split(/[-/]/).map(Number);if(f.length!==3)return!1;try{switch(t){case"brazilianDate":[u,c,a]=f,o.validateDateParts(a,c,u);break;case"isoDate":[c,u,a]=f,o.validateDateParts(a,c,u);break;case"timestamp":[a,c,u]=f,o.validateDateParts(a,c,u);break}return!(a<n||a>r)}catch{return!1}}function gs(e){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)}function ys(e){return!(e.length===0||e.length>64||e.startsWith(".")||e.endsWith(".")||e.includes("..")||!/^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+$/.test(e))}function ps(e){return!(e.length===0||e.length>63||e.startsWith("-")||e.endsWith("-")||!/^[a-zA-Z0-9-]+$/.test(e))}function Ts(e){if(e.length===0||e.length>253||e.startsWith(".")||e.endsWith(".")||e.startsWith("-")||e.endsWith("-"))return!1;const s=e.split(".");if(s.length<2)return!1;for(const n of s)if(!ps(n))return!1;const t=s[s.length-1];return!(t.length<2||!/^[a-zA-Z]+$/.test(t))}function bs(e){const s=e.split("@");if(s.length!==2)return!1;const[t,n]=s;return!(!ys(t)||!Ts(n))}function Rs(e){const s=e.split("@");return s.length===2?s[1].toLowerCase():null}const xs=["MX","A","AAAA"];async function Ss(e,s){var t;try{return await((t=D==null?void 0:D.promises)==null?void 0:t.resolve(e,s)),!0}catch{return!1}}async function vs(e){for(const s of xs)if(await Ss(e,s))return!0;return!1}async function Ns(e){if(!e||typeof e!="string")return!1;const s=e.trim();if(!gs(s)||!bs(s))return!1;const t=Rs(s);return t?await vs(t):!1}function Ds(e){if(!e)return!1;const s=e.length>=8,t=/[A-Z]/.test(e),n=/[a-z]/.test(e),r=/\d/.test(e),o=/[!@#$%^&*(),.?":;{}|<>_\-+=~`[\]\\\/]/.test(e);return[s,t,n,r,o].every(u=>u)}function Es(e){for(const s of L.countries){const t=s.code,n=s.prefix?`-${s.prefix}`:"",r=s.mask.replace(/[^_]/g,"").length;if(s.iso==="BR"){if(new RegExp(`^\\${t} \\d{2}9?\\d{8}$`).test(e))return!0;continue}if(new RegExp(`^\\${t}${n} \\d{${r}}$`).test(e))return!0}return!1}function Ps(e){if(!e||!/^[0-9a-zA-Z.-]+$/.test(e))return!1;const t=e.replace(/[^a-zA-Z0-9]/g,"");return t.length<7||t.length>9?!1:/^[0-9]{7,8}[0-9Xx]?$/.test(t)}i.ApiService=Q,i.BadGateway=P,i.BadRequest=S,i.Conflict=$,i.Created=w,i.DebugService=x,i.Forbidden=B,i.Found=J,i.LogService=E,i.NoContent=C,i.NotFound=k,i.NotImplemented=O,i.SchemaValidator=rs,i.ServerError=v,i.Success=_,i.Unauthorized=j,i.UnprocessableEntity=N,i.Updated=I,i.decodeRequestBody=X,i.decodeRequestErrorMessage=ss,i.errorHandler=es,i.flushDebugLogs=y,i.formAsyncParse=U,i.formParse=q,i.getScopedParams=ts,i.validateCep=os,i.validateCnpj=cs,i.validateCpf=ds,i.validateDate=ms,i.validateEmail=Ns,i.validatePassword=Ds,i.validatePhone=Es,i.validateRg=Ps,Object.defineProperty(i,Symbol.toStringTag,{value:"Module"})});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"apiService.d.ts","sourceRoot":"","sources":["../../src/services/apiService.ts"],"names":[],"mappings":"AAOA,KAAK,0BAA0B,GAAG;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AAEF,KAAK,8BAA8B,GAAG;IACpC,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACpC,CAAC;AAEF,KAAK,2BAA2B,GAAG;IACjC,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACpC,CAAC;
|
|
1
|
+
{"version":3,"file":"apiService.d.ts","sourceRoot":"","sources":["../../src/services/apiService.ts"],"names":[],"mappings":"AAOA,KAAK,0BAA0B,GAAG;IAChC,OAAO,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,WAAW,CAAC;IAC1B,SAAS,CAAC,EAAE,MAAM,GAAG,IAAI,CAAC;IAC1B,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AAEF,KAAK,8BAA8B,GAAG;IACpC,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACpC,CAAC;AAEF,KAAK,2BAA2B,GAAG;IACjC,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACpC,CAAC;AAUF,cAAM,UAAU;IACd,OAAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,WAAW,CAAC,CAAc;IAClC,OAAO,CAAC,SAAS,CAAC,CAAS;IAC3B,OAAO,CAAC,WAAW,CAAC,CAAU;gBAElB,KAAK,EAAE,0BAA0B;IAO7C,OAAO,CAAC,OAAO;IAkBf,OAAO,CAAC,eAAe;IAcvB;;;;;OAKG;IAEG,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,8BAA8B;IAmBjE;;;;;OAKG;IAEG,IAAI,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,2BAA2B;IAsB/D;;;;;OAKG;IAEG,GAAG,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,2BAA2B;IAsB9D;;;;;OAKG;IAEG,KAAK,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,2BAA2B;IAsBhE;;;;;OAKG;IAEG,MAAM,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,2BAA2B;CAqBlE;AAED,OAAO,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -18,13 +18,15 @@ class ApiService {
|
|
|
18
18
|
onDebug(endpoint, method, data) {
|
|
19
19
|
if (this.enableDebug) {
|
|
20
20
|
const debugs = [];
|
|
21
|
+
const json = (data) => (data ? JSON.stringify(data, null, 2) : null);
|
|
21
22
|
debugs.push(`Base URL: ${this.baseUrl}`);
|
|
22
23
|
debugs.push(`Endpoint: ${endpoint}`);
|
|
23
24
|
debugs.push(`Method: ${method}`);
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
25
|
+
debugs.push(`Status: ${data.status}`);
|
|
26
|
+
debugs.push(`Message: ${data.message}`);
|
|
27
|
+
debugs.push(`Headers: ${json(data.headers)}`);
|
|
28
|
+
debugs.push(`Body: ${json(data.body)}`);
|
|
29
|
+
debugs.push(`Response: ${json(data.response)}`);
|
|
28
30
|
flushDebugLogs({ debugs, name: "ApiDebug", scheme: "yellow" });
|
|
29
31
|
}
|
|
30
32
|
}
|
|
@@ -48,12 +50,18 @@ class ApiService {
|
|
|
48
50
|
*/
|
|
49
51
|
async get(endpoint, data) {
|
|
50
52
|
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
51
|
-
|
|
52
|
-
return await getRequest({
|
|
53
|
+
const response = await getRequest({
|
|
53
54
|
url: this.baseUrl + endpoint,
|
|
54
55
|
urlParams: data?.urlParams || {},
|
|
55
56
|
headers,
|
|
56
57
|
});
|
|
58
|
+
this.onDebug(endpoint, "get", {
|
|
59
|
+
headers,
|
|
60
|
+
message: response.message,
|
|
61
|
+
status: response.status,
|
|
62
|
+
response: response.response,
|
|
63
|
+
});
|
|
64
|
+
return response;
|
|
57
65
|
}
|
|
58
66
|
/**
|
|
59
67
|
* Sends a post request to the specified endpoint.
|
|
@@ -64,13 +72,20 @@ class ApiService {
|
|
|
64
72
|
async post(endpoint, data) {
|
|
65
73
|
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
66
74
|
const body = data?.body;
|
|
67
|
-
|
|
68
|
-
return await postRequest({
|
|
75
|
+
const response = await postRequest({
|
|
69
76
|
url: this.baseUrl + endpoint,
|
|
70
77
|
urlParams: data?.urlParams || {},
|
|
71
78
|
headers,
|
|
72
79
|
body,
|
|
73
80
|
});
|
|
81
|
+
this.onDebug(endpoint, "post", {
|
|
82
|
+
headers,
|
|
83
|
+
body,
|
|
84
|
+
message: response.message,
|
|
85
|
+
status: response.status,
|
|
86
|
+
response: response.response,
|
|
87
|
+
});
|
|
88
|
+
return response;
|
|
74
89
|
}
|
|
75
90
|
/**
|
|
76
91
|
* Sends a put request to the specified endpoint.
|
|
@@ -81,13 +96,20 @@ class ApiService {
|
|
|
81
96
|
async put(endpoint, data) {
|
|
82
97
|
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
83
98
|
const body = data?.body;
|
|
84
|
-
|
|
85
|
-
return await putRequest({
|
|
99
|
+
const response = await putRequest({
|
|
86
100
|
url: this.baseUrl + endpoint,
|
|
87
101
|
urlParams: data?.urlParams || {},
|
|
88
102
|
headers,
|
|
89
103
|
body,
|
|
90
104
|
});
|
|
105
|
+
this.onDebug(endpoint, "put", {
|
|
106
|
+
headers,
|
|
107
|
+
body,
|
|
108
|
+
message: response.message,
|
|
109
|
+
status: response.status,
|
|
110
|
+
response: response.response,
|
|
111
|
+
});
|
|
112
|
+
return response;
|
|
91
113
|
}
|
|
92
114
|
/**
|
|
93
115
|
* Sends a patch request to the specified endpoint.
|
|
@@ -98,13 +120,20 @@ class ApiService {
|
|
|
98
120
|
async patch(endpoint, data) {
|
|
99
121
|
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
100
122
|
const body = data?.body;
|
|
101
|
-
|
|
102
|
-
return await patchRequest({
|
|
123
|
+
const response = await patchRequest({
|
|
103
124
|
url: this.baseUrl + endpoint,
|
|
104
125
|
urlParams: data?.urlParams || {},
|
|
105
126
|
headers,
|
|
106
127
|
body,
|
|
107
128
|
});
|
|
129
|
+
this.onDebug(endpoint, "patch", {
|
|
130
|
+
headers,
|
|
131
|
+
body,
|
|
132
|
+
message: response.message,
|
|
133
|
+
status: response.status,
|
|
134
|
+
response: response.response,
|
|
135
|
+
});
|
|
136
|
+
return response;
|
|
108
137
|
}
|
|
109
138
|
/**
|
|
110
139
|
* Sends a delete request to the specified endpoint.
|
|
@@ -115,13 +144,20 @@ class ApiService {
|
|
|
115
144
|
async delete(endpoint, data) {
|
|
116
145
|
const headers = this.generateHeaders(data?.headers || {}, data?.token);
|
|
117
146
|
const body = data?.body;
|
|
118
|
-
|
|
119
|
-
return await deleteRequest({
|
|
147
|
+
const response = await deleteRequest({
|
|
120
148
|
url: this.baseUrl + endpoint,
|
|
121
149
|
urlParams: data?.urlParams || {},
|
|
122
150
|
headers,
|
|
123
151
|
body,
|
|
124
152
|
});
|
|
153
|
+
this.onDebug(endpoint, "delete", {
|
|
154
|
+
headers,
|
|
155
|
+
body,
|
|
156
|
+
message: response.message,
|
|
157
|
+
status: response.status,
|
|
158
|
+
response: response.response,
|
|
159
|
+
});
|
|
160
|
+
return response;
|
|
125
161
|
}
|
|
126
162
|
}
|
|
127
163
|
export { ApiService };
|