@seayoo-web/request 4.0.4 → 4.1.0

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