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