@seayoo-web/request 3.4.2 → 3.4.5

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 CHANGED
@@ -141,7 +141,7 @@ setConfig({ timeout: 10000 });
141
141
 
142
142
  类型:string
143
143
 
144
- 说明:默认的类型守卫错误提示,仅支持全局配置,默认 “响应数据未能正确识别“,单个 api 可通过传入完整的类型守卫({ guard, message })来定制提示消息。
144
+ 说明:默认的类型守卫错误提示,仅支持全局配置,默认 “响应数据未能正确识别”,单个 api 可通过传入完整的类型守卫({ guard, message })来定制提示消息。
145
145
 
146
146
  ### message
147
147
 
@@ -180,11 +180,11 @@ defaultMessage: string
180
180
 
181
181
  **statusField**: string
182
182
 
183
- 解析错误消息的状态字段,仅在 resolve 为 json 时有效,有值的话会替换 response 的 code
183
+ 解析状态的字段,仅在 resolve 为 json 时有效,有值的话会替换 response 的 code
184
184
 
185
185
  **messageField**: string | string[]
186
186
 
187
- 错误消息解析字段,仅在 resolve 为 json 时有效,默认值 "message"
187
+ 解析错误消息的字段,仅在 resolve 为 json 时有效,默认值 "message"
188
188
 
189
189
  - **OKRule**: { resolve, converter?, statusField?, statusOKValue?, dataField?, messageField?, ignoreMessage? }
190
190
 
package/dist/index.js CHANGED
@@ -1,78 +1,78 @@
1
- import { h as m, r as q, R as T, c as x, B as R, a as w, N as H, f as C, b as E, X as L } from "./retry-FiwuCZIu.js";
2
- import { g as Q } from "./retry-FiwuCZIu.js";
3
- import { f as k } from "./request.fetch-Cbt_az4v.js";
4
- const S = async function(e, o, t) {
5
- return m(await q(y, e, o, t), e, o, t);
1
+ import { h as m, r as q, R as T, c as x, Q as R, a as w, _ as H, l as C, N as E, Z as L } from "./retry-BjOxSyvC.js";
2
+ import { g as z } from "./retry-BjOxSyvC.js";
3
+ import { f as j } from "./request.fetch-6HUu6_l5.js";
4
+ const k = async function(t, s, e) {
5
+ return m(await q(y, t, s, e), t, s, e);
6
6
  };
7
- async function U(e, o, t, n) {
8
- const r = t?.body, l = t?.method === "PUT" ? "PUT" : "POST";
9
- if (o instanceof Blob) {
10
- const i = new T(n), u = await y(e, i, {
11
- ...t,
7
+ async function S(t, s, e, n) {
8
+ const r = e?.body, l = e?.method === "PUT" ? "PUT" : "POST";
9
+ if (s instanceof Blob) {
10
+ const i = new T(n), u = await y(t, i, {
11
+ ...e,
12
12
  method: l,
13
- body: o
13
+ body: s
14
14
  });
15
- return m(u, e, i, t);
15
+ return m(u, t, i, e);
16
16
  }
17
- const a = new FormData(), d = { ...o };
17
+ const a = new FormData(), d = { ...s };
18
18
  r instanceof Object && Object.entries(r).forEach(([i, u]) => {
19
- u instanceof Blob ? d[i] = u : Array.isArray(u) ? u.forEach((s, g) => {
20
- a.append(`${i}[${g}]`, String(s));
19
+ u instanceof Blob ? d[i] = u : Array.isArray(u) ? u.forEach((o, g) => {
20
+ a.append(`${i}[${g}]`, String(o));
21
21
  }) : a.append(i, String(u));
22
22
  });
23
23
  for (const i in d)
24
24
  a.append(i, d[i]);
25
- const b = new T(n), p = await y(e, b, {
26
- ...t,
25
+ const b = new T(n), h = await y(t, b, {
26
+ ...e,
27
27
  method: l,
28
28
  body: a
29
29
  });
30
- return m(p, e, b, t);
30
+ return m(h, t, b, e);
31
31
  }
32
- const y = async function(e, o, t) {
33
- const n = await x(e, o, t), r = n.method, l = t?.onUploadProgress, a = R(n.url, n.params);
32
+ const y = async function(t, s, e) {
33
+ const n = await x(t, s, e), r = n.method, l = e?.onUploadProgress, a = R(n.url, n.params);
34
34
  return await new Promise((d) => {
35
- let b = null, p = !1;
35
+ let b = null, h = !1;
36
36
  const i = function() {
37
- p || (s.abort(), p = !0);
37
+ h || (o.abort(), h = !0);
38
38
  };
39
39
  function u() {
40
40
  b !== null && clearTimeout(b), n.abort && n.abort.removeEventListener("abort", i);
41
41
  }
42
- const s = new XMLHttpRequest();
42
+ const o = new XMLHttpRequest();
43
43
  let g = !1;
44
- if (s.open(r, a, !0), l) {
44
+ if (o.open(r, a, !0), l) {
45
45
  let f = 1;
46
- s.upload.addEventListener("progress", (h) => {
47
- f = h.total, l({ total: h.total, loaded: h.loaded });
48
- }), s.addEventListener("load", () => {
46
+ o.upload.addEventListener("progress", (p) => {
47
+ f = p.total, l({ total: p.total, loaded: p.loaded });
48
+ }), o.addEventListener("load", () => {
49
49
  l({ loaded: f, total: f });
50
50
  });
51
51
  }
52
- s.addEventListener("load", () => {
53
- const f = s.status;
52
+ o.addEventListener("load", () => {
53
+ const f = o.status, p = o.responseType;
54
54
  u(), d({
55
55
  url: a,
56
56
  method: r,
57
57
  status: f,
58
- statusText: s.statusText,
59
- headers: j(s),
60
- body: r === "HEAD" || f === 204 ? "" : s.responseText
58
+ statusText: o.statusText,
59
+ headers: U(o),
60
+ body: r === "HEAD" || f === 204 ? "" : p === "" || p === "text" || p === "json" || p === "document" ? o.responseText : ""
61
61
  });
62
- }), s.addEventListener(
62
+ }), o.addEventListener(
63
63
  "error",
64
64
  (f) => {
65
65
  u(), d({
66
66
  url: a,
67
67
  method: r,
68
- status: s.status || -1,
69
- statusText: s.statusText || w.NetworkError,
68
+ status: o.status || -1,
69
+ statusText: o.statusText || w.NetworkError,
70
70
  body: "",
71
71
  rawError: f
72
72
  });
73
73
  },
74
74
  !0
75
- ), s.addEventListener("abort", () => {
75
+ ), o.addEventListener("abort", () => {
76
76
  u(), d({
77
77
  url: a,
78
78
  method: r,
@@ -81,53 +81,53 @@ const y = async function(e, o, t) {
81
81
  statusText: g ? w.Timeout : w.Aborted,
82
82
  body: ""
83
83
  });
84
- }), Object.entries(n.headers).forEach(([f, h]) => {
85
- s.setRequestHeader(f, h);
86
- }), n.credentials === "include" && (s.withCredentials = !0), s.send(n.body || void 0), n.abort && n.abort.addEventListener("abort", i), n.timeout > 0 && (b = setTimeout(function() {
84
+ }), Object.entries(n.headers).forEach(([f, p]) => {
85
+ o.setRequestHeader(f, p);
86
+ }), n.credentials === "include" && (o.withCredentials = !0), o.send(n.body || void 0), n.abort && n.abort.addEventListener("abort", i), n.timeout > 0 && (b = setTimeout(function() {
87
87
  g = !0, i();
88
88
  }, n.timeout));
89
89
  });
90
90
  };
91
- function j(e) {
92
- const o = {};
93
- if (!e)
94
- return o;
95
- const t = e.getAllResponseHeaders();
96
- return t && t !== "null" && t.replace(/\r/g, "").split(`
91
+ function U(t) {
92
+ const s = {};
93
+ if (!t)
94
+ return s;
95
+ const e = t.getAllResponseHeaders();
96
+ return e && e !== "null" && e.replace(/\r/g, "").split(`
97
97
  `).forEach((n) => {
98
98
  const r = n.trim();
99
99
  if (!r)
100
100
  return;
101
101
  const l = r.split(":"), a = l[0].trim();
102
- a && (o[a] = (l[1] || "").trim());
103
- }), o;
102
+ a && (s[a] = (l[1] || "").trim());
103
+ }), s;
104
104
  }
105
- async function O(e, o, t = {}) {
105
+ async function P(t, s, e = {}) {
106
106
  const n = window;
107
- "callback" in t || (t.callback = "jsonxData" + Math.random().toString(16).slice(2));
108
- const r = t.callback + "";
109
- if (!e)
107
+ "callback" in e || (e.callback = "jsonxData" + Math.random().toString(16).slice(2));
108
+ const r = e.callback + "";
109
+ if (!t)
110
110
  return null;
111
- const l = R(e, t, !0);
111
+ const l = R(t, e, !0);
112
112
  return new Promise((a) => {
113
113
  n[r] = function(d) {
114
- if (r in window && delete n[r], o(d))
114
+ if (r in window && delete n[r], s(d))
115
115
  return d;
116
- console.warn("response type check failed", e, d), a(null);
116
+ console.warn("response type check failed", t, d), a(null);
117
117
  }, H(l).catch(function() {
118
118
  a(null), delete n[r];
119
119
  });
120
120
  });
121
121
  }
122
- async function P(e, o, t = {}) {
122
+ async function A(t, s, e = {}) {
123
123
  const n = window;
124
- return "var" in t || (t.var = "jsonxData" + Math.random().toString(16).slice(2)), e ? await H(R(e, t, !0)).then(() => {
125
- const r = n[t.var + ""];
126
- return o(r) ? r : (console.warn("response type check failed", e, r), null);
124
+ return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), t ? await H(R(t, e, !0)).then(() => {
125
+ const r = n[e.var + ""];
126
+ return s(r) ? r : (console.warn("response type check failed", t, r), null);
127
127
  }).catch(() => null) : null;
128
128
  }
129
- const A = async function(e, o, t) {
130
- return await U(e, o, t, {
129
+ const M = async function(t, s, e) {
130
+ return await S(t, s, e, {
131
131
  baseURL: c.getConfig("baseURL"),
132
132
  logHandler: c.getConfig("logHandler"),
133
133
  errorHandler: c.getConfig("errorHandler"),
@@ -136,25 +136,26 @@ const A = async function(e, o, t) {
136
136
  responseHandler: c.getConfig("responseHandler")
137
137
  });
138
138
  };
139
- function v(e) {
139
+ // @__NO_SIDE_EFFECTS__
140
+ function v(t) {
140
141
  if (!C())
141
142
  throw new Error("Default Module Only Support In Browser");
142
- return L() ? new E(k, e) : new E(S, e);
143
+ return L() ? new E(j, t) : new E(k, t);
143
144
  }
144
- const c = v(), B = c.setConfig, M = c.request, X = c.head, F = c.get, G = c.post, I = c.del, $ = c.put, z = c.patch;
145
+ const c = /* @__PURE__ */ v(), N = c.setConfig, B = c.request, X = c.head, F = c.get, G = c.post, I = c.del, Q = c.put, Z = c.patch;
145
146
  export {
146
147
  v as NetRequest,
147
148
  w as RequestInternalError,
148
149
  I as del,
149
150
  F as get,
150
- Q as getResponseRulesDescription,
151
+ z as getResponseRulesDescription,
151
152
  X as head,
152
- O as jsonp,
153
- P as jsonx,
154
- z as patch,
153
+ P as jsonp,
154
+ A as jsonx,
155
+ Z as patch,
155
156
  G as post,
156
- $ as put,
157
- M as request,
158
- B as setGlobalConfig,
159
- A as upload
157
+ Q as put,
158
+ B as request,
159
+ N as setGlobalConfig,
160
+ M as upload
160
161
  };
package/dist/node.js CHANGED
@@ -1,26 +1,26 @@
1
- import { h as y, r as q, c as w, e as g, a as c, d as E, b as f, X as x } from "./retry-FiwuCZIu.js";
2
- import { g as z } from "./retry-FiwuCZIu.js";
3
- import { f as C } from "./request.fetch-Cbt_az4v.js";
4
- import T from "node:http";
5
- import j from "node:https";
6
- const L = async function(o, a, u) {
7
- return y(await q(N, o, a, u), o, a, u);
8
- }, N = async function(o, a, u) {
1
+ import { h as y, r as q, c as w, b as g, a as d, f as E, N as f, Z as x } from "./retry-BjOxSyvC.js";
2
+ import { g as z } from "./retry-BjOxSyvC.js";
3
+ import { f as C } from "./request.fetch-6HUu6_l5.js";
4
+ import N from "node:http";
5
+ import T from "node:https";
6
+ const j = async function(o, a, u) {
7
+ return y(await q(L, o, a, u), o, a, u);
8
+ }, L = async function(o, a, u) {
9
9
  const t = await w(o, a, u);
10
10
  if (!g(t.url))
11
11
  return {
12
12
  url: t.url,
13
13
  method: t.method,
14
14
  status: -1,
15
- statusText: c.URLFormatError,
15
+ statusText: d.URLFormatError,
16
16
  headers: {},
17
17
  body: ""
18
18
  };
19
- const R = /^https:\/\//i.test(t.url) ? j : T, n = new URL(t.url), i = t.params;
19
+ const R = /^https:\/\//i.test(t.url) ? T : N, n = new URL(t.url), i = t.params;
20
20
  i instanceof Object && Object.keys(i).forEach((r) => n.searchParams.set(r, i[r]));
21
21
  const l = t.method === "HEAD";
22
22
  return new Promise((r) => {
23
- const d = R.request(
23
+ const c = R.request(
24
24
  n,
25
25
  {
26
26
  headers: t.headers,
@@ -37,41 +37,42 @@ const L = async function(o, a, u) {
37
37
  url: n.toString(),
38
38
  method: t.method,
39
39
  status: e.statusCode || -1,
40
- statusText: e.statusMessage || c.Unknown,
40
+ statusText: e.statusMessage || d.Unknown,
41
41
  headers: m,
42
42
  body: l || e.statusCode === 204 ? "" : Buffer.concat(p).toString("utf-8")
43
43
  });
44
44
  });
45
45
  }
46
46
  );
47
- d.on("error", (e) => {
47
+ c.on("error", (e) => {
48
48
  r({
49
49
  url: n.toString(),
50
50
  method: t.method,
51
51
  status: -1,
52
- statusText: c.NetworkError,
52
+ statusText: d.NetworkError,
53
53
  body: "",
54
54
  rawError: e
55
55
  });
56
- }), d.on("timeout", () => {
56
+ }), c.on("timeout", () => {
57
57
  r({
58
58
  url: n.toString(),
59
59
  method: t.method,
60
60
  status: -1,
61
- statusText: c.Timeout,
61
+ statusText: d.Timeout,
62
62
  body: ""
63
63
  });
64
- }), t.body && d.write(t.body), d.end();
64
+ }), t.body && c.write(t.body), c.end();
65
65
  });
66
66
  };
67
+ // @__NO_SIDE_EFFECTS__
67
68
  function O(o) {
68
- return x() ? new f(C, o) : new f(L, o);
69
+ return x() ? new f(C, o) : new f(j, o);
69
70
  }
70
- const s = O(), k = s.setConfig, B = s.head, D = s.get, P = s.post, X = s.del, F = s.put, G = s.patch;
71
+ const s = /* @__PURE__ */ O(), k = s.setConfig, B = s.head, D = s.get, P = s.post, Z = s.del, F = s.put, G = s.patch;
71
72
  export {
72
73
  O as NetRequest,
73
- c as RequestInternalError,
74
- X as del,
74
+ d as RequestInternalError,
75
+ Z as del,
75
76
  D as get,
76
77
  z as getResponseRulesDescription,
77
78
  B as head,
@@ -1,8 +1,8 @@
1
- import { h as w, r as y, c as E, s as T, d as R, a as c } from "./retry-FiwuCZIu.js";
2
- const x = async function(s, a, n) {
3
- return w(await y(g, s, a, n), s, a, n);
4
- }, g = async function(s, a, n) {
5
- const t = await E(s, a, n), i = new URL(t.url), u = t.params;
1
+ import { h as w, r as y, c as E, u as T, f as R, a as c } from "./retry-BjOxSyvC.js";
2
+ const x = async function(a, s, n) {
3
+ return w(await y(g, a, s, n), a, s, n);
4
+ }, g = async function(a, s, n) {
5
+ const t = await E(a, s, n), i = new URL(t.url), u = t.params;
6
6
  u instanceof Object && Object.keys(u).forEach((e) => i.searchParams.set(e, u[e]));
7
7
  const r = T() ? new AbortController() : null;
8
8
  function d() {
@@ -12,7 +12,7 @@ const x = async function(s, a, n) {
12
12
  let l = !1;
13
13
  const b = t.timeout > 0 ? setTimeout(function() {
14
14
  l = !0, d();
15
- }, t.timeout) : null, m = t.method === "HEAD";
15
+ }, t.timeout) : null, f = t.method === "HEAD";
16
16
  return await fetch(i, {
17
17
  method: t.method,
18
18
  headers: Object.keys(t.headers).length > 0 ? new Headers(t.headers) : void 0,
@@ -27,7 +27,7 @@ const x = async function(s, a, n) {
27
27
  status: e.status,
28
28
  statusText: e.statusText,
29
29
  headers: R([...e.headers.entries()])
30
- }, h = m || e.status === 204 ? "" : await e.text().catch((f) => f);
30
+ }, h = f || e.status === 204 ? "" : await e.text().catch((m) => m);
31
31
  return h instanceof Error ? { ...o, body: "", statusText: c.Unknown, rawError: h } : { ...o, body: h };
32
32
  }).catch((e) => {
33
33
  const o = r?.signal.aborted;
@@ -1,41 +1,41 @@
1
1
  const h = typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")();
2
- function x() {
2
+ function S() {
3
3
  return h;
4
4
  }
5
- function m(t) {
5
+ function p(t) {
6
6
  return typeof t == "function";
7
7
  }
8
8
  function C(t) {
9
9
  return typeof t == "object" && t !== null;
10
10
  }
11
- function P() {
12
- return "wx" in h && C(h.wx) && "getSystemInfo" in h.wx && m(h.wx.getSystemInfo);
11
+ function D() {
12
+ return "wx" in h && C(h.wx) && "getSystemInfo" in h.wx && p(h.wx.getSystemInfo);
13
13
  }
14
14
  function de() {
15
- return "fetch" in h && m(h.fetch);
15
+ return "fetch" in h && p(h.fetch);
16
16
  }
17
17
  function ge() {
18
- return "window" in h && C(h.window) && !P();
18
+ return "window" in h && C(h.window) && !D();
19
19
  }
20
20
  function M() {
21
- return "Blob" in h && m(h.Blob);
21
+ return "Blob" in h && p(h.Blob);
22
22
  }
23
23
  function v() {
24
- return "FormData" in h && m(h.FormData);
25
- }
26
- function pe() {
27
- return "TextDecoder" in h && m(h.TextDecoder);
24
+ return "FormData" in h && p(h.FormData);
28
25
  }
29
26
  function me() {
30
- return "AbortController" in h && m(h.AbortController);
27
+ return "TextDecoder" in h && p(h.TextDecoder);
31
28
  }
32
- function D() {
33
- return "URLSearchParams" in h && m(h.URLSearchParams);
29
+ function pe() {
30
+ return "AbortController" in h && p(h.AbortController);
34
31
  }
35
- function _(t) {
36
- return new Promise((e) => setTimeout(e, Math.min(Number.MAX_SAFE_INTEGER, Math.max(0, t))));
32
+ function P() {
33
+ return "URLSearchParams" in h && p(h.URLSearchParams);
37
34
  }
38
35
  function q(t) {
36
+ return new Promise((e) => setTimeout(e, Math.min(Number.MAX_SAFE_INTEGER, Math.max(0, t))));
37
+ }
38
+ function I(t) {
39
39
  const e = t || "CustomError";
40
40
  return class extends Error {
41
41
  constructor(s) {
@@ -47,20 +47,28 @@ function q(t) {
47
47
  }
48
48
  };
49
49
  }
50
- const N = /^(?:https?:)?\/\/.+$/i, B = /^https?:\/\/.+$/i, I = /^\{[\d\D]*\}$/, H = /^\[[\d\D]*\]$/;
50
+ const B = /^(?:https?:)?\/\/.+$/i, H = /^https?:\/\/.+$/i, N = /^\{[\d\D]*\}$/, G = /^\[[\d\D]*\]$/;
51
51
  function j(t, e = !1) {
52
- return e ? N.test(t) : B.test(t);
52
+ return e ? B.test(t) : H.test(t);
53
53
  }
54
- function G(t) {
54
+ function _(t) {
55
55
  return t == null;
56
56
  }
57
- function A(t, ...e) {
57
+ function U(t, ...e) {
58
58
  if (!t || typeof t != "object") return !1;
59
59
  const s = Object.getPrototypeOf(t);
60
60
  return s !== Object.prototype && s !== null ? !1 : e.every((r) => r in t);
61
61
  }
62
- function U(t) {
63
- return I.test(t) || H.test(t);
62
+ function A(t) {
63
+ return N.test(t) || G.test(t);
64
+ }
65
+ function R(t, e) {
66
+ try {
67
+ const s = JSON.parse(t);
68
+ return e ? e(s) ? s : null : s;
69
+ } catch {
70
+ return null;
71
+ }
64
72
  }
65
73
  async function ye(t) {
66
74
  return await new Promise(function(e) {
@@ -82,10 +90,10 @@ function K(t, e = "数据未能正确识别") {
82
90
  };
83
91
  }
84
92
  function J(t, e = "") {
85
- return !e || j(t, !0) ? $(t) : ($(e) + "/" + t).replace(/\/{2,}/g, "/").replace(/:\//, "://");
93
+ return !e || j(t, !0) ? T(t) : (T(e) + "/" + t).replace(/\/{2,}/g, "/").replace(/:\//, "://");
86
94
  }
87
- function $(t) {
88
- const e = x();
95
+ function T(t) {
96
+ const e = S();
89
97
  return j(t, !0) ? t.startsWith("http") ? t : ("location" in e ? location.protocol : "https:") + t : ("location" in e ? location.origin : "http://127.0.0.1") + "/" + t.replace(/^\/+/, "");
90
98
  }
91
99
  function V(t, e) {
@@ -115,11 +123,11 @@ function be(t, e, s = !1) {
115
123
  const o = n[0].indexOf("?") + 1 ? "&" : "?";
116
124
  return (n[0] + o + r + (n.length > 1 ? "#" + n[1] : "")).replace(/\?&/, "?");
117
125
  }
118
- function X(t) {
126
+ function Q(t) {
119
127
  const e = t.match(/(?:\?|&)([^=]+)(?:&|$)/g);
120
128
  return e ? e.join("").replace(/(?:\?|^&+|&+$)/g, "").replace(/&{2}/g, "&").split("&").sort() : [];
121
129
  }
122
- class z {
130
+ class Z {
123
131
  ttl;
124
132
  cache;
125
133
  timer = null;
@@ -147,7 +155,7 @@ class z {
147
155
  } catch (l) {
148
156
  return console.warn("[RequestCache] cacheResolve error", s, r, l), "";
149
157
  }
150
- const i = Object.assign(V(a, !0), r), g = X(a), u = Object.keys(i).sort().map((l) => `${l}#${i[l]}`);
158
+ const i = Object.assign(V(a, !0), r), g = Q(a), u = Object.keys(i).sort().map((l) => `${l}#${i[l]}`);
151
159
  return `${c}_${o}_${u.join(",")}_${g.join(",")}`;
152
160
  }
153
161
  updateTTL(e) {
@@ -170,7 +178,7 @@ class z {
170
178
  this.cache[e] && delete this.cache[e];
171
179
  }
172
180
  }
173
- class Y {
181
+ class z {
174
182
  // 保存的配置需要部分字段强制设置默认值
175
183
  config = {
176
184
  baseURL: "/",
@@ -211,7 +219,7 @@ class Y {
211
219
  this.config.messageHandler && s && this.config.messageHandler(e, s, r, n);
212
220
  }
213
221
  }
214
- const S = {
222
+ const L = {
215
223
  /** 响应数据校验失败 */
216
224
  UnexpectResponse: "UnexpectResponse",
217
225
  /** 请求被取消 */
@@ -227,10 +235,10 @@ const S = {
227
235
  /** url 格式错误 */
228
236
  URLFormatError: "URLFormatError"
229
237
  };
230
- function Q(t, e, s, r) {
231
- if (e.ok && !G(e.data) && r) {
238
+ function X(t, e, s, r) {
239
+ if (e.ok && !_(e.data) && r) {
232
240
  const n = K(r, s.get("defaultTypeGuardMessage"));
233
- return n.guard(e.data) || (e.code = S.UnexpectResponse, s.showMessage(!0, `${t} ${n.message}`, e.code, e.status), console.error(e.code, t, e.data), e.data = null, e.message = n.message), e;
241
+ return n.guard(e.data) || (e.code = L.UnexpectResponse, s.showMessage(!0, `${t} ${n.message}`, e.code, e.status), console.error(e.code, t, e.data), e.data = null, e.message = n.message), e;
234
242
  }
235
243
  return e;
236
244
  }
@@ -239,7 +247,7 @@ class we {
239
247
  config;
240
248
  cache;
241
249
  constructor(e, s) {
242
- this.config = new Y(s), this.agent = e, this.cache = new z(this.config.get("cacheTTL")), this.setConfig = this.setConfig.bind(this), this.getConfig = this.getConfig.bind(this), this.request = this.request.bind(this), this.get = this.get.bind(this), this.post = this.post.bind(this), this.del = this.del.bind(this), this.patch = this.patch.bind(this), this.put = this.put.bind(this), this.head = this.head.bind(this);
250
+ this.config = new z(s), this.agent = e, this.cache = new Z(this.config.get("cacheTTL")), this.setConfig = this.setConfig.bind(this), this.getConfig = this.getConfig.bind(this), this.request = this.request.bind(this), this.get = this.get.bind(this), this.post = this.post.bind(this), this.del = this.del.bind(this), this.patch = this.patch.bind(this), this.put = this.put.bind(this), this.head = this.head.bind(this);
243
251
  }
244
252
  /**
245
253
  * 执行网络请求
@@ -260,7 +268,7 @@ class we {
260
268
  return console.error("RequestError", n), {
261
269
  ok: !1,
262
270
  status: -9,
263
- code: S.Unknown,
271
+ code: L.Unknown,
264
272
  message: n instanceof Error ? `${n.message}
265
273
  ${n.stack || ""}` : String(n),
266
274
  headers: {},
@@ -272,7 +280,7 @@ ${n.stack || ""}` : String(n),
272
280
  * 检查响应的数据类型
273
281
  */
274
282
  async guard(e, s, r) {
275
- return Q(e, s, this.config, r);
283
+ return X(e, s, this.config, r);
276
284
  }
277
285
  /**
278
286
  * 修改默认请求配置: baseURL / timeout / credentials / errorHandler / messageHandler / responseHandler / logHandler / responseRule
@@ -325,14 +333,14 @@ async function Re(t, e, s) {
325
333
  },
326
334
  r.headers
327
335
  ), i = r.params || {}, g = {};
328
- Object.keys(i).forEach((p) => {
329
- i[p] !== void 0 && (g[p] = Z(i[p]));
336
+ Object.keys(i).forEach((m) => {
337
+ i[m] !== void 0 && (g[m] = Y(i[m]));
330
338
  });
331
- const u = e.getFullUrl(t), l = ee(r.body), f = r.timeout || e.get("timeout"), d = await async function() {
332
- const p = e.get("requestTransformer");
333
- if (p)
334
- return await p({ headers: c, params: g, method: o, url: u, body: l });
335
- }(), y = typeof d == "string" && d ? d : u;
339
+ const u = e.getFullUrl(t), l = ee(r.body), f = r.timeout || e.get("timeout"), d = await (async function() {
340
+ const m = e.get("requestTransformer");
341
+ if (m)
342
+ return await m({ headers: c, params: g, method: o, url: u, body: l });
343
+ })(), y = typeof d == "string" && d ? d : u;
336
344
  return e.get("logHandler")?.({ type: "ready", url: y, method: o, headers: c, timeout: f, body: l }), {
337
345
  url: y,
338
346
  method: o,
@@ -344,49 +352,19 @@ async function Re(t, e, s) {
344
352
  credentials: r.credentials || e.get("credentials")
345
353
  };
346
354
  }
347
- function Z(t) {
355
+ function Y(t) {
348
356
  return t == null ? "" : typeof t == "string" ? t : typeof t == "number" || typeof t == "boolean" ? String(t) : Array.isArray(t) ? t.filter(
349
357
  (s) => s != null && typeof s != "object" && typeof s != "function"
350
358
  ).join(",") : (console.warn("参数类型不支持,已被过滤", typeof t), "");
351
359
  }
352
360
  function ee(t) {
353
361
  if (t)
354
- return typeof t == "string" || D() && t instanceof URLSearchParams || t instanceof ArrayBuffer || M() && t instanceof Blob || v() && t instanceof FormData ? t : JSON.stringify(t);
355
- }
356
- function te(t) {
357
- const e = {};
358
- for (const [s, r] of t)
359
- if (s && typeof s == "string") {
360
- if (s === "__proto__" || s === "constructor" || s === "prototype") {
361
- console.warn("检测到危险的对象属性,已被过滤", s);
362
- continue;
363
- }
364
- if (s.includes("\0") || s.includes("\r") || s.includes(`
365
- `)) {
366
- console.warn("参数名中包含可疑字符,已被过滤", s);
367
- continue;
368
- }
369
- e[s] = r || "";
370
- }
371
- return e;
372
- }
373
- function R(t) {
374
- try {
375
- const e = JSON.parse(t);
376
- if (e && typeof e == "object" && !Array.isArray(e)) {
377
- for (const s in e)
378
- if (s === "__proto__" || s === "constructor" || s === "prototype" || Object.prototype.hasOwnProperty.call(e, "__proto__") || Object.prototype.hasOwnProperty.call(e, "constructor"))
379
- return console.warn("JSON 数据包含危险属性,已拒绝解析", s), null;
380
- }
381
- return e;
382
- } catch (e) {
383
- return console.warn("JSON 解析失败", e), null;
384
- }
362
+ return typeof t == "string" || P() && t instanceof URLSearchParams || t instanceof ArrayBuffer || M() && t instanceof Blob || v() && t instanceof FormData ? t : JSON.stringify(t);
385
363
  }
386
- const L = "data", b = "message";
387
- function se(t, e, s, r, n) {
364
+ const x = "data", b = "message";
365
+ function te(t, e, s, r, n) {
388
366
  const o = n || r;
389
- return O(t) ? ie(o.ok || r.ok, t, e, s) : re(o.failed || r.failed, e, s);
367
+ return $(t) ? ce(o.ok || r.ok, t, e, s) : se(o.failed || r.failed, e, s);
390
368
  }
391
369
  const Ee = function(t) {
392
370
  const e = [], s = t.failed || { resolve: "json" };
@@ -405,7 +383,7 @@ const Ee = function(t) {
405
383
  break;
406
384
  case "json":
407
385
  e.push(
408
- " 将响应解析为 json,读取 " + (r.dataField || L) + " 作为响应数据,读取 " + (r.messageField || b) + " 作为提示消息"
386
+ " 将响应解析为 json,读取 " + (r.dataField || x) + " 作为响应数据,读取 " + (r.messageField || b) + " 作为提示消息"
409
387
  ), r.statusField && e.push(
410
388
  " 当 " + r.statusField + " 为 " + (r.statusOKValue || "空值") + " 时是成功提示,否则是错误消息"
411
389
  ), r.ignoreMessage && e.push(" 并忽略以下消息:" + r.ignoreMessage);
@@ -414,7 +392,7 @@ const Ee = function(t) {
414
392
  return e.join(`
415
393
  `);
416
394
  };
417
- function re(t, e, s) {
395
+ function se(t, e, s) {
418
396
  const r = t || { resolve: "json", messageField: b }, n = {
419
397
  ok: !1,
420
398
  code: e,
@@ -423,20 +401,20 @@ function re(t, e, s) {
423
401
  };
424
402
  switch (r.resolve) {
425
403
  case "body":
426
- n.message = T(s) || s;
404
+ n.message = O(s) || s;
427
405
  break;
428
406
  case "json":
429
- const { code: o, message: a } = ne(s, r.converter, r.statusField, r.messageField);
430
- n.code = o || e, n.message = T(s) || a;
407
+ const { code: o, message: a } = re(s, r.converter, r.statusField, r.messageField);
408
+ n.code = o || e, n.message = O(s) || a;
431
409
  break;
432
410
  }
433
411
  return n;
434
412
  }
435
- function ne(t, e, s, r = b) {
436
- if (!U(t))
413
+ function re(t, e, s, r = b) {
414
+ if (!A(t))
437
415
  return { message: "" };
438
416
  const n = k(R(t), e);
439
- return !n || !A(n) ? { message: t } : {
417
+ return !n || !U(n) ? { message: t } : {
440
418
  code: s ? E(n, s) : "",
441
419
  message: E(n, r) || t
442
420
  };
@@ -445,21 +423,21 @@ function E(t, e) {
445
423
  const s = Array.isArray(e) ? e : [e];
446
424
  for (const r of s)
447
425
  if (r in t)
448
- return oe(t[r]);
426
+ return ne(t[r]);
449
427
  return "";
450
428
  }
451
- function oe(t) {
429
+ function ne(t) {
452
430
  return t ? typeof t == "string" ? t : JSON.stringify(t) : "";
453
431
  }
454
- const ae = /<title>([^<]+)<\/title>/i, ce = /<message>([^<]+)<\/message>/i;
455
- function T(t) {
456
- const e = t.match(ae);
432
+ const oe = /<title>([^<]+)<\/title>/i, ae = /<message>([^<]+)<\/message>/i;
433
+ function O(t) {
434
+ const e = t.match(oe);
457
435
  if (e)
458
436
  return e[1];
459
- const s = t.match(ce);
437
+ const s = t.match(ae);
460
438
  return s ? s[1] : "";
461
439
  }
462
- function ie(t, e, s, r) {
440
+ function ce(t, e, s, r) {
463
441
  const n = t || { resolve: "body" }, o = {
464
442
  ok: !0,
465
443
  code: s,
@@ -469,33 +447,33 @@ function ie(t, e, s, r) {
469
447
  if (e === 204 || !r)
470
448
  return o;
471
449
  if (n.resolve === "body")
472
- return o.data = U(r) ? k(R(r), t.converter) : r, o;
450
+ return o.data = A(r) ? k(R(r), t.converter) : r, o;
473
451
  const a = R(r);
474
452
  if (!a)
475
453
  return o.ok = !1, o.code = "ResponseFormatError", o.message = "响应内容无法安全解析", o;
476
454
  const c = k(a, t.converter);
477
- if (!c || !A(c))
455
+ if (!c || !U(c))
478
456
  return o.ok = !1, o.code = "ResponseFormatError", o.message = "响应内容无法格式化为 Object", o;
479
- const i = n.statusField, g = n.statusOKValue || "", u = n.dataField || L, l = n.messageField || b, f = n.ignoreMessage || "";
457
+ const i = n.statusField, g = n.statusOKValue || "", u = n.dataField || x, l = n.messageField || b, f = n.ignoreMessage || "";
480
458
  if (i && !(i in c))
481
459
  return o.ok = !1, o.code = "ResponseFieldMissing", o.message = "响应内容找不到状态字段 " + i, o;
482
460
  const d = i ? c[i] + "" : "";
483
461
  return o.ok = i ? d === g : !0, o.code = d || s, o.data = u === !0 ? c : u in c ? c[u] : null, o.message = E(c, l), f && o.message && (Array.isArray(f) && f.includes(o.message) || typeof f == "string" && o.message === f) && (o.message = ""), o;
484
462
  }
485
- function O(t) {
463
+ function $(t) {
486
464
  return t >= 200 && t < 400;
487
465
  }
488
466
  function k(t, e) {
489
- return e ? typeof e == "function" ? e(t) : (console.warn('工具不再支持 "camelize", "snakify" 参数,请自行提供函数来转化 body 内容'), t) : t;
467
+ return e && typeof e == "function" ? e(t) : t;
490
468
  }
491
- const ue = q("APIError");
492
- function le(t) {
469
+ const ie = I("APIError");
470
+ function ue(t) {
493
471
  const e = {};
494
472
  for (const n in t.headers)
495
473
  (n.startsWith("x-") || n.includes("trace") || n.includes("server") || /\b(?:id|uuid)\b/.test(n)) && (e[n] = t.headers[n]);
496
474
  const s = t.url.replace(/^(?:https?:)?\/*/i, "").replace(/\?.+/, ""), r = t.status < 0 ? "unknown" : t.status;
497
475
  return {
498
- sentryError: new ue(`${s} | ${r}${t.code ? ` | ${t.code}` : ""}`),
476
+ sentryError: new ie(`${s} | ${r}${t.code ? ` | ${t.code}` : ""}`),
499
477
  sentryTags: {
500
478
  ...e,
501
479
  status: r,
@@ -511,18 +489,24 @@ function le(t) {
511
489
  }
512
490
  };
513
491
  }
492
+ function le(t) {
493
+ const e = {};
494
+ for (const [s, r] of t)
495
+ s && typeof s == "string" && (e[s] = r || "");
496
+ return e;
497
+ }
514
498
  function ke(t, e, s, r) {
515
- const n = t.status, o = t.method, a = te(
499
+ const n = t.status, o = t.method, a = le(
516
500
  Object.entries(t.headers || {}).map(([d, y]) => [d.toLowerCase(), y])
517
- ), { ok: c, code: i, data: g, message: u } = se(
501
+ ), { ok: c, code: i, data: g, message: u } = te(
518
502
  n,
519
503
  t.statusText,
520
504
  t.body,
521
505
  s.get("responseRule"),
522
506
  r?.responseRule
523
507
  );
524
- if (!O(n)) {
525
- const d = le({
508
+ if (!$(n)) {
509
+ const d = ue({
526
510
  url: t.url,
527
511
  method: t.method,
528
512
  status: n,
@@ -562,7 +546,7 @@ function F(t, e, s, r, n, o) {
562
546
  const a = n.get("message"), c = a === !1 || o?.message === !1 ? !1 : o?.message || a;
563
547
  if (c !== !1) {
564
548
  const i = typeof c == "function" ? c(t, s, r, e) : e;
565
- i instanceof Error ? n.showMessage(!0, w(i.message), t.code, t.status) : i && typeof i == "object" && "message" in i && typeof i.message == "string" ? n.showMessage(!1, w(i.message), t.code, t.status) : n.showMessage(!t.ok, w(String(i)), t.code, t.status);
549
+ i instanceof Error ? n.showMessage(!0, w(i.message), t.code, t.status) : i && typeof i == "object" && "message" in i && typeof i.message == "string" ? n.showMessage(!1, w(i.message), t.code, t.status) : i && n.showMessage(!t.ok, w(String(i)), t.code, t.status);
566
550
  }
567
551
  return t;
568
552
  }
@@ -599,32 +583,32 @@ async function fe(t, e, s, r, n) {
599
583
  headers: u.headers,
600
584
  cost: f
601
585
  });
602
- const y = O(l);
586
+ const y = $(l);
603
587
  if (!a || o >= a || c === "network" && l > 0 || c === "status" && y || Array.isArray(c) && !c.includes(l) || typeof c == "function" && c(u, o) !== !0)
604
588
  return u;
605
- const p = r?.retryInterval ?? s.get("retryInterval") ?? 100;
606
- return await _(
589
+ const m = r?.retryInterval ?? s.get("retryInterval") ?? 100;
590
+ return await q(
607
591
  Math.max(
608
592
  100,
609
- p === "2EB" ? Math.pow(2, o) * 100 : typeof p == "function" ? p(o + 1, { url: e, status: l, method: u.method }) || 0 : p
593
+ m === "2EB" ? Math.pow(2, o) * 100 : typeof m == "function" ? m(o + 1, { url: e, status: l, method: u.method }) || 0 : m
610
594
  )
611
595
  ), await fe(t, e, s, r, o + 1);
612
596
  }
613
597
  export {
614
- be as B,
615
- ye as N,
616
- Y as R,
617
- de as X,
618
- S as a,
619
- we as b,
598
+ D as E,
599
+ we as N,
600
+ be as Q,
601
+ z as R,
602
+ de as Z,
603
+ ye as _,
604
+ L as a,
605
+ j as b,
620
606
  Re as c,
621
- te as d,
622
- j as e,
623
- ge as f,
607
+ le as f,
624
608
  Ee as g,
625
609
  ke as h,
626
- P as i,
627
- pe as o,
610
+ ge as l,
628
611
  fe as r,
629
- me as s
612
+ me as s,
613
+ pe as u
630
614
  };
package/dist/wx.js CHANGED
@@ -1,5 +1,5 @@
1
- import { h as i, r as p, c as f, B as l, i as h, a as d, o as y, b as g } from "./retry-FiwuCZIu.js";
2
- import { g as H } from "./retry-FiwuCZIu.js";
1
+ import { h as i, r as p, c as f, Q as l, E as h, a as d, s as y, N as g } from "./retry-BjOxSyvC.js";
2
+ import { g as H } from "./retry-BjOxSyvC.js";
3
3
  const m = async function(t, n, r) {
4
4
  return i(await p(x, t, n, r), t, n, r);
5
5
  }, x = async function(t, n, r) {
@@ -43,10 +43,11 @@ const m = async function(t, n, r) {
43
43
  function R(t) {
44
44
  return typeof t == "string" ? t : t instanceof ArrayBuffer && y() ? new TextDecoder().decode(t) : JSON.stringify(t);
45
45
  }
46
+ // @__NO_SIDE_EFFECTS__
46
47
  function q(t) {
47
48
  return new g(m, t);
48
49
  }
49
- const s = q(), T = s.setConfig, b = s.head, C = s.get, N = s.post, S = s.del, A = s.put;
50
+ const s = /* @__PURE__ */ q(), T = s.setConfig, b = s.head, C = s.get, N = s.post, S = s.del, A = s.put;
50
51
  export {
51
52
  q as NetRequest,
52
53
  d as RequestInternalError,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seayoo-web/request",
3
- "version": "3.4.2",
3
+ "version": "3.4.5",
4
4
  "description": "request tools for seayoo web",
5
5
  "type": "module",
6
6
  "source": "./src/index.ts",
@@ -56,7 +56,7 @@
56
56
  "happy-dom": "^12.10.3",
57
57
  "msw": "^2.10.5",
58
58
  "vitest": "^3.2.4",
59
- "@seayoo-web/utils": "^4.1.3"
59
+ "@seayoo-web/utils": "^4.4.0"
60
60
  },
61
61
  "scripts": {
62
62
  "prebuild": "pnpm --F utils build",
@@ -2,8 +2,3 @@
2
2
  * 简版 Object.fromEntries 以处理某些低版本浏览器和 webview 的问题
3
3
  */
4
4
  export declare function fromEntries(kv: [string, string | undefined][]): Record<string, string>;
5
- /**
6
- * 安全解析 JSON
7
- * 防止原型污染和原型链攻击
8
- */
9
- export declare function safeJSONParse<T = unknown>(text: string): T | null;