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