@seayoo-web/request 2.0.8 → 2.0.10
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/dist/index.cjs +2 -2
- package/dist/index.js +56 -55
- package/dist/node.cjs +1 -1
- package/dist/node.js +28 -28
- package/dist/{request.fetch-DfFYJr1l.js → request.fetch-DR4KeRZ0.js} +14 -14
- package/dist/request.fetch-K4MJcDmZ.cjs +1 -0
- package/dist/{retry-CaBI_Pfh.js → type-CRH6iqXf.js} +162 -154
- package/dist/type-CrucJEBR.cjs +2 -0
- package/dist/wx.cjs +1 -1
- package/dist/wx.js +29 -29
- package/package.json +2 -2
- package/types/inc/request.fetch.d.ts +1 -1
- package/types/inc/request.node.d.ts +1 -1
- package/types/inc/request.wx.d.ts +1 -1
- package/types/inc/request.xhr.d.ts +1 -1
- package/types/inc/type.d.ts +12 -0
- package/types/inc/utils.d.ts +4 -0
- package/types/index.d.ts +1 -0
- package/dist/request.fetch-Dc_ujBlU.cjs +0 -1
- package/dist/retry-DrW-Az-k.cjs +0 -2
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
const
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
Blob: "Blob" in
|
|
9
|
-
|
|
10
|
-
TextDecoder: "TextDecoder" in
|
|
11
|
-
AbortController: "AbortController" in
|
|
1
|
+
var K = Object.defineProperty;
|
|
2
|
+
var N = (e, t, s) => t in e ? K(e, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : e[t] = s;
|
|
3
|
+
var p = (e, t, s) => N(e, typeof t != "symbol" ? t + "" : t, s);
|
|
4
|
+
const h = typeof globalThis < "u" ? globalThis : typeof global < "u" ? global : typeof self < "u" ? self : typeof window < "u" ? window : {}, O = {
|
|
5
|
+
wx: "wx" in h && D(h.wx),
|
|
6
|
+
fetch: "fetch" in h && w(h.fetch),
|
|
7
|
+
window: "window" in h && D(h.window),
|
|
8
|
+
Blob: "Blob" in h && w(h.Blob),
|
|
9
|
+
FormData: "FormData" in h && w(h.FormData),
|
|
10
|
+
TextDecoder: "TextDecoder" in h && w(h.TextDecoder),
|
|
11
|
+
AbortController: "AbortController" in h && w(h.AbortController)
|
|
12
12
|
};
|
|
13
|
-
function
|
|
14
|
-
return e
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
13
|
+
function w(e) {
|
|
14
|
+
return typeof e == "function";
|
|
15
|
+
}
|
|
16
|
+
function D(e) {
|
|
17
|
+
return typeof e == "object" && e !== null;
|
|
18
18
|
}
|
|
19
|
-
function
|
|
19
|
+
function I(e) {
|
|
20
20
|
return new Promise((t) => setTimeout(t, Math.max(0, e)));
|
|
21
21
|
}
|
|
22
|
-
function
|
|
22
|
+
function G() {
|
|
23
23
|
}
|
|
24
|
-
function
|
|
24
|
+
function J(e) {
|
|
25
25
|
const t = e || "CustomError";
|
|
26
26
|
return class extends Error {
|
|
27
27
|
constructor(s) {
|
|
@@ -33,33 +33,33 @@ function G(e) {
|
|
|
33
33
|
}
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
|
-
const
|
|
37
|
-
function
|
|
38
|
-
return t ?
|
|
36
|
+
const V = /^(?:https?:)?\/\/.+$/i, W = /^https?:\/\/.+$/i, z = /^\{[\d\D]*\}$/, Y = /^\[[\d\D]*\]$/;
|
|
37
|
+
function A(e, t = !1) {
|
|
38
|
+
return t ? V.test(e) : W.test(e);
|
|
39
39
|
}
|
|
40
|
-
function
|
|
40
|
+
function Z(e) {
|
|
41
41
|
return e == null;
|
|
42
42
|
}
|
|
43
|
-
function
|
|
43
|
+
function k(e, ...t) {
|
|
44
44
|
if (!e || typeof e != "object") return !1;
|
|
45
45
|
const s = Object.getPrototypeOf(e);
|
|
46
46
|
return s !== Object.prototype && s !== null ? !1 : t.every((r) => r in e);
|
|
47
47
|
}
|
|
48
|
-
function
|
|
49
|
-
return
|
|
48
|
+
function B(e) {
|
|
49
|
+
return z.test(e) || Y.test(e);
|
|
50
50
|
}
|
|
51
|
-
function
|
|
51
|
+
function $(e) {
|
|
52
52
|
return e ? e[0].toLowerCase() + e.slice(1) : "";
|
|
53
53
|
}
|
|
54
|
-
const
|
|
55
|
-
function
|
|
56
|
-
const t =
|
|
57
|
-
return
|
|
54
|
+
const _ = /_\w*/, H = /-\w*/;
|
|
55
|
+
function U(e) {
|
|
56
|
+
const t = _.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;
|
|
57
|
+
return $(t);
|
|
58
58
|
}
|
|
59
59
|
function M(e) {
|
|
60
|
-
return
|
|
60
|
+
return _.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()}`);
|
|
61
61
|
}
|
|
62
|
-
function
|
|
62
|
+
function v(e, t) {
|
|
63
63
|
try {
|
|
64
64
|
const s = JSON.parse(e);
|
|
65
65
|
return t ? t(s) ? s : null : s;
|
|
@@ -69,24 +69,24 @@ function k(e, t) {
|
|
|
69
69
|
}
|
|
70
70
|
"" + Math.random().toString(32).slice(2);
|
|
71
71
|
function R(e, t = !1) {
|
|
72
|
-
return typeof e == "string" ?
|
|
72
|
+
return typeof e == "string" ? U(e) : Array.isArray(e) ? e.map((s) => !t || typeof s == "string" ? R(s) : typeof s == "object" && s ? R(s, !0) : s) : k(e) ? Object.keys(e).reduce(
|
|
73
73
|
(s, r) => {
|
|
74
|
-
const a =
|
|
74
|
+
const a = U(String(r)), n = a.charAt(0).toLowerCase() + a.slice(1);
|
|
75
75
|
return s[n] = t ? e[r] : R(e[r]), s;
|
|
76
76
|
},
|
|
77
77
|
{}
|
|
78
78
|
) : e;
|
|
79
79
|
}
|
|
80
|
-
function
|
|
81
|
-
return typeof e == "string" ? M(e) : Array.isArray(e) ? e.map((s) => !t || typeof s == "string" ?
|
|
80
|
+
function E(e, t = !1) {
|
|
81
|
+
return typeof e == "string" ? M(e) : Array.isArray(e) ? e.map((s) => !t || typeof s == "string" ? E(s) : typeof s == "object" && s ? E(s, t) : s) : k(e) ? Object.keys(e).reduce(
|
|
82
82
|
(s, r) => {
|
|
83
83
|
const a = M(String(r));
|
|
84
|
-
return s[a] = t ? e[r] :
|
|
84
|
+
return s[a] = t ? e[r] : E(e[r]), s;
|
|
85
85
|
},
|
|
86
86
|
{}
|
|
87
87
|
) : e;
|
|
88
88
|
}
|
|
89
|
-
async function
|
|
89
|
+
async function Re(e) {
|
|
90
90
|
return await new Promise(function(t) {
|
|
91
91
|
const s = document.getElementsByTagName("head")[0], r = document.createElement("script");
|
|
92
92
|
r.setAttribute("type", "text/javascript"), r.setAttribute("charset", "utf-8"), r.onload = function() {
|
|
@@ -96,7 +96,7 @@ async function we(e) {
|
|
|
96
96
|
}, r.setAttribute("src", e), s.appendChild(r);
|
|
97
97
|
});
|
|
98
98
|
}
|
|
99
|
-
function
|
|
99
|
+
function Q(e, t = "数据未能正确识别") {
|
|
100
100
|
return typeof e == "function" ? {
|
|
101
101
|
guard: e,
|
|
102
102
|
message: t
|
|
@@ -105,20 +105,20 @@ function Z(e, t = "数据未能正确识别") {
|
|
|
105
105
|
message: e.message || t
|
|
106
106
|
};
|
|
107
107
|
}
|
|
108
|
-
function
|
|
109
|
-
return !t ||
|
|
108
|
+
function X(e, t = "") {
|
|
109
|
+
return !t || A(e, !0) ? L(e) : (L(t) + "/" + e).replace(/\/{2,}/g, "/").replace(/:\//, "://");
|
|
110
110
|
}
|
|
111
|
-
function
|
|
112
|
-
return
|
|
111
|
+
function L(e) {
|
|
112
|
+
return A(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(/^\/+/, "");
|
|
113
113
|
}
|
|
114
|
-
function
|
|
114
|
+
function ee(e, t) {
|
|
115
115
|
const s = {};
|
|
116
116
|
return (e.match(/([^=&#?]+)=[^&#]*/g) || []).forEach(function(r) {
|
|
117
117
|
const a = r.split("="), n = a[0], o = decodeURIComponent(a[1] || "");
|
|
118
118
|
s[n] !== void 0 ? s[n] += "," + o : s[n] = o;
|
|
119
119
|
}), t !== !0 ? s[t] || "" : s;
|
|
120
120
|
}
|
|
121
|
-
function
|
|
121
|
+
function te(e, t) {
|
|
122
122
|
if (t) {
|
|
123
123
|
if (t === !0)
|
|
124
124
|
return e.replace(/\?[^#]*/, "");
|
|
@@ -126,30 +126,30 @@ function ee(e, t) {
|
|
|
126
126
|
const s = e.split("#"), r = s[0].split("?"), a = r[0], n = r.length > 1 ? r[1] : "", o = s.length > 1 ? "#" + s[1] : "", c = typeof t == "string" ? [t] : Array.isArray(t) ? t : [];
|
|
127
127
|
return !c.length || !n ? s[0] + o : (c.map((i) => i.replace(/([\\(){}[\]^$+\-*?|])/g, "\\$1")), (a + "?" + n.replace(new RegExp("(?:^|&)(?:" + c.join("|") + ")=[^&$]+", "g"), "").replace(/^&/, "")).replace(/\?$/, "") + o);
|
|
128
128
|
}
|
|
129
|
-
function
|
|
129
|
+
function Ee(e, t, s = !1) {
|
|
130
130
|
const r = typeof t == "string" ? t : Object.keys(t).map((o) => `${o}=${encodeURIComponent(t[o])}`).join("&");
|
|
131
131
|
if (!r)
|
|
132
132
|
return e;
|
|
133
133
|
const a = e.split("#");
|
|
134
|
-
s && (a[0] =
|
|
134
|
+
s && (a[0] = te(
|
|
135
135
|
a[0],
|
|
136
136
|
(r.match(/([^=&#?]+)=[^&#]+/g) || []).map((o) => o.replace(/=.+$/, ""))
|
|
137
137
|
));
|
|
138
138
|
const n = a[0].indexOf("?") + 1 ? "&" : "?";
|
|
139
139
|
return (a[0] + n + r + (a.length > 1 ? "#" + a[1] : "")).replace(/\?&/, "?");
|
|
140
140
|
}
|
|
141
|
-
function
|
|
141
|
+
function se(e) {
|
|
142
142
|
const t = e.match(/(?:\?|&)([^=]+)(?:&|$)/g);
|
|
143
143
|
return t ? t.join("").replace(/(?:\?|^&+|&+$)/g, "").replace(/&{2}/g, "&").split("&").sort() : [];
|
|
144
144
|
}
|
|
145
|
-
class
|
|
145
|
+
class re {
|
|
146
146
|
constructor(t = 500) {
|
|
147
|
-
|
|
148
|
-
|
|
147
|
+
p(this, "ttl");
|
|
148
|
+
p(this, "cache");
|
|
149
149
|
this.cache = {}, this.ttl = Math.max(t, 0);
|
|
150
150
|
}
|
|
151
151
|
getKey(t, s) {
|
|
152
|
-
const r = t.replace(/#.+/, ""), a = r.replace(/\?.+/g, ""), n = Object.assign(
|
|
152
|
+
const r = t.replace(/#.+/, ""), a = r.replace(/\?.+/g, ""), n = Object.assign(ee(r, !0), s), o = se(r), c = Object.keys(n).sort().map((i) => `${i}#${n[i]}`);
|
|
153
153
|
return `${a}_${c.join(",")}_${o.join(",")}`;
|
|
154
154
|
}
|
|
155
155
|
updateTTL(t) {
|
|
@@ -168,10 +168,10 @@ class se {
|
|
|
168
168
|
});
|
|
169
169
|
}
|
|
170
170
|
}
|
|
171
|
-
class
|
|
171
|
+
class ne {
|
|
172
172
|
constructor(t) {
|
|
173
173
|
// 保存的配置需要部分字段强制设置默认值
|
|
174
|
-
|
|
174
|
+
p(this, "config", {
|
|
175
175
|
baseURL: "",
|
|
176
176
|
maxRetry: 0,
|
|
177
177
|
retryInterval: 1e3,
|
|
@@ -192,7 +192,7 @@ class re {
|
|
|
192
192
|
t && this.set(t);
|
|
193
193
|
}
|
|
194
194
|
set(t) {
|
|
195
|
-
if (t.baseURL && !/^\/.+/.test(t.baseURL) && !
|
|
195
|
+
if (t.baseURL && !/^\/.+/.test(t.baseURL) && !A(t.baseURL))
|
|
196
196
|
throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"), new Error("BaseURLError");
|
|
197
197
|
Object.assign(this.config, t);
|
|
198
198
|
}
|
|
@@ -201,27 +201,27 @@ class re {
|
|
|
201
201
|
}
|
|
202
202
|
/** 基于 baseURL 返回完整的 url 地址 */
|
|
203
203
|
getFullUrl(t) {
|
|
204
|
-
return
|
|
204
|
+
return X(t, this.config.baseURL);
|
|
205
205
|
}
|
|
206
206
|
/** 提示消息 */
|
|
207
207
|
showMessage(t, s, r, a) {
|
|
208
208
|
this.config.messageHandler && s && this.config.messageHandler(t, s, r, a);
|
|
209
209
|
}
|
|
210
210
|
}
|
|
211
|
-
const
|
|
212
|
-
function
|
|
213
|
-
if (t.ok && !
|
|
214
|
-
const a =
|
|
215
|
-
return a.guard(t.data) || (s.showMessage(!0, `${e} ${a.message}`, t.code, t.status), console.error(
|
|
211
|
+
const x = "UnexpectResponse";
|
|
212
|
+
function ae(e, t, s, r) {
|
|
213
|
+
if (t.ok && !Z(t.data) && r) {
|
|
214
|
+
const a = Q(r, "响应数据未能正确识别");
|
|
215
|
+
return a.guard(t.data) || (s.showMessage(!0, `${e} ${a.message}`, t.code, t.status), console.error(x, e, t.data), t.data = null, t.code = x, t.message = a.message), t;
|
|
216
216
|
}
|
|
217
217
|
return t;
|
|
218
218
|
}
|
|
219
|
-
class
|
|
219
|
+
class Oe {
|
|
220
220
|
constructor(t, s) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
this.config = new
|
|
221
|
+
p(this, "agent");
|
|
222
|
+
p(this, "config");
|
|
223
|
+
p(this, "cache");
|
|
224
|
+
this.config = new ne(s), this.agent = t, this.cache = new re(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);
|
|
225
225
|
}
|
|
226
226
|
/**
|
|
227
227
|
* 执行网络请求
|
|
@@ -244,7 +244,7 @@ class Re {
|
|
|
244
244
|
* 检查响应的数据类型
|
|
245
245
|
*/
|
|
246
246
|
async guard(t, s, r) {
|
|
247
|
-
return
|
|
247
|
+
return ae(t, s, this.config, r);
|
|
248
248
|
}
|
|
249
249
|
/**
|
|
250
250
|
* 修改默认请求配置: baesURL / timeout / credentials / errorHandler / messageHandler / responseHandler / logHandler / responseRule
|
|
@@ -291,55 +291,55 @@ class Re {
|
|
|
291
291
|
return n.method = "PATCH", n.body = s || {}, this.guard(t, await this.request(t, n), r || null);
|
|
292
292
|
}
|
|
293
293
|
}
|
|
294
|
-
async function
|
|
295
|
-
var
|
|
296
|
-
const r = Object.assign({ method: "GET" }, s), a =
|
|
294
|
+
async function ke(e, t, s) {
|
|
295
|
+
var m;
|
|
296
|
+
const r = Object.assign({ method: "GET" }, s), a = O.FormData ? r.body instanceof FormData : !1, n = a && r.method !== "POST" && r.method !== "PUT" ? "POST" : r.method, o = n === "GET" || n === "HEAD" || n === "DELETE";
|
|
297
297
|
o && r.body !== void 0 && (console.warn("request body is invalid with method get, head, delete"), delete r.body);
|
|
298
298
|
const c = Object.assign(
|
|
299
299
|
a || o ? {} : {
|
|
300
|
-
"Content-Type":
|
|
300
|
+
"Content-Type": O.Blob && r.body instanceof Blob ? r.body.type || "application/octet-stream" : "application/json;charset=utf-8"
|
|
301
301
|
},
|
|
302
302
|
r.headers
|
|
303
|
-
), i = r.params || {},
|
|
303
|
+
), i = r.params || {}, f = {};
|
|
304
304
|
Object.keys(i).forEach((b) => {
|
|
305
|
-
i[b] !== void 0 && (
|
|
305
|
+
i[b] !== void 0 && (f[b] = oe(i[b]));
|
|
306
306
|
});
|
|
307
|
-
const u = t.getFullUrl(e), l =
|
|
307
|
+
const u = t.getFullUrl(e), l = ce(r.body), d = r.timeout || t.get("timeout"), g = await async function() {
|
|
308
308
|
const b = t.get("requestTransformer");
|
|
309
309
|
if (b)
|
|
310
|
-
return await b({ headers: c, params:
|
|
311
|
-
}(),
|
|
312
|
-
return (
|
|
313
|
-
url:
|
|
310
|
+
return await b({ headers: c, params: f, method: n, url: u, body: l });
|
|
311
|
+
}(), y = typeof g == "string" && g ? g : u;
|
|
312
|
+
return (m = t.get("logHandler")) == null || m({ type: "ready", url: y, method: n, headers: c, timeout: d, body: l }), {
|
|
313
|
+
url: y,
|
|
314
314
|
method: n,
|
|
315
315
|
body: l,
|
|
316
|
-
params:
|
|
316
|
+
params: f,
|
|
317
317
|
headers: c,
|
|
318
318
|
timeout: d,
|
|
319
319
|
abort: r.abort,
|
|
320
320
|
credentials: r.credentials || t.get("credentials")
|
|
321
321
|
};
|
|
322
322
|
}
|
|
323
|
-
function
|
|
323
|
+
function oe(e) {
|
|
324
324
|
return typeof e == "string" ? e : Array.isArray(e) ? e.join(",") : e + "";
|
|
325
325
|
}
|
|
326
|
-
function
|
|
326
|
+
function ce(e) {
|
|
327
327
|
if (e)
|
|
328
|
-
return typeof e == "string" || e instanceof URLSearchParams || e instanceof ArrayBuffer ||
|
|
328
|
+
return typeof e == "string" || e instanceof URLSearchParams || e instanceof ArrayBuffer || O.Blob && e instanceof Blob || O.FormData && e instanceof FormData ? e : JSON.stringify(e);
|
|
329
329
|
}
|
|
330
|
-
const
|
|
331
|
-
function
|
|
330
|
+
const q = "data", j = "message";
|
|
331
|
+
function ie(e, t, s, r, a) {
|
|
332
332
|
const n = a || r;
|
|
333
|
-
return
|
|
333
|
+
return C(e) ? ge(n.ok || r.ok, e, t, s) : ue(n.failed || r.failed, t, s);
|
|
334
334
|
}
|
|
335
|
-
const
|
|
335
|
+
const ve = function(e) {
|
|
336
336
|
const t = [], s = e.failed || { resolve: "json" };
|
|
337
337
|
switch (t.push("- 当http状态码 <200 或者 >=400 时"), s.resolve) {
|
|
338
338
|
case "body":
|
|
339
339
|
t.push(" 将响应内容格式化为字符串并作为错误消息");
|
|
340
340
|
break;
|
|
341
341
|
case "json":
|
|
342
|
-
t.push(" 将响应解析为json,并读取 " + (s.messageField ||
|
|
342
|
+
t.push(" 将响应解析为json,并读取 " + (s.messageField || j) + " 作为错误消息");
|
|
343
343
|
break;
|
|
344
344
|
}
|
|
345
345
|
const r = e.ok || { resolve: "body" };
|
|
@@ -349,7 +349,7 @@ const Ee = function(e) {
|
|
|
349
349
|
break;
|
|
350
350
|
case "json":
|
|
351
351
|
t.push(
|
|
352
|
-
" 将响应解析为 json,读取 " + (r.dataField ||
|
|
352
|
+
" 将响应解析为 json,读取 " + (r.dataField || q) + " 作为响应数据,读取 " + (r.messageField || j) + " 作为提示消息"
|
|
353
353
|
), r.statusField && t.push(
|
|
354
354
|
" 当 " + r.statusField + " 为 " + (r.statusOKValue || "空值") + " 时是成功提示,否则是错误消息"
|
|
355
355
|
), r.ignoreMessage && t.push(" 并忽略以下消息:" + r.ignoreMessage);
|
|
@@ -358,8 +358,8 @@ const Ee = function(e) {
|
|
|
358
358
|
return t.join(`
|
|
359
359
|
`);
|
|
360
360
|
};
|
|
361
|
-
function
|
|
362
|
-
const r = e || { resolve: "json", messageField:
|
|
361
|
+
function ue(e, t, s) {
|
|
362
|
+
const r = e || { resolve: "json", messageField: j }, a = {
|
|
363
363
|
ok: !1,
|
|
364
364
|
code: t,
|
|
365
365
|
message: s,
|
|
@@ -367,43 +367,43 @@ function ie(e, t, s) {
|
|
|
367
367
|
};
|
|
368
368
|
switch (r.resolve) {
|
|
369
369
|
case "body":
|
|
370
|
-
a.message =
|
|
370
|
+
a.message = P(s) || s;
|
|
371
371
|
break;
|
|
372
372
|
case "json":
|
|
373
|
-
const { code: n, message: o } =
|
|
374
|
-
a.code = n || t, a.message =
|
|
373
|
+
const { code: n, message: o } = le(s, r.converter, r.statusField, r.messageField);
|
|
374
|
+
a.code = n || t, a.message = P(s) || o;
|
|
375
375
|
break;
|
|
376
376
|
}
|
|
377
377
|
return a;
|
|
378
378
|
}
|
|
379
|
-
function
|
|
380
|
-
if (!
|
|
379
|
+
function le(e, t, s, r = j) {
|
|
380
|
+
if (!B(e))
|
|
381
381
|
return { message: "" };
|
|
382
|
-
const a =
|
|
383
|
-
return !a || !
|
|
384
|
-
code: s ?
|
|
385
|
-
message:
|
|
382
|
+
const a = F(v(e), t);
|
|
383
|
+
return !a || !k(a) ? { message: e } : {
|
|
384
|
+
code: s ? T(a, s) : "",
|
|
385
|
+
message: T(a, r) || e
|
|
386
386
|
};
|
|
387
387
|
}
|
|
388
|
-
function
|
|
388
|
+
function T(e, t) {
|
|
389
389
|
const s = Array.isArray(t) ? t : [t];
|
|
390
390
|
for (const r of s)
|
|
391
391
|
if (r in e)
|
|
392
|
-
return
|
|
392
|
+
return he(e[r]);
|
|
393
393
|
return "";
|
|
394
394
|
}
|
|
395
|
-
function
|
|
395
|
+
function he(e) {
|
|
396
396
|
return e ? typeof e == "string" ? e : JSON.stringify(e) : "";
|
|
397
397
|
}
|
|
398
|
-
const
|
|
399
|
-
function
|
|
400
|
-
const t = e.match(
|
|
398
|
+
const fe = /<title>([^<]+)<\/title>/i, de = /<message>([^<]+)<\/message>/i;
|
|
399
|
+
function P(e) {
|
|
400
|
+
const t = e.match(fe);
|
|
401
401
|
if (t)
|
|
402
402
|
return t[1];
|
|
403
403
|
const s = e.match(de);
|
|
404
404
|
return s ? s[1] : "";
|
|
405
405
|
}
|
|
406
|
-
function
|
|
406
|
+
function ge(e, t, s, r) {
|
|
407
407
|
const a = e || { resolve: "body" }, n = {
|
|
408
408
|
ok: !0,
|
|
409
409
|
code: s,
|
|
@@ -413,30 +413,30 @@ function fe(e, t, s, r) {
|
|
|
413
413
|
if (t === 204 || !r)
|
|
414
414
|
return n;
|
|
415
415
|
if (a.resolve === "body")
|
|
416
|
-
return n.data =
|
|
417
|
-
const o =
|
|
418
|
-
if (!o || !
|
|
416
|
+
return n.data = B(r) ? F(v(r), e.converter) : r, n;
|
|
417
|
+
const o = F(v(r), e.converter);
|
|
418
|
+
if (!o || !k(o))
|
|
419
419
|
return n.ok = !1, n.code = "ResponseFormatError", n.message = "响应内容无法格式化为 Object", n;
|
|
420
|
-
const c = a.statusField, i = a.statusOKValue || "",
|
|
420
|
+
const c = a.statusField, i = a.statusOKValue || "", f = a.dataField || q, u = a.messageField || j, l = a.ignoreMessage || "";
|
|
421
421
|
if (c && !(c in o))
|
|
422
422
|
return n.ok = !1, n.code = "ResponseFieldMissing", n.message = "响应内容找不到状态字段 " + c, n;
|
|
423
423
|
const d = c ? o[c] + "" : "";
|
|
424
|
-
return n.ok = c ? d === i : !0, n.code = d || s, n.data =
|
|
424
|
+
return n.ok = c ? d === i : !0, n.code = d || s, n.data = f === !0 ? o : f in o ? o[f] : null, n.message = T(o, u), l && n.message && (Array.isArray(l) && l.includes(n.message) || typeof l == "string" && n.message === l) && (n.message = ""), n;
|
|
425
425
|
}
|
|
426
|
-
function
|
|
426
|
+
function C(e) {
|
|
427
427
|
return e >= 200 && e < 400;
|
|
428
428
|
}
|
|
429
|
-
function
|
|
430
|
-
return t === "camelize" ? R(e) : t === "snakify" ?
|
|
429
|
+
function F(e, t) {
|
|
430
|
+
return t === "camelize" ? R(e) : t === "snakify" ? E(e) : e;
|
|
431
431
|
}
|
|
432
|
-
const
|
|
433
|
-
function
|
|
432
|
+
const me = J("APIError");
|
|
433
|
+
function ye(e) {
|
|
434
434
|
const t = {};
|
|
435
435
|
for (const a in e.headers)
|
|
436
436
|
(a.startsWith("x-") || a.includes("trace") || a.includes("server") || /\b(?:id|uuid)\b/.test(a)) && (t[a] = e.headers[a]);
|
|
437
437
|
const s = e.url.replace(/^(?:https?:)?\/*/i, "").replace(/\?.+/, ""), r = e.status < 0 ? "unknown" : e.status;
|
|
438
438
|
return {
|
|
439
|
-
sentryError: new
|
|
439
|
+
sentryError: new me(`${s} | ${r}${e.code ? ` | ${e.code}` : ""}`),
|
|
440
440
|
sentryTags: {
|
|
441
441
|
...t,
|
|
442
442
|
status: r,
|
|
@@ -452,19 +452,25 @@ function me(e) {
|
|
|
452
452
|
}
|
|
453
453
|
};
|
|
454
454
|
}
|
|
455
|
-
function
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
)
|
|
455
|
+
function pe(e) {
|
|
456
|
+
return e.reduce(
|
|
457
|
+
(t, [s, r]) => (s && (t[s] = r || ""), t),
|
|
458
|
+
{}
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
function Te(e, t, s, r) {
|
|
462
|
+
var g;
|
|
463
|
+
const a = e.status, n = e.method, o = pe(
|
|
464
|
+
Object.entries(e.headers || {}).map(([y, m]) => [y.toLowerCase(), m])
|
|
465
|
+
), { ok: c, code: i, data: f, message: u } = ie(
|
|
460
466
|
a,
|
|
461
467
|
e.statusText,
|
|
462
468
|
e.body,
|
|
463
469
|
s.get("responseRule"),
|
|
464
470
|
r == null ? void 0 : r.responseRule
|
|
465
471
|
);
|
|
466
|
-
if (!
|
|
467
|
-
const
|
|
472
|
+
if (!C(a)) {
|
|
473
|
+
const y = ye({
|
|
468
474
|
url: e.url,
|
|
469
475
|
method: e.method,
|
|
470
476
|
status: a,
|
|
@@ -474,7 +480,7 @@ function Oe(e, t, s, r) {
|
|
|
474
480
|
headers: o,
|
|
475
481
|
error: e.rawError
|
|
476
482
|
});
|
|
477
|
-
(s.get("errorHandler") ||
|
|
483
|
+
(s.get("errorHandler") || be)({
|
|
478
484
|
url: t,
|
|
479
485
|
method: n,
|
|
480
486
|
status: a,
|
|
@@ -483,11 +489,11 @@ function Oe(e, t, s, r) {
|
|
|
483
489
|
headers: o,
|
|
484
490
|
rawError: e.rawError,
|
|
485
491
|
responseBody: e.body,
|
|
486
|
-
...
|
|
492
|
+
...y
|
|
487
493
|
});
|
|
488
494
|
}
|
|
489
495
|
if (a < 0)
|
|
490
|
-
return
|
|
496
|
+
return S(
|
|
491
497
|
{ ok: !1, status: a, code: e.statusText, headers: {}, message: "", data: null },
|
|
492
498
|
`${n} ${t} ${e.statusText}`,
|
|
493
499
|
n,
|
|
@@ -495,12 +501,12 @@ function Oe(e, t, s, r) {
|
|
|
495
501
|
s,
|
|
496
502
|
r
|
|
497
503
|
);
|
|
498
|
-
const l = { ok: c, data:
|
|
499
|
-
(
|
|
504
|
+
const l = { ok: c, data: f, code: i, message: u, status: a, headers: o };
|
|
505
|
+
(g = s.get("responseHandler")) == null || g({ ...l }, n, t);
|
|
500
506
|
const d = c ? u : u || e.statusText;
|
|
501
|
-
return
|
|
507
|
+
return S(l, d, n, t, s, r);
|
|
502
508
|
}
|
|
503
|
-
function
|
|
509
|
+
function S(e, t, s, r, a, n) {
|
|
504
510
|
const o = a.get("message"), c = o === !1 || (n == null ? void 0 : n.message) === !1 ? !1 : (n == null ? void 0 : n.message) || o;
|
|
505
511
|
if (c !== !1) {
|
|
506
512
|
const i = typeof c == "function" ? c(e, s, r, t) : t;
|
|
@@ -508,14 +514,14 @@ function B(e, t, s, r, a, n) {
|
|
|
508
514
|
}
|
|
509
515
|
return e;
|
|
510
516
|
}
|
|
511
|
-
function
|
|
517
|
+
function be(e) {
|
|
512
518
|
const t = {};
|
|
513
519
|
for (const s in e)
|
|
514
520
|
s.startsWith("sentry") || (t[s] = e[s]);
|
|
515
521
|
console.error("RequestError", t);
|
|
516
522
|
}
|
|
517
|
-
async function
|
|
518
|
-
const n = a || 0, o = 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") ||
|
|
523
|
+
async function we(e, t, s, r, a) {
|
|
524
|
+
const n = a || 0, o = 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") || G;
|
|
519
525
|
i({
|
|
520
526
|
type: "prepear",
|
|
521
527
|
url: t,
|
|
@@ -526,39 +532,41 @@ async function pe(e, t, s, r, a) {
|
|
|
526
532
|
headers: r == null ? void 0 : r.headers,
|
|
527
533
|
options: r
|
|
528
534
|
});
|
|
529
|
-
const
|
|
535
|
+
const f = Date.now(), u = await e(t, s, r), l = u.status, d = Date.now() - f, g = `[cost ${d}][${l}] ${l < 0 ? u.body : ""}`;
|
|
530
536
|
i({
|
|
531
537
|
type: "finished",
|
|
532
538
|
url: t,
|
|
533
539
|
method: u.method,
|
|
534
540
|
retry: n,
|
|
535
541
|
maxRetry: o,
|
|
536
|
-
message: n === 0 ? `finish ${
|
|
542
|
+
message: n === 0 ? `finish ${g}` : `retry ${n}/${o} finish ${g}`,
|
|
537
543
|
response: u,
|
|
538
544
|
headers: u.headers,
|
|
539
545
|
cost: d
|
|
540
546
|
});
|
|
541
|
-
const
|
|
542
|
-
if (!o || c === "network" && l > 0 || c === "status" &&
|
|
547
|
+
const y = C(l);
|
|
548
|
+
if (!o || c === "network" && l > 0 || c === "status" && y || Array.isArray(c) && !c.includes(l) || typeof c == "function" && c(u, n) !== !0 || n >= o)
|
|
543
549
|
return u;
|
|
544
|
-
const
|
|
545
|
-
return await
|
|
550
|
+
const m = (r == null ? void 0 : r.retryInterval) ?? s.get("retryInterval") ?? 1e3;
|
|
551
|
+
return await I(
|
|
546
552
|
Math.max(
|
|
547
553
|
100,
|
|
548
|
-
|
|
554
|
+
m === "2EB" ? Math.pow(2, n) * 1e3 : typeof m == "function" ? m(n + 1) || 0 : m
|
|
549
555
|
)
|
|
550
|
-
), await
|
|
556
|
+
), await we(e, t, s, r, n + 1);
|
|
551
557
|
}
|
|
558
|
+
var je = /* @__PURE__ */ ((e) => (e.Aborted = "Aborted", e.Unknown = "Unknown", e.Timeout = "Timeout", e.NotSupport = "NotSupport", e.URLFormatError = "URLFormatError", e))(je || {});
|
|
552
559
|
export {
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
560
|
+
Ee as D,
|
|
561
|
+
O as K,
|
|
562
|
+
Re as M,
|
|
563
|
+
Oe as N,
|
|
564
|
+
ne as R,
|
|
565
|
+
A as Y,
|
|
566
|
+
je as a,
|
|
567
|
+
ke as c,
|
|
568
|
+
pe as f,
|
|
569
|
+
ve as g,
|
|
570
|
+
Te as h,
|
|
571
|
+
we as r
|
|
564
572
|
};
|