@seayoo-web/request 3.5.2 → 4.0.1

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