@arkyn/server 3.0.1-beta.64 → 3.0.1-beta.65

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 CHANGED
@@ -79,8 +79,8 @@ function R() {
79
79
  const a = c.match(/at\s+(.+)/);
80
80
  if (a) {
81
81
  i = a[1];
82
- const p = i.match(/at\s+([^(\s]+)\s+/);
83
- p && p[1] !== "new" && (h = p[1]);
82
+ const d = i.match(/at\s+([^(\s]+)\s+/);
83
+ d && d[1] !== "new" && (h = d[1]);
84
84
  }
85
85
  }
86
86
  i.includes("(") && (i = i.substring(
@@ -93,7 +93,7 @@ function R() {
93
93
  }
94
94
  return { functionName: h, callerInfo: i };
95
95
  }
96
- class d {
96
+ class y {
97
97
  /**
98
98
  * Logs debug information for bad responses including caller context and response details.
99
99
  *
@@ -117,7 +117,7 @@ class d {
117
117
  `), f({ scheme: "red", name: "ARKYN-BAD-RESPONSE-DEBUG", debugs: o });
118
118
  }
119
119
  }
120
- class j extends d {
120
+ class j extends y {
121
121
  /**
122
122
  * Creates an instance of the `BadGateway` class.
123
123
  *
@@ -160,7 +160,7 @@ class j extends d {
160
160
  return Response.json(this.body, s);
161
161
  }
162
162
  }
163
- class g extends d {
163
+ class g extends y {
164
164
  /**
165
165
  * Creates an instance of the `BadRequest` class.
166
166
  *
@@ -203,7 +203,7 @@ class g extends d {
203
203
  return Response.json(this.body, s);
204
204
  }
205
205
  }
206
- class J extends d {
206
+ class J extends y {
207
207
  /**
208
208
  * Creates an instance of the `Conflict` class.
209
209
  *
@@ -246,7 +246,7 @@ class J extends d {
246
246
  return Response.json(this.body, s);
247
247
  }
248
248
  }
249
- class D extends d {
249
+ class D extends y {
250
250
  /**
251
251
  * Creates an instance of the `Forbidden` class.
252
252
  *
@@ -289,7 +289,7 @@ class D extends d {
289
289
  return Response.json(this.body, s);
290
290
  }
291
291
  }
292
- class C extends d {
292
+ class C extends y {
293
293
  /**
294
294
  * Creates an instance of the `NotFound` class.
295
295
  *
@@ -332,7 +332,7 @@ class C extends d {
332
332
  return Response.json(this.body, s);
333
333
  }
334
334
  }
335
- class B extends d {
335
+ class B extends y {
336
336
  /**
337
337
  * Creates an instance of the `NotImplemented` class.
338
338
  *
@@ -375,7 +375,7 @@ class B extends d {
375
375
  return Response.json(this.body, s);
376
376
  }
377
377
  }
378
- class x extends d {
378
+ class x extends y {
379
379
  /**
380
380
  * Creates an instance of the `ServerError` class.
381
381
  *
@@ -418,7 +418,7 @@ class x extends d {
418
418
  return Response.json(this.body, s);
419
419
  }
420
420
  }
421
- class q extends d {
421
+ class q extends y {
422
422
  /**
423
423
  * Creates an instance of the `Unauthorized` class.
424
424
  *
@@ -461,7 +461,7 @@ class q extends d {
461
461
  return Response.json(this.body, s);
462
462
  }
463
463
  }
464
- class N extends d {
464
+ class N extends y {
465
465
  /**
466
466
  * Creates an instance of the `UnprocessableEntity` class.
467
467
  *
@@ -632,7 +632,7 @@ class H extends T {
632
632
  return Response.json(this.body, s);
633
633
  }
634
634
  }
635
- class L extends T {
635
+ class $ extends T {
636
636
  /**
637
637
  * Creates an instance of the `NoContent` class.
638
638
  *
@@ -660,7 +660,7 @@ class L extends T {
660
660
  return new Response(null, s);
661
661
  }
662
662
  }
663
- class $ extends T {
663
+ class A extends T {
664
664
  /**
665
665
  * Creates an instance of the `Success` class.
666
666
  *
@@ -704,7 +704,7 @@ class $ extends T {
704
704
  return Response.json(this.body, s);
705
705
  }
706
706
  }
707
- class A extends T {
707
+ class L extends T {
708
708
  /**
709
709
  * Creates an instance of the `Updated` class.
710
710
  *
@@ -823,19 +823,19 @@ async function F(n) {
823
823
  requestHeaders: i,
824
824
  responseBody: l,
825
825
  responseHeaders: a,
826
- status: p,
826
+ status: d,
827
827
  token: Q,
828
828
  rawUrl: E
829
829
  } = n;
830
830
  if (process.env.NODE_ENV !== "development")
831
831
  try {
832
- const y = new URL(E);
832
+ const p = new URL(E);
833
833
  let m = "HTTPS";
834
- y.protocol === "http:" && (m = "HTTP");
834
+ p.protocol === "http:" && (m = "HTTP");
835
835
  const O = JSON.stringify({
836
- domainUrl: y.protocol + "//" + y.host,
837
- pathnameUrl: y.pathname,
838
- status: p,
836
+ domainUrl: p.protocol + "//" + p.host,
837
+ pathnameUrl: p.pathname,
838
+ status: d,
839
839
  protocol: m,
840
840
  method: u,
841
841
  trafficUserId: null,
@@ -860,9 +860,9 @@ async function F(n) {
860
860
  }
861
861
  }
862
862
  );
863
- } catch (y) {
863
+ } catch (p) {
864
864
  f({
865
- debugs: [`Error sending request: ${y}`],
865
+ debugs: [`Error sending request: ${p}`],
866
866
  name: "ARKYN_LOG_ERROR",
867
867
  scheme: "red"
868
868
  });
@@ -888,7 +888,7 @@ async function b(n, t, s = {}, e) {
888
888
  } catch {
889
889
  a = null;
890
890
  }
891
- const p = v.handle({
891
+ const d = v.handle({
892
892
  elapsedTime: i,
893
893
  method: n,
894
894
  queryParams: new URL(t).searchParams,
@@ -899,7 +899,7 @@ async function b(n, t, s = {}, e) {
899
899
  status: l,
900
900
  url: t
901
901
  });
902
- return F(p), h.ok ? {
902
+ return F(d), h.ok ? {
903
903
  success: !0,
904
904
  status: l,
905
905
  message: (a == null ? void 0 : a.message) || o[n],
@@ -965,7 +965,7 @@ class X {
965
965
  onDebug(t, s, e) {
966
966
  if (this.enableDebug) {
967
967
  const o = [];
968
- o.push(`Base URL: ${this.baseUrl}`), o.push(`Endpoint URL: ${t}`), o.push(`Method: ${s}`), e[0] && o.push(`Headers: ${JSON.stringify(e[0])}`), e[1] && o.push(`Body: ${JSON.stringify(e[1])}`), f({ debugs: o, name: "ARKYN-API-DEBUG", scheme: "yellow" });
968
+ o.push(`Base URL: ${this.baseUrl}`), o.push(`Endpoint: ${t}`), o.push(`Method: ${s}`), e[0] && o.push(`Headers: ${JSON.stringify(e[0])}`), e[1] && o.push(`Body: ${JSON.stringify(e[1])}`), f({ debugs: o, name: "ARKYN-API-DEBUG", scheme: "yellow" });
969
969
  }
970
970
  }
971
971
  generateURL(t) {
@@ -989,7 +989,7 @@ class X {
989
989
  */
990
990
  async get(t, s) {
991
991
  const e = this.generateURL(t), o = this.generateHeaders((s == null ? void 0 : s.headers) || {}, s == null ? void 0 : s.token);
992
- return this.onDebug(e, "get", [o]), await _(e, o);
992
+ return this.onDebug(t, "get", [o]), await _(e, o);
993
993
  }
994
994
  /**
995
995
  * Sends a post request to the specified endpoint.
@@ -999,7 +999,7 @@ class X {
999
999
  */
1000
1000
  async post(t, s) {
1001
1001
  const e = this.generateURL(t), o = this.generateHeaders((s == null ? void 0 : s.headers) || {}, s == null ? void 0 : s.token), u = s == null ? void 0 : s.body;
1002
- return this.onDebug(e, "post", [o, u]), await G(e, o, u);
1002
+ return this.onDebug(t, "post", [o, u]), await G(e, o, u);
1003
1003
  }
1004
1004
  /**
1005
1005
  * Sends a put request to the specified endpoint.
@@ -1019,7 +1019,7 @@ class X {
1019
1019
  */
1020
1020
  async patch(t, s) {
1021
1021
  const e = this.generateURL(t), o = this.generateHeaders((s == null ? void 0 : s.headers) || {}, s == null ? void 0 : s.token), u = s == null ? void 0 : s.body;
1022
- return this.onDebug(e, "patch", [o, u]), await z(e, o, u);
1022
+ return this.onDebug(t, "patch", [o, u]), await z(e, o, u);
1023
1023
  }
1024
1024
  /**
1025
1025
  * Sends a delete request to the specified endpoint.
@@ -1029,7 +1029,7 @@ class X {
1029
1029
  */
1030
1030
  async delete(t, s) {
1031
1031
  const e = this.generateURL(t), o = this.generateHeaders((s == null ? void 0 : s.headers) || {}, s == null ? void 0 : s.token), u = s == null ? void 0 : s.body;
1032
- return this.onDebug(e, "delete", [o, u]), await M(e, o, u);
1032
+ return this.onDebug(t, "delete", [o, u]), await M(e, o, u);
1033
1033
  }
1034
1034
  }
1035
1035
  function Z(n, t) {
@@ -1062,12 +1062,12 @@ function es(n) {
1062
1062
  return n.toResponse();
1063
1063
  case n instanceof P:
1064
1064
  return n.toResponse();
1065
+ case n instanceof L:
1066
+ return n.toResponse();
1065
1067
  case n instanceof A:
1066
1068
  return n.toResponse();
1067
1069
  case n instanceof $:
1068
1070
  return n.toResponse();
1069
- case n instanceof L:
1070
- return n.toResponse();
1071
1071
  }
1072
1072
  switch (!0) {
1073
1073
  case n instanceof j:
@@ -1171,15 +1171,15 @@ export {
1171
1171
  S as DebugService,
1172
1172
  D as Forbidden,
1173
1173
  H as Found,
1174
- L as NoContent,
1174
+ $ as NoContent,
1175
1175
  C as NotFound,
1176
1176
  B as NotImplemented,
1177
1177
  ns as SchemaValidator,
1178
1178
  x as ServerError,
1179
- $ as Success,
1179
+ A as Success,
1180
1180
  q as Unauthorized,
1181
1181
  N as UnprocessableEntity,
1182
- A as Updated,
1182
+ L as Updated,
1183
1183
  Z as decodeErrorMessageFromRequest,
1184
1184
  ss as decodeRequestBody,
1185
1185
  es as errorHandler,
@@ -11,5 +11,5 @@
11
11
  `),o.push(`Caller Location: ${u}
12
12
  `),o.push(`Body: ${JSON.stringify(s,null,2)}
13
13
  `),e&&o.push(`Cause: ${JSON.stringify(e,null,2)}
14
- `),c({scheme:"green",name:"ARKYN-SUCCESS-RESPONSE-DEBUG",debugs:o})}}class J extends T{constructor(s,e){super();r(this,"body");r(this,"headers");r(this,"status");r(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",this.onDebug("Created",s)}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 D extends T{constructor(s,e){super();r(this,"body");r(this,"headers");r(this,"status");r(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",this.onDebug("Found",s)}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 B extends T{constructor(s){super();r(this,"headers");r(this,"status");r(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",this.onDebug("No content",null)}toResponse(){const s={headers:this.headers,status:this.status,statusText:this.statusText};return new Response(null,s)}}class q extends T{constructor(s,e){super();r(this,"body");r(this,"headers");r(this,"status");r(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",this.onDebug("Success",s)}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 extends T{constructor(s,e){super();r(this,"body");r(this,"headers");r(this,"status");r(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",this.onDebug("Updated",s)}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 A{static mapHeaders(t){return t instanceof Headers?Object.fromEntries(t.entries()):typeof t=="object"?Object.entries(t).reduce((s,[e,o])=>(typeof o=="string"?s[e]=o:Array.isArray(o)?s[e]=o.join(", "):s[e]=JSON.stringify(o),s),{}):{}}static mapQueryParams(t){const s={};return t.forEach((e,o)=>{s[o]=e}),s}static handle(t){return{rawUrl:t.url,status:t.status,method:t.method,token:null,elapsedTime:t.elapsedTime,requestHeaders:this.mapHeaders(t.requestHeaders),requestBody:t.requestBody||null,queryParams:this.mapQueryParams(t.queryParams),responseHeaders:this.mapHeaders(t.responseHeaders),responseBody:t.responseBody||null}}}class E{static setArkynConfig(t){if(this.arkynConfig)return;let e=t.arkynLogBaseApiUrl||"https://logs-arkyn-flow-logs.vw6wo7.easypanel.host";e=e+"/http-traffic-records/:trafficSourceId",this.arkynConfig={arkynTrafficSourceId:t.arkynTrafficSourceId,arkynUserToken:t.arkynUserToken,arkynApiUrl:e}}static getArkynConfig(){return this.arkynConfig}static resetArkynConfig(){this.arkynConfig=void 0}}r(E,"arkynConfig");async function H(n){const t=E.getArkynConfig();if(!t)return;const{arkynUserToken:s,arkynApiUrl:e}=t,{elapsedTime:o,method:u,queryParams:l,requestBody:d,requestHeaders:h,responseBody:f,responseHeaders:i,status:p,token:ns,rawUrl:Z}=n;if(process.env.NODE_ENV!=="development")try{const g=new URL(Z);let v="HTTPS";g.protocol==="http:"&&(v="HTTP");const ss=JSON.stringify({domainUrl:g.protocol+"//"+g.host,pathnameUrl:g.pathname,status:p,protocol:v,method:u,trafficUserId:null,elapsedTime:o,requestHeaders:h,requestBody:d,queryParams:l,responseHeaders:i,responseBody:f});await fetch(e.replace(":trafficSourceId",t.arkynTrafficSourceId),{method:"POST",body:ss,headers:{"Content-Type":"application/json",Authorization:`Bearer ${s}`}})}catch(g){c({debugs:[`Error sending request: ${g}`],name:"ARKYN_LOG_ERROR",scheme:"red"})}}async function m(n,t,s={},e){const o={POST:"Resource created successfully",PUT:"Resource updated successfully",DELETE:"Resource deleted successfully",PATCH:"Resource patched successfully",GET:"Request successful"};try{const u=performance.now(),l={...s,"Content-Type":"application/json"},d=await fetch(t,{method:n,headers:l,body:e?JSON.stringify(e):void 0}),h=performance.now()-u,f=d.status;let i=null;try{i=await d.json()}catch{i=null}const p=A.handle({elapsedTime:h,method:n,queryParams:new URL(t).searchParams,requestHeaders:l,requestBody:e,responseBody:i,responseHeaders:d.headers,status:f,url:t});return H(p),d.ok?{success:!0,status:f,message:(i==null?void 0:i.message)||o[n],response:i,cause:null}:{success:!1,status:f,message:(i==null?void 0:i.message)||d.statusText||"Request failed",response:i,cause:null}}catch(u){return c({debugs:[`Network error or request failed: ${u}`],name:"ARKYN_MAKE_REQUEST_ERROR",scheme:"red"}),{success:!1,status:0,message:"Network error or request failed",response:null,cause:u instanceof Error?u.message:String(u)}}}async function $(n,t={},s){return m("DELETE",n,t,s)}async function F(n,t={}){return m("GET",n,t)}async function M(n,t={},s){return m("PATCH",n,t,s)}async function _(n,t={},s){return m("POST",n,t,s)}async function z(n,t={},s){return m("PUT",n,t,s)}class G{constructor(t){r(this,"baseUrl");r(this,"baseHeaders");r(this,"baseToken");r(this,"enableDebug");this.baseUrl=t.baseUrl,this.baseHeaders=t.baseHeaders||void 0,this.baseToken=t.baseToken||void 0,this.enableDebug=t.enableDebug||!1}onDebug(t,s,e){if(this.enableDebug){const o=[];o.push(`Base URL: ${this.baseUrl}`),o.push(`Endpoint URL: ${t}`),o.push(`Method: ${s}`),e[0]&&o.push(`Headers: ${JSON.stringify(e[0])}`),e[1]&&o.push(`Body: ${JSON.stringify(e[1])}`),c({debugs:o,name:"ARKYN-API-DEBUG",scheme:"yellow"})}}generateURL(t){return this.baseUrl+t}generateHeaders(t,s){let e={};return this.baseToken&&(e={Authorization:`Bearer ${this.baseToken}`}),this.baseHeaders&&(e={...e,...this.baseHeaders}),t&&(e={...e,...t}),s&&(e={...e,Authorization:`Bearer ${s}`}),e}async get(t,s){const e=this.generateURL(t),o=this.generateHeaders((s==null?void 0:s.headers)||{},s==null?void 0:s.token);return this.onDebug(e,"get",[o]),await F(e,o)}async post(t,s){const e=this.generateURL(t),o=this.generateHeaders((s==null?void 0:s.headers)||{},s==null?void 0:s.token),u=s==null?void 0:s.body;return this.onDebug(e,"post",[o,u]),await _(e,o,u)}async put(t,s){const e=this.generateURL(t),o=this.generateHeaders((s==null?void 0:s.headers)||{},s==null?void 0:s.token),u=s==null?void 0:s.body;return this.onDebug(t,"put",[o,u]),await z(e,o,u)}async patch(t,s){const e=this.generateURL(t),o=this.generateHeaders((s==null?void 0:s.headers)||{},s==null?void 0:s.token),u=s==null?void 0:s.body;return this.onDebug(e,"patch",[o,u]),await M(e,o,u)}async delete(t,s){const e=this.generateURL(t),o=this.generateHeaders((s==null?void 0:s.headers)||{},s==null?void 0:s.token),u=s==null?void 0:s.body;return this.onDebug(e,"delete",[o,u]),await $(e,o,u)}}function K(n,t){var s,e,o;return n!=null&&n.message&&typeof(n==null?void 0:n.message)=="string"?n==null?void 0:n.message:n!=null&&n.error&&typeof(n==null?void 0:n.error)=="string"?n==null?void 0:n.error:(s=n==null?void 0:n.error)!=null&&s.message&&typeof((e=n==null?void 0:n.error)==null?void 0:e.message)=="string"?(o=n==null?void 0:n.error)==null?void 0:o.message:t!=null&&t.statusText&&typeof(t==null?void 0:t.statusText)=="string"?t==null?void 0:t.statusText:"Missing error message"}const V=async n=>{let t;const s=await n.arrayBuffer(),e=new TextDecoder().decode(s);try{t=JSON.parse(e)}catch{try{if(e.includes("=")){const u=new URLSearchParams(e);t=Object.fromEntries(u.entries())}else throw new x("Invalid URLSearchParams format")}catch{throw new x("Failed to extract data from request")}}return t};function Y(n){switch(!0){case n instanceof Response:return n;case n instanceof D:return n.toResponse();case n instanceof J:return n.toResponse();case n instanceof P:return n.toResponse();case n instanceof q:return n.toResponse();case n instanceof B:return n.toResponse()}switch(!0){case n instanceof O:return n.toResponse();case n instanceof x:return n.toResponse();case n instanceof U:return n.toResponse();case n instanceof I:return n.toResponse();case n instanceof k:return n.toResponse();case n instanceof j:return n.toResponse();case n instanceof S:return n.toResponse();case n instanceof C:return n.toResponse();case n instanceof w:return n.toResponse()}return new S("Server error",n).toResponse()}function L([n,t]){const s=t.safeParse(n);if(s.success===!1){const e=Object.fromEntries(s.error.issues.map(o=>[o.path.join("."),o.message]));return{success:s.success,fieldErrors:e,fields:n}}else return{success:s.success,data:s.data}}const Q=(n,t="")=>{const s=new URL(n.url);if(t==="")return s.searchParams;const e=Array.from(s.searchParams.entries()).filter(([o])=>o.startsWith(`${t}:`)).map(([o,u])=>[o.replace(`${t}:`,""),u]);return new URLSearchParams(e)};function W(n){const t="Error validating:",s=n.issues.map(({path:e,message:o})=>`-> ${e.join(".")}: ${o}`);return[t,...s].join(`
15
- `)}class X{constructor(t){r(this,"functionName");r(this,"callerInfo");this.schema=t;const{callerInfo:s,functionName:e}=R();this.callerInfo=s,this.functionName=e}isValid(t){return this.schema.safeParse(t).success}safeValidate(t){return this.schema.safeParse(t)}validate(t){try{return this.schema.parse(t)}catch(s){throw new S(W(s))}}formValidate(t,s){const e=L([t,this.schema]);if(!e.success){const o=Object.keys(e.fieldErrors)[0];throw new w({fields:e.fields,fieldErrors:e.fieldErrors,data:{scrollTo:o},message:s})}return e.data}}a.ApiService=G,a.ArkynLogService=E,a.BadGateway=O,a.BadRequest=x,a.Conflict=U,a.Created=J,a.DebugService=N,a.Forbidden=I,a.Found=D,a.NoContent=B,a.NotFound=k,a.NotImplemented=j,a.SchemaValidator=X,a.ServerError=S,a.Success=q,a.Unauthorized=C,a.UnprocessableEntity=w,a.Updated=P,a.decodeErrorMessageFromRequest=K,a.decodeRequestBody=V,a.errorHandler=Y,a.flushDebugLogs=c,a.formParse=L,a.getCaller=R,a.getScopedParams=Q,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
14
+ `),c({scheme:"green",name:"ARKYN-SUCCESS-RESPONSE-DEBUG",debugs:o})}}class J extends T{constructor(s,e){super();r(this,"body");r(this,"headers");r(this,"status");r(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",this.onDebug("Created",s)}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 D extends T{constructor(s,e){super();r(this,"body");r(this,"headers");r(this,"status");r(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",this.onDebug("Found",s)}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 B extends T{constructor(s){super();r(this,"headers");r(this,"status");r(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",this.onDebug("No content",null)}toResponse(){const s={headers:this.headers,status:this.status,statusText:this.statusText};return new Response(null,s)}}class q extends T{constructor(s,e){super();r(this,"body");r(this,"headers");r(this,"status");r(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",this.onDebug("Success",s)}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 extends T{constructor(s,e){super();r(this,"body");r(this,"headers");r(this,"status");r(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",this.onDebug("Updated",s)}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 H{static mapHeaders(t){return t instanceof Headers?Object.fromEntries(t.entries()):typeof t=="object"?Object.entries(t).reduce((s,[e,o])=>(typeof o=="string"?s[e]=o:Array.isArray(o)?s[e]=o.join(", "):s[e]=JSON.stringify(o),s),{}):{}}static mapQueryParams(t){const s={};return t.forEach((e,o)=>{s[o]=e}),s}static handle(t){return{rawUrl:t.url,status:t.status,method:t.method,token:null,elapsedTime:t.elapsedTime,requestHeaders:this.mapHeaders(t.requestHeaders),requestBody:t.requestBody||null,queryParams:this.mapQueryParams(t.queryParams),responseHeaders:this.mapHeaders(t.responseHeaders),responseBody:t.responseBody||null}}}class E{static setArkynConfig(t){if(this.arkynConfig)return;let e=t.arkynLogBaseApiUrl||"https://logs-arkyn-flow-logs.vw6wo7.easypanel.host";e=e+"/http-traffic-records/:trafficSourceId",this.arkynConfig={arkynTrafficSourceId:t.arkynTrafficSourceId,arkynUserToken:t.arkynUserToken,arkynApiUrl:e}}static getArkynConfig(){return this.arkynConfig}static resetArkynConfig(){this.arkynConfig=void 0}}r(E,"arkynConfig");async function L(n){const t=E.getArkynConfig();if(!t)return;const{arkynUserToken:s,arkynApiUrl:e}=t,{elapsedTime:o,method:u,queryParams:l,requestBody:d,requestHeaders:h,responseBody:f,responseHeaders:i,status:p,token:ns,rawUrl:Z}=n;if(process.env.NODE_ENV!=="development")try{const g=new URL(Z);let A="HTTPS";g.protocol==="http:"&&(A="HTTP");const ss=JSON.stringify({domainUrl:g.protocol+"//"+g.host,pathnameUrl:g.pathname,status:p,protocol:A,method:u,trafficUserId:null,elapsedTime:o,requestHeaders:h,requestBody:d,queryParams:l,responseHeaders:i,responseBody:f});await fetch(e.replace(":trafficSourceId",t.arkynTrafficSourceId),{method:"POST",body:ss,headers:{"Content-Type":"application/json",Authorization:`Bearer ${s}`}})}catch(g){c({debugs:[`Error sending request: ${g}`],name:"ARKYN_LOG_ERROR",scheme:"red"})}}async function m(n,t,s={},e){const o={POST:"Resource created successfully",PUT:"Resource updated successfully",DELETE:"Resource deleted successfully",PATCH:"Resource patched successfully",GET:"Request successful"};try{const u=performance.now(),l={...s,"Content-Type":"application/json"},d=await fetch(t,{method:n,headers:l,body:e?JSON.stringify(e):void 0}),h=performance.now()-u,f=d.status;let i=null;try{i=await d.json()}catch{i=null}const p=H.handle({elapsedTime:h,method:n,queryParams:new URL(t).searchParams,requestHeaders:l,requestBody:e,responseBody:i,responseHeaders:d.headers,status:f,url:t});return L(p),d.ok?{success:!0,status:f,message:(i==null?void 0:i.message)||o[n],response:i,cause:null}:{success:!1,status:f,message:(i==null?void 0:i.message)||d.statusText||"Request failed",response:i,cause:null}}catch(u){return c({debugs:[`Network error or request failed: ${u}`],name:"ARKYN_MAKE_REQUEST_ERROR",scheme:"red"}),{success:!1,status:0,message:"Network error or request failed",response:null,cause:u instanceof Error?u.message:String(u)}}}async function $(n,t={},s){return m("DELETE",n,t,s)}async function F(n,t={}){return m("GET",n,t)}async function M(n,t={},s){return m("PATCH",n,t,s)}async function _(n,t={},s){return m("POST",n,t,s)}async function z(n,t={},s){return m("PUT",n,t,s)}class G{constructor(t){r(this,"baseUrl");r(this,"baseHeaders");r(this,"baseToken");r(this,"enableDebug");this.baseUrl=t.baseUrl,this.baseHeaders=t.baseHeaders||void 0,this.baseToken=t.baseToken||void 0,this.enableDebug=t.enableDebug||!1}onDebug(t,s,e){if(this.enableDebug){const o=[];o.push(`Base URL: ${this.baseUrl}`),o.push(`Endpoint: ${t}`),o.push(`Method: ${s}`),e[0]&&o.push(`Headers: ${JSON.stringify(e[0])}`),e[1]&&o.push(`Body: ${JSON.stringify(e[1])}`),c({debugs:o,name:"ARKYN-API-DEBUG",scheme:"yellow"})}}generateURL(t){return this.baseUrl+t}generateHeaders(t,s){let e={};return this.baseToken&&(e={Authorization:`Bearer ${this.baseToken}`}),this.baseHeaders&&(e={...e,...this.baseHeaders}),t&&(e={...e,...t}),s&&(e={...e,Authorization:`Bearer ${s}`}),e}async get(t,s){const e=this.generateURL(t),o=this.generateHeaders((s==null?void 0:s.headers)||{},s==null?void 0:s.token);return this.onDebug(t,"get",[o]),await F(e,o)}async post(t,s){const e=this.generateURL(t),o=this.generateHeaders((s==null?void 0:s.headers)||{},s==null?void 0:s.token),u=s==null?void 0:s.body;return this.onDebug(t,"post",[o,u]),await _(e,o,u)}async put(t,s){const e=this.generateURL(t),o=this.generateHeaders((s==null?void 0:s.headers)||{},s==null?void 0:s.token),u=s==null?void 0:s.body;return this.onDebug(t,"put",[o,u]),await z(e,o,u)}async patch(t,s){const e=this.generateURL(t),o=this.generateHeaders((s==null?void 0:s.headers)||{},s==null?void 0:s.token),u=s==null?void 0:s.body;return this.onDebug(t,"patch",[o,u]),await M(e,o,u)}async delete(t,s){const e=this.generateURL(t),o=this.generateHeaders((s==null?void 0:s.headers)||{},s==null?void 0:s.token),u=s==null?void 0:s.body;return this.onDebug(t,"delete",[o,u]),await $(e,o,u)}}function K(n,t){var s,e,o;return n!=null&&n.message&&typeof(n==null?void 0:n.message)=="string"?n==null?void 0:n.message:n!=null&&n.error&&typeof(n==null?void 0:n.error)=="string"?n==null?void 0:n.error:(s=n==null?void 0:n.error)!=null&&s.message&&typeof((e=n==null?void 0:n.error)==null?void 0:e.message)=="string"?(o=n==null?void 0:n.error)==null?void 0:o.message:t!=null&&t.statusText&&typeof(t==null?void 0:t.statusText)=="string"?t==null?void 0:t.statusText:"Missing error message"}const V=async n=>{let t;const s=await n.arrayBuffer(),e=new TextDecoder().decode(s);try{t=JSON.parse(e)}catch{try{if(e.includes("=")){const u=new URLSearchParams(e);t=Object.fromEntries(u.entries())}else throw new x("Invalid URLSearchParams format")}catch{throw new x("Failed to extract data from request")}}return t};function Y(n){switch(!0){case n instanceof Response:return n;case n instanceof D:return n.toResponse();case n instanceof J:return n.toResponse();case n instanceof P:return n.toResponse();case n instanceof q:return n.toResponse();case n instanceof B:return n.toResponse()}switch(!0){case n instanceof O:return n.toResponse();case n instanceof x:return n.toResponse();case n instanceof U:return n.toResponse();case n instanceof I:return n.toResponse();case n instanceof k:return n.toResponse();case n instanceof j:return n.toResponse();case n instanceof S:return n.toResponse();case n instanceof C:return n.toResponse();case n instanceof w:return n.toResponse()}return new S("Server error",n).toResponse()}function v([n,t]){const s=t.safeParse(n);if(s.success===!1){const e=Object.fromEntries(s.error.issues.map(o=>[o.path.join("."),o.message]));return{success:s.success,fieldErrors:e,fields:n}}else return{success:s.success,data:s.data}}const Q=(n,t="")=>{const s=new URL(n.url);if(t==="")return s.searchParams;const e=Array.from(s.searchParams.entries()).filter(([o])=>o.startsWith(`${t}:`)).map(([o,u])=>[o.replace(`${t}:`,""),u]);return new URLSearchParams(e)};function W(n){const t="Error validating:",s=n.issues.map(({path:e,message:o})=>`-> ${e.join(".")}: ${o}`);return[t,...s].join(`
15
+ `)}class X{constructor(t){r(this,"functionName");r(this,"callerInfo");this.schema=t;const{callerInfo:s,functionName:e}=R();this.callerInfo=s,this.functionName=e}isValid(t){return this.schema.safeParse(t).success}safeValidate(t){return this.schema.safeParse(t)}validate(t){try{return this.schema.parse(t)}catch(s){throw new S(W(s))}}formValidate(t,s){const e=v([t,this.schema]);if(!e.success){const o=Object.keys(e.fieldErrors)[0];throw new w({fields:e.fields,fieldErrors:e.fieldErrors,data:{scrollTo:o},message:s})}return e.data}}a.ApiService=G,a.ArkynLogService=E,a.BadGateway=O,a.BadRequest=x,a.Conflict=U,a.Created=J,a.DebugService=N,a.Forbidden=I,a.Found=D,a.NoContent=B,a.NotFound=k,a.NotImplemented=j,a.SchemaValidator=X,a.ServerError=S,a.Success=q,a.Unauthorized=C,a.UnprocessableEntity=w,a.Updated=P,a.decodeErrorMessageFromRequest=K,a.decodeRequestBody=V,a.errorHandler=Y,a.flushDebugLogs=c,a.formParse=v,a.getCaller=R,a.getScopedParams=Q,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})});
@@ -35,7 +35,7 @@ class ApiService {
35
35
  if (this.enableDebug) {
36
36
  const debugs = [];
37
37
  debugs.push(`Base URL: ${this.baseUrl}`);
38
- debugs.push(`Endpoint URL: ${endpoint}`);
38
+ debugs.push(`Endpoint: ${endpoint}`);
39
39
  debugs.push(`Method: ${method}`);
40
40
  if (data[0])
41
41
  debugs.push(`Headers: ${JSON.stringify(data[0])}`);
@@ -74,7 +74,7 @@ class ApiService {
74
74
  async get(endpoint, data) {
75
75
  const url = this.generateURL(endpoint);
76
76
  const headers = this.generateHeaders(data?.headers || {}, data?.token);
77
- this.onDebug(url, "get", [headers]);
77
+ this.onDebug(endpoint, "get", [headers]);
78
78
  return await getRequest(url, headers);
79
79
  }
80
80
  /**
@@ -87,7 +87,7 @@ class ApiService {
87
87
  const url = this.generateURL(endpoint);
88
88
  const headers = this.generateHeaders(data?.headers || {}, data?.token);
89
89
  const body = data?.body;
90
- this.onDebug(url, "post", [headers, body]);
90
+ this.onDebug(endpoint, "post", [headers, body]);
91
91
  return await postRequest(url, headers, body);
92
92
  }
93
93
  /**
@@ -113,7 +113,7 @@ class ApiService {
113
113
  const url = this.generateURL(endpoint);
114
114
  const headers = this.generateHeaders(data?.headers || {}, data?.token);
115
115
  const body = data?.body;
116
- this.onDebug(url, "patch", [headers, body]);
116
+ this.onDebug(endpoint, "patch", [headers, body]);
117
117
  return await patchRequest(url, headers, body);
118
118
  }
119
119
  /**
@@ -126,7 +126,7 @@ class ApiService {
126
126
  const url = this.generateURL(endpoint);
127
127
  const headers = this.generateHeaders(data?.headers || {}, data?.token);
128
128
  const body = data?.body;
129
- this.onDebug(url, "delete", [headers, body]);
129
+ this.onDebug(endpoint, "delete", [headers, body]);
130
130
  return await deleteRequest(url, headers, body);
131
131
  }
132
132
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@arkyn/server",
3
- "version": "3.0.1-beta.64",
3
+ "version": "3.0.1-beta.65",
4
4
  "author": "Arkyn | Lucas Gonçalves",
5
5
  "main": "./dist/bundle.js",
6
6
  "module": "./dist/bundle.js",