@argent/x-shared 1.76.0 → 1.77.0

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.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("lodash-es"),n=require("./HttpError.cjs"),u=require("./IHttpService.cjs");class d{constructor(i,e="json"){this.requestInit=i,this.responseType=e}async get(i,e,s){const o=h.isFunction(this.requestInit)?await this.requestInit():this.requestInit,c={...o,...e,method:"GET",headers:{...o?.headers,...e?.headers}},t=await fetch(i,c).catch(async r=>{let a;throw r instanceof Response&&(a=await r.json().catch(()=>{})),new n.HttpError(u.HTTP_ERROR_MESSAGE.FAILED_TO_FETCH_URL,0,a)});if(this.responseType==="json"){if(!t.ok)throw new n.HttpError(t.statusText,t.status,await t.json().catch(()=>{}));const r=await t.json();if(s)try{return s.parse(r)}catch(a){throw new n.HttpError(a.message,0)}return r}return t}async post(i,e,s){const o=h.isFunction(this.requestInit)?await this.requestInit():this.requestInit,c={...o,...e,method:"POST",headers:{...o?.headers,...e?.headers}},t=await fetch(i,c).catch(()=>{throw new n.HttpError(u.HTTP_ERROR_MESSAGE.FAILED_TO_POST_URL,0)});if(!t.ok)throw new n.HttpError(t.statusText,t.status,await t.json());if(t.status===204)return{};const r=await t.json();if(s)try{return s.parse(r)}catch(a){throw new n.HttpError(a.message,0)}return r}async put(i,e,s){const o=h.isFunction(this.requestInit)?await this.requestInit():this.requestInit,c={...o,...e,method:"PUT",headers:{...o?.headers,...e?.headers}},t=await fetch(i,c).catch(()=>{throw new n.HttpError(u.HTTP_ERROR_MESSAGE.FAILED_TO_PUT_URL,0)});if(!t.ok)throw new n.HttpError(t.statusText,t.status,await t.json().catch(()=>{}));if(t.status===204)return{};const r=await t.json();if(s)try{return s.parse(r)}catch(a){throw new n.HttpError(a.message,0)}return r}async delete(i,e){const s=h.isFunction(this.requestInit)?await this.requestInit():this.requestInit,o={...s,...e,method:"DELETE",headers:{...s?.headers,...e?.headers}},c=await fetch(i,o).catch(()=>{throw new n.HttpError(u.HTTP_ERROR_MESSAGE.FAILED_TO_DELETE_URL,0)});if(!c.ok)throw new n.HttpError(c.statusText,c.status)}}exports.HTTPService=d;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const h=require("lodash-es"),a=require("./HttpError.cjs"),u=require("./IHttpService.cjs");class w{constructor(o,e="json"){this.requestInit=o,this.responseType=e}async get(o,e,s){const n=h.isFunction(this.requestInit)?await this.requestInit():this.requestInit,r={...n,...e,method:"GET",headers:{...n?.headers,...e?.headers}},t=await fetch(o,r).catch(async i=>{let c;throw i instanceof Response&&(c=await i.json().catch(()=>{})),new a.HttpError(u.HTTP_ERROR_MESSAGE.FAILED_TO_FETCH_URL,0,c)});if(this.responseType==="json"){if(!t.ok)throw new a.HttpError(t.statusText,t.status,await t.json().catch(()=>{}));const i=await t.json();if(s)try{return s.parse(i)}catch(c){throw new a.HttpError(c.message,0)}return i}return t}async post(o,e,s){const n=h.isFunction(this.requestInit)?await this.requestInit():this.requestInit,r={...n,...e,method:"POST",headers:{...n?.headers,...e?.headers}},t=await fetch(o,r).catch(()=>{throw new a.HttpError(u.HTTP_ERROR_MESSAGE.FAILED_TO_POST_URL,0)});if(!t.ok)throw new a.HttpError(t.statusText,t.status,await t.json());if(t.status===204)return{};const i=await t.json();if(s)try{return s.parse(i)}catch(c){throw new a.HttpError(c.message,0)}return i}async put(o,e,s){const n=h.isFunction(this.requestInit)?await this.requestInit():this.requestInit,r={...n,...e,method:"PUT",headers:{...n?.headers,...e?.headers}},t=await fetch(o,r).catch(()=>{throw new a.HttpError(u.HTTP_ERROR_MESSAGE.FAILED_TO_PUT_URL,0)});if(!t.ok)throw new a.HttpError(t.statusText,t.status,await t.json().catch(()=>{}));if(t.status===204)return{};const i=await t.json();if(s)try{return s.parse(i)}catch(c){throw new a.HttpError(c.message,0)}return i}async delete(o,e){const s=h.isFunction(this.requestInit)?await this.requestInit():this.requestInit,n={...s,...e,method:"DELETE",headers:{...s?.headers,...e?.headers}},r=await fetch(o,n).catch(()=>{throw new a.HttpError(u.HTTP_ERROR_MESSAGE.FAILED_TO_DELETE_URL,0)});if(!r.ok)throw new a.HttpError(r.statusText,r.status)}async patch(o,e){const s=h.isFunction(this.requestInit)?await this.requestInit():this.requestInit,n={...s,...e,method:"PATCH",headers:{...s?.headers,...e?.headers}},r=await fetch(o,n).catch(()=>{throw new a.HttpError("Failed to patch url",0)});if(!r.ok)throw new a.HttpError(r.statusText,r.status);return await r.json()}}exports.HTTPService=w;
@@ -11,5 +11,6 @@ export declare class HTTPService implements IHttpService {
11
11
  post<T>(url: string, requestInit?: RequestInit, validationSchema?: ZodType<T>): Promise<T>;
12
12
  put<T>(url: string, requestInit?: RequestInit, validationSchema?: ZodType<T>): Promise<T>;
13
13
  delete(url: string, requestInit?: RequestInit): Promise<void>;
14
+ patch<T>(url: string, requestInit?: RequestInit): Promise<T>;
14
15
  }
15
16
  export {};
@@ -1,95 +1,95 @@
1
1
  import { isFunction as c } from "lodash-es";
2
- import { HttpError as r } from "./HttpError.js";
2
+ import { HttpError as n } from "./HttpError.js";
3
3
  import { HTTP_ERROR_MESSAGE as u } from "./IHttpService.js";
4
- class f {
5
- constructor(i, e = "json") {
6
- this.requestInit = i, this.responseType = e;
4
+ class p {
5
+ constructor(o, e = "json") {
6
+ this.requestInit = o, this.responseType = e;
7
7
  }
8
- async get(i, e, s) {
9
- const o = c(this.requestInit) ? await this.requestInit() : this.requestInit, h = {
10
- ...o,
8
+ async get(o, e, s) {
9
+ const r = c(this.requestInit) ? await this.requestInit() : this.requestInit, a = {
10
+ ...r,
11
11
  ...e,
12
12
  method: "GET",
13
13
  // merge headers
14
14
  headers: {
15
- ...o?.headers,
15
+ ...r?.headers,
16
16
  ...e?.headers
17
17
  }
18
- }, t = await fetch(i, h).catch(async (a) => {
19
- let n;
20
- throw a instanceof Response && (n = await a.json().catch(() => {
21
- })), new r(u.FAILED_TO_FETCH_URL, 0, n);
18
+ }, t = await fetch(o, a).catch(async (i) => {
19
+ let h;
20
+ throw i instanceof Response && (h = await i.json().catch(() => {
21
+ })), new n(u.FAILED_TO_FETCH_URL, 0, h);
22
22
  });
23
23
  if (this.responseType === "json") {
24
24
  if (!t.ok)
25
- throw new r(t.statusText, t.status, await t.json().catch(() => {
25
+ throw new n(t.statusText, t.status, await t.json().catch(() => {
26
26
  }));
27
- const a = await t.json();
27
+ const i = await t.json();
28
28
  if (s)
29
29
  try {
30
- return s.parse(a);
31
- } catch (n) {
32
- throw new r(n.message, 0);
30
+ return s.parse(i);
31
+ } catch (h) {
32
+ throw new n(h.message, 0);
33
33
  }
34
- return a;
34
+ return i;
35
35
  }
36
36
  return t;
37
37
  }
38
- async post(i, e, s) {
39
- const o = c(this.requestInit) ? await this.requestInit() : this.requestInit, h = {
40
- ...o,
38
+ async post(o, e, s) {
39
+ const r = c(this.requestInit) ? await this.requestInit() : this.requestInit, a = {
40
+ ...r,
41
41
  ...e,
42
42
  method: "POST",
43
43
  // merge headers
44
44
  headers: {
45
- ...o?.headers,
45
+ ...r?.headers,
46
46
  ...e?.headers
47
47
  }
48
- }, t = await fetch(i, h).catch(() => {
49
- throw new r(u.FAILED_TO_POST_URL, 0);
48
+ }, t = await fetch(o, a).catch(() => {
49
+ throw new n(u.FAILED_TO_POST_URL, 0);
50
50
  });
51
51
  if (!t.ok)
52
- throw new r(t.statusText, t.status, await t.json());
52
+ throw new n(t.statusText, t.status, await t.json());
53
53
  if (t.status === 204)
54
54
  return {};
55
- const a = await t.json();
55
+ const i = await t.json();
56
56
  if (s)
57
57
  try {
58
- return s.parse(a);
59
- } catch (n) {
60
- throw new r(n.message, 0);
58
+ return s.parse(i);
59
+ } catch (h) {
60
+ throw new n(h.message, 0);
61
61
  }
62
- return a;
62
+ return i;
63
63
  }
64
- async put(i, e, s) {
65
- const o = c(this.requestInit) ? await this.requestInit() : this.requestInit, h = {
66
- ...o,
64
+ async put(o, e, s) {
65
+ const r = c(this.requestInit) ? await this.requestInit() : this.requestInit, a = {
66
+ ...r,
67
67
  ...e,
68
68
  method: "PUT",
69
69
  // merge headers
70
70
  headers: {
71
- ...o?.headers,
71
+ ...r?.headers,
72
72
  ...e?.headers
73
73
  }
74
- }, t = await fetch(i, h).catch(() => {
75
- throw new r(u.FAILED_TO_PUT_URL, 0);
74
+ }, t = await fetch(o, a).catch(() => {
75
+ throw new n(u.FAILED_TO_PUT_URL, 0);
76
76
  });
77
77
  if (!t.ok)
78
- throw new r(t.statusText, t.status, await t.json().catch(() => {
78
+ throw new n(t.statusText, t.status, await t.json().catch(() => {
79
79
  }));
80
80
  if (t.status === 204)
81
81
  return {};
82
- const a = await t.json();
82
+ const i = await t.json();
83
83
  if (s)
84
84
  try {
85
- return s.parse(a);
86
- } catch (n) {
87
- throw new r(n.message, 0);
85
+ return s.parse(i);
86
+ } catch (h) {
87
+ throw new n(h.message, 0);
88
88
  }
89
- return a;
89
+ return i;
90
90
  }
91
- async delete(i, e) {
92
- const s = c(this.requestInit) ? await this.requestInit() : this.requestInit, o = {
91
+ async delete(o, e) {
92
+ const s = c(this.requestInit) ? await this.requestInit() : this.requestInit, r = {
93
93
  ...s,
94
94
  ...e,
95
95
  method: "DELETE",
@@ -98,13 +98,29 @@ class f {
98
98
  ...s?.headers,
99
99
  ...e?.headers
100
100
  }
101
- }, h = await fetch(i, o).catch(() => {
102
- throw new r(u.FAILED_TO_DELETE_URL, 0);
101
+ }, a = await fetch(o, r).catch(() => {
102
+ throw new n(u.FAILED_TO_DELETE_URL, 0);
103
103
  });
104
- if (!h.ok)
105
- throw new r(h.statusText, h.status);
104
+ if (!a.ok)
105
+ throw new n(a.statusText, a.status);
106
+ }
107
+ async patch(o, e) {
108
+ const s = c(this.requestInit) ? await this.requestInit() : this.requestInit, r = {
109
+ ...s,
110
+ ...e,
111
+ method: "PATCH",
112
+ headers: {
113
+ ...s?.headers,
114
+ ...e?.headers
115
+ }
116
+ }, a = await fetch(o, r).catch(() => {
117
+ throw new n("Failed to patch url", 0);
118
+ });
119
+ if (!a.ok)
120
+ throw new n(a.statusText, a.status);
121
+ return await a.json();
106
122
  }
107
123
  }
108
124
  export {
109
- f as HTTPService
125
+ p as HTTPService
110
126
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@argent/x-shared",
3
- "version": "1.76.0",
3
+ "version": "1.77.0",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "git+https://github.com/argentlabs/x-shared.git"