@seayoo-web/request 1.6.0 → 1.6.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/inc/detect.d.ts +9 -0
- package/dist/index.cjs +2 -2
- package/dist/index.js +83 -83
- package/dist/node.cjs +1 -1
- package/dist/node.js +9 -9
- package/dist/version-D6D6EXmg.cjs +2 -0
- package/dist/version-D9usx_Q7.js +437 -0
- package/dist/version.d.ts +1 -1
- package/dist/wx.cjs +1 -1
- package/dist/wx.js +17 -17
- package/package.json +8 -8
- package/dist/version-CLBYgaXw.js +0 -427
- package/dist/version-DJIdoURF.cjs +0 -2
package/dist/version-CLBYgaXw.js
DELETED
|
@@ -1,427 +0,0 @@
|
|
|
1
|
-
var C = Object.defineProperty;
|
|
2
|
-
var L = (t, e, a) => e in t ? C(t, e, { enumerable: !0, configurable: !0, writable: !0, value: a }) : t[e] = a;
|
|
3
|
-
var f = (t, e, a) => (L(t, typeof e != "symbol" ? e + "" : e, a), a);
|
|
4
|
-
async function te(t, e, a) {
|
|
5
|
-
var b;
|
|
6
|
-
const s = Object.assign({ method: "GET" }, a), r = "FormData" in globalThis ? s.body instanceof FormData : !1, n = r && s.method !== "POST" && s.method !== "PUT" ? "POST" : s.method;
|
|
7
|
-
(n === "GET" || n === "HEAD" || n === "DELETE") && s.body !== void 0 && (console.warn("request body is invalid with method get, head, delete"), delete s.body);
|
|
8
|
-
const o = Object.assign(
|
|
9
|
-
r ? {} : {
|
|
10
|
-
"Content-Type": "Blob" in globalThis && s.body instanceof Blob ? s.body.type || "application/octet-stream" : "application/json;charset=utf-8"
|
|
11
|
-
},
|
|
12
|
-
s.headers
|
|
13
|
-
), i = s.params || {}, c = {};
|
|
14
|
-
Object.keys(i).forEach((h) => {
|
|
15
|
-
i[h] !== void 0 && (c[h] = D(i[h]));
|
|
16
|
-
});
|
|
17
|
-
const d = e.getFullUrl(t), l = A(s.body), u = s.timeout || e.get("timeout"), g = await async function() {
|
|
18
|
-
var O;
|
|
19
|
-
const h = e.get("requestTransformer");
|
|
20
|
-
return h ? await h({ headers: o, params: c, method: n, url: d, body: l }) : await ((O = e.get("requestHandler")) == null ? void 0 : O(o, c, n, t));
|
|
21
|
-
}(), m = typeof g == "string" && g ? g : d;
|
|
22
|
-
return (b = e.get("logHandler")) == null || b({ type: "ready", url: m, method: n, headers: o, timeout: u, body: l }), {
|
|
23
|
-
url: m,
|
|
24
|
-
method: n,
|
|
25
|
-
body: l,
|
|
26
|
-
params: c,
|
|
27
|
-
headers: o,
|
|
28
|
-
timeout: u,
|
|
29
|
-
abort: s.abort,
|
|
30
|
-
credentials: s.credentials || e.get("credentials")
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
function D(t) {
|
|
34
|
-
return typeof t == "string" ? t : Array.isArray(t) ? t.join(",") : t + "";
|
|
35
|
-
}
|
|
36
|
-
function A(t) {
|
|
37
|
-
if (t)
|
|
38
|
-
return typeof t == "string" || t instanceof URLSearchParams || t instanceof ArrayBuffer || "Blob" in globalThis && t instanceof Blob || "FormData" in globalThis && t instanceof FormData ? t : JSON.stringify(t);
|
|
39
|
-
}
|
|
40
|
-
function U(t) {
|
|
41
|
-
return new Promise((e) => setTimeout(e, Math.max(0, t)));
|
|
42
|
-
}
|
|
43
|
-
function x() {
|
|
44
|
-
}
|
|
45
|
-
function p(t, e) {
|
|
46
|
-
try {
|
|
47
|
-
const a = JSON.parse(t);
|
|
48
|
-
return e ? e(a) ? a : null : a;
|
|
49
|
-
} catch {
|
|
50
|
-
return null;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
"" + Math.random().toString(32).slice(2);
|
|
54
|
-
const P = /^(?:https?:)?\/\/.+$/i, B = /^https?:\/\/.+$/i, H = /^\{[\d\D]*\}$/, I = /^\[[\d\D]*\]$/;
|
|
55
|
-
function R(t, e = !1) {
|
|
56
|
-
return e ? P.test(t) : B.test(t);
|
|
57
|
-
}
|
|
58
|
-
function K(t) {
|
|
59
|
-
return t == null;
|
|
60
|
-
}
|
|
61
|
-
function k(t, ...e) {
|
|
62
|
-
if (!t || typeof t != "object")
|
|
63
|
-
return !1;
|
|
64
|
-
const a = Object.getPrototypeOf(t);
|
|
65
|
-
return a !== Object.prototype && a !== null ? !1 : e.every((s) => s in t);
|
|
66
|
-
}
|
|
67
|
-
function M(t) {
|
|
68
|
-
return H.test(t) || I.test(t);
|
|
69
|
-
}
|
|
70
|
-
async function se(t) {
|
|
71
|
-
return await new Promise(function(e) {
|
|
72
|
-
const a = document.getElementsByTagName("head")[0], s = document.createElement("script");
|
|
73
|
-
s.setAttribute("type", "text/javascript"), s.setAttribute("charset", "utf-8"), s.onload = function() {
|
|
74
|
-
a.removeChild(s), e(!0);
|
|
75
|
-
}, s.onerror = function() {
|
|
76
|
-
a.removeChild(s), e(!1);
|
|
77
|
-
}, s.setAttribute("src", t), a.appendChild(s);
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
function S(t, e = "数据未能正确识别") {
|
|
81
|
-
return typeof t == "function" ? {
|
|
82
|
-
guard: t,
|
|
83
|
-
message: e
|
|
84
|
-
} : {
|
|
85
|
-
guard: t.guard,
|
|
86
|
-
message: t.message || e
|
|
87
|
-
};
|
|
88
|
-
}
|
|
89
|
-
function $(t, e = "") {
|
|
90
|
-
return !e || R(t, !0) ? v(t) : (v(e) + "/" + t).replace(/\/{2,}/g, "/").replace(/:\//, "://");
|
|
91
|
-
}
|
|
92
|
-
function v(t) {
|
|
93
|
-
return R(t, !0) ? t.startsWith("http") ? t : ("location" in globalThis ? location.protocol : "https:") + t : ("location" in globalThis ? location.origin : "http://127.0.0.1") + "/" + t.replace(/^\/+/, "");
|
|
94
|
-
}
|
|
95
|
-
function N(t, e) {
|
|
96
|
-
if (e) {
|
|
97
|
-
if (e === !0)
|
|
98
|
-
return t.replace(/\?[^#]*/, "");
|
|
99
|
-
} else
|
|
100
|
-
return t;
|
|
101
|
-
const a = t.split("#"), s = a[0].split("?"), r = s[0], n = s.length > 1 ? s[1] : "", o = a.length > 1 ? "#" + a[1] : "", i = typeof e == "string" ? [e] : Array.isArray(e) ? e : [];
|
|
102
|
-
return !i.length || !n ? a[0] + o : (i.map((c) => c.replace(/([\\(){}[\]^$+\-*?|])/g, "\\$1")), (r + "?" + n.replace(new RegExp("(?:^|&)(?:" + i.join("|") + ")=[^&$]+", "g"), "").replace(/^&/, "")).replace(/\?$/, "") + o);
|
|
103
|
-
}
|
|
104
|
-
function ae(t, e, a = !1) {
|
|
105
|
-
const s = typeof e == "string" ? e : Object.keys(e).map((o) => `${o}=${encodeURIComponent(e[o])}`).join("&");
|
|
106
|
-
if (!s)
|
|
107
|
-
return t;
|
|
108
|
-
const r = t.split("#");
|
|
109
|
-
a && (r[0] = N(
|
|
110
|
-
r[0],
|
|
111
|
-
(s.match(/([^=&#?]+)=[^&#]+/g) || []).map((o) => o.replace(/=.+$/, ""))
|
|
112
|
-
));
|
|
113
|
-
const n = r[0].indexOf("?") + 1 ? "&" : "?";
|
|
114
|
-
return (r[0] + n + s + (r.length > 1 ? "#" + r[1] : "")).replace(/\?&/, "?");
|
|
115
|
-
}
|
|
116
|
-
const E = "data", y = "message";
|
|
117
|
-
function q(t, e, a, s, r) {
|
|
118
|
-
const n = r || s;
|
|
119
|
-
return j(t) ? Y(n.ok || s.ok, t, e, a) : G(n.failed || s.failed, e, a);
|
|
120
|
-
}
|
|
121
|
-
const ne = function(t) {
|
|
122
|
-
const e = [], a = t.failed || { resolve: "json" };
|
|
123
|
-
switch (e.push("- 当http状态码 <200 或者 >=400 时"), a.resolve) {
|
|
124
|
-
case "body":
|
|
125
|
-
e.push(" 将响应内容格式化为字符串并作为错误消息");
|
|
126
|
-
break;
|
|
127
|
-
case "json":
|
|
128
|
-
e.push(" 将响应解析为json,并读取 " + (a.messageField || y) + " 作为错误消息");
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
const s = t.ok || { resolve: "body" };
|
|
132
|
-
switch (e.push("- 当http状态码 >=200 并且 <400 时"), s.resolve) {
|
|
133
|
-
case "body":
|
|
134
|
-
e.push(" 将响应尝试解析为 json,并作为数据内容返回");
|
|
135
|
-
break;
|
|
136
|
-
case "json":
|
|
137
|
-
e.push(
|
|
138
|
-
" 将响应解析为 json,读取 " + (s.dataField || E) + " 作为响应数据,读取 " + (s.messageField || y) + " 作为提示消息"
|
|
139
|
-
), s.statusField && e.push(" 当 " + s.statusField + " 为 " + (s.statusOKValue || "空值") + " 时是成功提示,否则是错误消息"), s.ignoreMessage && e.push(" 并忽略以下消息:" + s.ignoreMessage);
|
|
140
|
-
break;
|
|
141
|
-
}
|
|
142
|
-
return e.join(`
|
|
143
|
-
`);
|
|
144
|
-
};
|
|
145
|
-
function G(t, e, a) {
|
|
146
|
-
const s = t || { resolve: "json", messageField: y }, r = {
|
|
147
|
-
ok: !1,
|
|
148
|
-
code: e,
|
|
149
|
-
message: a,
|
|
150
|
-
data: null
|
|
151
|
-
};
|
|
152
|
-
switch (s.resolve) {
|
|
153
|
-
case "body":
|
|
154
|
-
r.message = F(a) || a;
|
|
155
|
-
break;
|
|
156
|
-
case "json":
|
|
157
|
-
const { code: n, message: o } = J(a, s.statusField, s.messageField);
|
|
158
|
-
r.code = n || e, r.message = F(a) || o;
|
|
159
|
-
break;
|
|
160
|
-
}
|
|
161
|
-
return r;
|
|
162
|
-
}
|
|
163
|
-
function J(t, e, a = y) {
|
|
164
|
-
if (!M(t))
|
|
165
|
-
return { message: "" };
|
|
166
|
-
const s = p(t);
|
|
167
|
-
return !s || !k(s) ? { message: t } : {
|
|
168
|
-
code: e ? w(s, e) : "",
|
|
169
|
-
message: w(s, a) || t
|
|
170
|
-
};
|
|
171
|
-
}
|
|
172
|
-
function w(t, e) {
|
|
173
|
-
const a = Array.isArray(e) ? e : [e];
|
|
174
|
-
for (const s of a)
|
|
175
|
-
if (s in t)
|
|
176
|
-
return _(t[s]);
|
|
177
|
-
return "";
|
|
178
|
-
}
|
|
179
|
-
function _(t) {
|
|
180
|
-
return t ? typeof t == "string" ? t : JSON.stringify(t) : "";
|
|
181
|
-
}
|
|
182
|
-
const V = /<title>([^<]+)<\/title>/i, W = /<message>([^<]+)<\/message>/i;
|
|
183
|
-
function F(t) {
|
|
184
|
-
const e = t.match(V);
|
|
185
|
-
if (e)
|
|
186
|
-
return e[1];
|
|
187
|
-
const a = t.match(W);
|
|
188
|
-
return a ? a[1] : "";
|
|
189
|
-
}
|
|
190
|
-
function Y(t, e, a, s) {
|
|
191
|
-
const r = t || { resolve: "body" }, n = {
|
|
192
|
-
ok: !0,
|
|
193
|
-
code: a,
|
|
194
|
-
message: "",
|
|
195
|
-
data: null
|
|
196
|
-
};
|
|
197
|
-
if (e === 202 || e === 204 || !s)
|
|
198
|
-
return n;
|
|
199
|
-
if (r.resolve === "body")
|
|
200
|
-
return n.data = M(s) ? p(s) : s, n;
|
|
201
|
-
const o = p(s);
|
|
202
|
-
if (!o || !k(o))
|
|
203
|
-
return n.ok = !1, n.code = "ResponseFormatError", n.message = "响应内容无法格式化为 Object", n;
|
|
204
|
-
const i = r.statusField, c = r.statusOKValue || "", d = r.dataField || E, l = r.messageField || y, u = r.ignoreMessage || "";
|
|
205
|
-
if (i && !(i in o))
|
|
206
|
-
return n.ok = !1, n.code = "ResponseFieldMissing", n.message = "响应内容找不到状态字段 " + i, n;
|
|
207
|
-
const g = i ? o[i] + "" : "";
|
|
208
|
-
return n.ok = i ? g === c : !0, n.code = g || a, n.data = d === !0 ? o : d in o ? o[d] : null, n.message = w(o, l), u && n.message && (Array.isArray(u) && u.includes(n.message) || typeof u == "string" && n.message === u) && (n.message = ""), n;
|
|
209
|
-
}
|
|
210
|
-
function j(t) {
|
|
211
|
-
return t >= 200 && t < 400;
|
|
212
|
-
}
|
|
213
|
-
async function X(t, e, a, s, r) {
|
|
214
|
-
const n = r || 0, o = Math.max(0, Math.min(10, (s == null ? void 0 : s.maxRetry) ?? a.get("maxRetry") ?? 0)), i = (s == null ? void 0 : s.retryResolve) ?? a.get("retryResolve"), c = a.get("logHandler") || x;
|
|
215
|
-
c({
|
|
216
|
-
type: "prepear",
|
|
217
|
-
url: e,
|
|
218
|
-
method: (s == null ? void 0 : s.method) || "GET",
|
|
219
|
-
retry: n,
|
|
220
|
-
maxRetry: o,
|
|
221
|
-
message: n === 0 ? "start" : `retry ${n}/${o} start`,
|
|
222
|
-
headers: s == null ? void 0 : s.headers,
|
|
223
|
-
options: s
|
|
224
|
-
});
|
|
225
|
-
const d = Date.now(), l = await t(e, a, s), u = l.status, g = Date.now() - d, m = `[cost ${g}][${u}] ${u < 0 ? l.body : ""}`;
|
|
226
|
-
c({
|
|
227
|
-
type: "finished",
|
|
228
|
-
url: e,
|
|
229
|
-
method: l.method,
|
|
230
|
-
retry: n,
|
|
231
|
-
maxRetry: o,
|
|
232
|
-
message: n === 0 ? `finish ${m}` : `retry ${n}/${o} finish ${m}`,
|
|
233
|
-
response: l,
|
|
234
|
-
headers: l.headers,
|
|
235
|
-
cost: g
|
|
236
|
-
});
|
|
237
|
-
const b = j(u);
|
|
238
|
-
if (!o || i === "network" && u > 0 || i === "status" && b || Array.isArray(i) && !i.includes(u) || typeof i == "function" && i(l, n) !== !0 || n >= o)
|
|
239
|
-
return l;
|
|
240
|
-
const h = (s == null ? void 0 : s.retryInterval) ?? a.get("retryInterval") ?? 1e3;
|
|
241
|
-
return await U(
|
|
242
|
-
Math.max(
|
|
243
|
-
100,
|
|
244
|
-
h === "2EB" ? Math.pow(2, n) * 1e3 : typeof h == "function" ? h(n + 1) || 0 : h
|
|
245
|
-
)
|
|
246
|
-
), await X(t, e, a, s, n + 1);
|
|
247
|
-
}
|
|
248
|
-
function re(t, e, a, s) {
|
|
249
|
-
var o, i;
|
|
250
|
-
if (t.status < 0)
|
|
251
|
-
return T(
|
|
252
|
-
{ ok: !1, status: t.status, code: t.statusText, headers: {}, message: "", data: null },
|
|
253
|
-
`${t.method} ${e} ${t.statusText}`,
|
|
254
|
-
t.method,
|
|
255
|
-
e,
|
|
256
|
-
a,
|
|
257
|
-
s
|
|
258
|
-
);
|
|
259
|
-
j(t.status) || (o = a.get("errorHandler")) == null || o(t.status, t.method, e);
|
|
260
|
-
const r = {
|
|
261
|
-
...q(t.status, t.statusText, t.body, a.get("responseRule"), s == null ? void 0 : s.responseRule),
|
|
262
|
-
status: t.status,
|
|
263
|
-
headers: Object.fromEntries(Object.entries(t.headers || {}).map(([c, d]) => [c.toLowerCase(), d]))
|
|
264
|
-
};
|
|
265
|
-
(i = a.get("responseHandler")) == null || i({ ...r }, t.method, e);
|
|
266
|
-
const n = r.ok ? r.message : `${t.method} ${e} [${r.code || t.statusText}] ${r.message || t.statusText}`;
|
|
267
|
-
return T(r, n, t.method, e, a, s);
|
|
268
|
-
}
|
|
269
|
-
function T(t, e, a, s, r, n) {
|
|
270
|
-
const o = r.get("message"), i = o === !1 || (n == null ? void 0 : n.message) === !1 ? !1 : (n == null ? void 0 : n.message) || o;
|
|
271
|
-
if (i !== !1) {
|
|
272
|
-
const c = typeof i == "function" ? i(t, a, s, e) : e;
|
|
273
|
-
c instanceof Error ? r.showMessage(!0, c.message) : c && typeof c == "object" && "message" in c ? r.showMessage(!1, c.message) : r.showMessage(!t.ok, c);
|
|
274
|
-
}
|
|
275
|
-
return t;
|
|
276
|
-
}
|
|
277
|
-
class z {
|
|
278
|
-
constructor(e) {
|
|
279
|
-
// 保存的配置需要部分字段强制设置默认值
|
|
280
|
-
f(this, "config", {
|
|
281
|
-
baseURL: "",
|
|
282
|
-
maxRetry: 0,
|
|
283
|
-
retryInterval: 1e3,
|
|
284
|
-
retryResolve: "network",
|
|
285
|
-
timeout: 5e3,
|
|
286
|
-
cacheTTL: 500,
|
|
287
|
-
credentials: "same-origin",
|
|
288
|
-
responseRule: {
|
|
289
|
-
ok: {
|
|
290
|
-
resolve: "body"
|
|
291
|
-
},
|
|
292
|
-
failed: {
|
|
293
|
-
resolve: "json",
|
|
294
|
-
messageField: "message"
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
});
|
|
298
|
-
e && this.set(e);
|
|
299
|
-
}
|
|
300
|
-
set(e) {
|
|
301
|
-
if (e.baseURL && !/^\/.+/.test(e.baseURL) && !R(e.baseURL))
|
|
302
|
-
throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"), new Error("BaseURLError");
|
|
303
|
-
Object.assign(this.config, e);
|
|
304
|
-
}
|
|
305
|
-
get(e) {
|
|
306
|
-
return this.config[e];
|
|
307
|
-
}
|
|
308
|
-
/** 基于 baseURL 返回完整的 url 地址, 如果 url 地址以 / 开头则表示忽略 baseURL 的值 */
|
|
309
|
-
getFullUrl(e) {
|
|
310
|
-
return e.startsWith("/") ? $(e) : $(e, this.config.baseURL);
|
|
311
|
-
}
|
|
312
|
-
/** 提示消息 */
|
|
313
|
-
showMessage(e, a) {
|
|
314
|
-
this.config.messageHandler && a && this.config.messageHandler(e, a);
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
class Q {
|
|
318
|
-
constructor(e = 500) {
|
|
319
|
-
f(this, "ttl");
|
|
320
|
-
f(this, "cache");
|
|
321
|
-
this.cache = {}, this.ttl = Math.max(e, 0);
|
|
322
|
-
}
|
|
323
|
-
getKey(e, a) {
|
|
324
|
-
const s = Object.keys(a || {}).sort().map((r) => `${r}#${a == null ? void 0 : a[r]}`);
|
|
325
|
-
return e + s.join(",");
|
|
326
|
-
}
|
|
327
|
-
updateTTL(e) {
|
|
328
|
-
this.ttl = Math.max(e, 0);
|
|
329
|
-
}
|
|
330
|
-
get(e) {
|
|
331
|
-
if (this.ttl === 0)
|
|
332
|
-
return null;
|
|
333
|
-
const a = this.cache[e];
|
|
334
|
-
return a ? a.ttl < Date.now() ? (delete this.cache[e], null) : a.res : null;
|
|
335
|
-
}
|
|
336
|
-
set(e, a) {
|
|
337
|
-
this.ttl !== 0 && (this.cache[e] = {
|
|
338
|
-
ttl: Date.now() + this.ttl,
|
|
339
|
-
res: a
|
|
340
|
-
});
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
function Z(t, e, a, s) {
|
|
344
|
-
if (e.ok && !K(e.data) && s) {
|
|
345
|
-
const r = S(s, "响应数据未能正确识别");
|
|
346
|
-
return r.guard(e.data) || (console.error("ResponseCheckFaild", t, e.data), a.showMessage(!0, `${t} ${r.message}`), e.data = null), e;
|
|
347
|
-
}
|
|
348
|
-
return e;
|
|
349
|
-
}
|
|
350
|
-
class oe {
|
|
351
|
-
constructor(e, a) {
|
|
352
|
-
f(this, "agent");
|
|
353
|
-
f(this, "config");
|
|
354
|
-
f(this, "cache");
|
|
355
|
-
this.config = new z(a), this.agent = e, this.cache = new Q(this.config.get("cacheTTL")), this.setConfig = this.setConfig.bind(this), this.getConfig = this.getConfig.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);
|
|
356
|
-
}
|
|
357
|
-
/**
|
|
358
|
-
* 执行网络请求
|
|
359
|
-
*/
|
|
360
|
-
async exec(e, a) {
|
|
361
|
-
return await this.agent(e, this.config, a);
|
|
362
|
-
}
|
|
363
|
-
/**
|
|
364
|
-
* 检查响应的数据类型
|
|
365
|
-
*/
|
|
366
|
-
async guard(e, a, s) {
|
|
367
|
-
return Z(e, a, this.config, s);
|
|
368
|
-
}
|
|
369
|
-
/**
|
|
370
|
-
* 修改默认请求配置: baesURL / timeout / credentials / errorHandler / messageHandler / responseHandler / logHandler / responseRule
|
|
371
|
-
*/
|
|
372
|
-
setConfig(e) {
|
|
373
|
-
this.config.set(e), this.cache.updateTTL(this.config.get("cacheTTL"));
|
|
374
|
-
}
|
|
375
|
-
/**
|
|
376
|
-
* 读取默认的请求配置
|
|
377
|
-
*/
|
|
378
|
-
getConfig(e) {
|
|
379
|
-
return this.config.get(e);
|
|
380
|
-
}
|
|
381
|
-
/**
|
|
382
|
-
* 发送一个 HEAD 请求,并且不处理响应 body
|
|
383
|
-
*/
|
|
384
|
-
async head(e, a) {
|
|
385
|
-
const s = Object.assign({}, a || null);
|
|
386
|
-
return s.method = "HEAD", this.guard(e, await this.exec(e, s), null);
|
|
387
|
-
}
|
|
388
|
-
async get(e, a, s) {
|
|
389
|
-
const r = Object.assign({}, s || null);
|
|
390
|
-
r.method = "GET";
|
|
391
|
-
const n = this.cache.getKey(e, r.params), o = this.cache.get(n);
|
|
392
|
-
if (o)
|
|
393
|
-
return this.guard(e, await o, a || null);
|
|
394
|
-
const i = this.exec(e, r);
|
|
395
|
-
return this.cache.set(n, i), this.guard(e, await i, a || null);
|
|
396
|
-
}
|
|
397
|
-
async post(e, a, s, r) {
|
|
398
|
-
const n = Object.assign({}, r || null);
|
|
399
|
-
return n.method = "POST", n.body = a, this.guard(e, await this.exec(e, n), s || null);
|
|
400
|
-
}
|
|
401
|
-
async del(e, a, s) {
|
|
402
|
-
const r = Object.assign({}, s || null);
|
|
403
|
-
return r.method = "DELETE", this.guard(e, await this.exec(e, r), a || null);
|
|
404
|
-
}
|
|
405
|
-
async put(e, a, s, r) {
|
|
406
|
-
const n = Object.assign({}, r || null);
|
|
407
|
-
return n.method = "PUT", n.body = a, this.guard(e, await this.exec(e, n), s || null);
|
|
408
|
-
}
|
|
409
|
-
async patch(e, a, s, r) {
|
|
410
|
-
const n = Object.assign({}, r || null);
|
|
411
|
-
return n.method = "PATCH", n.body = a, this.guard(e, await this.exec(e, n), s || null);
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
const ie = "1.6.0";
|
|
415
|
-
export {
|
|
416
|
-
ae as C,
|
|
417
|
-
x as L,
|
|
418
|
-
oe as N,
|
|
419
|
-
z as R,
|
|
420
|
-
R as a,
|
|
421
|
-
te as c,
|
|
422
|
-
ne as g,
|
|
423
|
-
re as h,
|
|
424
|
-
se as o,
|
|
425
|
-
X as r,
|
|
426
|
-
ie as v
|
|
427
|
-
};
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";var A=Object.defineProperty;var U=(t,e,n)=>e in t?A(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var f=(t,e,n)=>(U(t,typeof e!="symbol"?e+"":e,n),n);async function x(t,e,n){var b;const s=Object.assign({method:"GET"},n),r="FormData"in globalThis?s.body instanceof FormData:!1,a=r&&s.method!=="POST"&&s.method!=="PUT"?"POST":s.method;(a==="GET"||a==="HEAD"||a==="DELETE")&&s.body!==void 0&&(console.warn("request body is invalid with method get, head, delete"),delete s.body);const o=Object.assign(r?{}:{"Content-Type":"Blob"in globalThis&&s.body instanceof Blob?s.body.type||"application/octet-stream":"application/json;charset=utf-8"},s.headers),i=s.params||{},c={};Object.keys(i).forEach(h=>{i[h]!==void 0&&(c[h]=P(i[h]))});const d=e.getFullUrl(t),l=H(s.body),u=s.timeout||e.get("timeout"),g=await async function(){var O;const h=e.get("requestTransformer");return h?await h({headers:o,params:c,method:a,url:d,body:l}):await((O=e.get("requestHandler"))==null?void 0:O(o,c,a,t))}(),m=typeof g=="string"&&g?g:d;return(b=e.get("logHandler"))==null||b({type:"ready",url:m,method:a,headers:o,timeout:u,body:l}),{url:m,method:a,body:l,params:c,headers:o,timeout:u,abort:s.abort,credentials:s.credentials||e.get("credentials")}}function P(t){return typeof t=="string"?t:Array.isArray(t)?t.join(","):t+""}function H(t){if(t)return typeof t=="string"||t instanceof URLSearchParams||t instanceof ArrayBuffer||"Blob"in globalThis&&t instanceof Blob||"FormData"in globalThis&&t instanceof FormData?t:JSON.stringify(t)}function B(t){return new Promise(e=>setTimeout(e,Math.max(0,t)))}function k(){}function R(t,e){try{const n=JSON.parse(t);return e?e(n)?n:null:n}catch{return null}}""+Math.random().toString(32).slice(2);const q=/^(?:https?:)?\/\/.+$/i,I=/^https?:\/\/.+$/i,K=/^\{[\d\D]*\}$/,S=/^\[[\d\D]*\]$/;function p(t,e=!1){return e?q.test(t):I.test(t)}function N(t){return t==null}function M(t,...e){if(!t||typeof t!="object")return!1;const n=Object.getPrototypeOf(t);return n!==Object.prototype&&n!==null?!1:e.every(s=>s in t)}function C(t){return K.test(t)||S.test(t)}async function G(t){return await new Promise(function(e){const n=document.getElementsByTagName("head")[0],s=document.createElement("script");s.setAttribute("type","text/javascript"),s.setAttribute("charset","utf-8"),s.onload=function(){n.removeChild(s),e(!0)},s.onerror=function(){n.removeChild(s),e(!1)},s.setAttribute("src",t),n.appendChild(s)})}function J(t,e="数据未能正确识别"){return typeof t=="function"?{guard:t,message:e}:{guard:t.guard,message:t.message||e}}function $(t,e=""){return!e||p(t,!0)?v(t):(v(e)+"/"+t).replace(/\/{2,}/g,"/").replace(/:\//,"://")}function v(t){return p(t,!0)?t.startsWith("http")?t:("location"in globalThis?location.protocol:"https:")+t:("location"in globalThis?location.origin:"http://127.0.0.1")+"/"+t.replace(/^\/+/,"")}function _(t,e){if(e){if(e===!0)return t.replace(/\?[^#]*/,"")}else return t;const n=t.split("#"),s=n[0].split("?"),r=s[0],a=s.length>1?s[1]:"",o=n.length>1?"#"+n[1]:"",i=typeof e=="string"?[e]:Array.isArray(e)?e:[];return!i.length||!a?n[0]+o:(i.map(c=>c.replace(/([\\(){}[\]^$+\-*?|])/g,"\\$1")),(r+"?"+a.replace(new RegExp("(?:^|&)(?:"+i.join("|")+")=[^&$]+","g"),"").replace(/^&/,"")).replace(/\?$/,"")+o)}function V(t,e,n=!1){const s=typeof e=="string"?e:Object.keys(e).map(o=>`${o}=${encodeURIComponent(e[o])}`).join("&");if(!s)return t;const r=t.split("#");n&&(r[0]=_(r[0],(s.match(/([^=&#?]+)=[^&#]+/g)||[]).map(o=>o.replace(/=.+$/,""))));const a=r[0].indexOf("?")+1?"&":"?";return(r[0]+a+s+(r.length>1?"#"+r[1]:"")).replace(/\?&/,"?")}const E="data",y="message";function W(t,e,n,s,r){const a=r||s;return j(t)?te(a.ok||s.ok,t,e,n):X(a.failed||s.failed,e,n)}const Y=function(t){const e=[],n=t.failed||{resolve:"json"};switch(e.push("- 当http状态码 <200 或者 >=400 时"),n.resolve){case"body":e.push(" 将响应内容格式化为字符串并作为错误消息");break;case"json":e.push(" 将响应解析为json,并读取 "+(n.messageField||y)+" 作为错误消息");break}const s=t.ok||{resolve:"body"};switch(e.push("- 当http状态码 >=200 并且 <400 时"),s.resolve){case"body":e.push(" 将响应尝试解析为 json,并作为数据内容返回");break;case"json":e.push(" 将响应解析为 json,读取 "+(s.dataField||E)+" 作为响应数据,读取 "+(s.messageField||y)+" 作为提示消息"),s.statusField&&e.push(" 当 "+s.statusField+" 为 "+(s.statusOKValue||"空值")+" 时是成功提示,否则是错误消息"),s.ignoreMessage&&e.push(" 并忽略以下消息:"+s.ignoreMessage);break}return e.join(`
|
|
2
|
-
`)};function X(t,e,n){const s=t||{resolve:"json",messageField:y},r={ok:!1,code:e,message:n,data:null};switch(s.resolve){case"body":r.message=F(n)||n;break;case"json":const{code:a,message:o}=z(n,s.statusField,s.messageField);r.code=a||e,r.message=F(n)||o;break}return r}function z(t,e,n=y){if(!C(t))return{message:""};const s=R(t);return!s||!M(s)?{message:t}:{code:e?w(s,e):"",message:w(s,n)||t}}function w(t,e){const n=Array.isArray(e)?e:[e];for(const s of n)if(s in t)return Q(t[s]);return""}function Q(t){return t?typeof t=="string"?t:JSON.stringify(t):""}const Z=/<title>([^<]+)<\/title>/i,ee=/<message>([^<]+)<\/message>/i;function F(t){const e=t.match(Z);if(e)return e[1];const n=t.match(ee);return n?n[1]:""}function te(t,e,n,s){const r=t||{resolve:"body"},a={ok:!0,code:n,message:"",data:null};if(e===202||e===204||!s)return a;if(r.resolve==="body")return a.data=C(s)?R(s):s,a;const o=R(s);if(!o||!M(o))return a.ok=!1,a.code="ResponseFormatError",a.message="响应内容无法格式化为 Object",a;const i=r.statusField,c=r.statusOKValue||"",d=r.dataField||E,l=r.messageField||y,u=r.ignoreMessage||"";if(i&&!(i in o))return a.ok=!1,a.code="ResponseFieldMissing",a.message="响应内容找不到状态字段 "+i,a;const g=i?o[i]+"":"";return a.ok=i?g===c:!0,a.code=g||n,a.data=d===!0?o:d in o?o[d]:null,a.message=w(o,l),u&&a.message&&(Array.isArray(u)&&u.includes(a.message)||typeof u=="string"&&a.message===u)&&(a.message=""),a}function j(t){return t>=200&&t<400}async function D(t,e,n,s,r){const a=r||0,o=Math.max(0,Math.min(10,(s==null?void 0:s.maxRetry)??n.get("maxRetry")??0)),i=(s==null?void 0:s.retryResolve)??n.get("retryResolve"),c=n.get("logHandler")||k;c({type:"prepear",url:e,method:(s==null?void 0:s.method)||"GET",retry:a,maxRetry:o,message:a===0?"start":`retry ${a}/${o} start`,headers:s==null?void 0:s.headers,options:s});const d=Date.now(),l=await t(e,n,s),u=l.status,g=Date.now()-d,m=`[cost ${g}][${u}] ${u<0?l.body:""}`;c({type:"finished",url:e,method:l.method,retry:a,maxRetry:o,message:a===0?`finish ${m}`:`retry ${a}/${o} finish ${m}`,response:l,headers:l.headers,cost:g});const b=j(u);if(!o||i==="network"&&u>0||i==="status"&&b||Array.isArray(i)&&!i.includes(u)||typeof i=="function"&&i(l,a)!==!0||a>=o)return l;const h=(s==null?void 0:s.retryInterval)??n.get("retryInterval")??1e3;return await B(Math.max(100,h==="2EB"?Math.pow(2,a)*1e3:typeof h=="function"?h(a+1)||0:h)),await D(t,e,n,s,a+1)}function se(t,e,n,s){var o,i;if(t.status<0)return T({ok:!1,status:t.status,code:t.statusText,headers:{},message:"",data:null},`${t.method} ${e} ${t.statusText}`,t.method,e,n,s);j(t.status)||(o=n.get("errorHandler"))==null||o(t.status,t.method,e);const r={...W(t.status,t.statusText,t.body,n.get("responseRule"),s==null?void 0:s.responseRule),status:t.status,headers:Object.fromEntries(Object.entries(t.headers||{}).map(([c,d])=>[c.toLowerCase(),d]))};(i=n.get("responseHandler"))==null||i({...r},t.method,e);const a=r.ok?r.message:`${t.method} ${e} [${r.code||t.statusText}] ${r.message||t.statusText}`;return T(r,a,t.method,e,n,s)}function T(t,e,n,s,r,a){const o=r.get("message"),i=o===!1||(a==null?void 0:a.message)===!1?!1:(a==null?void 0:a.message)||o;if(i!==!1){const c=typeof i=="function"?i(t,n,s,e):e;c instanceof Error?r.showMessage(!0,c.message):c&&typeof c=="object"&&"message"in c?r.showMessage(!1,c.message):r.showMessage(!t.ok,c)}return t}class L{constructor(e){f(this,"config",{baseURL:"",maxRetry:0,retryInterval:1e3,retryResolve:"network",timeout:5e3,cacheTTL:500,credentials:"same-origin",responseRule:{ok:{resolve:"body"},failed:{resolve:"json",messageField:"message"}}});e&&this.set(e)}set(e){if(e.baseURL&&!/^\/.+/.test(e.baseURL)&&!p(e.baseURL))throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"),new Error("BaseURLError");Object.assign(this.config,e)}get(e){return this.config[e]}getFullUrl(e){return e.startsWith("/")?$(e):$(e,this.config.baseURL)}showMessage(e,n){this.config.messageHandler&&n&&this.config.messageHandler(e,n)}}class ne{constructor(e=500){f(this,"ttl");f(this,"cache");this.cache={},this.ttl=Math.max(e,0)}getKey(e,n){const s=Object.keys(n||{}).sort().map(r=>`${r}#${n==null?void 0:n[r]}`);return e+s.join(",")}updateTTL(e){this.ttl=Math.max(e,0)}get(e){if(this.ttl===0)return null;const n=this.cache[e];return n?n.ttl<Date.now()?(delete this.cache[e],null):n.res:null}set(e,n){this.ttl!==0&&(this.cache[e]={ttl:Date.now()+this.ttl,res:n})}}function ae(t,e,n,s){if(e.ok&&!N(e.data)&&s){const r=J(s,"响应数据未能正确识别");return r.guard(e.data)||(console.error("ResponseCheckFaild",t,e.data),n.showMessage(!0,`${t} ${r.message}`),e.data=null),e}return e}class re{constructor(e,n){f(this,"agent");f(this,"config");f(this,"cache");this.config=new L(n),this.agent=e,this.cache=new ne(this.config.get("cacheTTL")),this.setConfig=this.setConfig.bind(this),this.getConfig=this.getConfig.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)}async exec(e,n){return await this.agent(e,this.config,n)}async guard(e,n,s){return ae(e,n,this.config,s)}setConfig(e){this.config.set(e),this.cache.updateTTL(this.config.get("cacheTTL"))}getConfig(e){return this.config.get(e)}async head(e,n){const s=Object.assign({},n||null);return s.method="HEAD",this.guard(e,await this.exec(e,s),null)}async get(e,n,s){const r=Object.assign({},s||null);r.method="GET";const a=this.cache.getKey(e,r.params),o=this.cache.get(a);if(o)return this.guard(e,await o,n||null);const i=this.exec(e,r);return this.cache.set(a,i),this.guard(e,await i,n||null)}async post(e,n,s,r){const a=Object.assign({},r||null);return a.method="POST",a.body=n,this.guard(e,await this.exec(e,a),s||null)}async del(e,n,s){const r=Object.assign({},s||null);return r.method="DELETE",this.guard(e,await this.exec(e,r),n||null)}async put(e,n,s,r){const a=Object.assign({},r||null);return a.method="PUT",a.body=n,this.guard(e,await this.exec(e,a),s||null)}async patch(e,n,s,r){const a=Object.assign({},r||null);return a.method="PATCH",a.body=n,this.guard(e,await this.exec(e,a),s||null)}}const oe="1.6.0";exports.Ct=V;exports.Lt=k;exports.NetRequestHandler=re;exports.R=p;exports.RequestGlobalConfig=L;exports.convertOptions=x;exports.getResponseRulesDescription=Y;exports.handleResponse=se;exports.on=G;exports.retryRequest=D;exports.version=oe;
|