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