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