@seayoo-web/request 3.5.2 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +58 -56
- package/dist/index.js +135 -121
- package/dist/node.js +58 -58
- package/dist/request.fetch-BNCxK4Wq.js +54 -0
- package/dist/retry-DRbCcUt8.js +646 -0
- package/dist/wx.js +58 -49
- package/package.json +1 -1
- package/types/inc/cache.d.ts +2 -2
- package/types/inc/config.d.ts +5 -5
- package/types/inc/guard.d.ts +3 -3
- package/types/inc/jsonp.d.ts +3 -2
- package/types/inc/main.d.ts +51 -34
- package/types/inc/option.d.ts +6 -7
- package/types/inc/request.xhr.d.ts +4 -4
- package/types/inc/response.d.ts +8 -3
- package/types/inc/retry.d.ts +3 -3
- package/types/inc/rule.d.ts +5 -5
- package/types/inc/sentry.d.ts +4 -4
- package/types/inc/type.d.ts +55 -50
- package/types/inc/utils.d.ts +6 -2
- package/types/index.d.ts +37 -25
- package/types/node.d.ts +26 -24
- package/types/wx.d.ts +22 -20
- package/dist/request.fetch-Jj3A4PnG.js +0 -48
- package/dist/retry-Cxkf8ke1.js +0 -624
|
@@ -0,0 +1,646 @@
|
|
|
1
|
+
const f = typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")();
|
|
2
|
+
function U() {
|
|
3
|
+
return f;
|
|
4
|
+
}
|
|
5
|
+
function m(e) {
|
|
6
|
+
return typeof e == "function";
|
|
7
|
+
}
|
|
8
|
+
function M(e) {
|
|
9
|
+
return typeof e == "object" && e !== null;
|
|
10
|
+
}
|
|
11
|
+
function q() {
|
|
12
|
+
return "wx" in f && M(f.wx) && "getSystemInfo" in f.wx && m(f.wx.getSystemInfo);
|
|
13
|
+
}
|
|
14
|
+
function be() {
|
|
15
|
+
return "fetch" in f && m(f.fetch);
|
|
16
|
+
}
|
|
17
|
+
function Ee() {
|
|
18
|
+
return "window" in f && M(f.window) && !q();
|
|
19
|
+
}
|
|
20
|
+
function j() {
|
|
21
|
+
return "Blob" in f && m(f.Blob);
|
|
22
|
+
}
|
|
23
|
+
function F() {
|
|
24
|
+
return "FormData" in f && m(f.FormData);
|
|
25
|
+
}
|
|
26
|
+
function S() {
|
|
27
|
+
return "TextDecoder" in f && m(f.TextDecoder);
|
|
28
|
+
}
|
|
29
|
+
function Te() {
|
|
30
|
+
return "AbortController" in f && m(f.AbortController);
|
|
31
|
+
}
|
|
32
|
+
function H() {
|
|
33
|
+
return "URLSearchParams" in f && m(f.URLSearchParams);
|
|
34
|
+
}
|
|
35
|
+
function N(e) {
|
|
36
|
+
return new Promise((t) => setTimeout(t, Math.min(Number.MAX_SAFE_INTEGER, Math.max(0, e))));
|
|
37
|
+
}
|
|
38
|
+
// @__NO_SIDE_EFFECTS__
|
|
39
|
+
function I(e) {
|
|
40
|
+
const t = e || "CustomError";
|
|
41
|
+
return class extends Error {
|
|
42
|
+
constructor(r) {
|
|
43
|
+
super(r), Object.defineProperty(this, "name", {
|
|
44
|
+
value: t,
|
|
45
|
+
enumerable: !1,
|
|
46
|
+
configurable: !0
|
|
47
|
+
}), "setPrototypeOf" in Object && Object.setPrototypeOf(this, new.target.prototype), "captureStackTrace" in Error && typeof Error.captureStackTrace == "function" && Error.captureStackTrace(this, this.constructor);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
const G = /^(?:https?:)?\/\/.+$/i, K = /^https?:\/\/.+$/i, _ = /^\{[\d\D]*\}$/, J = /^\[[\d\D]*\]$/;
|
|
52
|
+
function $(e, t = !1) {
|
|
53
|
+
return t ? G.test(e) : K.test(e);
|
|
54
|
+
}
|
|
55
|
+
function x(e) {
|
|
56
|
+
return e == null;
|
|
57
|
+
}
|
|
58
|
+
function v(e, ...t) {
|
|
59
|
+
if (!e || typeof e != "object") return !1;
|
|
60
|
+
const r = Object.getPrototypeOf(e);
|
|
61
|
+
return r !== Object.prototype && r !== null ? !1 : t.every((s) => s in e);
|
|
62
|
+
}
|
|
63
|
+
function L(e) {
|
|
64
|
+
return _.test(e) || J.test(e);
|
|
65
|
+
}
|
|
66
|
+
function T(e, t) {
|
|
67
|
+
try {
|
|
68
|
+
const r = JSON.parse(e);
|
|
69
|
+
return t ? t(r) ? r : null : r;
|
|
70
|
+
} catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async function ke(e) {
|
|
75
|
+
return await new Promise(function(t) {
|
|
76
|
+
const r = document.getElementsByTagName("head")[0], s = document.createElement("script");
|
|
77
|
+
s.setAttribute("type", "text/javascript"), s.setAttribute("charset", "utf-8"), s.onload = function() {
|
|
78
|
+
r.removeChild(s), t(!0);
|
|
79
|
+
}, s.onerror = function() {
|
|
80
|
+
r.removeChild(s), t(!1);
|
|
81
|
+
}, s.setAttribute("src", e), r.appendChild(s);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
function V(e, t = "数据未能正确识别") {
|
|
85
|
+
return typeof e == "function" ? {
|
|
86
|
+
guard: e,
|
|
87
|
+
message: t
|
|
88
|
+
} : {
|
|
89
|
+
guard: e.guard,
|
|
90
|
+
message: e.message || t
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function W(e, t = "") {
|
|
94
|
+
return !t || $(e, !0) ? C(e) : (C(t) + "/" + e).replace(/\/{2,}/g, "/").replace(/:\//, "://");
|
|
95
|
+
}
|
|
96
|
+
function C(e) {
|
|
97
|
+
const t = U();
|
|
98
|
+
return $(e, !0) ? e.startsWith("http") ? e : ("location" in t ? location.protocol : "https:") + e : ("location" in t ? location.origin : "http://127.0.0.1") + "/" + e.replace(/^\/+/, "");
|
|
99
|
+
}
|
|
100
|
+
function X(e, t) {
|
|
101
|
+
const r = {};
|
|
102
|
+
return (e.match(/([^=&#?]+)=[^&#]*/g) || []).forEach(function(s) {
|
|
103
|
+
const n = s.split("="), o = n[0], a = decodeURIComponent(n[1] || "");
|
|
104
|
+
r[o] !== void 0 ? r[o] += "," + a : r[o] = a;
|
|
105
|
+
}), t !== !0 ? r[t] || "" : r;
|
|
106
|
+
}
|
|
107
|
+
function z(e, t) {
|
|
108
|
+
if (t) {
|
|
109
|
+
if (t === !0)
|
|
110
|
+
return e.replace(/\?[^#]*/, "");
|
|
111
|
+
} else return e;
|
|
112
|
+
const r = e.split("#"), s = r[0].split("?"), n = s[0], o = s.length > 1 ? s[1] : "", a = r.length > 1 ? "#" + r[1] : "", i = typeof t == "string" ? [t] : Array.isArray(t) ? t : [];
|
|
113
|
+
return !i.length || !o ? r[0] + a : (i.map((c) => c.replace(/([\\(){}[\]^$+\-*?|])/g, "\\$1")), (n + "?" + o.replace(new RegExp("(?:^|&)(?:" + i.join("|") + ")=[^&$]+", "g"), "").replace(/^&/, "")).replace(/\?$/, "") + a);
|
|
114
|
+
}
|
|
115
|
+
function Q(e, t, r = !1) {
|
|
116
|
+
const s = typeof t == "string" ? t : Object.keys(t).map((a) => `${a}=${encodeURIComponent(t[a])}`).join("&");
|
|
117
|
+
if (!s)
|
|
118
|
+
return e;
|
|
119
|
+
const n = e.split("#");
|
|
120
|
+
r && (n[0] = z(
|
|
121
|
+
n[0],
|
|
122
|
+
(s.match(/([^=&#?]+)=[^&#]+/g) || []).map((a) => a.replace(/=.+$/, ""))
|
|
123
|
+
));
|
|
124
|
+
const o = n[0].indexOf("?") + 1 ? "&" : "?";
|
|
125
|
+
return (n[0] + o + s + (n.length > 1 ? "#" + n[1] : "")).replace(/\?&/, "?");
|
|
126
|
+
}
|
|
127
|
+
function Y(e) {
|
|
128
|
+
const t = e.match(/(?:\?|&)([^=]+)(?:&|$)/g);
|
|
129
|
+
return t ? t.join("").replace(/(?:\?|^&+|&+$)/g, "").replace(/&{2}/g, "&").split("&").sort() : [];
|
|
130
|
+
}
|
|
131
|
+
class Z {
|
|
132
|
+
ttl;
|
|
133
|
+
cache;
|
|
134
|
+
timer = null;
|
|
135
|
+
// 定时循环以清理缓存
|
|
136
|
+
loopCheck() {
|
|
137
|
+
if (Object.keys(this.cache).forEach((r) => {
|
|
138
|
+
this.cache[r].ttl < Date.now() && delete this.cache[r];
|
|
139
|
+
}), Object.keys(this.cache).length === 0) {
|
|
140
|
+
this.timer = null;
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
this.timer = setTimeout(this.loopCheck, 5e3);
|
|
144
|
+
}
|
|
145
|
+
constructor(t = 500) {
|
|
146
|
+
this.cache = {}, this.ttl = Math.max(t, 0), this.loopCheck = this.loopCheck.bind(this);
|
|
147
|
+
}
|
|
148
|
+
getKey(t, r, s, n) {
|
|
149
|
+
if (t.toLowerCase() !== "get" && !n)
|
|
150
|
+
return "";
|
|
151
|
+
const o = t.toLowerCase(), a = r.replace(/#.+/, ""), i = a.replace(/\?.+/g, "");
|
|
152
|
+
if (n)
|
|
153
|
+
try {
|
|
154
|
+
const h = n() || "";
|
|
155
|
+
return h ? `${i}_${o}_${h}` : "";
|
|
156
|
+
} catch (h) {
|
|
157
|
+
return console.warn("[RequestCache] cacheResolve error", r, s, h), "";
|
|
158
|
+
}
|
|
159
|
+
const c = Object.assign(X(a, !0), s), l = Y(a), u = Object.keys(c).sort().map((h) => `${h}#${c[h]}`);
|
|
160
|
+
return `${i}_${o}_${u.join(",")}_${l.join(",")}`;
|
|
161
|
+
}
|
|
162
|
+
updateTTL(t) {
|
|
163
|
+
this.ttl = Math.max(t, 0);
|
|
164
|
+
}
|
|
165
|
+
get(t, r) {
|
|
166
|
+
if (Math.max(r ?? this.ttl, 0) === 0)
|
|
167
|
+
return null;
|
|
168
|
+
const n = this.cache[t];
|
|
169
|
+
return n ? n.ttl < Date.now() ? (delete this.cache[t], null) : n.res : null;
|
|
170
|
+
}
|
|
171
|
+
set(t, r, s) {
|
|
172
|
+
const n = Math.max(s ?? this.ttl, 0);
|
|
173
|
+
n !== 0 && (this.cache[t] = {
|
|
174
|
+
ttl: Date.now() + n,
|
|
175
|
+
res: r
|
|
176
|
+
}, this.timer === null && (this.timer = setTimeout(this.loopCheck, 5e3)));
|
|
177
|
+
}
|
|
178
|
+
del(t) {
|
|
179
|
+
this.cache[t] && delete this.cache[t];
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
class ee {
|
|
183
|
+
// 保存的配置需要部分字段强制设置默认值
|
|
184
|
+
config = {
|
|
185
|
+
baseURL: "/",
|
|
186
|
+
maxRetry: 1,
|
|
187
|
+
retryInterval: 200,
|
|
188
|
+
retryResolve: "default",
|
|
189
|
+
timeout: 1e4,
|
|
190
|
+
cacheTTL: 500,
|
|
191
|
+
credentials: "same-origin",
|
|
192
|
+
defaultTypeGuardMessage: "响应数据未能正确识别",
|
|
193
|
+
responseRule: {
|
|
194
|
+
ok: {
|
|
195
|
+
resolve: "body"
|
|
196
|
+
},
|
|
197
|
+
failed: {
|
|
198
|
+
resolve: "json",
|
|
199
|
+
messageField: "message"
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
};
|
|
203
|
+
constructor(t) {
|
|
204
|
+
t && this.set(t);
|
|
205
|
+
}
|
|
206
|
+
set(t) {
|
|
207
|
+
if (t.baseURL && !/^\/.+/.test(t.baseURL) && !$(t.baseURL))
|
|
208
|
+
throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"), new Error("BaseURLError");
|
|
209
|
+
Object.assign(this.config, t);
|
|
210
|
+
}
|
|
211
|
+
get(t) {
|
|
212
|
+
return this.config[t];
|
|
213
|
+
}
|
|
214
|
+
/** 基于 baseURL 返回完整的 url 地址 */
|
|
215
|
+
getFullUrl(t) {
|
|
216
|
+
return W(t, this.config.baseURL);
|
|
217
|
+
}
|
|
218
|
+
/** 提示消息 */
|
|
219
|
+
showMessage(t, r, s, n) {
|
|
220
|
+
this.config.messageHandler && r && this.config.messageHandler(t, r, s, n);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
const w = {
|
|
224
|
+
/** 响应数据校验失败 */
|
|
225
|
+
UnexpectResponse: "UnexpectResponse",
|
|
226
|
+
/** 请求被取消 */
|
|
227
|
+
Aborted: "Aborted",
|
|
228
|
+
/** 未知错误 */
|
|
229
|
+
Unknown: "Unknown",
|
|
230
|
+
/** 网络错误 */
|
|
231
|
+
NetworkError: "NetworkError",
|
|
232
|
+
/** 超时 */
|
|
233
|
+
Timeout: "Timeout",
|
|
234
|
+
/** 请求方法不被支持,在微信小程序环境下有效 */
|
|
235
|
+
NotSupport: "NotSupport"
|
|
236
|
+
};
|
|
237
|
+
function te(e, t, r, s) {
|
|
238
|
+
if (t.ok && !x(t.data) && s) {
|
|
239
|
+
const { guard: n, message: o } = V(s, r.get("defaultTypeGuardMessage"));
|
|
240
|
+
return n(t.data) || (t.code = w.UnexpectResponse, r.showMessage(!0, `${e} ${o}`, t.code, t.status), console.error(t.code, e, t.data), t.data = null, t.message = o), t;
|
|
241
|
+
}
|
|
242
|
+
return t;
|
|
243
|
+
}
|
|
244
|
+
class Re {
|
|
245
|
+
agent;
|
|
246
|
+
config;
|
|
247
|
+
cache;
|
|
248
|
+
constructor(t, r) {
|
|
249
|
+
this.config = new ee(r), this.agent = t, 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);
|
|
250
|
+
}
|
|
251
|
+
/** 发送网络请求,含有缓存控制逻辑,不含有 guard 逻辑 */
|
|
252
|
+
async send(t, r) {
|
|
253
|
+
const s = this.cache.getKey(r?.method || "GET", t, r?.params, r?.cacheResolve);
|
|
254
|
+
if (s) {
|
|
255
|
+
const n = this.cache.get(s, r?.cacheTTL);
|
|
256
|
+
if (n)
|
|
257
|
+
return await n;
|
|
258
|
+
}
|
|
259
|
+
try {
|
|
260
|
+
const n = this.agent(t, this.config, r);
|
|
261
|
+
s && this.cache.set(s, n, r?.cacheTTL);
|
|
262
|
+
const o = await n;
|
|
263
|
+
return (!o.ok || o.status < 0) && this.cache.del(s), o;
|
|
264
|
+
} catch (n) {
|
|
265
|
+
return console.error("RequestError", n), {
|
|
266
|
+
ok: !1,
|
|
267
|
+
status: -1,
|
|
268
|
+
code: w.Unknown,
|
|
269
|
+
message: n instanceof Error ? n.message : String(n),
|
|
270
|
+
headers: {},
|
|
271
|
+
data: null
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
async request(t) {
|
|
276
|
+
const r = await this.send(t.url, t.options);
|
|
277
|
+
return te(t.url, r, this.config, t.guard ?? null);
|
|
278
|
+
}
|
|
279
|
+
/** 整理合并通用参数 */
|
|
280
|
+
gen(t, r, s, n, o) {
|
|
281
|
+
const a = Object.assign(
|
|
282
|
+
o ? { ...o, method: r } : { method: r },
|
|
283
|
+
n ? { body: n } : null,
|
|
284
|
+
s ? { params: s } : null
|
|
285
|
+
);
|
|
286
|
+
return typeof t == "object" ? (a.params = t.params, { options: a, url: t.url }) : { options: a, url: t };
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* 修改默认请求配置: baseURL / timeout / credentials / errorHandler / messageHandler / responseHandler / logHandler / responseRule
|
|
290
|
+
*/
|
|
291
|
+
setConfig(t) {
|
|
292
|
+
this.config.set(t), this.cache.updateTTL(this.config.get("cacheTTL"));
|
|
293
|
+
}
|
|
294
|
+
/**
|
|
295
|
+
* 读取默认的请求配置
|
|
296
|
+
*/
|
|
297
|
+
getConfig(t) {
|
|
298
|
+
return this.config.get(t);
|
|
299
|
+
}
|
|
300
|
+
/**
|
|
301
|
+
* 发送一个 HEAD 请求,并且不处理响应 body
|
|
302
|
+
*/
|
|
303
|
+
async head(t, r, s) {
|
|
304
|
+
return this.request(this.gen(t, "HEAD", r, null, s));
|
|
305
|
+
}
|
|
306
|
+
async get(t, r, s, n) {
|
|
307
|
+
const o = this.gen(t, "GET", r, null, n);
|
|
308
|
+
return s ? this.request({ ...o, guard: s }) : this.request(o);
|
|
309
|
+
}
|
|
310
|
+
async post(t, r, s, n) {
|
|
311
|
+
const o = this.gen(t, "POST", null, r, n);
|
|
312
|
+
return s ? this.request({ ...o, guard: s }) : this.request(o);
|
|
313
|
+
}
|
|
314
|
+
async del(t, r, s, n) {
|
|
315
|
+
const o = this.gen(t, "DELETE", r, null, n);
|
|
316
|
+
return s ? this.request({ ...o, guard: s }) : this.request(o);
|
|
317
|
+
}
|
|
318
|
+
async put(t, r, s, n) {
|
|
319
|
+
const o = this.gen(t, "PUT", null, r, n);
|
|
320
|
+
return s ? this.request({ ...o, guard: s }) : this.request(o);
|
|
321
|
+
}
|
|
322
|
+
async patch(t, r, s, n) {
|
|
323
|
+
const o = this.gen(t, "PATCH", null, r, n);
|
|
324
|
+
return s ? this.request({ ...o, guard: s }) : this.request(o);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
function se(e) {
|
|
328
|
+
const t = {};
|
|
329
|
+
for (const [r, s] of e)
|
|
330
|
+
r && typeof r == "string" && (t[r] = s || "");
|
|
331
|
+
return t;
|
|
332
|
+
}
|
|
333
|
+
function B(e) {
|
|
334
|
+
return x(e) ? "" : typeof e == "string" ? e : e instanceof ArrayBuffer && S() ? new TextDecoder().decode(e) : JSON.stringify(e);
|
|
335
|
+
}
|
|
336
|
+
function re(e) {
|
|
337
|
+
return e instanceof Error ? e : new Error(B(e));
|
|
338
|
+
}
|
|
339
|
+
async function je(e) {
|
|
340
|
+
if (S())
|
|
341
|
+
return new TextEncoder().encode(e).buffer;
|
|
342
|
+
if (j())
|
|
343
|
+
return await new Blob([e]).arrayBuffer();
|
|
344
|
+
throw new Error("cannot convert string to ArrayBuffer");
|
|
345
|
+
}
|
|
346
|
+
function ne(e, t) {
|
|
347
|
+
if ("URL" in U()) {
|
|
348
|
+
const s = new URL(e);
|
|
349
|
+
return Object.keys(t).forEach((n) => s.searchParams.set(n, t[n])), s.toString();
|
|
350
|
+
}
|
|
351
|
+
return Q(e, t);
|
|
352
|
+
}
|
|
353
|
+
function oe(e) {
|
|
354
|
+
return typeof e == "function" ? e : Array.isArray(e) && e.length > 0 ? function(t) {
|
|
355
|
+
return e.includes(t.status);
|
|
356
|
+
} : e === "network" ? ae : ce;
|
|
357
|
+
}
|
|
358
|
+
function ae({ error: e }) {
|
|
359
|
+
return e === w.NetworkError;
|
|
360
|
+
}
|
|
361
|
+
function ce({ status: e, headers: t, error: r }) {
|
|
362
|
+
return r === w.NetworkError ? !0 : t?.server === "TencentEdgeOne" ? e === 522 || e === 552 : !1;
|
|
363
|
+
}
|
|
364
|
+
async function $e(e, t, r) {
|
|
365
|
+
const s = Object.assign({ method: "GET" }, r), n = F() ? s.body instanceof FormData : !1, o = n && s.method !== "POST" && s.method !== "PUT" ? "POST" : s.method, a = o === "GET" || o === "HEAD" || o === "DELETE";
|
|
366
|
+
a && s.body !== void 0 && (console.warn("request body is invalid with method get, head, delete"), delete s.body);
|
|
367
|
+
const i = Object.assign(
|
|
368
|
+
n || a ? {} : {
|
|
369
|
+
"Content-Type": j() && s.body instanceof Blob ? s.body.type || "application/octet-stream" : "application/json;charset=utf-8"
|
|
370
|
+
},
|
|
371
|
+
s.headers
|
|
372
|
+
), c = s.params || {}, l = {};
|
|
373
|
+
Object.keys(c).forEach((y) => {
|
|
374
|
+
c[y] !== void 0 && (l[y] = ie(c[y]));
|
|
375
|
+
});
|
|
376
|
+
const u = t.getFullUrl(e), h = ue(s.body), p = s.timeout || t.get("timeout"), d = await (async function() {
|
|
377
|
+
const y = t.get("requestTransformer");
|
|
378
|
+
if (y)
|
|
379
|
+
return await y({ headers: i, params: l, method: o, url: u, body: h });
|
|
380
|
+
})(), g = ne(typeof d == "string" && d ? d : u, l);
|
|
381
|
+
return t.get("logHandler")?.({ type: "ready", url: g, method: o, headers: i, timeout: p, body: h }), {
|
|
382
|
+
url: g,
|
|
383
|
+
method: o,
|
|
384
|
+
body: h,
|
|
385
|
+
headers: i,
|
|
386
|
+
timeout: p,
|
|
387
|
+
abort: s.abort,
|
|
388
|
+
asBuffer: !!s.asBuffer,
|
|
389
|
+
credentials: s.credentials || t.get("credentials")
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
function ie(e) {
|
|
393
|
+
return x(e) ? "" : typeof e == "string" ? e : typeof e == "number" || typeof e == "boolean" ? String(e) : Array.isArray(e) ? e.filter(
|
|
394
|
+
(r) => r != null && typeof r != "object" && typeof r != "function"
|
|
395
|
+
).join(",") : (console.warn("参数类型不支持,已被过滤", typeof e, e), "");
|
|
396
|
+
}
|
|
397
|
+
function ue(e) {
|
|
398
|
+
if (e)
|
|
399
|
+
return typeof e == "string" || H() && e instanceof URLSearchParams || e instanceof ArrayBuffer || j() && e instanceof Blob || F() && e instanceof FormData ? e : JSON.stringify(e);
|
|
400
|
+
}
|
|
401
|
+
const D = "data", b = "message";
|
|
402
|
+
function fe(e, t, r) {
|
|
403
|
+
return P(e.status) ? he(r?.ok || t.ok, e) : le(r?.failed || t.failed, e);
|
|
404
|
+
}
|
|
405
|
+
function he(e, { statusText: t, body: r }) {
|
|
406
|
+
const s = {
|
|
407
|
+
ok: !0,
|
|
408
|
+
code: t,
|
|
409
|
+
message: "",
|
|
410
|
+
data: null
|
|
411
|
+
};
|
|
412
|
+
if (!r)
|
|
413
|
+
return s;
|
|
414
|
+
if (typeof r != "string")
|
|
415
|
+
return s.data = r, s;
|
|
416
|
+
if (e.resolve === "body")
|
|
417
|
+
return s.data = L(r) ? k(T(r), e.converter) : r, s;
|
|
418
|
+
const n = T(r);
|
|
419
|
+
if (!n)
|
|
420
|
+
return s.ok = !1, s.code = "ResponseFormatError", s.message = "响应内容无法解析为 json", s;
|
|
421
|
+
const o = k(n, e.converter);
|
|
422
|
+
if (!o || !v(o))
|
|
423
|
+
return s.ok = !1, s.code = "ResponseFormatError", s.message = "响应内容无法格式化为 object", s;
|
|
424
|
+
const a = e.statusField;
|
|
425
|
+
if (a && !(a in o))
|
|
426
|
+
return s.ok = !1, s.code = "ResponseFieldMissing", s.message = `响应内容找不到状态字段 ${a}`, s;
|
|
427
|
+
const i = e.statusOKValue || "", c = e.dataField || D, l = e.messageField || b, u = a ? String(o[a] ?? "") : "";
|
|
428
|
+
return s.ok = a ? u === i : !0, s.code = u || s.code, s.data = c === !0 ? o : c in o ? o[c] : null, s.message = R(o, l), e.ignoreMessage && s.message && (Array.isArray(e.ignoreMessage) ? e.ignoreMessage.includes(s.message) : e.ignoreMessage === s.message) && (s.message = ""), s;
|
|
429
|
+
}
|
|
430
|
+
function le(e, { statusText: t, body: r, error: s }) {
|
|
431
|
+
const n = typeof r == "string" ? r : "", o = {
|
|
432
|
+
ok: !1,
|
|
433
|
+
code: s || t,
|
|
434
|
+
message: n,
|
|
435
|
+
data: null
|
|
436
|
+
};
|
|
437
|
+
switch (e.resolve) {
|
|
438
|
+
case "body":
|
|
439
|
+
o.message = O(n) || n;
|
|
440
|
+
break;
|
|
441
|
+
case "json":
|
|
442
|
+
const { code: a, message: i } = de(n, e.converter, e.statusField, e.messageField);
|
|
443
|
+
o.code = a || s || t, o.message = O(n) || i;
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
446
|
+
return o;
|
|
447
|
+
}
|
|
448
|
+
function P(e) {
|
|
449
|
+
return e >= 200 && e < 400;
|
|
450
|
+
}
|
|
451
|
+
function k(e, t) {
|
|
452
|
+
return t && typeof t == "function" ? t(e) : e;
|
|
453
|
+
}
|
|
454
|
+
const xe = function(e) {
|
|
455
|
+
const t = [], r = e.failed || { resolve: "json" };
|
|
456
|
+
switch (t.push("- 当http状态码 <200 或者 >=400 时"), r.resolve) {
|
|
457
|
+
case "body":
|
|
458
|
+
t.push(" 将响应内容格式化为字符串并作为错误消息");
|
|
459
|
+
break;
|
|
460
|
+
case "json":
|
|
461
|
+
t.push(" 将响应解析为json,并读取 " + (r.messageField || b) + " 作为错误消息");
|
|
462
|
+
break;
|
|
463
|
+
}
|
|
464
|
+
const s = e.ok || { resolve: "body" };
|
|
465
|
+
switch (t.push("- 当http状态码 >=200 并且 <400 时"), s.resolve) {
|
|
466
|
+
case "body":
|
|
467
|
+
t.push(" 将响应尝试解析为 json,并作为数据内容返回");
|
|
468
|
+
break;
|
|
469
|
+
case "json":
|
|
470
|
+
t.push(
|
|
471
|
+
" 将响应解析为 json,读取 " + (s.dataField || D) + " 作为响应数据,读取 " + (s.messageField || b) + " 作为提示消息"
|
|
472
|
+
), s.statusField && t.push(
|
|
473
|
+
" 当 " + s.statusField + " 为 " + (s.statusOKValue || "空值") + " 时是成功提示,否则是错误消息"
|
|
474
|
+
), s.ignoreMessage && t.push(" 并忽略以下消息:" + s.ignoreMessage);
|
|
475
|
+
break;
|
|
476
|
+
}
|
|
477
|
+
return t.join(`
|
|
478
|
+
`);
|
|
479
|
+
};
|
|
480
|
+
function de(e, t, r, s = b) {
|
|
481
|
+
if (!L(e))
|
|
482
|
+
return { message: "" };
|
|
483
|
+
const n = k(T(e), t);
|
|
484
|
+
return !n || !v(n) ? { message: e } : {
|
|
485
|
+
code: r ? R(n, r) : "",
|
|
486
|
+
message: R(n, s) || e
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
function R(e, t) {
|
|
490
|
+
const r = Array.isArray(t) ? t : [t];
|
|
491
|
+
for (const s of r)
|
|
492
|
+
if (s in e)
|
|
493
|
+
return B(e[s]);
|
|
494
|
+
return "";
|
|
495
|
+
}
|
|
496
|
+
const ge = [/<title>([^<]+)<\/title>/i, /<message>([^<]+)<\/message>/i];
|
|
497
|
+
function O(e) {
|
|
498
|
+
for (const t of ge) {
|
|
499
|
+
const r = e.match(t);
|
|
500
|
+
if (r)
|
|
501
|
+
return r[1];
|
|
502
|
+
}
|
|
503
|
+
return "";
|
|
504
|
+
}
|
|
505
|
+
const pe = /* @__PURE__ */ I("APIError");
|
|
506
|
+
function me(e) {
|
|
507
|
+
const t = {};
|
|
508
|
+
for (const n in e.headers)
|
|
509
|
+
(n.startsWith("x-") || n.includes("trace") || n.includes("server") || n.includes("status") || /\b(?:id|uuid)\b/.test(n)) && (t[n] = e.headers[n]);
|
|
510
|
+
const r = e.url.replace(/^(?:https?:)?\/*/i, "").replace(/\?.+/, ""), s = e.status < 0 ? "unknown" : e.status;
|
|
511
|
+
return {
|
|
512
|
+
sentryError: new pe(`${r}${s === "unknown" ? "" : ` | ${s}`} | ${e.code}`),
|
|
513
|
+
sentryTags: {
|
|
514
|
+
...t,
|
|
515
|
+
status: s,
|
|
516
|
+
method: e.method,
|
|
517
|
+
code: e.code || "unknown",
|
|
518
|
+
message: e.message || "empty"
|
|
519
|
+
},
|
|
520
|
+
sentryExtra: {
|
|
521
|
+
url: e.url,
|
|
522
|
+
responseBody: typeof e.body == "string" || e.body === null ? e.body || "empty" : "[ArrayBuffer]",
|
|
523
|
+
responseHeaders: e.headers,
|
|
524
|
+
rawError: e.error
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
}
|
|
528
|
+
async function Ce(e, t) {
|
|
529
|
+
if (e.method === "HEAD" || e.status === 204)
|
|
530
|
+
return e.body = null, e;
|
|
531
|
+
try {
|
|
532
|
+
e.body = t.asBuffer ? await t.buffer() : await t.text();
|
|
533
|
+
} catch (r) {
|
|
534
|
+
e.body = null, e.error = w.Unknown, e.rawError = re(r);
|
|
535
|
+
}
|
|
536
|
+
return e;
|
|
537
|
+
}
|
|
538
|
+
function Oe(e, t, r, s) {
|
|
539
|
+
const n = e.status, o = e.method, a = se(
|
|
540
|
+
Object.entries(e.headers || {}).map(([d, g]) => [d.toLowerCase(), g])
|
|
541
|
+
), { ok: i, code: c, data: l, message: u } = fe(e, r.get("responseRule"), s?.responseRule);
|
|
542
|
+
if (!P(n)) {
|
|
543
|
+
const d = me({
|
|
544
|
+
url: e.url,
|
|
545
|
+
method: e.method,
|
|
546
|
+
status: n,
|
|
547
|
+
code: c,
|
|
548
|
+
message: u,
|
|
549
|
+
body: e.body,
|
|
550
|
+
headers: a,
|
|
551
|
+
error: e.rawError
|
|
552
|
+
});
|
|
553
|
+
(r.get("errorHandler") || ye)({
|
|
554
|
+
url: t,
|
|
555
|
+
method: o,
|
|
556
|
+
status: n,
|
|
557
|
+
code: c,
|
|
558
|
+
message: u,
|
|
559
|
+
headers: a,
|
|
560
|
+
rawError: e.rawError,
|
|
561
|
+
...d
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
if (n < 0)
|
|
565
|
+
return A(
|
|
566
|
+
{ ok: !1, status: n, code: e.statusText, headers: {}, message: "", data: null },
|
|
567
|
+
`${o} ${t} ${e.statusText}`,
|
|
568
|
+
o,
|
|
569
|
+
t,
|
|
570
|
+
r,
|
|
571
|
+
s
|
|
572
|
+
);
|
|
573
|
+
const h = { ok: i, data: l, code: c, message: u, status: n, headers: a };
|
|
574
|
+
r.get("responseHandler")?.({ ...h }, o, t);
|
|
575
|
+
const p = i ? u : u || e.statusText;
|
|
576
|
+
return A(h, p, o, t, r, s);
|
|
577
|
+
}
|
|
578
|
+
function A(e, t, r, s, n, o) {
|
|
579
|
+
const a = n.get("message"), i = a === !1 || o?.message === !1 ? !1 : o?.message || a;
|
|
580
|
+
if (i !== !1) {
|
|
581
|
+
const c = typeof i == "function" ? i(e, r, s, t) : t;
|
|
582
|
+
c instanceof Error ? n.showMessage(!0, E(c.message), e.code, e.status) : c && typeof c == "object" && "message" in c && typeof c.message == "string" ? n.showMessage(!1, E(c.message), e.code, e.status) : c && n.showMessage(!e.ok, E(String(c)), e.code, e.status);
|
|
583
|
+
}
|
|
584
|
+
return e;
|
|
585
|
+
}
|
|
586
|
+
function ye(e) {
|
|
587
|
+
const t = {};
|
|
588
|
+
for (const r in e)
|
|
589
|
+
r.startsWith("sentry") || (t[r] = e[r]);
|
|
590
|
+
console.error("RequestError", t);
|
|
591
|
+
}
|
|
592
|
+
function E(e) {
|
|
593
|
+
return e.replace(/<script[\s\S]*?>[\s\S]*?<\/script>/gi, "").replace(/<[^>]+>/g, "").trim();
|
|
594
|
+
}
|
|
595
|
+
async function we(e, t, r, s, n) {
|
|
596
|
+
const o = n || 0, a = Math.max(0, Math.min(10, s?.maxRetry ?? r.get("maxRetry") ?? 0)), i = oe(s?.retryResolve ?? r.get("retryResolve")), c = r.get("logHandler");
|
|
597
|
+
c?.({
|
|
598
|
+
type: "prepare",
|
|
599
|
+
url: t,
|
|
600
|
+
method: s?.method || "GET",
|
|
601
|
+
retry: o,
|
|
602
|
+
maxRetry: a,
|
|
603
|
+
message: o === 0 ? "start" : `retry ${o}/${a} start`,
|
|
604
|
+
headers: s?.headers,
|
|
605
|
+
options: s
|
|
606
|
+
});
|
|
607
|
+
const l = Date.now(), u = await e(t, r, s), h = u.status, p = Date.now() - l, d = `[cost ${p}][${h}] ${h < 0 ? u.body : ""}`;
|
|
608
|
+
if (c?.({
|
|
609
|
+
type: "finished",
|
|
610
|
+
url: t,
|
|
611
|
+
method: u.method,
|
|
612
|
+
retry: o,
|
|
613
|
+
maxRetry: a,
|
|
614
|
+
message: o === 0 ? `finish ${d}` : `retry ${o}/${a} finish ${d}`,
|
|
615
|
+
response: u,
|
|
616
|
+
headers: u.headers,
|
|
617
|
+
cost: p
|
|
618
|
+
}), !a || o >= a || i(u, o) !== !0)
|
|
619
|
+
return u;
|
|
620
|
+
const g = s?.retryInterval ?? r.get("retryInterval") ?? 100;
|
|
621
|
+
return await N(
|
|
622
|
+
Math.max(
|
|
623
|
+
100,
|
|
624
|
+
g === "2EB" ? Math.pow(2, o) * 100 : typeof g == "function" ? g(o + 1, { url: t, status: h, method: u.method }) || 0 : g
|
|
625
|
+
)
|
|
626
|
+
), await we(e, t, r, s, o + 1);
|
|
627
|
+
}
|
|
628
|
+
export {
|
|
629
|
+
q as E,
|
|
630
|
+
ee as G,
|
|
631
|
+
Q as H,
|
|
632
|
+
Re as N,
|
|
633
|
+
w as R,
|
|
634
|
+
be as X,
|
|
635
|
+
je as a,
|
|
636
|
+
Ee as b,
|
|
637
|
+
$e as c,
|
|
638
|
+
se as d,
|
|
639
|
+
Te as e,
|
|
640
|
+
Ce as f,
|
|
641
|
+
xe as g,
|
|
642
|
+
Oe as h,
|
|
643
|
+
we as r,
|
|
644
|
+
B as t,
|
|
645
|
+
ke as v
|
|
646
|
+
};
|