@seayoo-web/request 2.1.0 → 2.1.2
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 +119 -120
- package/dist/index.cjs +2 -2
- package/dist/index.js +64 -62
- package/dist/node.cjs +1 -1
- package/dist/node.js +22 -22
- package/dist/request.fetch-DF7WikXa.js +46 -0
- package/dist/request.fetch-rPuFhiKx.cjs +1 -0
- package/dist/{retry-CRrgnQtP.cjs → retry-B9vBp2mw.cjs} +2 -2
- package/dist/{retry-8Z8eLh90.js → retry-Ch1GFMrA.js} +54 -54
- package/dist/wx.cjs +1 -1
- package/dist/wx.js +21 -21
- package/package.json +2 -2
- package/types/inc/type.d.ts +5 -3
- package/dist/request.fetch-YPs3rgIZ.cjs +0 -1
- package/dist/request.fetch-idcpAgJt.js +0 -51
package/dist/index.js
CHANGED
|
@@ -1,89 +1,91 @@
|
|
|
1
|
-
import { h as w, r as T, R as y, c as x,
|
|
2
|
-
import { g as
|
|
3
|
-
import { f as C } from "./request.fetch-
|
|
1
|
+
import { h as w, r as T, R as y, c as x, Z as p, a as R, I as q, l as E, N as H } from "./retry-Ch1GFMrA.js";
|
|
2
|
+
import { g as z } from "./retry-Ch1GFMrA.js";
|
|
3
|
+
import { f as C } from "./request.fetch-DF7WikXa.js";
|
|
4
4
|
const L = async function(t, a, e) {
|
|
5
5
|
return w(await T(m, t, a, e), t, a, e);
|
|
6
6
|
};
|
|
7
|
-
async function
|
|
8
|
-
const r = e == null ? void 0 : e.body,
|
|
7
|
+
async function k(t, a, e, n) {
|
|
8
|
+
const r = e == null ? void 0 : e.body, d = (e == null ? void 0 : e.method) === "PUT" ? "PUT" : "POST";
|
|
9
9
|
if (a instanceof Blob) {
|
|
10
|
-
const l = new y(n),
|
|
10
|
+
const l = new y(n), f = await m(t, l, {
|
|
11
11
|
...e,
|
|
12
|
-
method:
|
|
12
|
+
method: d,
|
|
13
13
|
body: a
|
|
14
14
|
});
|
|
15
|
-
return w(
|
|
15
|
+
return w(f, t, l, e);
|
|
16
16
|
}
|
|
17
|
-
const s = new FormData(),
|
|
18
|
-
r instanceof Object && Object.entries(r).forEach(([l,
|
|
19
|
-
|
|
20
|
-
s.append(`${l}[${
|
|
21
|
-
}) : s.append(l, String(
|
|
17
|
+
const s = new FormData(), u = { ...a };
|
|
18
|
+
r instanceof Object && Object.entries(r).forEach(([l, f]) => {
|
|
19
|
+
f instanceof Blob ? u[l] = f : Array.isArray(f) ? f.forEach((o, i) => {
|
|
20
|
+
s.append(`${l}[${i}]`, String(o));
|
|
21
|
+
}) : s.append(l, String(f));
|
|
22
22
|
});
|
|
23
|
-
for (const l in
|
|
24
|
-
s.append(l,
|
|
23
|
+
for (const l in u)
|
|
24
|
+
s.append(l, u[l]);
|
|
25
25
|
const b = new y(n), h = await m(t, b, {
|
|
26
26
|
...e,
|
|
27
|
-
method:
|
|
27
|
+
method: d,
|
|
28
28
|
body: s
|
|
29
29
|
});
|
|
30
30
|
return w(h, t, b, e);
|
|
31
31
|
}
|
|
32
32
|
const m = async function(t, a, e) {
|
|
33
|
-
const n = await x(t, a, e), r = n.method,
|
|
34
|
-
return await new Promise((
|
|
33
|
+
const n = await x(t, a, e), r = n.method, d = e == null ? void 0 : e.onUploadProgress, s = p(n.url, n.params);
|
|
34
|
+
return await new Promise((u) => {
|
|
35
35
|
let b = null, h = !1;
|
|
36
36
|
const l = function() {
|
|
37
37
|
h || (o.abort(), h = !0);
|
|
38
38
|
};
|
|
39
|
-
function
|
|
39
|
+
function f() {
|
|
40
40
|
b !== null && clearTimeout(b), n.abort && n.abort.removeEventListener("abort", l);
|
|
41
41
|
}
|
|
42
42
|
const o = new XMLHttpRequest();
|
|
43
|
-
if (o.open(r, s, !0),
|
|
44
|
-
let
|
|
43
|
+
if (o.open(r, s, !0), d) {
|
|
44
|
+
let i = 1;
|
|
45
45
|
o.upload.addEventListener("progress", (g) => {
|
|
46
|
-
|
|
46
|
+
i = g.total, d({ total: g.total, loaded: g.loaded });
|
|
47
47
|
}), o.addEventListener("load", () => {
|
|
48
|
-
|
|
48
|
+
d({ loaded: i, total: i });
|
|
49
49
|
});
|
|
50
50
|
}
|
|
51
51
|
o.addEventListener("load", () => {
|
|
52
|
-
|
|
52
|
+
const i = o.status;
|
|
53
|
+
f(), u({
|
|
53
54
|
url: s,
|
|
54
55
|
method: r,
|
|
55
|
-
status:
|
|
56
|
+
status: i,
|
|
56
57
|
statusText: o.statusText,
|
|
57
|
-
headers:
|
|
58
|
-
body: r === "HEAD" ? "" : o.responseText
|
|
58
|
+
headers: S(o),
|
|
59
|
+
body: r === "HEAD" || i === 204 ? "" : o.responseText
|
|
59
60
|
});
|
|
60
61
|
}), o.addEventListener(
|
|
61
62
|
"error",
|
|
62
|
-
(
|
|
63
|
-
|
|
63
|
+
(i) => {
|
|
64
|
+
f(), u({
|
|
64
65
|
url: s,
|
|
65
66
|
method: r,
|
|
66
|
-
status: -1,
|
|
67
|
-
statusText: R.
|
|
67
|
+
status: o.status || -1,
|
|
68
|
+
statusText: o.statusText || R.NetworkError,
|
|
68
69
|
body: "",
|
|
69
|
-
rawError:
|
|
70
|
+
rawError: i
|
|
70
71
|
});
|
|
71
72
|
},
|
|
72
73
|
!0
|
|
73
74
|
), o.addEventListener("abort", () => {
|
|
74
|
-
|
|
75
|
+
f(), u({
|
|
75
76
|
url: s,
|
|
76
77
|
method: r,
|
|
77
|
-
status:
|
|
78
|
+
status: 0,
|
|
79
|
+
// 设置为 0 可以防止自动重试的默认策略
|
|
78
80
|
statusText: R.Aborted,
|
|
79
81
|
body: ""
|
|
80
82
|
});
|
|
81
|
-
}), Object.entries(n.headers).forEach(([
|
|
82
|
-
o.setRequestHeader(
|
|
83
|
+
}), Object.entries(n.headers).forEach(([i, g]) => {
|
|
84
|
+
o.setRequestHeader(i, g);
|
|
83
85
|
}), n.credentials === "include" && (o.withCredentials = !0), o.send(n.body || void 0), n.abort && n.abort.addEventListener("abort", l), n.timeout > 0 && (b = setTimeout(l, n.timeout));
|
|
84
86
|
});
|
|
85
87
|
};
|
|
86
|
-
function
|
|
88
|
+
function S(t) {
|
|
87
89
|
const a = {};
|
|
88
90
|
if (!t)
|
|
89
91
|
return a;
|
|
@@ -93,36 +95,36 @@ function k(t) {
|
|
|
93
95
|
const r = n.trim();
|
|
94
96
|
if (!r)
|
|
95
97
|
return;
|
|
96
|
-
const
|
|
97
|
-
s && (a[s] = (
|
|
98
|
+
const d = r.split(":"), s = d[0].trim();
|
|
99
|
+
s && (a[s] = (d[1] || "").trim());
|
|
98
100
|
}), a;
|
|
99
101
|
}
|
|
100
|
-
async function
|
|
102
|
+
async function D(t, a, e = {}) {
|
|
101
103
|
const n = window;
|
|
102
104
|
"callback" in e || (e.callback = "jsonxData" + Math.random().toString(16).slice(2));
|
|
103
105
|
const r = e.callback + "";
|
|
104
106
|
if (!t)
|
|
105
107
|
return null;
|
|
106
|
-
const
|
|
108
|
+
const d = p(t, e, !0);
|
|
107
109
|
return new Promise((s) => {
|
|
108
|
-
n[r] = function(
|
|
109
|
-
if (r in window && delete n[r], a(
|
|
110
|
-
return
|
|
111
|
-
console.warn("response type check faild", t,
|
|
112
|
-
},
|
|
110
|
+
n[r] = function(u) {
|
|
111
|
+
if (r in window && delete n[r], a(u))
|
|
112
|
+
return u;
|
|
113
|
+
console.warn("response type check faild", t, u), s(null);
|
|
114
|
+
}, q(d).catch(function() {
|
|
113
115
|
s(null), delete n[r];
|
|
114
116
|
});
|
|
115
117
|
});
|
|
116
118
|
}
|
|
117
|
-
async function
|
|
119
|
+
async function O(t, a, e = {}) {
|
|
118
120
|
const n = window;
|
|
119
|
-
return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), t ? await
|
|
121
|
+
return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), t ? await q(p(t, e, !0)).then(() => {
|
|
120
122
|
const r = n[e.var + ""];
|
|
121
123
|
return a(r) ? r : (console.warn("response type check faild", t, r), null);
|
|
122
124
|
}).catch(() => null) : null;
|
|
123
125
|
}
|
|
124
126
|
const P = async function(t, a, e) {
|
|
125
|
-
return await
|
|
127
|
+
return await k(t, a, e, {
|
|
126
128
|
baseURL: c.getConfig("baseURL"),
|
|
127
129
|
logHandler: c.getConfig("logHandler"),
|
|
128
130
|
errorHandler: c.getConfig("errorHandler"),
|
|
@@ -131,24 +133,24 @@ const P = async function(t, a, e) {
|
|
|
131
133
|
responseHandler: c.getConfig("responseHandler")
|
|
132
134
|
});
|
|
133
135
|
};
|
|
134
|
-
function
|
|
135
|
-
if (!
|
|
136
|
+
function U(t) {
|
|
137
|
+
if (!E.window)
|
|
136
138
|
throw new Error("Default Module Only Support In Browser");
|
|
137
|
-
return
|
|
139
|
+
return E.fetch ? new H(C, t) : new H(L, t);
|
|
138
140
|
}
|
|
139
|
-
const c =
|
|
141
|
+
const c = U(), A = c.setConfig, M = c.request, N = c.head, I = c.get, B = c.post, X = c.del, F = c.put, G = c.patch;
|
|
140
142
|
export {
|
|
141
|
-
|
|
143
|
+
U as NetRequest,
|
|
142
144
|
R as RequestInteralError,
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
145
|
+
X as del,
|
|
146
|
+
I as get,
|
|
147
|
+
z as getResponseRulesDescription,
|
|
146
148
|
N as head,
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
149
|
+
D as jsonp,
|
|
150
|
+
O as jsonx,
|
|
151
|
+
G as patch,
|
|
152
|
+
B as post,
|
|
153
|
+
F as put,
|
|
152
154
|
M as request,
|
|
153
155
|
A as setGlobalConfig,
|
|
154
156
|
P as upload
|
package/dist/node.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./retry-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./retry-B9vBp2mw.cjs"),b=require("./request.fetch-rPuFhiKx.cjs"),y=require("node:http"),g=require("node:https"),w=async function(r,u,c){return e.handleResponse(await e.retryRequest(E,r,u,c),r,u,c)},E=async function(r,u,c){const t=await e.convertOptions(r,u,c);if(!e.tt(t.url))return{url:t.url,method:t.method,status:-1,statusText:e.RequestInteralError.URLFormatError,headers:{},body:""};const p=/^https:\/\//i.test(t.url)?g:y,a=new URL(t.url),i=t.params;i instanceof Object&&Object.keys(i).forEach(n=>a.searchParams.set(n,i[n]));const f=t.method==="HEAD";return new Promise(n=>{const d=p.request(a,{headers:t.headers,method:t.method,timeout:t.timeout>0?t.timeout:void 0},function(o){const m=[];o.on("data",h=>m.push(h)),o.on("end",()=>{const h=e.fromEntries(Object.entries(o.headers).map(([R,l])=>[R.toLowerCase(),Array.isArray(l)?l.join(","):l]));n({url:a.toString(),method:t.method,status:o.statusCode||-1,statusText:o.statusMessage||e.RequestInteralError.Unknown,headers:h,body:f||o.statusCode===204?"":Buffer.concat(m).toString("utf-8")})})});d.on("error",o=>{n({url:a.toString(),method:t.method,status:-1,statusText:e.RequestInteralError.NetworkError,body:"",rawError:o})}),d.on("timeout",()=>{n({url:a.toString(),method:t.method,status:-1,statusText:e.RequestInteralError.Timeout,body:""})}),t.body&&d.write(t.body),d.end()})};function q(r){return e.lt.fetch?new e.NetRequestHandler(b.fetchRequest,r):new e.NetRequestHandler(w,r)}const s=q(),C=s.setConfig,N=s.head,S=s.get,T=s.post,j=s.del,O=s.put,x=s.patch;exports.NetRequest=q;exports.del=j;exports.get=S;exports.head=N;exports.patch=x;exports.post=T;exports.put=O;exports.setGlobalConfig=C;
|
package/dist/node.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { h as y, r as q, c as w,
|
|
2
|
-
import { f as
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
const
|
|
6
|
-
return y(await q(L,
|
|
7
|
-
}, L = async function(
|
|
8
|
-
const t = await w(
|
|
1
|
+
import { h as y, r as q, c as w, t as g, a as d, f as E, N as p, l as x } from "./retry-Ch1GFMrA.js";
|
|
2
|
+
import { f as C } from "./request.fetch-DF7WikXa.js";
|
|
3
|
+
import N from "node:http";
|
|
4
|
+
import T from "node:https";
|
|
5
|
+
const j = async function(o, a, u) {
|
|
6
|
+
return y(await q(L, o, a, u), o, a, u);
|
|
7
|
+
}, L = async function(o, a, u) {
|
|
8
|
+
const t = await w(o, a, u);
|
|
9
9
|
if (!g(t.url))
|
|
10
10
|
return {
|
|
11
11
|
url: t.url,
|
|
@@ -15,7 +15,7 @@ const C = async function(e, a, u) {
|
|
|
15
15
|
headers: {},
|
|
16
16
|
body: ""
|
|
17
17
|
};
|
|
18
|
-
const l = /^https:\/\//i.test(t.url) ?
|
|
18
|
+
const l = /^https:\/\//i.test(t.url) ? T : N, n = new URL(t.url), i = t.params;
|
|
19
19
|
i instanceof Object && Object.keys(i).forEach((r) => n.searchParams.set(r, i[r]));
|
|
20
20
|
const R = t.method === "HEAD";
|
|
21
21
|
return new Promise((r) => {
|
|
@@ -26,31 +26,31 @@ const C = async function(e, a, u) {
|
|
|
26
26
|
method: t.method,
|
|
27
27
|
timeout: t.timeout > 0 ? t.timeout : void 0
|
|
28
28
|
},
|
|
29
|
-
function(
|
|
29
|
+
function(e) {
|
|
30
30
|
const f = [];
|
|
31
|
-
|
|
31
|
+
e.on("data", (m) => f.push(m)), e.on("end", () => {
|
|
32
32
|
const m = E(
|
|
33
|
-
Object.entries(
|
|
33
|
+
Object.entries(e.headers).map(([b, h]) => [b.toLowerCase(), Array.isArray(h) ? h.join(",") : h])
|
|
34
34
|
);
|
|
35
35
|
r({
|
|
36
36
|
url: n.toString(),
|
|
37
37
|
method: t.method,
|
|
38
|
-
status:
|
|
39
|
-
statusText:
|
|
38
|
+
status: e.statusCode || -1,
|
|
39
|
+
statusText: e.statusMessage || d.Unknown,
|
|
40
40
|
headers: m,
|
|
41
|
-
body: R ? "" : Buffer.concat(f).toString("utf-8")
|
|
41
|
+
body: R || e.statusCode === 204 ? "" : Buffer.concat(f).toString("utf-8")
|
|
42
42
|
});
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
45
|
);
|
|
46
|
-
c.on("error", (
|
|
46
|
+
c.on("error", (e) => {
|
|
47
47
|
r({
|
|
48
48
|
url: n.toString(),
|
|
49
49
|
method: t.method,
|
|
50
50
|
status: -1,
|
|
51
|
-
statusText: d.
|
|
51
|
+
statusText: d.NetworkError,
|
|
52
52
|
body: "",
|
|
53
|
-
rawError:
|
|
53
|
+
rawError: e
|
|
54
54
|
});
|
|
55
55
|
}), c.on("timeout", () => {
|
|
56
56
|
r({
|
|
@@ -63,12 +63,12 @@ const C = async function(e, a, u) {
|
|
|
63
63
|
}), t.body && c.write(t.body), c.end();
|
|
64
64
|
});
|
|
65
65
|
};
|
|
66
|
-
function
|
|
67
|
-
return x.fetch ? new p(
|
|
66
|
+
function O(o) {
|
|
67
|
+
return x.fetch ? new p(C, o) : new p(j, o);
|
|
68
68
|
}
|
|
69
|
-
const s =
|
|
69
|
+
const s = O(), k = s.setConfig, P = s.head, B = s.get, D = s.post, F = s.del, G = s.put, I = s.patch;
|
|
70
70
|
export {
|
|
71
|
-
|
|
71
|
+
O as NetRequest,
|
|
72
72
|
F as del,
|
|
73
73
|
B as get,
|
|
74
74
|
P as head,
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { h as m, r as w, c as y, l as E, f as R, a as b } from "./retry-Ch1GFMrA.js";
|
|
2
|
+
const x = async function(s, o, n) {
|
|
3
|
+
return m(await w(g, s, o, n), s, o, n);
|
|
4
|
+
}, g = async function(s, o, n) {
|
|
5
|
+
const t = await y(s, o, n), i = new URL(t.url), c = t.params;
|
|
6
|
+
c instanceof Object && Object.keys(c).forEach((e) => i.searchParams.set(e, c[e]));
|
|
7
|
+
const r = E.AbortController ? new AbortController() : null;
|
|
8
|
+
function d() {
|
|
9
|
+
r && !r.signal.aborted && r.abort();
|
|
10
|
+
}
|
|
11
|
+
t.abort && t.abort.addEventListener("abort", d);
|
|
12
|
+
const h = t.timeout > 0 ? setTimeout(d, t.timeout) : null, l = t.method === "HEAD";
|
|
13
|
+
return await fetch(i, {
|
|
14
|
+
method: t.method,
|
|
15
|
+
headers: Object.keys(t.headers).length > 0 ? new Headers(t.headers) : void 0,
|
|
16
|
+
body: t.body,
|
|
17
|
+
credentials: t.credentials,
|
|
18
|
+
signal: r == null ? void 0 : r.signal,
|
|
19
|
+
redirect: "follow"
|
|
20
|
+
}).then(async (e) => {
|
|
21
|
+
const a = {
|
|
22
|
+
url: i.toString(),
|
|
23
|
+
method: t.method,
|
|
24
|
+
status: e.status,
|
|
25
|
+
statusText: e.statusText,
|
|
26
|
+
headers: R([...e.headers.entries()])
|
|
27
|
+
}, u = l || e.status === 204 ? "" : await e.text().catch((f) => f);
|
|
28
|
+
return u instanceof Error ? { ...a, body: "", statusText: b.Unknown, rawError: u } : { ...a, body: u };
|
|
29
|
+
}).catch((e) => {
|
|
30
|
+
const a = r == null ? void 0 : r.signal.aborted;
|
|
31
|
+
return {
|
|
32
|
+
url: i.toString(),
|
|
33
|
+
method: t.method,
|
|
34
|
+
status: a ? 0 : -1,
|
|
35
|
+
// 设置为 0 可以防止自动重试的默认策略
|
|
36
|
+
statusText: a ? b.Aborted : b.NetworkError,
|
|
37
|
+
body: "",
|
|
38
|
+
rawError: e
|
|
39
|
+
};
|
|
40
|
+
}).finally(() => {
|
|
41
|
+
h !== null && clearTimeout(h), t.abort && t.abort.removeEventListener("abort", d);
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
export {
|
|
45
|
+
x as f
|
|
46
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const s=require("./retry-B9vBp2mw.cjs"),m=async function(a,n,i){return s.handleResponse(await s.retryRequest(w,a,n,i),a,n,i)},w=async function(a,n,i){const t=await s.convertOptions(a,n,i),c=new URL(t.url),u=t.params;u instanceof Object&&Object.keys(u).forEach(e=>c.searchParams.set(e,u[e]));const r=s.lt.AbortController?new AbortController:null;function d(){r&&!r.signal.aborted&&r.abort()}t.abort&&t.abort.addEventListener("abort",d);const h=t.timeout>0?setTimeout(d,t.timeout):null,l=t.method==="HEAD";return await fetch(c,{method:t.method,headers:Object.keys(t.headers).length>0?new Headers(t.headers):void 0,body:t.body,credentials:t.credentials,signal:r==null?void 0:r.signal,redirect:"follow"}).then(async e=>{const o={url:c.toString(),method:t.method,status:e.status,statusText:e.statusText,headers:s.fromEntries([...e.headers.entries()])},b=l||e.status===204?"":await e.text().catch(f=>f);return b instanceof Error?{...o,body:"",statusText:s.RequestInteralError.Unknown,rawError:b}:{...o,body:b}}).catch(e=>{const o=r==null?void 0:r.signal.aborted;return{url:c.toString(),method:t.method,status:o?0:-1,statusText:o?s.RequestInteralError.Aborted:s.RequestInteralError.NetworkError,body:"",rawError:e}}).finally(()=>{h!==null&&clearTimeout(h),t.abort&&t.abort.removeEventListener("abort",d)})};exports.fetchRequest=m;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`)};function pe(e,t,s){const r=e||{resolve:"json",messageField:j},o={ok:!1,code:t,message:s,data:null};switch(r.resolve){case"body":o.message=P(s)||s;break;case"json":const{code:n,message:a}=ye(s,r.converter,r.statusField,r.messageField);o.code=n||t,o.message=P(s)||a;break}return o}function ye(e,t,s,r=j){if(!_(e))return{message:""};const o=A(T(e),t);return!o||!v(o)?{message:e}:{code:s?F(o,s):"",message:F(o,r)||e}}function F(e,t){const s=Array.isArray(t)?t:[t];for(const r of s)if(r in e)return be(e[r]);return""}function be(e){return e?typeof e=="string"?e:JSON.stringify(e):""}const we=/<title>([^<]+)<\/title>/i,Re=/<message>([^<]+)<\/message>/i;function P(e){const t=e.match(we);if(t)return t[1];const s=e.match(Re);return s?s[1]:""}function je(e,t,s,r){const o=e||{resolve:"body"},n={ok:!0,code:s,message:"",data:null};if(t===204||!r)return n;if(o.resolve==="body")return n.data=_(r)?A(T(r),e.converter):r,n;const a=A(T(r),e.converter);if(!a||!v(a))return n.ok=!1,n.code="ResponseFormatError",n.message="响应内容无法格式化为 Object",n;const c=o.statusField,i=o.statusOKValue||"",d=o.dataField||N,u=o.messageField||j,l=o.ignoreMessage||"";if(c&&!(c in a))return n.ok=!1,n.code="ResponseFieldMissing",n.message="响应内容找不到状态字段 "+c,n;const f=c?a[c]+"":"";return n.ok=c?f===i:!0,n.code=f||s,n.data=d===!0?a:d in a?a[d]:null,n.message=F(a,u),l&&n.message&&(Array.isArray(l)&&l.includes(n.message)||typeof l=="string"&&n.message===l)&&(n.message=""),n}function U(e){return e>=200&&e<400}function A(e,t){return t==="camelize"?
|
|
1
|
+
"use strict";var G=Object.defineProperty;var J=(e,t,s)=>t in e?G(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s;var y=(e,t,s)=>J(e,typeof t!="symbol"?t+"":t,s);const h=typeof globalThis<"u"?globalThis:typeof global<"u"?global:typeof self<"u"?self:typeof window<"u"?window:{},R={wx:"wx"in h&&D(h.wx),fetch:"fetch"in h&&w(h.fetch),window:"window"in h&&D(h.window),Blob:"Blob"in h&&w(h.Blob),FormData:"FormData"in h&&w(h.FormData),TextDecoder:"TextDecoder"in h&&w(h.TextDecoder),AbortController:"AbortController"in h&&w(h.AbortController)};function w(e){return typeof e=="function"}function D(e){return typeof e=="object"&&e!==null}function W(e){return new Promise(t=>setTimeout(t,Math.max(0,e)))}function V(){}function Z(e){const t=e||"CustomError";return class extends Error{constructor(s){super(s),Object.defineProperty(this,"name",{value:t,enumerable:!1,configurable:!0}),"setPrototypeOf"in Object&&Object.setPrototypeOf(this,new.target.prototype),"captureStackTrace"in Error&&typeof Error.captureStackTrace=="function"&&Error.captureStackTrace(this,this.constructor)}}}const Q=/^(?:https?:)?\/\/.+$/i,z=/^https?:\/\/.+$/i,Y=/^\{[\d\D]*\}$/,X=/^\[[\d\D]*\]$/;function O(e,t=!1){return t?Q.test(e):z.test(e)}function ee(e){return e==null}function v(e,...t){if(!e||typeof e!="object")return!1;const s=Object.getPrototypeOf(e);return s!==Object.prototype&&s!==null?!1:t.every(r=>r in e)}function _(e){return Y.test(e)||X.test(e)}function $(e){return e?e[0].toLowerCase()+e.slice(1):""}const B=/_\w*/,H=/-\w*/;function M(e){const t=B.test(e)?e.replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(s,r)=>r.toUpperCase()):H.test(e)?e.replace(/(?:^-*|-*$)/g,"").replace(/-+(\w)/g,(s,r)=>r.toUpperCase()):e;return $(t)}function L(e){return B.test(e)?$(e).replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(t,s)=>"_"+s.toLowerCase()):H.test(e)?$(e.replace(/(?:^-*|-*$)/g,"")).replace(/-+(\w)/g,(t,s)=>"_"+s.toLowerCase()):$(e).replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`)}function T(e,t){try{const s=JSON.parse(e);return t?t(s)?s:null:s}catch{return null}}""+Math.random().toString(32).slice(2);function k(e,t=!1){return typeof e=="string"?M(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?k(s):typeof s=="object"&&s?k(s,!0):s):v(e)?Object.keys(e).reduce((s,r)=>{const o=M(String(r)),n=o.charAt(0).toLowerCase()+o.slice(1);return s[n]=t?e[r]:k(e[r]),s},{}):e}function E(e,t=!1){return typeof e=="string"?L(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?E(s):typeof s=="object"&&s?E(s,t):s):v(e)?Object.keys(e).reduce((s,r)=>{const o=L(String(r));return s[o]=t?e[r]:E(e[r]),s},{}):e}async function te(e){return await new Promise(function(t){const s=document.getElementsByTagName("head")[0],r=document.createElement("script");r.setAttribute("type","text/javascript"),r.setAttribute("charset","utf-8"),r.onload=function(){s.removeChild(r),t(!0)},r.onerror=function(){s.removeChild(r),t(!1)},r.setAttribute("src",e),s.appendChild(r)})}function se(e,t="数据未能正确识别"){return typeof e=="function"?{guard:e,message:t}:{guard:e.guard,message:e.message||t}}function re(e,t=""){return!t||O(e,!0)?x(e):(x(t)+"/"+e).replace(/\/{2,}/g,"/").replace(/:\//,"://")}function x(e){return O(e,!0)?e.startsWith("http")?e:("location"in globalThis?location.protocol:"https:")+e:("location"in globalThis?location.origin:"http://127.0.0.1")+"/"+e.replace(/^\/+/,"")}function ne(e,t){const s={};return(e.match(/([^=&#?]+)=[^&#]*/g)||[]).forEach(function(r){const o=r.split("="),n=o[0],a=decodeURIComponent(o[1]||"");s[n]!==void 0?s[n]+=","+a:s[n]=a}),t!==!0?s[t]||"":s}function oe(e,t){if(t){if(t===!0)return e.replace(/\?[^#]*/,"")}else return e;const s=e.split("#"),r=s[0].split("?"),o=r[0],n=r.length>1?r[1]:"",a=s.length>1?"#"+s[1]:"",c=typeof t=="string"?[t]:Array.isArray(t)?t:[];return!c.length||!n?s[0]+a:(c.map(i=>i.replace(/([\\(){}[\]^$+\-*?|])/g,"\\$1")),(o+"?"+n.replace(new RegExp("(?:^|&)(?:"+c.join("|")+")=[^&$]+","g"),"").replace(/^&/,"")).replace(/\?$/,"")+a)}function ae(e,t,s=!1){const r=typeof t=="string"?t:Object.keys(t).map(a=>`${a}=${encodeURIComponent(t[a])}`).join("&");if(!r)return e;const o=e.split("#");s&&(o[0]=oe(o[0],(r.match(/([^=&#?]+)=[^&#]+/g)||[]).map(a=>a.replace(/=.+$/,""))));const n=o[0].indexOf("?")+1?"&":"?";return(o[0]+n+r+(o.length>1?"#"+o[1]:"")).replace(/\?&/,"?")}function ce(e){const t=e.match(/(?:\?|&)([^=]+)(?:&|$)/g);return t?t.join("").replace(/(?:\?|^&+|&+$)/g,"").replace(/&{2}/g,"&").split("&").sort():[]}class ie{constructor(t=500){y(this,"ttl");y(this,"cache");this.cache={},this.ttl=Math.max(t,0)}getKey(t,s){const r=t.replace(/#.+/,""),o=r.replace(/\?.+/g,""),n=Object.assign(ne(r,!0),s),a=ce(r),c=Object.keys(n).sort().map(i=>`${i}#${n[i]}`);return`${o}_${c.join(",")}_${a.join(",")}`}updateTTL(t){this.ttl=Math.max(t,0)}get(t){if(this.ttl===0)return null;const s=this.cache[t];return s?s.ttl<Date.now()?(delete this.cache[t],null):s.res:null}set(t,s){this.ttl!==0&&(this.cache[t]={ttl:Date.now()+this.ttl,res:s})}}class q{constructor(t){y(this,"config",{baseURL:"/",maxRetry:0,retryInterval:100,retryResolve:"network",timeout:1e4,cacheTTL:500,credentials:"same-origin",defaultTypeGuardMessage:"响应数据未能正确识别",responseRule:{ok:{resolve:"body"},failed:{resolve:"json",messageField:"message"}}});t&&this.set(t)}set(t){if(t.baseURL&&!/^\/.+/.test(t.baseURL)&&!O(t.baseURL))throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"),new Error("BaseURLError");Object.assign(this.config,t)}get(t){return this.config[t]}getFullUrl(t){return re(t,this.config.baseURL)}showMessage(t,s,r,o){this.config.messageHandler&&s&&this.config.messageHandler(t,s,r,o)}}var C=(e=>(e.UnexpectResponse="UnexpectResponse",e.Aborted="Aborted",e.Unknown="Unknown",e.NetworkError="NetworkError",e.Timeout="Timeout",e.NotSupport="NotSupport",e.URLFormatError="URLFormatError",e))(C||{});function ue(e,t,s,r){if(t.ok&&!ee(t.data)&&r){const o=se(r,s.get("defaultTypeGuardMessage"));return o.guard(t.data)||(t.code=C.UnexpectResponse,s.showMessage(!0,`${e} ${o.message}`,t.code,t.status),console.error(t.code,e,t.data),t.data=null,t.message=o.message),t}return t}class le{constructor(t,s){y(this,"agent");y(this,"config");y(this,"cache");this.config=new q(s),this.agent=t,this.cache=new ie(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)}async request(t,s){try{return await this.agent(t,this.config,s)}catch(r){return console.error("RequestError",r),{ok:!1,status:-9,code:"Unkonwn",message:String(r),headers:{},data:null}}}async guard(t,s,r){return ue(t,s,this.config,r)}setConfig(t){this.config.set(t),this.cache.updateTTL(this.config.get("cacheTTL"))}getConfig(t){return this.config.get(t)}async head(t,s){const r=Object.assign({},s||null);return r.method="HEAD",this.guard(t,await this.request(t,r),null)}async get(t,s,r){const o=Object.assign({},r||null);o.method="GET";const n=this.cache.getKey(t,o.params),a=this.cache.get(n);if(a)return this.guard(t,await a,s||null);const c=this.request(t,o);return this.cache.set(n,c),this.guard(t,await c,s||null)}async post(t,s,r,o){const n=Object.assign({},o||null);return n.method="POST",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}async del(t,s,r){const o=Object.assign({},r||null);return o.method="DELETE",this.guard(t,await this.request(t,o),s||null)}async put(t,s,r,o){const n=Object.assign({},o||null);return n.method="PUT",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}async patch(t,s,r,o){const n=Object.assign({},o||null);return n.method="PATCH",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}}async function he(e,t,s){var m;const r=Object.assign({method:"GET"},s),o=R.FormData?r.body instanceof FormData:!1,n=o&&r.method!=="POST"&&r.method!=="PUT"?"POST":r.method,a=n==="GET"||n==="HEAD"||n==="DELETE";a&&r.body!==void 0&&(console.warn("request body is invalid with method get, head, delete"),delete r.body);const c=Object.assign(o||a?{}:{"Content-Type":R.Blob&&r.body instanceof Blob?r.body.type||"application/octet-stream":"application/json;charset=utf-8"},r.headers),i=r.params||{},d={};Object.keys(i).forEach(b=>{i[b]!==void 0&&(d[b]=de(i[b]))});const u=t.getFullUrl(e),l=fe(r.body),f=r.timeout||t.get("timeout"),g=await async function(){const b=t.get("requestTransformer");if(b)return await b({headers:c,params:d,method:n,url:u,body:l})}(),p=typeof g=="string"&&g?g:u;return(m=t.get("logHandler"))==null||m({type:"ready",url:p,method:n,headers:c,timeout:f,body:l}),{url:p,method:n,body:l,params:d,headers:c,timeout:f,abort:r.abort,credentials:r.credentials||t.get("credentials")}}function de(e){return typeof e=="string"?e:Array.isArray(e)?e.join(","):e+""}function fe(e){if(e)return typeof e=="string"||e instanceof URLSearchParams||e instanceof ArrayBuffer||R.Blob&&e instanceof Blob||R.FormData&&e instanceof FormData?e:JSON.stringify(e)}const N="data",j="message";function ge(e,t,s,r,o){const n=o||r;return U(e)?je(n.ok||r.ok,e,t,s):pe(n.failed||r.failed,t,s)}const me=function(e){const t=[],s=e.failed||{resolve:"json"};switch(t.push("- 当http状态码 <200 或者 >=400 时"),s.resolve){case"body":t.push(" 将响应内容格式化为字符串并作为错误消息");break;case"json":t.push(" 将响应解析为json,并读取 "+(s.messageField||j)+" 作为错误消息");break}const r=e.ok||{resolve:"body"};switch(t.push("- 当http状态码 >=200 并且 <400 时"),r.resolve){case"body":t.push(" 将响应尝试解析为 json,并作为数据内容返回");break;case"json":t.push(" 将响应解析为 json,读取 "+(r.dataField||N)+" 作为响应数据,读取 "+(r.messageField||j)+" 作为提示消息"),r.statusField&&t.push(" 当 "+r.statusField+" 为 "+(r.statusOKValue||"空值")+" 时是成功提示,否则是错误消息"),r.ignoreMessage&&t.push(" 并忽略以下消息:"+r.ignoreMessage);break}return t.join(`
|
|
2
|
+
`)};function pe(e,t,s){const r=e||{resolve:"json",messageField:j},o={ok:!1,code:t,message:s,data:null};switch(r.resolve){case"body":o.message=P(s)||s;break;case"json":const{code:n,message:a}=ye(s,r.converter,r.statusField,r.messageField);o.code=n||t,o.message=P(s)||a;break}return o}function ye(e,t,s,r=j){if(!_(e))return{message:""};const o=A(T(e),t);return!o||!v(o)?{message:e}:{code:s?F(o,s):"",message:F(o,r)||e}}function F(e,t){const s=Array.isArray(t)?t:[t];for(const r of s)if(r in e)return be(e[r]);return""}function be(e){return e?typeof e=="string"?e:JSON.stringify(e):""}const we=/<title>([^<]+)<\/title>/i,Re=/<message>([^<]+)<\/message>/i;function P(e){const t=e.match(we);if(t)return t[1];const s=e.match(Re);return s?s[1]:""}function je(e,t,s,r){const o=e||{resolve:"body"},n={ok:!0,code:s,message:"",data:null};if(t===204||!r)return n;if(o.resolve==="body")return n.data=_(r)?A(T(r),e.converter):r,n;const a=A(T(r),e.converter);if(!a||!v(a))return n.ok=!1,n.code="ResponseFormatError",n.message="响应内容无法格式化为 Object",n;const c=o.statusField,i=o.statusOKValue||"",d=o.dataField||N,u=o.messageField||j,l=o.ignoreMessage||"";if(c&&!(c in a))return n.ok=!1,n.code="ResponseFieldMissing",n.message="响应内容找不到状态字段 "+c,n;const f=c?a[c]+"":"";return n.ok=c?f===i:!0,n.code=f||s,n.data=d===!0?a:d in a?a[d]:null,n.message=F(a,u),l&&n.message&&(Array.isArray(l)&&l.includes(n.message)||typeof l=="string"&&n.message===l)&&(n.message=""),n}function U(e){return e>=200&&e<400}function A(e,t){return t==="camelize"?k(e):t==="snakify"?E(e):e}const $e=Z("APIError");function ke(e){const t={};for(const o in e.headers)(o.startsWith("x-")||o.includes("trace")||o.includes("server")||/\b(?:id|uuid)\b/.test(o))&&(t[o]=e.headers[o]);const s=e.url.replace(/^(?:https?:)?\/*/i,"").replace(/\?.+/,""),r=e.status<0?"unknown":e.status;return{sentryError:new $e(`${s} | ${r}${e.code?` | ${e.code}`:""}`),sentryTags:{...t,status:r,method:e.method,code:e.code||"unknown",message:e.message||"empty"},sentryExtra:{url:e.url,responseBody:e.body||"empty",responseHeaders:e.headers,rawError:e.error}}}function I(e){return e.reduce((t,[s,r])=>(s&&(t[s]=r||""),t),{})}function Ee(e,t,s,r){var g;const o=e.status,n=e.method,a=I(Object.entries(e.headers||{}).map(([p,m])=>[p.toLowerCase(),m])),{ok:c,code:i,data:d,message:u}=ge(o,e.statusText,e.body,s.get("responseRule"),r==null?void 0:r.responseRule);if(!U(o)){const p=ke({url:e.url,method:e.method,status:o,code:i,message:u,body:e.body,headers:a,error:e.rawError});(s.get("errorHandler")||Oe)({url:t,method:n,status:o,code:i,message:u,headers:a,rawError:e.rawError,responseBody:e.body,...p})}if(o<0)return S({ok:!1,status:o,code:e.statusText,headers:{},message:"",data:null},`${n} ${t} ${e.statusText}`,n,t,s,r);const l={ok:c,data:d,code:i,message:u,status:o,headers:a};(g=s.get("responseHandler"))==null||g({...l},n,t);const f=c?u:u||e.statusText;return S(l,f,n,t,s,r)}function S(e,t,s,r,o,n){const a=o.get("message"),c=a===!1||(n==null?void 0:n.message)===!1?!1:(n==null?void 0:n.message)||a;if(c!==!1){const i=typeof c=="function"?c(e,s,r,t):t;i instanceof Error?o.showMessage(!0,i.message,e.code,e.status):i&&typeof i=="object"&&"message"in i?o.showMessage(!1,i.message,e.code,e.status):o.showMessage(!e.ok,i,e.code,e.status)}return e}function Oe(e){const t={};for(const s in e)s.startsWith("sentry")||(t[s]=e[s]);console.error("RequestError",t)}async function K(e,t,s,r,o){const n=o||0,a=Math.max(0,Math.min(10,(r==null?void 0:r.maxRetry)??s.get("maxRetry")??0)),c=(r==null?void 0:r.retryResolve)??s.get("retryResolve"),i=s.get("logHandler")||V;i({type:"prepear",url:t,method:(r==null?void 0:r.method)||"GET",retry:n,maxRetry:a,message:n===0?"start":`retry ${n}/${a} start`,headers:r==null?void 0:r.headers,options:r});const d=Date.now(),u=await e(t,s,r),l=u.status,f=Date.now()-d,g=`[cost ${f}][${l}] ${l<0?u.body:""}`;i({type:"finished",url:t,method:u.method,retry:n,maxRetry:a,message:n===0?`finish ${g}`:`retry ${n}/${a} finish ${g}`,response:u,headers:u.headers,cost:f});const p=U(l);if(!a||c==="network"&&l>0||c==="status"&&p||Array.isArray(c)&&!c.includes(l)||typeof c=="function"&&c(u,n)!==!0||n>=a)return u;const m=(r==null?void 0:r.retryInterval)??s.get("retryInterval")??100;return await W(Math.max(100,m==="2EB"?Math.pow(2,n)*100:typeof m=="function"?m(n+1)||0:m)),await K(e,t,s,r,n+1)}exports.Ie=te;exports.NetRequestHandler=le;exports.RequestGlobalConfig=q;exports.RequestInteralError=C;exports.Zt=ae;exports.convertOptions=he;exports.fromEntries=I;exports.getResponseRulesDescription=me;exports.handleResponse=Ee;exports.lt=R;exports.retryRequest=K;exports.tt=O;
|