@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"),
|
|
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
|
|
2
|
+
import { HttpError as n } from "./HttpError.js";
|
|
3
3
|
import { HTTP_ERROR_MESSAGE as u } from "./IHttpService.js";
|
|
4
|
-
class
|
|
5
|
-
constructor(
|
|
6
|
-
this.requestInit =
|
|
4
|
+
class p {
|
|
5
|
+
constructor(o, e = "json") {
|
|
6
|
+
this.requestInit = o, this.responseType = e;
|
|
7
7
|
}
|
|
8
|
-
async get(
|
|
9
|
-
const
|
|
10
|
-
...
|
|
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
|
-
...
|
|
15
|
+
...r?.headers,
|
|
16
16
|
...e?.headers
|
|
17
17
|
}
|
|
18
|
-
}, t = await fetch(
|
|
19
|
-
let
|
|
20
|
-
throw
|
|
21
|
-
})), new
|
|
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
|
|
25
|
+
throw new n(t.statusText, t.status, await t.json().catch(() => {
|
|
26
26
|
}));
|
|
27
|
-
const
|
|
27
|
+
const i = await t.json();
|
|
28
28
|
if (s)
|
|
29
29
|
try {
|
|
30
|
-
return s.parse(
|
|
31
|
-
} catch (
|
|
32
|
-
throw new
|
|
30
|
+
return s.parse(i);
|
|
31
|
+
} catch (h) {
|
|
32
|
+
throw new n(h.message, 0);
|
|
33
33
|
}
|
|
34
|
-
return
|
|
34
|
+
return i;
|
|
35
35
|
}
|
|
36
36
|
return t;
|
|
37
37
|
}
|
|
38
|
-
async post(
|
|
39
|
-
const
|
|
40
|
-
...
|
|
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
|
-
...
|
|
45
|
+
...r?.headers,
|
|
46
46
|
...e?.headers
|
|
47
47
|
}
|
|
48
|
-
}, t = await fetch(
|
|
49
|
-
throw new
|
|
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
|
|
52
|
+
throw new n(t.statusText, t.status, await t.json());
|
|
53
53
|
if (t.status === 204)
|
|
54
54
|
return {};
|
|
55
|
-
const
|
|
55
|
+
const i = await t.json();
|
|
56
56
|
if (s)
|
|
57
57
|
try {
|
|
58
|
-
return s.parse(
|
|
59
|
-
} catch (
|
|
60
|
-
throw new
|
|
58
|
+
return s.parse(i);
|
|
59
|
+
} catch (h) {
|
|
60
|
+
throw new n(h.message, 0);
|
|
61
61
|
}
|
|
62
|
-
return
|
|
62
|
+
return i;
|
|
63
63
|
}
|
|
64
|
-
async put(
|
|
65
|
-
const
|
|
66
|
-
...
|
|
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
|
-
...
|
|
71
|
+
...r?.headers,
|
|
72
72
|
...e?.headers
|
|
73
73
|
}
|
|
74
|
-
}, t = await fetch(
|
|
75
|
-
throw new
|
|
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
|
|
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
|
|
82
|
+
const i = await t.json();
|
|
83
83
|
if (s)
|
|
84
84
|
try {
|
|
85
|
-
return s.parse(
|
|
86
|
-
} catch (
|
|
87
|
-
throw new
|
|
85
|
+
return s.parse(i);
|
|
86
|
+
} catch (h) {
|
|
87
|
+
throw new n(h.message, 0);
|
|
88
88
|
}
|
|
89
|
-
return
|
|
89
|
+
return i;
|
|
90
90
|
}
|
|
91
|
-
async delete(
|
|
92
|
-
const s = c(this.requestInit) ? await this.requestInit() : this.requestInit,
|
|
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
|
-
},
|
|
102
|
-
throw new
|
|
101
|
+
}, a = await fetch(o, r).catch(() => {
|
|
102
|
+
throw new n(u.FAILED_TO_DELETE_URL, 0);
|
|
103
103
|
});
|
|
104
|
-
if (!
|
|
105
|
-
throw new
|
|
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
|
-
|
|
125
|
+
p as HTTPService
|
|
110
126
|
};
|