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