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