@seayoo-web/request 3.3.2 → 3.4.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/dist/index.js +61 -61
- package/dist/node.js +9 -9
- package/dist/{request.fetch-b7wSTFXy.js → request.fetch-B3ED8nRu.js} +6 -6
- package/dist/retry-CdUkYrFw.js +606 -0
- package/dist/wx.js +23 -23
- package/package.json +2 -6
- package/dist/index.cjs +0 -2
- package/dist/node.cjs +0 -1
- package/dist/request.fetch-zj8Hd-er.cjs +0 -1
- package/dist/retry-D-T4xE0P.cjs +0 -3
- package/dist/retry-DUa9q26R.js +0 -1178
- package/dist/wx.cjs +0 -1
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
const l = typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")();
|
|
2
|
+
function x() {
|
|
3
|
+
return l;
|
|
4
|
+
}
|
|
5
|
+
function p(t) {
|
|
6
|
+
return typeof t == "function";
|
|
7
|
+
}
|
|
8
|
+
function v(t) {
|
|
9
|
+
return typeof t == "object" && t !== null;
|
|
10
|
+
}
|
|
11
|
+
function P() {
|
|
12
|
+
return "wx" in l && v(l.wx) && "getSystemInfo" in l.wx && p(l.wx.getSystemInfo);
|
|
13
|
+
}
|
|
14
|
+
function fe() {
|
|
15
|
+
return "fetch" in l && p(l.fetch);
|
|
16
|
+
}
|
|
17
|
+
function de() {
|
|
18
|
+
return "window" in l && v(l.window) && !P();
|
|
19
|
+
}
|
|
20
|
+
function F() {
|
|
21
|
+
return "Blob" in l && p(l.Blob);
|
|
22
|
+
}
|
|
23
|
+
function C() {
|
|
24
|
+
return "FormData" in l && p(l.FormData);
|
|
25
|
+
}
|
|
26
|
+
function ge() {
|
|
27
|
+
return "TextDecoder" in l && p(l.TextDecoder);
|
|
28
|
+
}
|
|
29
|
+
function me() {
|
|
30
|
+
return "AbortController" in l && p(l.AbortController);
|
|
31
|
+
}
|
|
32
|
+
function D() {
|
|
33
|
+
return "URLSearchParams" in l && p(l.URLSearchParams);
|
|
34
|
+
}
|
|
35
|
+
function S(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(n) {
|
|
42
|
+
super(n), 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, I = /^https?:\/\/.+$/i, H = /^\{[\d\D]*\}$/, N = /^\[[\d\D]*\]$/;
|
|
51
|
+
function k(t, e = !1) {
|
|
52
|
+
return e ? B.test(t) : I.test(t);
|
|
53
|
+
}
|
|
54
|
+
function G(t) {
|
|
55
|
+
return t == null;
|
|
56
|
+
}
|
|
57
|
+
function M(t, ...e) {
|
|
58
|
+
if (!t || typeof t != "object") return !1;
|
|
59
|
+
const n = Object.getPrototypeOf(t);
|
|
60
|
+
return n !== Object.prototype && n !== null ? !1 : e.every((s) => s in t);
|
|
61
|
+
}
|
|
62
|
+
function U(t) {
|
|
63
|
+
return H.test(t) || N.test(t);
|
|
64
|
+
}
|
|
65
|
+
function w(t, e) {
|
|
66
|
+
try {
|
|
67
|
+
const n = JSON.parse(t);
|
|
68
|
+
return e ? e(n) ? n : null : n;
|
|
69
|
+
} catch {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
async function pe(t) {
|
|
74
|
+
return await new Promise(function(e) {
|
|
75
|
+
const n = document.getElementsByTagName("head")[0], s = document.createElement("script");
|
|
76
|
+
s.setAttribute("type", "text/javascript"), s.setAttribute("charset", "utf-8"), s.onload = function() {
|
|
77
|
+
n.removeChild(s), e(!0);
|
|
78
|
+
}, s.onerror = function() {
|
|
79
|
+
n.removeChild(s), e(!1);
|
|
80
|
+
}, s.setAttribute("src", t), n.appendChild(s);
|
|
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 _(t, e = "") {
|
|
93
|
+
return !e || k(t, !0) ? $(t) : ($(e) + "/" + t).replace(/\/{2,}/g, "/").replace(/:\//, "://");
|
|
94
|
+
}
|
|
95
|
+
function $(t) {
|
|
96
|
+
const e = x();
|
|
97
|
+
return k(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 n = {};
|
|
101
|
+
return (t.match(/([^=&#?]+)=[^&#]*/g) || []).forEach(function(s) {
|
|
102
|
+
const r = s.split("="), o = r[0], a = decodeURIComponent(r[1] || "");
|
|
103
|
+
n[o] !== void 0 ? n[o] += "," + a : n[o] = a;
|
|
104
|
+
}), e !== !0 ? n[e] || "" : n;
|
|
105
|
+
}
|
|
106
|
+
function W(t, e) {
|
|
107
|
+
if (e) {
|
|
108
|
+
if (e === !0)
|
|
109
|
+
return t.replace(/\?[^#]*/, "");
|
|
110
|
+
} else return t;
|
|
111
|
+
const n = t.split("#"), s = n[0].split("?"), r = s[0], o = s.length > 1 ? s[1] : "", a = n.length > 1 ? "#" + n[1] : "", c = typeof e == "string" ? [e] : Array.isArray(e) ? e : [];
|
|
112
|
+
return !c.length || !o ? n[0] + a : (c.map((u) => u.replace(/([\\(){}[\]^$+\-*?|])/g, "\\$1")), (r + "?" + o.replace(new RegExp("(?:^|&)(?:" + c.join("|") + ")=[^&$]+", "g"), "").replace(/^&/, "")).replace(/\?$/, "") + a);
|
|
113
|
+
}
|
|
114
|
+
function ye(t, e, n = !1) {
|
|
115
|
+
const s = typeof e == "string" ? e : Object.keys(e).map((a) => `${a}=${encodeURIComponent(e[a])}`).join("&");
|
|
116
|
+
if (!s)
|
|
117
|
+
return t;
|
|
118
|
+
const r = t.split("#");
|
|
119
|
+
n && (r[0] = W(
|
|
120
|
+
r[0],
|
|
121
|
+
(s.match(/([^=&#?]+)=[^&#]+/g) || []).map((a) => a.replace(/=.+$/, ""))
|
|
122
|
+
));
|
|
123
|
+
const o = r[0].indexOf("?") + 1 ? "&" : "?";
|
|
124
|
+
return (r[0] + o + s + (r.length > 1 ? "#" + r[1] : "")).replace(/\?&/, "?");
|
|
125
|
+
}
|
|
126
|
+
function J(t) {
|
|
127
|
+
const e = t.match(/(?:\?|&)([^=]+)(?:&|$)/g);
|
|
128
|
+
return e ? e.join("").replace(/(?:\?|^&+|&+$)/g, "").replace(/&{2}/g, "&").split("&").sort() : [];
|
|
129
|
+
}
|
|
130
|
+
class Q {
|
|
131
|
+
ttl;
|
|
132
|
+
cache;
|
|
133
|
+
timer = null;
|
|
134
|
+
// 定时循环以清理缓存
|
|
135
|
+
loopCheck() {
|
|
136
|
+
if (Object.keys(this.cache).forEach((n) => {
|
|
137
|
+
this.cache[n].ttl < Date.now() && delete this.cache[n];
|
|
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, n, s, r) {
|
|
148
|
+
if (e.toLowerCase() !== "get" && !r)
|
|
149
|
+
return "";
|
|
150
|
+
const o = e.toLowerCase(), a = n.replace(/#.+/, ""), c = a.replace(/\?.+/g, "");
|
|
151
|
+
if (r)
|
|
152
|
+
try {
|
|
153
|
+
const i = r() || "";
|
|
154
|
+
return i ? `${c}_${o}_${i}` : "";
|
|
155
|
+
} catch (i) {
|
|
156
|
+
return console.warn("[RequestCache] cacheResolve error", n, s, i), "";
|
|
157
|
+
}
|
|
158
|
+
const u = Object.assign(V(a, !0), s), f = J(a), h = Object.keys(u).sort().map((i) => `${i}#${u[i]}`);
|
|
159
|
+
return `${c}_${o}_${h.join(",")}_${f.join(",")}`;
|
|
160
|
+
}
|
|
161
|
+
updateTTL(e) {
|
|
162
|
+
this.ttl = Math.max(e, 0);
|
|
163
|
+
}
|
|
164
|
+
get(e, n) {
|
|
165
|
+
if (Math.max(n ?? this.ttl, 0) === 0)
|
|
166
|
+
return null;
|
|
167
|
+
const r = this.cache[e];
|
|
168
|
+
return r ? r.ttl < Date.now() ? (delete this.cache[e], null) : r.res : null;
|
|
169
|
+
}
|
|
170
|
+
set(e, n, s) {
|
|
171
|
+
const r = Math.max(s ?? this.ttl, 0);
|
|
172
|
+
r !== 0 && (this.cache[e] = {
|
|
173
|
+
ttl: Date.now() + r,
|
|
174
|
+
res: n
|
|
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) && !k(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 _(e, this.config.baseURL);
|
|
216
|
+
}
|
|
217
|
+
/** 提示消息 */
|
|
218
|
+
showMessage(e, n, s, r) {
|
|
219
|
+
this.config.messageHandler && n && this.config.messageHandler(e, n, s, r);
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
const A = {
|
|
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, n, s) {
|
|
239
|
+
if (e.ok && !G(e.data) && s) {
|
|
240
|
+
const r = K(s, n.get("defaultTypeGuardMessage"));
|
|
241
|
+
return r.guard(e.data) || (e.code = A.UnexpectResponse, n.showMessage(!0, `${t} ${r.message}`, e.code, e.status), console.error(e.code, t, e.data), e.data = null, e.message = r.message), e;
|
|
242
|
+
}
|
|
243
|
+
return e;
|
|
244
|
+
}
|
|
245
|
+
class be {
|
|
246
|
+
agent;
|
|
247
|
+
config;
|
|
248
|
+
cache;
|
|
249
|
+
constructor(e, n) {
|
|
250
|
+
this.config = new z(n), 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);
|
|
251
|
+
}
|
|
252
|
+
/**
|
|
253
|
+
* 执行网络请求
|
|
254
|
+
*/
|
|
255
|
+
async request(e, n) {
|
|
256
|
+
const s = this.cache.getKey(n?.method || "GET", e, n?.params, n?.cacheResolve);
|
|
257
|
+
if (s) {
|
|
258
|
+
const r = this.cache.get(s, n?.cacheTTL);
|
|
259
|
+
if (r)
|
|
260
|
+
return await r;
|
|
261
|
+
}
|
|
262
|
+
try {
|
|
263
|
+
const r = this.agent(e, this.config, n);
|
|
264
|
+
s && this.cache.set(s, r, n?.cacheTTL);
|
|
265
|
+
const o = await r;
|
|
266
|
+
return (!o.ok || o.status < 0) && this.cache.del(s), o;
|
|
267
|
+
} catch (r) {
|
|
268
|
+
return console.error("RequestError", r), {
|
|
269
|
+
ok: !1,
|
|
270
|
+
status: -9,
|
|
271
|
+
code: A.Unknown,
|
|
272
|
+
message: r instanceof Error ? `${r.message}
|
|
273
|
+
${r.stack || ""}` : String(r),
|
|
274
|
+
headers: {},
|
|
275
|
+
data: null
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* 检查响应的数据类型
|
|
281
|
+
*/
|
|
282
|
+
async guard(e, n, s) {
|
|
283
|
+
return X(e, n, this.config, s);
|
|
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, n) {
|
|
301
|
+
const s = Object.assign({}, n || null);
|
|
302
|
+
return s.method = "HEAD", this.guard(e, await this.request(e, s), null);
|
|
303
|
+
}
|
|
304
|
+
async get(e, n, s) {
|
|
305
|
+
const r = Object.assign({}, s || null);
|
|
306
|
+
r.method = "GET";
|
|
307
|
+
const o = this.request(e, r);
|
|
308
|
+
return this.guard(e, await o, n || null);
|
|
309
|
+
}
|
|
310
|
+
async post(e, n, s, r) {
|
|
311
|
+
const o = Object.assign({}, r || null);
|
|
312
|
+
return o.method = "POST", o.body = n || {}, this.guard(e, await this.request(e, o), s || null);
|
|
313
|
+
}
|
|
314
|
+
async del(e, n, s) {
|
|
315
|
+
const r = Object.assign({}, s || null);
|
|
316
|
+
return r.method = "DELETE", this.guard(e, await this.request(e, r), n || null);
|
|
317
|
+
}
|
|
318
|
+
async put(e, n, s, r) {
|
|
319
|
+
const o = Object.assign({}, r || null);
|
|
320
|
+
return o.method = "PUT", o.body = n || {}, this.guard(e, await this.request(e, o), s || null);
|
|
321
|
+
}
|
|
322
|
+
async patch(e, n, s, r) {
|
|
323
|
+
const o = Object.assign({}, r || null);
|
|
324
|
+
return o.method = "PATCH", o.body = n || {}, this.guard(e, await this.request(e, o), s || null);
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
async function we(t, e, n) {
|
|
328
|
+
const s = Object.assign({ method: "GET" }, n), r = C() ? s.body instanceof FormData : !1, o = r && s.method !== "POST" && s.method !== "PUT" ? "POST" : s.method, a = o === "GET" || o === "HEAD" || o === "DELETE";
|
|
329
|
+
a && s.body !== void 0 && (console.warn("request body is invalid with method get, head, delete"), delete s.body);
|
|
330
|
+
const c = Object.assign(
|
|
331
|
+
r || a ? {} : {
|
|
332
|
+
"Content-Type": F() && s.body instanceof Blob ? s.body.type || "application/octet-stream" : "application/json;charset=utf-8"
|
|
333
|
+
},
|
|
334
|
+
s.headers
|
|
335
|
+
), u = s.params || {}, f = {};
|
|
336
|
+
Object.keys(u).forEach((d) => {
|
|
337
|
+
u[d] !== void 0 && (f[d] = Y(u[d]));
|
|
338
|
+
});
|
|
339
|
+
const h = e.getFullUrl(t), i = Z(s.body), g = s.timeout || e.get("timeout"), m = await async function() {
|
|
340
|
+
const d = e.get("requestTransformer");
|
|
341
|
+
if (d)
|
|
342
|
+
return await d({ headers: c, params: f, method: o, url: h, body: i });
|
|
343
|
+
}(), y = typeof m == "string" && m ? m : h;
|
|
344
|
+
return e.get("logHandler")?.({ type: "ready", url: y, method: o, headers: c, timeout: g, body: i }), {
|
|
345
|
+
url: y,
|
|
346
|
+
method: o,
|
|
347
|
+
body: i,
|
|
348
|
+
params: f,
|
|
349
|
+
headers: c,
|
|
350
|
+
timeout: g,
|
|
351
|
+
abort: s.abort,
|
|
352
|
+
credentials: s.credentials || e.get("credentials")
|
|
353
|
+
};
|
|
354
|
+
}
|
|
355
|
+
function Y(t) {
|
|
356
|
+
return typeof t == "string" ? t : Array.isArray(t) ? t.join(",") : t + "";
|
|
357
|
+
}
|
|
358
|
+
function Z(t) {
|
|
359
|
+
if (t)
|
|
360
|
+
return typeof t == "string" || D() && t instanceof URLSearchParams || t instanceof ArrayBuffer || F() && t instanceof Blob || C() && t instanceof FormData ? t : JSON.stringify(t);
|
|
361
|
+
}
|
|
362
|
+
const L = "data", b = "message";
|
|
363
|
+
function ee(t, e, n, s, r) {
|
|
364
|
+
const o = r || s;
|
|
365
|
+
return j(t) ? ae(o.ok || s.ok, t, e, n) : te(o.failed || s.failed, e, n);
|
|
366
|
+
}
|
|
367
|
+
const Re = function(t) {
|
|
368
|
+
const e = [], n = t.failed || { resolve: "json" };
|
|
369
|
+
switch (e.push("- 当http状态码 <200 或者 >=400 时"), n.resolve) {
|
|
370
|
+
case "body":
|
|
371
|
+
e.push(" 将响应内容格式化为字符串并作为错误消息");
|
|
372
|
+
break;
|
|
373
|
+
case "json":
|
|
374
|
+
e.push(" 将响应解析为json,并读取 " + (n.messageField || b) + " 作为错误消息");
|
|
375
|
+
break;
|
|
376
|
+
}
|
|
377
|
+
const s = t.ok || { resolve: "body" };
|
|
378
|
+
switch (e.push("- 当http状态码 >=200 并且 <400 时"), s.resolve) {
|
|
379
|
+
case "body":
|
|
380
|
+
e.push(" 将响应尝试解析为 json,并作为数据内容返回");
|
|
381
|
+
break;
|
|
382
|
+
case "json":
|
|
383
|
+
e.push(
|
|
384
|
+
" 将响应解析为 json,读取 " + (s.dataField || L) + " 作为响应数据,读取 " + (s.messageField || b) + " 作为提示消息"
|
|
385
|
+
), s.statusField && e.push(
|
|
386
|
+
" 当 " + s.statusField + " 为 " + (s.statusOKValue || "空值") + " 时是成功提示,否则是错误消息"
|
|
387
|
+
), s.ignoreMessage && e.push(" 并忽略以下消息:" + s.ignoreMessage);
|
|
388
|
+
break;
|
|
389
|
+
}
|
|
390
|
+
return e.join(`
|
|
391
|
+
`);
|
|
392
|
+
};
|
|
393
|
+
function te(t, e, n) {
|
|
394
|
+
const s = t || { resolve: "json", messageField: b }, r = {
|
|
395
|
+
ok: !1,
|
|
396
|
+
code: e,
|
|
397
|
+
message: n,
|
|
398
|
+
data: null
|
|
399
|
+
};
|
|
400
|
+
switch (s.resolve) {
|
|
401
|
+
case "body":
|
|
402
|
+
r.message = T(n) || n;
|
|
403
|
+
break;
|
|
404
|
+
case "json":
|
|
405
|
+
const { code: o, message: a } = se(n, s.converter, s.statusField, s.messageField);
|
|
406
|
+
r.code = o || e, r.message = T(n) || a;
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
return r;
|
|
410
|
+
}
|
|
411
|
+
function se(t, e, n, s = b) {
|
|
412
|
+
if (!U(t))
|
|
413
|
+
return { message: "" };
|
|
414
|
+
const r = E(w(t), e);
|
|
415
|
+
return !r || !M(r) ? { message: t } : {
|
|
416
|
+
code: n ? R(r, n) : "",
|
|
417
|
+
message: R(r, s) || t
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
function R(t, e) {
|
|
421
|
+
const n = Array.isArray(e) ? e : [e];
|
|
422
|
+
for (const s of n)
|
|
423
|
+
if (s in t)
|
|
424
|
+
return ne(t[s]);
|
|
425
|
+
return "";
|
|
426
|
+
}
|
|
427
|
+
function ne(t) {
|
|
428
|
+
return t ? typeof t == "string" ? t : JSON.stringify(t) : "";
|
|
429
|
+
}
|
|
430
|
+
const re = /<title>([^<]+)<\/title>/i, oe = /<message>([^<]+)<\/message>/i;
|
|
431
|
+
function T(t) {
|
|
432
|
+
const e = t.match(re);
|
|
433
|
+
if (e)
|
|
434
|
+
return e[1];
|
|
435
|
+
const n = t.match(oe);
|
|
436
|
+
return n ? n[1] : "";
|
|
437
|
+
}
|
|
438
|
+
function ae(t, e, n, s) {
|
|
439
|
+
const r = t || { resolve: "body" }, o = {
|
|
440
|
+
ok: !0,
|
|
441
|
+
code: n,
|
|
442
|
+
message: "",
|
|
443
|
+
data: null
|
|
444
|
+
};
|
|
445
|
+
if (e === 204 || !s)
|
|
446
|
+
return o;
|
|
447
|
+
if (r.resolve === "body")
|
|
448
|
+
return o.data = U(s) ? E(w(s), t.converter) : s, o;
|
|
449
|
+
const a = E(w(s), t.converter);
|
|
450
|
+
if (!a || !M(a))
|
|
451
|
+
return o.ok = !1, o.code = "ResponseFormatError", o.message = "响应内容无法格式化为 Object", o;
|
|
452
|
+
const c = r.statusField, u = r.statusOKValue || "", f = r.dataField || L, h = r.messageField || b, i = r.ignoreMessage || "";
|
|
453
|
+
if (c && !(c in a))
|
|
454
|
+
return o.ok = !1, o.code = "ResponseFieldMissing", o.message = "响应内容找不到状态字段 " + c, o;
|
|
455
|
+
const g = c ? a[c] + "" : "";
|
|
456
|
+
return o.ok = c ? g === u : !0, o.code = g || n, o.data = f === !0 ? a : f in a ? a[f] : null, o.message = R(a, h), i && o.message && (Array.isArray(i) && i.includes(o.message) || typeof i == "string" && o.message === i) && (o.message = ""), o;
|
|
457
|
+
}
|
|
458
|
+
function j(t) {
|
|
459
|
+
return t >= 200 && t < 400;
|
|
460
|
+
}
|
|
461
|
+
function E(t, e) {
|
|
462
|
+
return e ? typeof e == "function" ? e(t) : (console.warn('工具不再支持 "camelize", "snakify" 参数,请自行提供函数来转化 body 内容'), t) : t;
|
|
463
|
+
}
|
|
464
|
+
const ce = q("APIError");
|
|
465
|
+
function ie(t) {
|
|
466
|
+
const e = {};
|
|
467
|
+
for (const r in t.headers)
|
|
468
|
+
(r.startsWith("x-") || r.includes("trace") || r.includes("server") || /\b(?:id|uuid)\b/.test(r)) && (e[r] = t.headers[r]);
|
|
469
|
+
const n = t.url.replace(/^(?:https?:)?\/*/i, "").replace(/\?.+/, ""), s = t.status < 0 ? "unknown" : t.status;
|
|
470
|
+
return {
|
|
471
|
+
sentryError: new ce(`${n} | ${s}${t.code ? ` | ${t.code}` : ""}`),
|
|
472
|
+
sentryTags: {
|
|
473
|
+
...e,
|
|
474
|
+
status: s,
|
|
475
|
+
method: t.method,
|
|
476
|
+
code: t.code || "unknown",
|
|
477
|
+
message: t.message || "empty"
|
|
478
|
+
},
|
|
479
|
+
sentryExtra: {
|
|
480
|
+
url: t.url,
|
|
481
|
+
responseBody: t.body || "empty",
|
|
482
|
+
responseHeaders: t.headers,
|
|
483
|
+
rawError: t.error
|
|
484
|
+
}
|
|
485
|
+
};
|
|
486
|
+
}
|
|
487
|
+
function ue(t) {
|
|
488
|
+
return t.reduce(
|
|
489
|
+
(e, [n, s]) => (n && (e[n] = s || ""), e),
|
|
490
|
+
{}
|
|
491
|
+
);
|
|
492
|
+
}
|
|
493
|
+
function Ee(t, e, n, s) {
|
|
494
|
+
const r = t.status, o = t.method, a = ue(
|
|
495
|
+
Object.entries(t.headers || {}).map(([m, y]) => [m.toLowerCase(), y])
|
|
496
|
+
), { ok: c, code: u, data: f, message: h } = ee(
|
|
497
|
+
r,
|
|
498
|
+
t.statusText,
|
|
499
|
+
t.body,
|
|
500
|
+
n.get("responseRule"),
|
|
501
|
+
s?.responseRule
|
|
502
|
+
);
|
|
503
|
+
if (!j(r)) {
|
|
504
|
+
const m = ie({
|
|
505
|
+
url: t.url,
|
|
506
|
+
method: t.method,
|
|
507
|
+
status: r,
|
|
508
|
+
code: u,
|
|
509
|
+
message: h,
|
|
510
|
+
body: t.body,
|
|
511
|
+
headers: a,
|
|
512
|
+
error: t.rawError
|
|
513
|
+
});
|
|
514
|
+
(n.get("errorHandler") || he)({
|
|
515
|
+
url: e,
|
|
516
|
+
method: o,
|
|
517
|
+
status: r,
|
|
518
|
+
code: u,
|
|
519
|
+
message: h,
|
|
520
|
+
headers: a,
|
|
521
|
+
rawError: t.rawError,
|
|
522
|
+
responseBody: t.body,
|
|
523
|
+
...m
|
|
524
|
+
});
|
|
525
|
+
}
|
|
526
|
+
if (r < 0)
|
|
527
|
+
return O(
|
|
528
|
+
{ ok: !1, status: r, code: t.statusText, headers: {}, message: "", data: null },
|
|
529
|
+
`${o} ${e} ${t.statusText}`,
|
|
530
|
+
o,
|
|
531
|
+
e,
|
|
532
|
+
n,
|
|
533
|
+
s
|
|
534
|
+
);
|
|
535
|
+
const i = { ok: c, data: f, code: u, message: h, status: r, headers: a };
|
|
536
|
+
n.get("responseHandler")?.({ ...i }, o, e);
|
|
537
|
+
const g = c ? h : h || t.statusText;
|
|
538
|
+
return O(i, g, o, e, n, s);
|
|
539
|
+
}
|
|
540
|
+
function O(t, e, n, s, r, o) {
|
|
541
|
+
const a = r.get("message"), c = a === !1 || o?.message === !1 ? !1 : o?.message || a;
|
|
542
|
+
if (c !== !1) {
|
|
543
|
+
const u = typeof c == "function" ? c(t, n, s, e) : e;
|
|
544
|
+
u instanceof Error ? r.showMessage(!0, u.message, t.code, t.status) : u && typeof u == "object" && "message" in u ? r.showMessage(!1, u.message, t.code, t.status) : r.showMessage(!t.ok, u, t.code, t.status);
|
|
545
|
+
}
|
|
546
|
+
return t;
|
|
547
|
+
}
|
|
548
|
+
function he(t) {
|
|
549
|
+
const e = {};
|
|
550
|
+
for (const n in t)
|
|
551
|
+
n.startsWith("sentry") || (e[n] = t[n]);
|
|
552
|
+
console.error("RequestError", e);
|
|
553
|
+
}
|
|
554
|
+
async function le(t, e, n, s, r) {
|
|
555
|
+
const o = r || 0, a = Math.max(0, Math.min(10, s?.maxRetry ?? n.get("maxRetry") ?? 0)), c = s?.retryResolve ?? n.get("retryResolve"), u = n.get("logHandler");
|
|
556
|
+
u?.({
|
|
557
|
+
type: "prepare",
|
|
558
|
+
url: e,
|
|
559
|
+
method: s?.method || "GET",
|
|
560
|
+
retry: o,
|
|
561
|
+
maxRetry: a,
|
|
562
|
+
message: o === 0 ? "start" : `retry ${o}/${a} start`,
|
|
563
|
+
headers: s?.headers,
|
|
564
|
+
options: s
|
|
565
|
+
});
|
|
566
|
+
const f = Date.now(), h = await t(e, n, s), i = h.status, g = Date.now() - f, m = `[cost ${g}][${i}] ${i < 0 ? h.body : ""}`;
|
|
567
|
+
u?.({
|
|
568
|
+
type: "finished",
|
|
569
|
+
url: e,
|
|
570
|
+
method: h.method,
|
|
571
|
+
retry: o,
|
|
572
|
+
maxRetry: a,
|
|
573
|
+
message: o === 0 ? `finish ${m}` : `retry ${o}/${a} finish ${m}`,
|
|
574
|
+
response: h,
|
|
575
|
+
headers: h.headers,
|
|
576
|
+
cost: g
|
|
577
|
+
});
|
|
578
|
+
const y = j(i);
|
|
579
|
+
if (!a || o >= a || c === "network" && i > 0 || c === "status" && y || Array.isArray(c) && !c.includes(i) || typeof c == "function" && c(h, o) !== !0)
|
|
580
|
+
return h;
|
|
581
|
+
const d = s?.retryInterval ?? n.get("retryInterval") ?? 100;
|
|
582
|
+
return await S(
|
|
583
|
+
Math.max(
|
|
584
|
+
100,
|
|
585
|
+
d === "2EB" ? Math.pow(2, o) * 100 : typeof d == "function" ? d(o + 1, { url: e, status: i, method: h.method }) || 0 : d
|
|
586
|
+
)
|
|
587
|
+
), await le(t, e, n, s, o + 1);
|
|
588
|
+
}
|
|
589
|
+
export {
|
|
590
|
+
be as N,
|
|
591
|
+
pe as P,
|
|
592
|
+
fe as Q,
|
|
593
|
+
z as R,
|
|
594
|
+
de as V,
|
|
595
|
+
ye as W,
|
|
596
|
+
A as a,
|
|
597
|
+
P as b,
|
|
598
|
+
we as c,
|
|
599
|
+
k as e,
|
|
600
|
+
ue as f,
|
|
601
|
+
Re as g,
|
|
602
|
+
Ee as h,
|
|
603
|
+
ge as n,
|
|
604
|
+
me as o,
|
|
605
|
+
le as r
|
|
606
|
+
};
|
package/dist/wx.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
import { h as
|
|
2
|
-
import { g as
|
|
3
|
-
const
|
|
4
|
-
return
|
|
5
|
-
},
|
|
6
|
-
const e = await
|
|
7
|
-
return
|
|
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);
|
|
7
|
+
return h() ? new Promise((c) => {
|
|
8
8
|
wx.request({
|
|
9
9
|
url: u,
|
|
10
10
|
data: e.body,
|
|
@@ -21,14 +21,14 @@ const x = async function(t, n, r) {
|
|
|
21
21
|
body: ""
|
|
22
22
|
});
|
|
23
23
|
},
|
|
24
|
-
success(
|
|
24
|
+
success(n) {
|
|
25
25
|
c({
|
|
26
26
|
url: u,
|
|
27
27
|
method: a,
|
|
28
|
-
status:
|
|
29
|
-
statusText:
|
|
30
|
-
headers: { ...
|
|
31
|
-
body: e.method === "HEAD" ||
|
|
28
|
+
status: n.statusCode,
|
|
29
|
+
statusText: n.statusCode + "",
|
|
30
|
+
headers: { ...n.header },
|
|
31
|
+
body: e.method === "HEAD" || n.statusCode === 204 ? "" : R(n.data)
|
|
32
32
|
});
|
|
33
33
|
}
|
|
34
34
|
});
|
|
@@ -40,21 +40,21 @@ const x = async function(t, n, r) {
|
|
|
40
40
|
body: ""
|
|
41
41
|
};
|
|
42
42
|
};
|
|
43
|
-
function m(t) {
|
|
44
|
-
return typeof t == "string" ? t : t instanceof ArrayBuffer && i.TextDecoder ? new TextDecoder().decode(t) : JSON.stringify(t);
|
|
45
|
-
}
|
|
46
43
|
function R(t) {
|
|
47
|
-
return new
|
|
44
|
+
return typeof t == "string" ? t : t instanceof ArrayBuffer && y() ? new TextDecoder().decode(t) : JSON.stringify(t);
|
|
45
|
+
}
|
|
46
|
+
function q(t) {
|
|
47
|
+
return new g(m, t);
|
|
48
48
|
}
|
|
49
|
-
const s =
|
|
49
|
+
const s = q(), T = s.setConfig, b = s.head, C = s.get, N = s.post, S = s.del, A = s.put;
|
|
50
50
|
export {
|
|
51
|
-
|
|
51
|
+
q as NetRequest,
|
|
52
52
|
d as RequestInternalError,
|
|
53
|
-
|
|
53
|
+
S as del,
|
|
54
54
|
C as get,
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
55
|
+
O as getResponseRulesDescription,
|
|
56
|
+
b as head,
|
|
57
|
+
N as post,
|
|
58
|
+
A as put,
|
|
59
59
|
T as setGlobalConfig
|
|
60
60
|
};
|