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