@seayoo-web/request 3.4.0 → 3.4.4

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