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