@seayoo-web/request 3.4.2 → 3.4.4
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/README.md +3 -3
- package/dist/index.js +71 -71
- package/dist/node.js +21 -21
- package/dist/{request.fetch-Cbt_az4v.js → request.fetch-6HUu6_l5.js} +7 -7
- package/dist/{retry-FiwuCZIu.js → retry-BjOxSyvC.js} +106 -122
- package/dist/wx.js +2 -2
- package/package.json +2 -2
- package/types/inc/utils.d.ts +0 -5
package/README.md
CHANGED
|
@@ -141,7 +141,7 @@ setConfig({ timeout: 10000 });
|
|
|
141
141
|
|
|
142
142
|
类型:string
|
|
143
143
|
|
|
144
|
-
说明:默认的类型守卫错误提示,仅支持全局配置,默认
|
|
144
|
+
说明:默认的类型守卫错误提示,仅支持全局配置,默认 “响应数据未能正确识别”,单个 api 可通过传入完整的类型守卫({ guard, message })来定制提示消息。
|
|
145
145
|
|
|
146
146
|
### message
|
|
147
147
|
|
|
@@ -180,11 +180,11 @@ defaultMessage: string
|
|
|
180
180
|
|
|
181
181
|
**statusField**: string
|
|
182
182
|
|
|
183
|
-
|
|
183
|
+
解析状态的字段,仅在 resolve 为 json 时有效,有值的话会替换 response 的 code
|
|
184
184
|
|
|
185
185
|
**messageField**: string | string[]
|
|
186
186
|
|
|
187
|
-
|
|
187
|
+
解析错误消息的字段,仅在 resolve 为 json 时有效,默认值 "message"
|
|
188
188
|
|
|
189
189
|
- **OKRule**: { resolve, converter?, statusField?, statusOKValue?, dataField?, messageField?, ignoreMessage? }
|
|
190
190
|
|
package/dist/index.js
CHANGED
|
@@ -1,78 +1,78 @@
|
|
|
1
|
-
import { h as m, r as q, R as T, c as x,
|
|
2
|
-
import { g as
|
|
3
|
-
import { f as
|
|
4
|
-
const
|
|
5
|
-
return m(await q(y,
|
|
1
|
+
import { h as m, r as q, R as T, c as x, Q as R, a as w, _ as H, l as C, N as E, Z as L } from "./retry-BjOxSyvC.js";
|
|
2
|
+
import { g as z } from "./retry-BjOxSyvC.js";
|
|
3
|
+
import { f as j } from "./request.fetch-6HUu6_l5.js";
|
|
4
|
+
const k = async function(t, s, e) {
|
|
5
|
+
return m(await q(y, t, s, e), t, s, e);
|
|
6
6
|
};
|
|
7
|
-
async function
|
|
8
|
-
const r =
|
|
9
|
-
if (
|
|
10
|
-
const i = new T(n), u = await y(
|
|
11
|
-
...
|
|
7
|
+
async function S(t, s, e, n) {
|
|
8
|
+
const r = e?.body, l = e?.method === "PUT" ? "PUT" : "POST";
|
|
9
|
+
if (s instanceof Blob) {
|
|
10
|
+
const i = new T(n), u = await y(t, i, {
|
|
11
|
+
...e,
|
|
12
12
|
method: l,
|
|
13
|
-
body:
|
|
13
|
+
body: s
|
|
14
14
|
});
|
|
15
|
-
return m(u,
|
|
15
|
+
return m(u, t, i, e);
|
|
16
16
|
}
|
|
17
|
-
const a = new FormData(), d = { ...
|
|
17
|
+
const a = new FormData(), d = { ...s };
|
|
18
18
|
r instanceof Object && Object.entries(r).forEach(([i, u]) => {
|
|
19
|
-
u instanceof Blob ? d[i] = u : Array.isArray(u) ? u.forEach((
|
|
20
|
-
a.append(`${i}[${g}]`, String(
|
|
19
|
+
u instanceof Blob ? d[i] = u : Array.isArray(u) ? u.forEach((o, g) => {
|
|
20
|
+
a.append(`${i}[${g}]`, String(o));
|
|
21
21
|
}) : a.append(i, String(u));
|
|
22
22
|
});
|
|
23
23
|
for (const i in d)
|
|
24
24
|
a.append(i, d[i]);
|
|
25
|
-
const b = new T(n),
|
|
26
|
-
...
|
|
25
|
+
const b = new T(n), h = await y(t, b, {
|
|
26
|
+
...e,
|
|
27
27
|
method: l,
|
|
28
28
|
body: a
|
|
29
29
|
});
|
|
30
|
-
return m(
|
|
30
|
+
return m(h, t, b, e);
|
|
31
31
|
}
|
|
32
|
-
const y = async function(
|
|
33
|
-
const n = await x(
|
|
32
|
+
const y = async function(t, s, e) {
|
|
33
|
+
const n = await x(t, s, e), r = n.method, l = e?.onUploadProgress, a = R(n.url, n.params);
|
|
34
34
|
return await new Promise((d) => {
|
|
35
|
-
let b = null,
|
|
35
|
+
let b = null, h = !1;
|
|
36
36
|
const i = function() {
|
|
37
|
-
|
|
37
|
+
h || (o.abort(), h = !0);
|
|
38
38
|
};
|
|
39
39
|
function u() {
|
|
40
40
|
b !== null && clearTimeout(b), n.abort && n.abort.removeEventListener("abort", i);
|
|
41
41
|
}
|
|
42
|
-
const
|
|
42
|
+
const o = new XMLHttpRequest();
|
|
43
43
|
let g = !1;
|
|
44
|
-
if (
|
|
44
|
+
if (o.open(r, a, !0), l) {
|
|
45
45
|
let f = 1;
|
|
46
|
-
|
|
47
|
-
f =
|
|
48
|
-
}),
|
|
46
|
+
o.upload.addEventListener("progress", (p) => {
|
|
47
|
+
f = p.total, l({ total: p.total, loaded: p.loaded });
|
|
48
|
+
}), o.addEventListener("load", () => {
|
|
49
49
|
l({ loaded: f, total: f });
|
|
50
50
|
});
|
|
51
51
|
}
|
|
52
|
-
|
|
53
|
-
const f =
|
|
52
|
+
o.addEventListener("load", () => {
|
|
53
|
+
const f = o.status, p = o.responseType;
|
|
54
54
|
u(), d({
|
|
55
55
|
url: a,
|
|
56
56
|
method: r,
|
|
57
57
|
status: f,
|
|
58
|
-
statusText:
|
|
59
|
-
headers:
|
|
60
|
-
body: r === "HEAD" || f === 204 ? "" :
|
|
58
|
+
statusText: o.statusText,
|
|
59
|
+
headers: U(o),
|
|
60
|
+
body: r === "HEAD" || f === 204 ? "" : p === "" || p === "text" || p === "json" || p === "document" ? o.responseText : ""
|
|
61
61
|
});
|
|
62
|
-
}),
|
|
62
|
+
}), o.addEventListener(
|
|
63
63
|
"error",
|
|
64
64
|
(f) => {
|
|
65
65
|
u(), d({
|
|
66
66
|
url: a,
|
|
67
67
|
method: r,
|
|
68
|
-
status:
|
|
69
|
-
statusText:
|
|
68
|
+
status: o.status || -1,
|
|
69
|
+
statusText: o.statusText || w.NetworkError,
|
|
70
70
|
body: "",
|
|
71
71
|
rawError: f
|
|
72
72
|
});
|
|
73
73
|
},
|
|
74
74
|
!0
|
|
75
|
-
),
|
|
75
|
+
), o.addEventListener("abort", () => {
|
|
76
76
|
u(), d({
|
|
77
77
|
url: a,
|
|
78
78
|
method: r,
|
|
@@ -81,53 +81,53 @@ const y = async function(e, o, t) {
|
|
|
81
81
|
statusText: g ? w.Timeout : w.Aborted,
|
|
82
82
|
body: ""
|
|
83
83
|
});
|
|
84
|
-
}), Object.entries(n.headers).forEach(([f,
|
|
85
|
-
|
|
86
|
-
}), n.credentials === "include" && (
|
|
84
|
+
}), Object.entries(n.headers).forEach(([f, p]) => {
|
|
85
|
+
o.setRequestHeader(f, p);
|
|
86
|
+
}), n.credentials === "include" && (o.withCredentials = !0), o.send(n.body || void 0), n.abort && n.abort.addEventListener("abort", i), n.timeout > 0 && (b = setTimeout(function() {
|
|
87
87
|
g = !0, i();
|
|
88
88
|
}, n.timeout));
|
|
89
89
|
});
|
|
90
90
|
};
|
|
91
|
-
function
|
|
92
|
-
const
|
|
93
|
-
if (!
|
|
94
|
-
return
|
|
95
|
-
const
|
|
96
|
-
return
|
|
91
|
+
function U(t) {
|
|
92
|
+
const s = {};
|
|
93
|
+
if (!t)
|
|
94
|
+
return s;
|
|
95
|
+
const e = t.getAllResponseHeaders();
|
|
96
|
+
return e && e !== "null" && e.replace(/\r/g, "").split(`
|
|
97
97
|
`).forEach((n) => {
|
|
98
98
|
const r = n.trim();
|
|
99
99
|
if (!r)
|
|
100
100
|
return;
|
|
101
101
|
const l = r.split(":"), a = l[0].trim();
|
|
102
|
-
a && (
|
|
103
|
-
}),
|
|
102
|
+
a && (s[a] = (l[1] || "").trim());
|
|
103
|
+
}), s;
|
|
104
104
|
}
|
|
105
|
-
async function
|
|
105
|
+
async function P(t, s, e = {}) {
|
|
106
106
|
const n = window;
|
|
107
|
-
"callback" in
|
|
108
|
-
const r =
|
|
109
|
-
if (!
|
|
107
|
+
"callback" in e || (e.callback = "jsonxData" + Math.random().toString(16).slice(2));
|
|
108
|
+
const r = e.callback + "";
|
|
109
|
+
if (!t)
|
|
110
110
|
return null;
|
|
111
|
-
const l = R(
|
|
111
|
+
const l = R(t, e, !0);
|
|
112
112
|
return new Promise((a) => {
|
|
113
113
|
n[r] = function(d) {
|
|
114
|
-
if (r in window && delete n[r],
|
|
114
|
+
if (r in window && delete n[r], s(d))
|
|
115
115
|
return d;
|
|
116
|
-
console.warn("response type check failed",
|
|
116
|
+
console.warn("response type check failed", t, d), a(null);
|
|
117
117
|
}, H(l).catch(function() {
|
|
118
118
|
a(null), delete n[r];
|
|
119
119
|
});
|
|
120
120
|
});
|
|
121
121
|
}
|
|
122
|
-
async function
|
|
122
|
+
async function A(t, s, e = {}) {
|
|
123
123
|
const n = window;
|
|
124
|
-
return "var" in
|
|
125
|
-
const r = n[
|
|
126
|
-
return
|
|
124
|
+
return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), t ? await H(R(t, e, !0)).then(() => {
|
|
125
|
+
const r = n[e.var + ""];
|
|
126
|
+
return s(r) ? r : (console.warn("response type check failed", t, r), null);
|
|
127
127
|
}).catch(() => null) : null;
|
|
128
128
|
}
|
|
129
|
-
const
|
|
130
|
-
return await
|
|
129
|
+
const M = async function(t, s, e) {
|
|
130
|
+
return await S(t, s, e, {
|
|
131
131
|
baseURL: c.getConfig("baseURL"),
|
|
132
132
|
logHandler: c.getConfig("logHandler"),
|
|
133
133
|
errorHandler: c.getConfig("errorHandler"),
|
|
@@ -136,25 +136,25 @@ const A = async function(e, o, t) {
|
|
|
136
136
|
responseHandler: c.getConfig("responseHandler")
|
|
137
137
|
});
|
|
138
138
|
};
|
|
139
|
-
function v(
|
|
139
|
+
function v(t) {
|
|
140
140
|
if (!C())
|
|
141
141
|
throw new Error("Default Module Only Support In Browser");
|
|
142
|
-
return L() ? new E(
|
|
142
|
+
return L() ? new E(j, t) : new E(k, t);
|
|
143
143
|
}
|
|
144
|
-
const c = v(),
|
|
144
|
+
const c = v(), N = c.setConfig, B = c.request, X = c.head, F = c.get, G = c.post, I = c.del, Q = c.put, Z = c.patch;
|
|
145
145
|
export {
|
|
146
146
|
v as NetRequest,
|
|
147
147
|
w as RequestInternalError,
|
|
148
148
|
I as del,
|
|
149
149
|
F as get,
|
|
150
|
-
|
|
150
|
+
z as getResponseRulesDescription,
|
|
151
151
|
X as head,
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
152
|
+
P as jsonp,
|
|
153
|
+
A as jsonx,
|
|
154
|
+
Z as patch,
|
|
155
155
|
G as post,
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
156
|
+
Q as put,
|
|
157
|
+
B as request,
|
|
158
|
+
N as setGlobalConfig,
|
|
159
|
+
M as upload
|
|
160
160
|
};
|
package/dist/node.js
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
import { h as y, r as q, c as w,
|
|
2
|
-
import { g as z } from "./retry-
|
|
3
|
-
import { f as C } from "./request.fetch-
|
|
4
|
-
import
|
|
5
|
-
import
|
|
6
|
-
const
|
|
7
|
-
return y(await q(
|
|
8
|
-
},
|
|
1
|
+
import { h as y, r as q, c as w, b as g, a as d, f as E, N as f, Z as x } from "./retry-BjOxSyvC.js";
|
|
2
|
+
import { g as z } from "./retry-BjOxSyvC.js";
|
|
3
|
+
import { f as C } from "./request.fetch-6HUu6_l5.js";
|
|
4
|
+
import N from "node:http";
|
|
5
|
+
import T from "node:https";
|
|
6
|
+
const j = async function(o, a, u) {
|
|
7
|
+
return y(await q(L, o, a, u), o, a, u);
|
|
8
|
+
}, L = async function(o, a, u) {
|
|
9
9
|
const t = await w(o, a, u);
|
|
10
10
|
if (!g(t.url))
|
|
11
11
|
return {
|
|
12
12
|
url: t.url,
|
|
13
13
|
method: t.method,
|
|
14
14
|
status: -1,
|
|
15
|
-
statusText:
|
|
15
|
+
statusText: d.URLFormatError,
|
|
16
16
|
headers: {},
|
|
17
17
|
body: ""
|
|
18
18
|
};
|
|
19
|
-
const R = /^https:\/\//i.test(t.url) ?
|
|
19
|
+
const R = /^https:\/\//i.test(t.url) ? T : N, n = new URL(t.url), i = t.params;
|
|
20
20
|
i instanceof Object && Object.keys(i).forEach((r) => n.searchParams.set(r, i[r]));
|
|
21
21
|
const l = t.method === "HEAD";
|
|
22
22
|
return new Promise((r) => {
|
|
23
|
-
const
|
|
23
|
+
const c = R.request(
|
|
24
24
|
n,
|
|
25
25
|
{
|
|
26
26
|
headers: t.headers,
|
|
@@ -37,41 +37,41 @@ const L = async function(o, a, u) {
|
|
|
37
37
|
url: n.toString(),
|
|
38
38
|
method: t.method,
|
|
39
39
|
status: e.statusCode || -1,
|
|
40
|
-
statusText: e.statusMessage ||
|
|
40
|
+
statusText: e.statusMessage || d.Unknown,
|
|
41
41
|
headers: m,
|
|
42
42
|
body: l || e.statusCode === 204 ? "" : Buffer.concat(p).toString("utf-8")
|
|
43
43
|
});
|
|
44
44
|
});
|
|
45
45
|
}
|
|
46
46
|
);
|
|
47
|
-
|
|
47
|
+
c.on("error", (e) => {
|
|
48
48
|
r({
|
|
49
49
|
url: n.toString(),
|
|
50
50
|
method: t.method,
|
|
51
51
|
status: -1,
|
|
52
|
-
statusText:
|
|
52
|
+
statusText: d.NetworkError,
|
|
53
53
|
body: "",
|
|
54
54
|
rawError: e
|
|
55
55
|
});
|
|
56
|
-
}),
|
|
56
|
+
}), c.on("timeout", () => {
|
|
57
57
|
r({
|
|
58
58
|
url: n.toString(),
|
|
59
59
|
method: t.method,
|
|
60
60
|
status: -1,
|
|
61
|
-
statusText:
|
|
61
|
+
statusText: d.Timeout,
|
|
62
62
|
body: ""
|
|
63
63
|
});
|
|
64
|
-
}), t.body &&
|
|
64
|
+
}), t.body && c.write(t.body), c.end();
|
|
65
65
|
});
|
|
66
66
|
};
|
|
67
67
|
function O(o) {
|
|
68
|
-
return x() ? new f(C, o) : new f(
|
|
68
|
+
return x() ? new f(C, o) : new f(j, o);
|
|
69
69
|
}
|
|
70
|
-
const s = O(), k = s.setConfig, B = s.head, D = s.get, P = s.post,
|
|
70
|
+
const s = O(), k = s.setConfig, B = s.head, D = s.get, P = s.post, Z = s.del, F = s.put, G = s.patch;
|
|
71
71
|
export {
|
|
72
72
|
O as NetRequest,
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
d as RequestInternalError,
|
|
74
|
+
Z as del,
|
|
75
75
|
D as get,
|
|
76
76
|
z as getResponseRulesDescription,
|
|
77
77
|
B as head,
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { h as w, r as y, c as E,
|
|
2
|
-
const x = async function(
|
|
3
|
-
return w(await y(g,
|
|
4
|
-
}, g = async function(
|
|
5
|
-
const t = await E(
|
|
1
|
+
import { h as w, r as y, c as E, u as T, f as R, a as c } from "./retry-BjOxSyvC.js";
|
|
2
|
+
const x = async function(a, s, n) {
|
|
3
|
+
return w(await y(g, a, s, n), a, s, n);
|
|
4
|
+
}, g = async function(a, s, n) {
|
|
5
|
+
const t = await E(a, s, n), i = new URL(t.url), u = t.params;
|
|
6
6
|
u instanceof Object && Object.keys(u).forEach((e) => i.searchParams.set(e, u[e]));
|
|
7
7
|
const r = T() ? new AbortController() : null;
|
|
8
8
|
function d() {
|
|
@@ -12,7 +12,7 @@ const x = async function(s, a, n) {
|
|
|
12
12
|
let l = !1;
|
|
13
13
|
const b = t.timeout > 0 ? setTimeout(function() {
|
|
14
14
|
l = !0, d();
|
|
15
|
-
}, t.timeout) : null,
|
|
15
|
+
}, t.timeout) : null, f = t.method === "HEAD";
|
|
16
16
|
return await fetch(i, {
|
|
17
17
|
method: t.method,
|
|
18
18
|
headers: Object.keys(t.headers).length > 0 ? new Headers(t.headers) : void 0,
|
|
@@ -27,7 +27,7 @@ const x = async function(s, a, n) {
|
|
|
27
27
|
status: e.status,
|
|
28
28
|
statusText: e.statusText,
|
|
29
29
|
headers: R([...e.headers.entries()])
|
|
30
|
-
}, h =
|
|
30
|
+
}, h = f || e.status === 204 ? "" : await e.text().catch((m) => m);
|
|
31
31
|
return h instanceof Error ? { ...o, body: "", statusText: c.Unknown, rawError: h } : { ...o, body: h };
|
|
32
32
|
}).catch((e) => {
|
|
33
33
|
const o = r?.signal.aborted;
|
|
@@ -1,41 +1,41 @@
|
|
|
1
1
|
const h = typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")();
|
|
2
|
-
function
|
|
2
|
+
function S() {
|
|
3
3
|
return h;
|
|
4
4
|
}
|
|
5
|
-
function
|
|
5
|
+
function p(t) {
|
|
6
6
|
return typeof t == "function";
|
|
7
7
|
}
|
|
8
8
|
function C(t) {
|
|
9
9
|
return typeof t == "object" && t !== null;
|
|
10
10
|
}
|
|
11
|
-
function
|
|
12
|
-
return "wx" in h && C(h.wx) && "getSystemInfo" in h.wx &&
|
|
11
|
+
function D() {
|
|
12
|
+
return "wx" in h && C(h.wx) && "getSystemInfo" in h.wx && p(h.wx.getSystemInfo);
|
|
13
13
|
}
|
|
14
14
|
function de() {
|
|
15
|
-
return "fetch" in h &&
|
|
15
|
+
return "fetch" in h && p(h.fetch);
|
|
16
16
|
}
|
|
17
17
|
function ge() {
|
|
18
|
-
return "window" in h && C(h.window) && !
|
|
18
|
+
return "window" in h && C(h.window) && !D();
|
|
19
19
|
}
|
|
20
20
|
function M() {
|
|
21
|
-
return "Blob" in h &&
|
|
21
|
+
return "Blob" in h && p(h.Blob);
|
|
22
22
|
}
|
|
23
23
|
function v() {
|
|
24
|
-
return "FormData" in h &&
|
|
25
|
-
}
|
|
26
|
-
function pe() {
|
|
27
|
-
return "TextDecoder" in h && m(h.TextDecoder);
|
|
24
|
+
return "FormData" in h && p(h.FormData);
|
|
28
25
|
}
|
|
29
26
|
function me() {
|
|
30
|
-
return "
|
|
27
|
+
return "TextDecoder" in h && p(h.TextDecoder);
|
|
31
28
|
}
|
|
32
|
-
function
|
|
33
|
-
return "
|
|
29
|
+
function pe() {
|
|
30
|
+
return "AbortController" in h && p(h.AbortController);
|
|
34
31
|
}
|
|
35
|
-
function
|
|
36
|
-
return
|
|
32
|
+
function P() {
|
|
33
|
+
return "URLSearchParams" in h && p(h.URLSearchParams);
|
|
37
34
|
}
|
|
38
35
|
function q(t) {
|
|
36
|
+
return new Promise((e) => setTimeout(e, Math.min(Number.MAX_SAFE_INTEGER, Math.max(0, t))));
|
|
37
|
+
}
|
|
38
|
+
function I(t) {
|
|
39
39
|
const e = t || "CustomError";
|
|
40
40
|
return class extends Error {
|
|
41
41
|
constructor(s) {
|
|
@@ -47,20 +47,28 @@ function q(t) {
|
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
49
|
}
|
|
50
|
-
const
|
|
50
|
+
const B = /^(?:https?:)?\/\/.+$/i, H = /^https?:\/\/.+$/i, N = /^\{[\d\D]*\}$/, G = /^\[[\d\D]*\]$/;
|
|
51
51
|
function j(t, e = !1) {
|
|
52
|
-
return e ?
|
|
52
|
+
return e ? B.test(t) : H.test(t);
|
|
53
53
|
}
|
|
54
|
-
function
|
|
54
|
+
function _(t) {
|
|
55
55
|
return t == null;
|
|
56
56
|
}
|
|
57
|
-
function
|
|
57
|
+
function U(t, ...e) {
|
|
58
58
|
if (!t || typeof t != "object") return !1;
|
|
59
59
|
const s = Object.getPrototypeOf(t);
|
|
60
60
|
return s !== Object.prototype && s !== null ? !1 : e.every((r) => r in t);
|
|
61
61
|
}
|
|
62
|
-
function
|
|
63
|
-
return
|
|
62
|
+
function A(t) {
|
|
63
|
+
return N.test(t) || G.test(t);
|
|
64
|
+
}
|
|
65
|
+
function R(t, e) {
|
|
66
|
+
try {
|
|
67
|
+
const s = JSON.parse(t);
|
|
68
|
+
return e ? e(s) ? s : null : s;
|
|
69
|
+
} catch {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
64
72
|
}
|
|
65
73
|
async function ye(t) {
|
|
66
74
|
return await new Promise(function(e) {
|
|
@@ -82,10 +90,10 @@ function K(t, e = "数据未能正确识别") {
|
|
|
82
90
|
};
|
|
83
91
|
}
|
|
84
92
|
function J(t, e = "") {
|
|
85
|
-
return !e || j(t, !0) ?
|
|
93
|
+
return !e || j(t, !0) ? T(t) : (T(e) + "/" + t).replace(/\/{2,}/g, "/").replace(/:\//, "://");
|
|
86
94
|
}
|
|
87
|
-
function
|
|
88
|
-
const e =
|
|
95
|
+
function T(t) {
|
|
96
|
+
const e = S();
|
|
89
97
|
return j(t, !0) ? t.startsWith("http") ? t : ("location" in e ? location.protocol : "https:") + t : ("location" in e ? location.origin : "http://127.0.0.1") + "/" + t.replace(/^\/+/, "");
|
|
90
98
|
}
|
|
91
99
|
function V(t, e) {
|
|
@@ -115,11 +123,11 @@ function be(t, e, s = !1) {
|
|
|
115
123
|
const o = n[0].indexOf("?") + 1 ? "&" : "?";
|
|
116
124
|
return (n[0] + o + r + (n.length > 1 ? "#" + n[1] : "")).replace(/\?&/, "?");
|
|
117
125
|
}
|
|
118
|
-
function
|
|
126
|
+
function Q(t) {
|
|
119
127
|
const e = t.match(/(?:\?|&)([^=]+)(?:&|$)/g);
|
|
120
128
|
return e ? e.join("").replace(/(?:\?|^&+|&+$)/g, "").replace(/&{2}/g, "&").split("&").sort() : [];
|
|
121
129
|
}
|
|
122
|
-
class
|
|
130
|
+
class Z {
|
|
123
131
|
ttl;
|
|
124
132
|
cache;
|
|
125
133
|
timer = null;
|
|
@@ -147,7 +155,7 @@ class z {
|
|
|
147
155
|
} catch (l) {
|
|
148
156
|
return console.warn("[RequestCache] cacheResolve error", s, r, l), "";
|
|
149
157
|
}
|
|
150
|
-
const i = Object.assign(V(a, !0), r), g =
|
|
158
|
+
const i = Object.assign(V(a, !0), r), g = Q(a), u = Object.keys(i).sort().map((l) => `${l}#${i[l]}`);
|
|
151
159
|
return `${c}_${o}_${u.join(",")}_${g.join(",")}`;
|
|
152
160
|
}
|
|
153
161
|
updateTTL(e) {
|
|
@@ -170,7 +178,7 @@ class z {
|
|
|
170
178
|
this.cache[e] && delete this.cache[e];
|
|
171
179
|
}
|
|
172
180
|
}
|
|
173
|
-
class
|
|
181
|
+
class z {
|
|
174
182
|
// 保存的配置需要部分字段强制设置默认值
|
|
175
183
|
config = {
|
|
176
184
|
baseURL: "/",
|
|
@@ -211,7 +219,7 @@ class Y {
|
|
|
211
219
|
this.config.messageHandler && s && this.config.messageHandler(e, s, r, n);
|
|
212
220
|
}
|
|
213
221
|
}
|
|
214
|
-
const
|
|
222
|
+
const L = {
|
|
215
223
|
/** 响应数据校验失败 */
|
|
216
224
|
UnexpectResponse: "UnexpectResponse",
|
|
217
225
|
/** 请求被取消 */
|
|
@@ -227,10 +235,10 @@ const S = {
|
|
|
227
235
|
/** url 格式错误 */
|
|
228
236
|
URLFormatError: "URLFormatError"
|
|
229
237
|
};
|
|
230
|
-
function
|
|
231
|
-
if (e.ok && !
|
|
238
|
+
function X(t, e, s, r) {
|
|
239
|
+
if (e.ok && !_(e.data) && r) {
|
|
232
240
|
const n = K(r, s.get("defaultTypeGuardMessage"));
|
|
233
|
-
return n.guard(e.data) || (e.code =
|
|
241
|
+
return n.guard(e.data) || (e.code = L.UnexpectResponse, s.showMessage(!0, `${t} ${n.message}`, e.code, e.status), console.error(e.code, t, e.data), e.data = null, e.message = n.message), e;
|
|
234
242
|
}
|
|
235
243
|
return e;
|
|
236
244
|
}
|
|
@@ -239,7 +247,7 @@ class we {
|
|
|
239
247
|
config;
|
|
240
248
|
cache;
|
|
241
249
|
constructor(e, s) {
|
|
242
|
-
this.config = new
|
|
250
|
+
this.config = new z(s), this.agent = e, this.cache = new Z(this.config.get("cacheTTL")), this.setConfig = this.setConfig.bind(this), this.getConfig = this.getConfig.bind(this), this.request = this.request.bind(this), this.get = this.get.bind(this), this.post = this.post.bind(this), this.del = this.del.bind(this), this.patch = this.patch.bind(this), this.put = this.put.bind(this), this.head = this.head.bind(this);
|
|
243
251
|
}
|
|
244
252
|
/**
|
|
245
253
|
* 执行网络请求
|
|
@@ -260,7 +268,7 @@ class we {
|
|
|
260
268
|
return console.error("RequestError", n), {
|
|
261
269
|
ok: !1,
|
|
262
270
|
status: -9,
|
|
263
|
-
code:
|
|
271
|
+
code: L.Unknown,
|
|
264
272
|
message: n instanceof Error ? `${n.message}
|
|
265
273
|
${n.stack || ""}` : String(n),
|
|
266
274
|
headers: {},
|
|
@@ -272,7 +280,7 @@ ${n.stack || ""}` : String(n),
|
|
|
272
280
|
* 检查响应的数据类型
|
|
273
281
|
*/
|
|
274
282
|
async guard(e, s, r) {
|
|
275
|
-
return
|
|
283
|
+
return X(e, s, this.config, r);
|
|
276
284
|
}
|
|
277
285
|
/**
|
|
278
286
|
* 修改默认请求配置: baseURL / timeout / credentials / errorHandler / messageHandler / responseHandler / logHandler / responseRule
|
|
@@ -325,14 +333,14 @@ async function Re(t, e, s) {
|
|
|
325
333
|
},
|
|
326
334
|
r.headers
|
|
327
335
|
), i = r.params || {}, g = {};
|
|
328
|
-
Object.keys(i).forEach((
|
|
329
|
-
i[
|
|
336
|
+
Object.keys(i).forEach((m) => {
|
|
337
|
+
i[m] !== void 0 && (g[m] = Y(i[m]));
|
|
330
338
|
});
|
|
331
|
-
const u = e.getFullUrl(t), l = ee(r.body), f = r.timeout || e.get("timeout"), d = await async function() {
|
|
332
|
-
const
|
|
333
|
-
if (
|
|
334
|
-
return await
|
|
335
|
-
}(), y = typeof d == "string" && d ? d : u;
|
|
339
|
+
const u = e.getFullUrl(t), l = ee(r.body), f = r.timeout || e.get("timeout"), d = await (async function() {
|
|
340
|
+
const m = e.get("requestTransformer");
|
|
341
|
+
if (m)
|
|
342
|
+
return await m({ headers: c, params: g, method: o, url: u, body: l });
|
|
343
|
+
})(), y = typeof d == "string" && d ? d : u;
|
|
336
344
|
return e.get("logHandler")?.({ type: "ready", url: y, method: o, headers: c, timeout: f, body: l }), {
|
|
337
345
|
url: y,
|
|
338
346
|
method: o,
|
|
@@ -344,49 +352,19 @@ async function Re(t, e, s) {
|
|
|
344
352
|
credentials: r.credentials || e.get("credentials")
|
|
345
353
|
};
|
|
346
354
|
}
|
|
347
|
-
function
|
|
355
|
+
function Y(t) {
|
|
348
356
|
return t == null ? "" : typeof t == "string" ? t : typeof t == "number" || typeof t == "boolean" ? String(t) : Array.isArray(t) ? t.filter(
|
|
349
357
|
(s) => s != null && typeof s != "object" && typeof s != "function"
|
|
350
358
|
).join(",") : (console.warn("参数类型不支持,已被过滤", typeof t), "");
|
|
351
359
|
}
|
|
352
360
|
function ee(t) {
|
|
353
361
|
if (t)
|
|
354
|
-
return typeof t == "string" ||
|
|
355
|
-
}
|
|
356
|
-
function te(t) {
|
|
357
|
-
const e = {};
|
|
358
|
-
for (const [s, r] of t)
|
|
359
|
-
if (s && typeof s == "string") {
|
|
360
|
-
if (s === "__proto__" || s === "constructor" || s === "prototype") {
|
|
361
|
-
console.warn("检测到危险的对象属性,已被过滤", s);
|
|
362
|
-
continue;
|
|
363
|
-
}
|
|
364
|
-
if (s.includes("\0") || s.includes("\r") || s.includes(`
|
|
365
|
-
`)) {
|
|
366
|
-
console.warn("参数名中包含可疑字符,已被过滤", s);
|
|
367
|
-
continue;
|
|
368
|
-
}
|
|
369
|
-
e[s] = r || "";
|
|
370
|
-
}
|
|
371
|
-
return e;
|
|
372
|
-
}
|
|
373
|
-
function R(t) {
|
|
374
|
-
try {
|
|
375
|
-
const e = JSON.parse(t);
|
|
376
|
-
if (e && typeof e == "object" && !Array.isArray(e)) {
|
|
377
|
-
for (const s in e)
|
|
378
|
-
if (s === "__proto__" || s === "constructor" || s === "prototype" || Object.prototype.hasOwnProperty.call(e, "__proto__") || Object.prototype.hasOwnProperty.call(e, "constructor"))
|
|
379
|
-
return console.warn("JSON 数据包含危险属性,已拒绝解析", s), null;
|
|
380
|
-
}
|
|
381
|
-
return e;
|
|
382
|
-
} catch (e) {
|
|
383
|
-
return console.warn("JSON 解析失败", e), null;
|
|
384
|
-
}
|
|
362
|
+
return typeof t == "string" || P() && t instanceof URLSearchParams || t instanceof ArrayBuffer || M() && t instanceof Blob || v() && t instanceof FormData ? t : JSON.stringify(t);
|
|
385
363
|
}
|
|
386
|
-
const
|
|
387
|
-
function
|
|
364
|
+
const x = "data", b = "message";
|
|
365
|
+
function te(t, e, s, r, n) {
|
|
388
366
|
const o = n || r;
|
|
389
|
-
return
|
|
367
|
+
return $(t) ? ce(o.ok || r.ok, t, e, s) : se(o.failed || r.failed, e, s);
|
|
390
368
|
}
|
|
391
369
|
const Ee = function(t) {
|
|
392
370
|
const e = [], s = t.failed || { resolve: "json" };
|
|
@@ -405,7 +383,7 @@ const Ee = function(t) {
|
|
|
405
383
|
break;
|
|
406
384
|
case "json":
|
|
407
385
|
e.push(
|
|
408
|
-
" 将响应解析为 json,读取 " + (r.dataField ||
|
|
386
|
+
" 将响应解析为 json,读取 " + (r.dataField || x) + " 作为响应数据,读取 " + (r.messageField || b) + " 作为提示消息"
|
|
409
387
|
), r.statusField && e.push(
|
|
410
388
|
" 当 " + r.statusField + " 为 " + (r.statusOKValue || "空值") + " 时是成功提示,否则是错误消息"
|
|
411
389
|
), r.ignoreMessage && e.push(" 并忽略以下消息:" + r.ignoreMessage);
|
|
@@ -414,7 +392,7 @@ const Ee = function(t) {
|
|
|
414
392
|
return e.join(`
|
|
415
393
|
`);
|
|
416
394
|
};
|
|
417
|
-
function
|
|
395
|
+
function se(t, e, s) {
|
|
418
396
|
const r = t || { resolve: "json", messageField: b }, n = {
|
|
419
397
|
ok: !1,
|
|
420
398
|
code: e,
|
|
@@ -423,20 +401,20 @@ function re(t, e, s) {
|
|
|
423
401
|
};
|
|
424
402
|
switch (r.resolve) {
|
|
425
403
|
case "body":
|
|
426
|
-
n.message =
|
|
404
|
+
n.message = O(s) || s;
|
|
427
405
|
break;
|
|
428
406
|
case "json":
|
|
429
|
-
const { code: o, message: a } =
|
|
430
|
-
n.code = o || e, n.message =
|
|
407
|
+
const { code: o, message: a } = re(s, r.converter, r.statusField, r.messageField);
|
|
408
|
+
n.code = o || e, n.message = O(s) || a;
|
|
431
409
|
break;
|
|
432
410
|
}
|
|
433
411
|
return n;
|
|
434
412
|
}
|
|
435
|
-
function
|
|
436
|
-
if (!
|
|
413
|
+
function re(t, e, s, r = b) {
|
|
414
|
+
if (!A(t))
|
|
437
415
|
return { message: "" };
|
|
438
416
|
const n = k(R(t), e);
|
|
439
|
-
return !n || !
|
|
417
|
+
return !n || !U(n) ? { message: t } : {
|
|
440
418
|
code: s ? E(n, s) : "",
|
|
441
419
|
message: E(n, r) || t
|
|
442
420
|
};
|
|
@@ -445,21 +423,21 @@ function E(t, e) {
|
|
|
445
423
|
const s = Array.isArray(e) ? e : [e];
|
|
446
424
|
for (const r of s)
|
|
447
425
|
if (r in t)
|
|
448
|
-
return
|
|
426
|
+
return ne(t[r]);
|
|
449
427
|
return "";
|
|
450
428
|
}
|
|
451
|
-
function
|
|
429
|
+
function ne(t) {
|
|
452
430
|
return t ? typeof t == "string" ? t : JSON.stringify(t) : "";
|
|
453
431
|
}
|
|
454
|
-
const
|
|
455
|
-
function
|
|
456
|
-
const e = t.match(
|
|
432
|
+
const oe = /<title>([^<]+)<\/title>/i, ae = /<message>([^<]+)<\/message>/i;
|
|
433
|
+
function O(t) {
|
|
434
|
+
const e = t.match(oe);
|
|
457
435
|
if (e)
|
|
458
436
|
return e[1];
|
|
459
|
-
const s = t.match(
|
|
437
|
+
const s = t.match(ae);
|
|
460
438
|
return s ? s[1] : "";
|
|
461
439
|
}
|
|
462
|
-
function
|
|
440
|
+
function ce(t, e, s, r) {
|
|
463
441
|
const n = t || { resolve: "body" }, o = {
|
|
464
442
|
ok: !0,
|
|
465
443
|
code: s,
|
|
@@ -469,33 +447,33 @@ function ie(t, e, s, r) {
|
|
|
469
447
|
if (e === 204 || !r)
|
|
470
448
|
return o;
|
|
471
449
|
if (n.resolve === "body")
|
|
472
|
-
return o.data =
|
|
450
|
+
return o.data = A(r) ? k(R(r), t.converter) : r, o;
|
|
473
451
|
const a = R(r);
|
|
474
452
|
if (!a)
|
|
475
453
|
return o.ok = !1, o.code = "ResponseFormatError", o.message = "响应内容无法安全解析", o;
|
|
476
454
|
const c = k(a, t.converter);
|
|
477
|
-
if (!c || !
|
|
455
|
+
if (!c || !U(c))
|
|
478
456
|
return o.ok = !1, o.code = "ResponseFormatError", o.message = "响应内容无法格式化为 Object", o;
|
|
479
|
-
const i = n.statusField, g = n.statusOKValue || "", u = n.dataField ||
|
|
457
|
+
const i = n.statusField, g = n.statusOKValue || "", u = n.dataField || x, l = n.messageField || b, f = n.ignoreMessage || "";
|
|
480
458
|
if (i && !(i in c))
|
|
481
459
|
return o.ok = !1, o.code = "ResponseFieldMissing", o.message = "响应内容找不到状态字段 " + i, o;
|
|
482
460
|
const d = i ? c[i] + "" : "";
|
|
483
461
|
return o.ok = i ? d === g : !0, o.code = d || s, o.data = u === !0 ? c : u in c ? c[u] : null, o.message = E(c, l), f && o.message && (Array.isArray(f) && f.includes(o.message) || typeof f == "string" && o.message === f) && (o.message = ""), o;
|
|
484
462
|
}
|
|
485
|
-
function
|
|
463
|
+
function $(t) {
|
|
486
464
|
return t >= 200 && t < 400;
|
|
487
465
|
}
|
|
488
466
|
function k(t, e) {
|
|
489
|
-
return e
|
|
467
|
+
return e && typeof e == "function" ? e(t) : t;
|
|
490
468
|
}
|
|
491
|
-
const
|
|
492
|
-
function
|
|
469
|
+
const ie = I("APIError");
|
|
470
|
+
function ue(t) {
|
|
493
471
|
const e = {};
|
|
494
472
|
for (const n in t.headers)
|
|
495
473
|
(n.startsWith("x-") || n.includes("trace") || n.includes("server") || /\b(?:id|uuid)\b/.test(n)) && (e[n] = t.headers[n]);
|
|
496
474
|
const s = t.url.replace(/^(?:https?:)?\/*/i, "").replace(/\?.+/, ""), r = t.status < 0 ? "unknown" : t.status;
|
|
497
475
|
return {
|
|
498
|
-
sentryError: new
|
|
476
|
+
sentryError: new ie(`${s} | ${r}${t.code ? ` | ${t.code}` : ""}`),
|
|
499
477
|
sentryTags: {
|
|
500
478
|
...e,
|
|
501
479
|
status: r,
|
|
@@ -511,18 +489,24 @@ function le(t) {
|
|
|
511
489
|
}
|
|
512
490
|
};
|
|
513
491
|
}
|
|
492
|
+
function le(t) {
|
|
493
|
+
const e = {};
|
|
494
|
+
for (const [s, r] of t)
|
|
495
|
+
s && typeof s == "string" && (e[s] = r || "");
|
|
496
|
+
return e;
|
|
497
|
+
}
|
|
514
498
|
function ke(t, e, s, r) {
|
|
515
|
-
const n = t.status, o = t.method, a =
|
|
499
|
+
const n = t.status, o = t.method, a = le(
|
|
516
500
|
Object.entries(t.headers || {}).map(([d, y]) => [d.toLowerCase(), y])
|
|
517
|
-
), { ok: c, code: i, data: g, message: u } =
|
|
501
|
+
), { ok: c, code: i, data: g, message: u } = te(
|
|
518
502
|
n,
|
|
519
503
|
t.statusText,
|
|
520
504
|
t.body,
|
|
521
505
|
s.get("responseRule"),
|
|
522
506
|
r?.responseRule
|
|
523
507
|
);
|
|
524
|
-
if (
|
|
525
|
-
const d =
|
|
508
|
+
if (!$(n)) {
|
|
509
|
+
const d = ue({
|
|
526
510
|
url: t.url,
|
|
527
511
|
method: t.method,
|
|
528
512
|
status: n,
|
|
@@ -562,7 +546,7 @@ function F(t, e, s, r, n, o) {
|
|
|
562
546
|
const a = n.get("message"), c = a === !1 || o?.message === !1 ? !1 : o?.message || a;
|
|
563
547
|
if (c !== !1) {
|
|
564
548
|
const i = typeof c == "function" ? c(t, s, r, e) : e;
|
|
565
|
-
i instanceof Error ? n.showMessage(!0, w(i.message), t.code, t.status) : i && typeof i == "object" && "message" in i && typeof i.message == "string" ? n.showMessage(!1, w(i.message), t.code, t.status) : n.showMessage(!t.ok, w(String(i)), t.code, t.status);
|
|
549
|
+
i instanceof Error ? n.showMessage(!0, w(i.message), t.code, t.status) : i && typeof i == "object" && "message" in i && typeof i.message == "string" ? n.showMessage(!1, w(i.message), t.code, t.status) : i && n.showMessage(!t.ok, w(String(i)), t.code, t.status);
|
|
566
550
|
}
|
|
567
551
|
return t;
|
|
568
552
|
}
|
|
@@ -599,32 +583,32 @@ async function fe(t, e, s, r, n) {
|
|
|
599
583
|
headers: u.headers,
|
|
600
584
|
cost: f
|
|
601
585
|
});
|
|
602
|
-
const y =
|
|
586
|
+
const y = $(l);
|
|
603
587
|
if (!a || o >= a || c === "network" && l > 0 || c === "status" && y || Array.isArray(c) && !c.includes(l) || typeof c == "function" && c(u, o) !== !0)
|
|
604
588
|
return u;
|
|
605
|
-
const
|
|
606
|
-
return await
|
|
589
|
+
const m = r?.retryInterval ?? s.get("retryInterval") ?? 100;
|
|
590
|
+
return await q(
|
|
607
591
|
Math.max(
|
|
608
592
|
100,
|
|
609
|
-
|
|
593
|
+
m === "2EB" ? Math.pow(2, o) * 100 : typeof m == "function" ? m(o + 1, { url: e, status: l, method: u.method }) || 0 : m
|
|
610
594
|
)
|
|
611
595
|
), await fe(t, e, s, r, o + 1);
|
|
612
596
|
}
|
|
613
597
|
export {
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
598
|
+
D as E,
|
|
599
|
+
we as N,
|
|
600
|
+
be as Q,
|
|
601
|
+
z as R,
|
|
602
|
+
de as Z,
|
|
603
|
+
ye as _,
|
|
604
|
+
L as a,
|
|
605
|
+
j as b,
|
|
620
606
|
Re as c,
|
|
621
|
-
|
|
622
|
-
j as e,
|
|
623
|
-
ge as f,
|
|
607
|
+
le as f,
|
|
624
608
|
Ee as g,
|
|
625
609
|
ke as h,
|
|
626
|
-
|
|
627
|
-
pe as o,
|
|
610
|
+
ge as l,
|
|
628
611
|
fe as r,
|
|
629
|
-
me as s
|
|
612
|
+
me as s,
|
|
613
|
+
pe as u
|
|
630
614
|
};
|
package/dist/wx.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { h as i, r as p, c as f,
|
|
2
|
-
import { g as H } from "./retry-
|
|
1
|
+
import { h as i, r as p, c as f, Q as l, E as h, a as d, s as y, N as g } from "./retry-BjOxSyvC.js";
|
|
2
|
+
import { g as H } from "./retry-BjOxSyvC.js";
|
|
3
3
|
const m = async function(t, n, r) {
|
|
4
4
|
return i(await p(x, t, n, r), t, n, r);
|
|
5
5
|
}, x = async function(t, n, r) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seayoo-web/request",
|
|
3
|
-
"version": "3.4.
|
|
3
|
+
"version": "3.4.4",
|
|
4
4
|
"description": "request tools for seayoo web",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -56,7 +56,7 @@
|
|
|
56
56
|
"happy-dom": "^12.10.3",
|
|
57
57
|
"msw": "^2.10.5",
|
|
58
58
|
"vitest": "^3.2.4",
|
|
59
|
-
"@seayoo-web/utils": "^4.
|
|
59
|
+
"@seayoo-web/utils": "^4.4.0"
|
|
60
60
|
},
|
|
61
61
|
"scripts": {
|
|
62
62
|
"prebuild": "pnpm --F utils build",
|
package/types/inc/utils.d.ts
CHANGED
|
@@ -2,8 +2,3 @@
|
|
|
2
2
|
* 简版 Object.fromEntries 以处理某些低版本浏览器和 webview 的问题
|
|
3
3
|
*/
|
|
4
4
|
export declare function fromEntries(kv: [string, string | undefined][]): Record<string, string>;
|
|
5
|
-
/**
|
|
6
|
-
* 安全解析 JSON
|
|
7
|
-
* 防止原型污染和原型链攻击
|
|
8
|
-
*/
|
|
9
|
-
export declare function safeJSONParse<T = unknown>(text: string): T | null;
|