@arkyn/server 3.0.1-beta.159 → 3.0.1-beta.166

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. package/dist/index.js +1026 -1262
  2. package/dist/modules/http/api/_logRequest.js +55 -68
  3. package/dist/modules/http/api/_makeRequest.js +69 -68
  4. package/dist/modules/http/api/deleteRequest.js +12 -12
  5. package/dist/modules/http/api/getRequest.js +11 -11
  6. package/dist/modules/http/api/patchRequest.js +11 -11
  7. package/dist/modules/http/api/postRequest.js +11 -11
  8. package/dist/modules/http/api/putRequest.js +11 -11
  9. package/dist/modules/http/badResponses/_badResponse.js +62 -58
  10. package/dist/modules/http/badResponses/badGateway.js +22 -28
  11. package/dist/modules/http/badResponses/badRequest.js +22 -28
  12. package/dist/modules/http/badResponses/conflict.js +22 -28
  13. package/dist/modules/http/badResponses/forbidden.js +22 -28
  14. package/dist/modules/http/badResponses/notFound.js +22 -28
  15. package/dist/modules/http/badResponses/notImplemented.js +22 -28
  16. package/dist/modules/http/badResponses/serverError.js +22 -28
  17. package/dist/modules/http/badResponses/unauthorized.js +22 -28
  18. package/dist/modules/http/badResponses/unprocessableEntity.js +27 -35
  19. package/dist/modules/http/successResponses/_successResponse.js +61 -68
  20. package/dist/modules/http/successResponses/created.js +22 -28
  21. package/dist/modules/http/successResponses/found.js +22 -28
  22. package/dist/modules/http/successResponses/noContent.js +16 -20
  23. package/dist/modules/http/successResponses/success.js +22 -28
  24. package/dist/modules/http/successResponses/updated.js +22 -28
  25. package/dist/modules/index.js +33 -67
  26. package/dist/modules/services/apiService.js +108 -128
  27. package/dist/modules/services/debugService.js +35 -65
  28. package/dist/modules/services/logMapperService.js +28 -51
  29. package/dist/modules/services/logService.js +20 -30
  30. package/dist/modules/utilities/decodeRequestBody.js +19 -21
  31. package/dist/modules/utilities/decodeRequestErrorMessage.js +5 -6
  32. package/dist/modules/utilities/errorHandler.js +36 -51
  33. package/dist/modules/utilities/flushDebugLogs.js +15 -19
  34. package/dist/modules/utilities/formAsyncParse.js +13 -18
  35. package/dist/modules/utilities/formParse.js +13 -18
  36. package/dist/modules/utilities/getScopedParams.js +8 -10
  37. package/dist/modules/utilities/schemaValidator.js +53 -104
  38. package/dist/modules/validations/validateCep.js +8 -8
  39. package/dist/modules/validations/validateCnpj.js +49 -26
  40. package/dist/modules/validations/validateCpf.js +22 -23
  41. package/dist/modules/validations/validateDate.js +25 -26
  42. package/dist/modules/validations/validateEmail.js +54 -53
  43. package/dist/modules/validations/validatePassword.js +11 -15
  44. package/dist/modules/validations/validatePhone.js +8 -8
  45. package/dist/modules/validations/validateRg.js +7 -7
  46. package/generate-version.ts +74 -0
  47. package/package.json +17 -9
@@ -1,69 +1,56 @@
1
- import { formatJsonObject as w } from "@arkyn/shared";
2
- import { LogService as x } from "../../services/logService.js";
3
- import { flushDebugLogs as n } from "../../utilities/flushDebugLogs.js";
4
- async function B(a) {
5
- const r = x.getConfig();
6
- if (!r) return;
7
- const { userToken: i, apiUrl: c, trafficSourceId: u } = r, {
8
- elapsedTime: p,
9
- method: d,
10
- queryParams: f,
11
- requestBody: m,
12
- requestHeaders: l,
13
- responseBody: g,
14
- responseHeaders: h,
15
- status: y,
16
- token: L,
17
- rawUrl: S
18
- } = a;
19
- if (process.env.NODE_ENV !== "development")
20
- try {
21
- const e = new URL(S);
22
- let o = "https";
23
- e.protocol === "http:" && (o = "http");
24
- const N = JSON.stringify({
25
- domainUrl: e.protocol + "//" + e.host,
26
- pathnameUrl: e.pathname,
27
- trafficSourceId: u,
28
- status: y,
29
- protocol: o,
30
- method: d.toLowerCase(),
31
- trafficUserId: null,
32
- elapsedTime: p,
33
- requestHeaders: JSON.stringify(l),
34
- requestBody: JSON.stringify(m),
35
- queryParams: JSON.stringify(f),
36
- responseHeaders: JSON.stringify(h),
37
- responseBody: JSON.stringify(g)
38
- }), O = {
39
- "Content-Type": "application/json",
40
- Authorization: `Bearer ${i}`
41
- }, t = await fetch(c, {
42
- method: "POST",
43
- body: N,
44
- headers: O
45
- });
46
- if (!t.ok) {
47
- const q = await t.text(), J = t.status, T = t.statusText, s = await t.json().catch(() => null);
48
- n({
49
- name: "LogError",
50
- scheme: "red",
51
- debugs: [
52
- "Failed to log request.",
53
- `Status: ${J} ${T}.`,
54
- `Status text: ${q}.`,
55
- `JSON Response: ${s ? w(s, 0) : "No JSON response"}`
56
- ]
57
- });
58
- }
59
- } catch (e) {
60
- n({
61
- debugs: [`Error sending request: ${e}`],
62
- name: "LogError",
63
- scheme: "red"
64
- });
65
- }
1
+ import { flushDebugLogs as e } from "../../utilities/flushDebugLogs.js";
2
+ import { LogService as t } from "../../services/logService.js";
3
+ import { formatJsonObject as n } from "@arkyn/shared";
4
+ //#region src/http/api/_logRequest.ts
5
+ async function r(r) {
6
+ let i = t.getConfig();
7
+ if (!i) return;
8
+ let { userToken: a, apiUrl: o, trafficSourceId: s } = i, { elapsedTime: c, method: l, queryParams: u, requestBody: d, requestHeaders: f, responseBody: p, responseHeaders: m, status: h, token: g, rawUrl: _ } = r;
9
+ if (process.env.NODE_ENV !== "development") try {
10
+ let t = new URL(_), r = "https";
11
+ t.protocol === "http:" && (r = "http");
12
+ let i = JSON.stringify({
13
+ domainUrl: t.protocol + "//" + t.host,
14
+ pathnameUrl: t.pathname,
15
+ trafficSourceId: s,
16
+ status: h,
17
+ protocol: r,
18
+ method: l.toLowerCase(),
19
+ trafficUserId: null,
20
+ elapsedTime: c,
21
+ requestHeaders: JSON.stringify(f),
22
+ requestBody: JSON.stringify(d),
23
+ queryParams: JSON.stringify(u),
24
+ responseHeaders: JSON.stringify(m),
25
+ responseBody: JSON.stringify(p)
26
+ }), g = {
27
+ "Content-Type": "application/json",
28
+ Authorization: `Bearer ${a}`
29
+ }, v = await fetch(o, {
30
+ method: "POST",
31
+ body: i,
32
+ headers: g
33
+ });
34
+ if (!v.ok) {
35
+ let t = await v.text(), r = v.status, i = v.statusText, a = await v.json().catch(() => null);
36
+ e({
37
+ name: "LogError",
38
+ scheme: "red",
39
+ debugs: [
40
+ "Failed to log request.",
41
+ `Status: ${r} ${i}.`,
42
+ `Status text: ${t}.`,
43
+ `JSON Response: ${a ? n(a, 0) : "No JSON response"}`
44
+ ]
45
+ });
46
+ }
47
+ } catch (t) {
48
+ e({
49
+ debugs: [`Error sending request: ${t}`],
50
+ name: "LogError",
51
+ scheme: "red"
52
+ });
53
+ }
66
54
  }
67
- export {
68
- B as logRequest
69
- };
55
+ //#endregion
56
+ export { r as logRequest };
@@ -1,69 +1,70 @@
1
- import { flushDebugLogs as n } from "../../utilities/flushDebugLogs.js";
2
- import { LogMapperService as d } from "../../services/logMapperService.js";
3
- import { logRequest as f } from "./_logRequest.js";
4
- async function i(e) {
5
- let a = e.url;
6
- e.urlParams && Object.entries(e.urlParams).forEach(([r, c]) => {
7
- a = a.replaceAll(`:${r}`, c);
8
- });
9
- const u = {
10
- POST: "Resource created successfully",
11
- PUT: "Resource updated successfully",
12
- DELETE: "Resource deleted successfully",
13
- PATCH: "Resource patched successfully",
14
- GET: "Request successful"
15
- };
16
- try {
17
- const r = performance.now(), c = { ...e.headers, "Content-Type": "application/json" }, o = await fetch(a, {
18
- headers: c,
19
- method: e.method,
20
- body: e.body ? JSON.stringify(e.body) : void 0
21
- }), l = performance.now() - r, t = o.status;
22
- let s = null;
23
- try {
24
- s = await o.json();
25
- } catch {
26
- s = null;
27
- }
28
- const m = d.handle({
29
- elapsedTime: l,
30
- method: e.method,
31
- queryParams: new URL(a).searchParams,
32
- requestHeaders: c,
33
- requestBody: e.body,
34
- responseBody: s,
35
- responseHeaders: o.headers,
36
- status: t,
37
- rawUrl: e.url,
38
- urlParams: e.urlParams
39
- });
40
- return f(m), o.ok ? {
41
- success: !0,
42
- status: t,
43
- message: (s == null ? void 0 : s.message) || u[e.method],
44
- response: s,
45
- cause: null
46
- } : {
47
- success: !1,
48
- status: t,
49
- message: (s == null ? void 0 : s.message) || o.statusText || "Request failed",
50
- response: s,
51
- cause: null
52
- };
53
- } catch (r) {
54
- return n({
55
- debugs: [`Network error or request failed: ${r}`],
56
- name: "MakeRequestError",
57
- scheme: "red"
58
- }), {
59
- success: !1,
60
- status: 0,
61
- message: "Network error or request failed",
62
- response: null,
63
- cause: r instanceof Error ? r.message : String(r)
64
- };
65
- }
1
+ import { flushDebugLogs as e } from "../../utilities/flushDebugLogs.js";
2
+ import { LogMapperService as t } from "../../services/logMapperService.js";
3
+ import { logRequest as n } from "./_logRequest.js";
4
+ //#region src/http/api/_makeRequest.ts
5
+ async function r(r) {
6
+ let i = r.url;
7
+ r.urlParams && Object.entries(r.urlParams).forEach(([e, t]) => {
8
+ i = i.replaceAll(`:${e}`, t);
9
+ });
10
+ let a = {
11
+ POST: "Resource created successfully",
12
+ PUT: "Resource updated successfully",
13
+ DELETE: "Resource deleted successfully",
14
+ PATCH: "Resource patched successfully",
15
+ GET: "Request successful"
16
+ };
17
+ try {
18
+ let e = performance.now(), o = {
19
+ ...r.headers,
20
+ "Content-Type": "application/json"
21
+ }, s = await fetch(i, {
22
+ headers: o,
23
+ method: r.method,
24
+ body: r.body ? JSON.stringify(r.body) : void 0
25
+ }), c = performance.now() - e, l = s.status, u = null;
26
+ try {
27
+ u = await s.json();
28
+ } catch {
29
+ u = null;
30
+ }
31
+ return n(t.handle({
32
+ elapsedTime: c,
33
+ method: r.method,
34
+ queryParams: new URL(i).searchParams,
35
+ requestHeaders: o,
36
+ requestBody: r.body,
37
+ responseBody: u,
38
+ responseHeaders: s.headers,
39
+ status: l,
40
+ rawUrl: r.url,
41
+ urlParams: r.urlParams
42
+ })), s.ok ? {
43
+ success: !0,
44
+ status: l,
45
+ message: u?.message || a[r.method],
46
+ response: u,
47
+ cause: null
48
+ } : {
49
+ success: !1,
50
+ status: l,
51
+ message: u?.message || s.statusText || "Request failed",
52
+ response: u,
53
+ cause: null
54
+ };
55
+ } catch (t) {
56
+ return e({
57
+ debugs: [`Network error or request failed: ${t}`],
58
+ name: "MakeRequestError",
59
+ scheme: "red"
60
+ }), {
61
+ success: !1,
62
+ status: 0,
63
+ message: "Network error or request failed",
64
+ response: null,
65
+ cause: t instanceof Error ? t.message : String(t)
66
+ };
67
+ }
66
68
  }
67
- export {
68
- i as makeRequest
69
- };
69
+ //#endregion
70
+ export { r as makeRequest };
@@ -1,13 +1,13 @@
1
- import { makeRequest as r } from "./_makeRequest.js";
2
- async function o(e) {
3
- return r({
4
- method: "DELETE",
5
- url: e.url,
6
- urlParams: e.urlParams,
7
- headers: e.headers,
8
- body: e.body
9
- });
1
+ import { makeRequest as e } from "./_makeRequest.js";
2
+ //#region src/http/api/deleteRequest.ts
3
+ async function t(t) {
4
+ return e({
5
+ method: "DELETE",
6
+ url: t.url,
7
+ urlParams: t.urlParams,
8
+ headers: t.headers,
9
+ body: t.body
10
+ });
10
11
  }
11
- export {
12
- o as deleteRequest
13
- };
12
+ //#endregion
13
+ export { t as deleteRequest };
@@ -1,12 +1,12 @@
1
- import { makeRequest as r } from "./_makeRequest.js";
2
- async function s(e) {
3
- return r({
4
- method: "GET",
5
- url: e.url,
6
- urlParams: e.urlParams,
7
- headers: e.headers
8
- });
1
+ import { makeRequest as e } from "./_makeRequest.js";
2
+ //#region src/http/api/getRequest.ts
3
+ async function t(t) {
4
+ return e({
5
+ method: "GET",
6
+ url: t.url,
7
+ urlParams: t.urlParams,
8
+ headers: t.headers
9
+ });
9
10
  }
10
- export {
11
- s as getRequest
12
- };
11
+ //#endregion
12
+ export { t as getRequest };
@@ -1,13 +1,13 @@
1
1
  import { makeRequest as e } from "./_makeRequest.js";
2
- async function o(r) {
3
- return e({
4
- method: "PATCH",
5
- url: r.url,
6
- urlParams: r.urlParams,
7
- headers: r.headers,
8
- body: r.body
9
- });
2
+ //#region src/http/api/patchRequest.ts
3
+ async function t(t) {
4
+ return e({
5
+ method: "PATCH",
6
+ url: t.url,
7
+ urlParams: t.urlParams,
8
+ headers: t.headers,
9
+ body: t.body
10
+ });
10
11
  }
11
- export {
12
- o as patchRequest
13
- };
12
+ //#endregion
13
+ export { t as patchRequest };
@@ -1,13 +1,13 @@
1
1
  import { makeRequest as e } from "./_makeRequest.js";
2
- async function o(r) {
3
- return e({
4
- method: "POST",
5
- url: r.url,
6
- urlParams: r.urlParams,
7
- headers: r.headers,
8
- body: r.body
9
- });
2
+ //#region src/http/api/postRequest.ts
3
+ async function t(t) {
4
+ return e({
5
+ method: "POST",
6
+ url: t.url,
7
+ urlParams: t.urlParams,
8
+ headers: t.headers,
9
+ body: t.body
10
+ });
10
11
  }
11
- export {
12
- o as postRequest
13
- };
12
+ //#endregion
13
+ export { t as postRequest };
@@ -1,13 +1,13 @@
1
1
  import { makeRequest as e } from "./_makeRequest.js";
2
- async function o(r) {
3
- return e({
4
- method: "PUT",
5
- url: r.url,
6
- urlParams: r.urlParams,
7
- headers: r.headers,
8
- body: r.body
9
- });
2
+ //#region src/http/api/putRequest.ts
3
+ async function t(t) {
4
+ return e({
5
+ method: "PUT",
6
+ url: t.url,
7
+ urlParams: t.urlParams,
8
+ headers: t.headers,
9
+ body: t.body
10
+ });
10
11
  }
11
- export {
12
- o as putRequest
13
- };
12
+ //#endregion
13
+ export { t as putRequest };
@@ -1,59 +1,63 @@
1
- var r = Object.defineProperty;
2
- var n = (t, s, e) => s in t ? r(t, s, { enumerable: !0, configurable: !0, writable: !0, value: e }) : t[s] = e;
3
- var u = (t, s, e) => n(t, typeof s != "symbol" ? s + "" : s, e);
4
- import { formatJsonString as o } from "@arkyn/shared";
5
- import { DebugService as i } from "../../services/debugService.js";
6
- import { flushDebugLogs as h } from "../../utilities/flushDebugLogs.js";
7
- class l {
8
- constructor() {
9
- u(this, "_cause");
10
- u(this, "_name", "BadResponse");
11
- u(this, "_status", 500);
12
- u(this, "_statusText", "Unknown error");
13
- u(this, "_debugColor", "red");
14
- }
15
- get cause() {
16
- return this._cause;
17
- }
18
- set cause(s) {
19
- this._cause = s;
20
- }
21
- get name() {
22
- return this._name;
23
- }
24
- set name(s) {
25
- this._name = s;
26
- }
27
- get status() {
28
- return this._status;
29
- }
30
- set status(s) {
31
- this._status = s;
32
- }
33
- get statusText() {
34
- return this._statusText;
35
- }
36
- set statusText(s) {
37
- this._statusText = s;
38
- }
39
- get debugColor() {
40
- return this._debugColor;
41
- }
42
- set debugColor(s) {
43
- ["green", "yellow", "cyan", "red"].includes(s) && (this._debugColor = s);
44
- }
45
- onDebug() {
46
- const s = [], { callerInfo: e, functionName: a } = i.getCaller();
47
- s.push(`Caller Function: ${a}`), s.push(`Caller Location: ${e}`), this._statusText && s.push(`Message: ${this._statusText}`), this._cause && s.push(`Cause: ${o(JSON.stringify(this._cause))}`), h({ scheme: "red", name: this._name, debugs: s });
48
- }
49
- makeBody() {
50
- return {
51
- name: this._name,
52
- message: this._statusText,
53
- cause: this._cause
54
- };
55
- }
56
- }
57
- export {
58
- l as BadResponse
1
+ import { DebugService as e } from "../../services/debugService.js";
2
+ import { flushDebugLogs as t } from "../../utilities/flushDebugLogs.js";
3
+ import { formatJsonString as n } from "@arkyn/shared";
4
+ //#region src/http/badResponses/_badResponse.ts
5
+ var r = class {
6
+ _cause;
7
+ _name = "BadResponse";
8
+ _status = 500;
9
+ _statusText = "Unknown error";
10
+ _debugColor = "red";
11
+ get cause() {
12
+ return this._cause;
13
+ }
14
+ set cause(e) {
15
+ this._cause = e;
16
+ }
17
+ get name() {
18
+ return this._name;
19
+ }
20
+ set name(e) {
21
+ this._name = e;
22
+ }
23
+ get status() {
24
+ return this._status;
25
+ }
26
+ set status(e) {
27
+ this._status = e;
28
+ }
29
+ get statusText() {
30
+ return this._statusText;
31
+ }
32
+ set statusText(e) {
33
+ this._statusText = e;
34
+ }
35
+ get debugColor() {
36
+ return this._debugColor;
37
+ }
38
+ set debugColor(e) {
39
+ [
40
+ "green",
41
+ "yellow",
42
+ "cyan",
43
+ "red"
44
+ ].includes(e) && (this._debugColor = e);
45
+ }
46
+ onDebug() {
47
+ let r = [], { callerInfo: i, functionName: a } = e.getCaller();
48
+ r.push(`Caller Function: ${a}`), r.push(`Caller Location: ${i}`), this._statusText && r.push(`Message: ${this._statusText}`), this._cause && r.push(`Cause: ${n(JSON.stringify(this._cause))}`), t({
49
+ scheme: "red",
50
+ name: this._name,
51
+ debugs: r
52
+ });
53
+ }
54
+ makeBody() {
55
+ return {
56
+ name: this._name,
57
+ message: this._statusText,
58
+ cause: this._cause
59
+ };
60
+ }
59
61
  };
62
+ //#endregion
63
+ export { r as BadResponse };
@@ -1,30 +1,24 @@
1
1
  import { BadResponse as e } from "./_badResponse.js";
2
- class o extends e {
3
- /**
4
- * @param message - Error description sent in the response body and logged for debugging.
5
- * @param cause - Optional extra context (serialized to JSON in the response).
6
- */
7
- constructor(s, t) {
8
- super(), this.name = "BadGateway", this.status = 502, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
9
- }
10
- /** Converts to a `Response` with `Content-Type: application/json` header. */
11
- toResponse() {
12
- const s = {
13
- headers: { "Content-Type": "application/json" },
14
- status: this.status,
15
- statusText: this.statusText
16
- };
17
- return new Response(JSON.stringify(this.makeBody()), s);
18
- }
19
- /** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
20
- toJson() {
21
- const s = {
22
- status: this.status,
23
- statusText: this.statusText
24
- };
25
- return Response.json(this.makeBody(), s);
26
- }
27
- }
28
- export {
29
- o as BadGateway
2
+ //#region src/http/badResponses/badGateway.ts
3
+ var t = class extends e {
4
+ constructor(e, t) {
5
+ super(), this.name = "BadGateway", this.status = 502, this.statusText = e, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
6
+ }
7
+ toResponse() {
8
+ let e = {
9
+ headers: { "Content-Type": "application/json" },
10
+ status: this.status,
11
+ statusText: this.statusText
12
+ };
13
+ return new Response(JSON.stringify(this.makeBody()), e);
14
+ }
15
+ toJson() {
16
+ let e = {
17
+ status: this.status,
18
+ statusText: this.statusText
19
+ };
20
+ return Response.json(this.makeBody(), e);
21
+ }
30
22
  };
23
+ //#endregion
24
+ export { t as BadGateway };
@@ -1,30 +1,24 @@
1
1
  import { BadResponse as e } from "./_badResponse.js";
2
- class i extends e {
3
- /**
4
- * @param message - Error description.
5
- * @param cause - Optional extra context (serialized to JSON).
6
- */
7
- constructor(s, t) {
8
- super(), this.name = "BadRequest", this.status = 400, this.statusText = s, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
9
- }
10
- /** Converts to a `Response` with `Content-Type: application/json` header. */
11
- toResponse() {
12
- const s = {
13
- headers: { "Content-Type": "application/json" },
14
- status: this.status,
15
- statusText: this.statusText
16
- };
17
- return new Response(JSON.stringify(this.makeBody()), s);
18
- }
19
- /** Converts to a `Response` using `Response.json()`. Alternative to `toResponse()`. */
20
- toJson() {
21
- const s = {
22
- status: this.status,
23
- statusText: this.statusText
24
- };
25
- return Response.json(this.makeBody(), s);
26
- }
27
- }
28
- export {
29
- i as BadRequest
2
+ //#region src/http/badResponses/badRequest.ts
3
+ var t = class extends e {
4
+ constructor(e, t) {
5
+ super(), this.name = "BadRequest", this.status = 400, this.statusText = e, this.cause = t ? JSON.stringify(t) : void 0, this.onDebug();
6
+ }
7
+ toResponse() {
8
+ let e = {
9
+ headers: { "Content-Type": "application/json" },
10
+ status: this.status,
11
+ statusText: this.statusText
12
+ };
13
+ return new Response(JSON.stringify(this.makeBody()), e);
14
+ }
15
+ toJson() {
16
+ let e = {
17
+ status: this.status,
18
+ statusText: this.statusText
19
+ };
20
+ return Response.json(this.makeBody(), e);
21
+ }
30
22
  };
23
+ //#endregion
24
+ export { t as BadRequest };