@seayoo-web/request 2.0.7 → 2.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +2 -2
- package/dist/index.js +58 -58
- package/dist/node.cjs +1 -1
- package/dist/node.js +33 -35
- package/dist/request.fetch-Bz5Ien15.js +51 -0
- package/dist/request.fetch-ORK1-DKh.cjs +1 -0
- package/dist/{version-DNvZe-sl.js → type-4NUOpogE.js} +89 -91
- package/dist/type-CzbYPhI4.cjs +2 -0
- package/dist/wx.cjs +1 -1
- package/dist/wx.js +28 -30
- package/package.json +4 -9
- package/types/inc/request.fetch.d.ts +1 -1
- package/types/inc/request.node.d.ts +1 -1
- package/types/inc/request.wx.d.ts +1 -1
- package/types/inc/request.xhr.d.ts +1 -1
- package/types/inc/type.d.ts +12 -0
- package/types/index.d.ts +1 -1
- package/types/node.d.ts +0 -1
- package/types/wx.d.ts +0 -1
- package/dist/request.fetch-CMHsH_k5.cjs +0 -1
- package/dist/request.fetch-qLDV6Xdq.js +0 -51
- package/dist/version-DgNkLRed.cjs +0 -2
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
2
|
-
`).forEach(n=>{const r=n.trim();if(!r)return;const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./type-CzbYPhI4.cjs"),q=require("./request.fetch-ORK1-DKh.cjs"),y=async function(t,s,e){return o.handleResponse(await o.retryRequest(w,t,s,e),t,s,e)};async function m(t,s,e,n){const r=e==null?void 0:e.body,i=(e==null?void 0:e.method)==="PUT"?"PUT":"POST";if(s instanceof Blob){const u=new o.RequestGlobalConfig(n),f=await w(t,u,{...e,method:i,body:s});return o.handleResponse(f,t,u,e)}const c=new FormData,d={...s};r instanceof Object&&Object.entries(r).forEach(([u,f])=>{f instanceof Blob?d[u]=f:Array.isArray(f)?f.forEach((a,g)=>{c.append(`${u}[${g}]`,String(a))}):c.append(u,String(f))});for(const u in d)c.append(u,d[u]);const b=new o.RequestGlobalConfig(n),p=await w(t,b,{...e,method:i,body:c});return o.handleResponse(p,t,b,e)}const w=async function(t,s,e){const n=await o.convertOptions(t,s,e),r=n.method,i=e==null?void 0:e.onUploadProgress,c=o.Bt(n.url,n.params);return await new Promise(d=>{let b=null,p=!1;const u=function(){p||(a.abort(),p=!0)};function f(){b!==null&&clearTimeout(b),n.abort&&n.abort.removeEventListener("abort",u)}const a=new XMLHttpRequest;if(a.open(r,c,!0),i){let g=1;a.upload.addEventListener("progress",h=>{g=h.total,i({total:h.total,loaded:h.loaded})}),a.addEventListener("load",()=>{i({loaded:g,total:g})})}a.addEventListener("load",()=>{f(),d({url:c,method:r,status:a.status,statusText:a.statusText,headers:E(a),body:r==="HEAD"?"":a.responseText})}),a.addEventListener("error",g=>{f(),d({url:c,method:r,status:-1,statusText:o.RequestInteralError.Unknown,body:"",rawError:g})},!0),a.addEventListener("abort",()=>{f(),d({url:c,method:r,status:-1,statusText:o.RequestInteralError.Aborted,body:""})}),Object.entries(n.headers).forEach(([g,h])=>{a.setRequestHeader(g,h)}),n.credentials==="include"&&(a.withCredentials=!0),a.send(n.body||void 0),n.abort&&n.abort.addEventListener("abort",u),n.timeout>0&&(b=setTimeout(u,n.timeout))})};function E(t){const s={};if(!t)return s;const e=t.getAllResponseHeaders();return e&&e!=="null"&&e.replace(/\r/g,"").split(`
|
|
2
|
+
`).forEach(n=>{const r=n.trim();if(!r)return;const i=r.split(":"),c=i[0].trim();c&&(s[c]=(i[1]||"").trim())}),s}async function H(t,s,e={}){const n=window;"callback"in e||(e.callback="jsonxData"+Math.random().toString(16).slice(2));const r=e.callback+"";if(!t)return null;const i=o.Bt(t,e,!0);return new Promise(c=>{n[r]=function(d){if(r in window&&delete n[r],s(d))return d;console.warn("response type check faild",t,d),c(null)},o.je(i).catch(function(){c(null),delete n[r]})})}async function T(t,s,e={}){const n=window;return"var"in e||(e.var="jsonxData"+Math.random().toString(16).slice(2)),t?await o.je(o.Bt(t,e,!0)).then(()=>{const r=n[e.var+""];return s(r)?r:(console.warn("response type check faild",t,r),null)}).catch(()=>null):null}const j=async function(t,s,e){return await m(t,s,e,{baseURL:l.getConfig("baseURL"),logHandler:l.getConfig("logHandler"),errorHandler:l.getConfig("errorHandler"),requestTransformer:l.getConfig("requestTransformer"),messageHandler:l.getConfig("messageHandler"),responseHandler:l.getConfig("responseHandler")})};function R(t){if(!o.Support.window)throw new Error("Default Module Only Support In Browser");return o.Support.fetch?new o.NetRequestHandler(q.fetchRequest,t):new o.NetRequestHandler(y,t)}const l=R(),C=l.setConfig,S=l.request,x=l.head,L=l.get,U=l.post,k=l.del,v=l.put,D=l.patch;exports.RequestInteralError=o.RequestInteralError;exports.getResponseRulesDescription=o.getResponseRulesDescription;exports.NetRequest=R;exports.del=k;exports.get=L;exports.head=x;exports.jsonp=H;exports.jsonx=T;exports.patch=D;exports.post=U;exports.put=v;exports.request=S;exports.setGlobalConfig=C;exports.upload=j;
|
package/dist/index.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
import { h as w, r as
|
|
2
|
-
import { g as
|
|
3
|
-
import { f as
|
|
4
|
-
const
|
|
5
|
-
return w(await
|
|
1
|
+
import { h as w, r as T, R as y, c as x, B as p, a as R, j as E, S as H, N as q } from "./type-4NUOpogE.js";
|
|
2
|
+
import { g as J } from "./type-4NUOpogE.js";
|
|
3
|
+
import { f as C } from "./request.fetch-Bz5Ien15.js";
|
|
4
|
+
const L = async function(t, a, e) {
|
|
5
|
+
return w(await T(m, t, a, e), t, a, e);
|
|
6
6
|
};
|
|
7
|
-
async function
|
|
7
|
+
async function S(t, a, e, n) {
|
|
8
8
|
const r = e == null ? void 0 : e.body, i = (e == null ? void 0 : e.method) === "PUT" ? "PUT" : "POST";
|
|
9
|
-
if (
|
|
9
|
+
if (a instanceof Blob) {
|
|
10
10
|
const l = new y(n), u = await m(t, l, {
|
|
11
11
|
...e,
|
|
12
12
|
method: i,
|
|
13
|
-
body:
|
|
13
|
+
body: a
|
|
14
14
|
});
|
|
15
15
|
return w(u, t, l, e);
|
|
16
16
|
}
|
|
17
|
-
const s = new FormData(), d = { ...
|
|
17
|
+
const s = new FormData(), d = { ...a };
|
|
18
18
|
r instanceof Object && Object.entries(r).forEach(([l, u]) => {
|
|
19
|
-
u instanceof Blob ? d[l] = u : Array.isArray(u) ? u.forEach((
|
|
20
|
-
s.append(`${l}[${f}]`, String(
|
|
19
|
+
u instanceof Blob ? d[l] = u : Array.isArray(u) ? u.forEach((o, f) => {
|
|
20
|
+
s.append(`${l}[${f}]`, String(o));
|
|
21
21
|
}) : s.append(l, String(u));
|
|
22
22
|
});
|
|
23
23
|
for (const l in d)
|
|
@@ -29,64 +29,64 @@ async function L(t, o, e, n) {
|
|
|
29
29
|
});
|
|
30
30
|
return w(h, t, b, e);
|
|
31
31
|
}
|
|
32
|
-
const m = async function(t,
|
|
33
|
-
const n = await
|
|
32
|
+
const m = async function(t, a, e) {
|
|
33
|
+
const n = await x(t, a, e), r = n.method, i = e == null ? void 0 : e.onUploadProgress, s = p(n.url, n.params);
|
|
34
34
|
return await new Promise((d) => {
|
|
35
35
|
let b = null, h = !1;
|
|
36
36
|
const l = function() {
|
|
37
|
-
h || (
|
|
37
|
+
h || (o.abort(), h = !0);
|
|
38
38
|
};
|
|
39
39
|
function u() {
|
|
40
40
|
b !== null && clearTimeout(b), n.abort && n.abort.removeEventListener("abort", l);
|
|
41
41
|
}
|
|
42
|
-
const
|
|
43
|
-
if (
|
|
42
|
+
const o = new XMLHttpRequest();
|
|
43
|
+
if (o.open(r, s, !0), i) {
|
|
44
44
|
let f = 1;
|
|
45
|
-
|
|
45
|
+
o.upload.addEventListener("progress", (g) => {
|
|
46
46
|
f = g.total, i({ total: g.total, loaded: g.loaded });
|
|
47
|
-
}),
|
|
47
|
+
}), o.addEventListener("load", () => {
|
|
48
48
|
i({ loaded: f, total: f });
|
|
49
49
|
});
|
|
50
50
|
}
|
|
51
|
-
|
|
51
|
+
o.addEventListener("load", () => {
|
|
52
52
|
u(), d({
|
|
53
53
|
url: s,
|
|
54
54
|
method: r,
|
|
55
|
-
status:
|
|
56
|
-
statusText:
|
|
57
|
-
headers:
|
|
58
|
-
body: r === "HEAD" ? "" :
|
|
55
|
+
status: o.status,
|
|
56
|
+
statusText: o.statusText,
|
|
57
|
+
headers: j(o),
|
|
58
|
+
body: r === "HEAD" ? "" : o.responseText
|
|
59
59
|
});
|
|
60
|
-
}),
|
|
60
|
+
}), o.addEventListener(
|
|
61
61
|
"error",
|
|
62
62
|
(f) => {
|
|
63
63
|
u(), d({
|
|
64
64
|
url: s,
|
|
65
65
|
method: r,
|
|
66
66
|
status: -1,
|
|
67
|
-
statusText:
|
|
67
|
+
statusText: R.Unknown,
|
|
68
68
|
body: "",
|
|
69
69
|
rawError: f
|
|
70
70
|
});
|
|
71
71
|
},
|
|
72
72
|
!0
|
|
73
|
-
),
|
|
73
|
+
), o.addEventListener("abort", () => {
|
|
74
74
|
u(), d({
|
|
75
75
|
url: s,
|
|
76
76
|
method: r,
|
|
77
77
|
status: -1,
|
|
78
|
-
statusText:
|
|
78
|
+
statusText: R.Aborted,
|
|
79
79
|
body: ""
|
|
80
80
|
});
|
|
81
81
|
}), Object.entries(n.headers).forEach(([f, g]) => {
|
|
82
|
-
|
|
83
|
-
}), n.credentials === "include" && (
|
|
82
|
+
o.setRequestHeader(f, g);
|
|
83
|
+
}), n.credentials === "include" && (o.withCredentials = !0), o.send(n.body || void 0), n.abort && n.abort.addEventListener("abort", l), n.timeout > 0 && (b = setTimeout(l, n.timeout));
|
|
84
84
|
});
|
|
85
85
|
};
|
|
86
|
-
function
|
|
87
|
-
const
|
|
86
|
+
function j(t) {
|
|
87
|
+
const a = {};
|
|
88
88
|
if (!t)
|
|
89
|
-
return
|
|
89
|
+
return a;
|
|
90
90
|
const e = t.getAllResponseHeaders();
|
|
91
91
|
return e && e !== "null" && e.replace(/\r/g, "").split(`
|
|
92
92
|
`).forEach((n) => {
|
|
@@ -94,10 +94,10 @@ function S(t) {
|
|
|
94
94
|
if (!r)
|
|
95
95
|
return;
|
|
96
96
|
const i = r.split(":"), s = i[0].trim();
|
|
97
|
-
s && (
|
|
98
|
-
}),
|
|
97
|
+
s && (a[s] = (i[1] || "").trim());
|
|
98
|
+
}), a;
|
|
99
99
|
}
|
|
100
|
-
async function
|
|
100
|
+
async function D(t, a, e = {}) {
|
|
101
101
|
const n = window;
|
|
102
102
|
"callback" in e || (e.callback = "jsonxData" + Math.random().toString(16).slice(2));
|
|
103
103
|
const r = e.callback + "";
|
|
@@ -106,23 +106,23 @@ async function j(t, o, e = {}) {
|
|
|
106
106
|
const i = p(t, e, !0);
|
|
107
107
|
return new Promise((s) => {
|
|
108
108
|
n[r] = function(d) {
|
|
109
|
-
if (r in window && delete n[r],
|
|
109
|
+
if (r in window && delete n[r], a(d))
|
|
110
110
|
return d;
|
|
111
111
|
console.warn("response type check faild", t, d), s(null);
|
|
112
|
-
},
|
|
112
|
+
}, E(i).catch(function() {
|
|
113
113
|
s(null), delete n[r];
|
|
114
114
|
});
|
|
115
115
|
});
|
|
116
116
|
}
|
|
117
|
-
async function
|
|
117
|
+
async function O(t, a, e = {}) {
|
|
118
118
|
const n = window;
|
|
119
|
-
return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), t ? await
|
|
119
|
+
return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), t ? await E(p(t, e, !0)).then(() => {
|
|
120
120
|
const r = n[e.var + ""];
|
|
121
|
-
return
|
|
121
|
+
return a(r) ? r : (console.warn("response type check faild", t, r), null);
|
|
122
122
|
}).catch(() => null) : null;
|
|
123
123
|
}
|
|
124
|
-
const
|
|
125
|
-
return await
|
|
124
|
+
const P = async function(t, a, e) {
|
|
125
|
+
return await S(t, a, e, {
|
|
126
126
|
baseURL: c.getConfig("baseURL"),
|
|
127
127
|
logHandler: c.getConfig("logHandler"),
|
|
128
128
|
errorHandler: c.getConfig("errorHandler"),
|
|
@@ -131,25 +131,25 @@ const O = async function(t, o, e) {
|
|
|
131
131
|
responseHandler: c.getConfig("responseHandler")
|
|
132
132
|
});
|
|
133
133
|
};
|
|
134
|
-
function
|
|
135
|
-
if (!
|
|
134
|
+
function U(t) {
|
|
135
|
+
if (!H.window)
|
|
136
136
|
throw new Error("Default Module Only Support In Browser");
|
|
137
|
-
return
|
|
137
|
+
return H.fetch ? new q(C, t) : new q(L, t);
|
|
138
138
|
}
|
|
139
|
-
const c =
|
|
139
|
+
const c = U(), A = c.setConfig, B = c.request, M = c.head, N = c.get, X = c.post, F = c.del, G = c.put, I = c.patch;
|
|
140
140
|
export {
|
|
141
|
-
|
|
142
|
-
|
|
141
|
+
U as NetRequest,
|
|
142
|
+
R as RequestInteralError,
|
|
143
|
+
F as del,
|
|
143
144
|
N as get,
|
|
144
|
-
|
|
145
|
+
J as getResponseRulesDescription,
|
|
145
146
|
M as head,
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
I as version
|
|
147
|
+
D as jsonp,
|
|
148
|
+
O as jsonx,
|
|
149
|
+
I as patch,
|
|
150
|
+
X as post,
|
|
151
|
+
G as put,
|
|
152
|
+
B as request,
|
|
153
|
+
A as setGlobalConfig,
|
|
154
|
+
P as upload
|
|
155
155
|
};
|
package/dist/node.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./type-CzbYPhI4.cjs"),b=require("./request.fetch-ORK1-DKh.cjs"),y=require("node:http"),g=require("node:https"),w=async function(o,a,c){return e.handleResponse(await e.retryRequest(E,o,a,c),o,a,c)},E=async function(o,a,c){const t=await e.convertOptions(o,a,c);if(!e.X(t.url))return{url:t.url,method:t.method,status:-1,statusText:e.RequestInteralError.URLFormatError,headers:{},body:""};const q=/^https:\/\//i.test(t.url)?g:y,u=new URL(t.url),i=t.params;i instanceof Object&&Object.keys(i).forEach(n=>u.searchParams.set(n,i[n]));const f=t.method==="HEAD";return new Promise(n=>{const d=q.request(u,{headers:t.headers,method:t.method,timeout:t.timeout>0?t.timeout:void 0},function(s){const l=[];s.on("data",h=>l.push(h)),s.on("end",()=>{const h=e.fromEntries(Object.entries(s.headers).map(([R,m])=>[R.toLowerCase(),Array.isArray(m)?m.join(","):m]));n({url:u.toString(),method:t.method,status:s.statusCode||-1,statusText:s.statusMessage||e.RequestInteralError.Unknown,headers:h,body:f?"":Buffer.concat(l).toString("utf-8")})})});d.on("error",s=>{n({url:u.toString(),method:t.method,status:-1,statusText:e.RequestInteralError.Unknown,body:"",rawError:s})}),d.on("timeout",()=>{n({url:u.toString(),method:t.method,status:-1,statusText:e.RequestInteralError.Timeout,body:""})}),t.body&&d.write(t.body),d.end()})};function p(o){return e.Support.fetch?new e.NetRequestHandler(b.fetchRequest,o):new e.NetRequestHandler(w,o)}const r=p(),S=r.setConfig,T=r.head,j=r.get,C=r.post,N=r.del,O=r.put,U=r.patch;exports.NetRequest=p;exports.del=N;exports.get=j;exports.head=T;exports.patch=U;exports.post=C;exports.put=O;exports.setGlobalConfig=S;
|
package/dist/node.js
CHANGED
|
@@ -1,26 +1,25 @@
|
|
|
1
|
-
import { h as y, r as
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}, j = async function(e, a, u) {
|
|
1
|
+
import { h as y, r as q, c as w, X as g, a as d, f as E, N as f, S } from "./type-4NUOpogE.js";
|
|
2
|
+
import { f as x } from "./request.fetch-Bz5Ien15.js";
|
|
3
|
+
import T from "node:http";
|
|
4
|
+
import U from "node:https";
|
|
5
|
+
const j = async function(e, a, u) {
|
|
6
|
+
return y(await q(C, e, a, u), e, a, u);
|
|
7
|
+
}, C = async function(e, a, u) {
|
|
9
8
|
const t = await w(e, a, u);
|
|
10
|
-
if (!
|
|
9
|
+
if (!g(t.url))
|
|
11
10
|
return {
|
|
12
11
|
url: t.url,
|
|
13
12
|
method: t.method,
|
|
14
13
|
status: -1,
|
|
15
|
-
statusText:
|
|
14
|
+
statusText: d.URLFormatError,
|
|
16
15
|
headers: {},
|
|
17
16
|
body: ""
|
|
18
17
|
};
|
|
19
|
-
const
|
|
20
|
-
|
|
21
|
-
const
|
|
18
|
+
const l = /^https:\/\//i.test(t.url) ? U : T, n = new URL(t.url), i = t.params;
|
|
19
|
+
i instanceof Object && Object.keys(i).forEach((r) => n.searchParams.set(r, i[r]));
|
|
20
|
+
const R = t.method === "HEAD";
|
|
22
21
|
return new Promise((r) => {
|
|
23
|
-
const c =
|
|
22
|
+
const c = l.request(
|
|
24
23
|
n,
|
|
25
24
|
{
|
|
26
25
|
headers: t.headers,
|
|
@@ -28,18 +27,18 @@ const U = async function(e, a, u) {
|
|
|
28
27
|
timeout: t.timeout > 0 ? t.timeout : void 0
|
|
29
28
|
},
|
|
30
29
|
function(o) {
|
|
31
|
-
const
|
|
32
|
-
o.on("data", (
|
|
33
|
-
const
|
|
34
|
-
Object.entries(o.headers).map(([
|
|
30
|
+
const p = [];
|
|
31
|
+
o.on("data", (m) => p.push(m)), o.on("end", () => {
|
|
32
|
+
const m = E(
|
|
33
|
+
Object.entries(o.headers).map(([b, h]) => [b.toLowerCase(), Array.isArray(h) ? h.join(",") : h])
|
|
35
34
|
);
|
|
36
35
|
r({
|
|
37
36
|
url: n.toString(),
|
|
38
37
|
method: t.method,
|
|
39
38
|
status: o.statusCode || -1,
|
|
40
|
-
statusText: o.statusMessage ||
|
|
41
|
-
headers:
|
|
42
|
-
body:
|
|
39
|
+
statusText: o.statusMessage || d.Unknown,
|
|
40
|
+
headers: m,
|
|
41
|
+
body: R ? "" : Buffer.concat(p).toString("utf-8")
|
|
43
42
|
});
|
|
44
43
|
});
|
|
45
44
|
}
|
|
@@ -49,7 +48,7 @@ const U = async function(e, a, u) {
|
|
|
49
48
|
url: n.toString(),
|
|
50
49
|
method: t.method,
|
|
51
50
|
status: -1,
|
|
52
|
-
statusText:
|
|
51
|
+
statusText: d.Unknown,
|
|
53
52
|
body: "",
|
|
54
53
|
rawError: o
|
|
55
54
|
});
|
|
@@ -58,24 +57,23 @@ const U = async function(e, a, u) {
|
|
|
58
57
|
url: n.toString(),
|
|
59
58
|
method: t.method,
|
|
60
59
|
status: -1,
|
|
61
|
-
statusText:
|
|
60
|
+
statusText: d.Timeout,
|
|
62
61
|
body: ""
|
|
63
62
|
});
|
|
64
63
|
}), t.body && c.write(t.body), c.end();
|
|
65
64
|
});
|
|
66
65
|
};
|
|
67
|
-
function
|
|
68
|
-
return
|
|
66
|
+
function L(e) {
|
|
67
|
+
return S.fetch ? new f(x, e) : new f(j, e);
|
|
69
68
|
}
|
|
70
|
-
const s =
|
|
69
|
+
const s = L(), k = s.setConfig, P = s.head, B = s.get, D = s.post, F = s.del, G = s.put, I = s.patch;
|
|
71
70
|
export {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
v as version
|
|
71
|
+
L as NetRequest,
|
|
72
|
+
F as del,
|
|
73
|
+
B as get,
|
|
74
|
+
P as head,
|
|
75
|
+
I as patch,
|
|
76
|
+
D as post,
|
|
77
|
+
G as put,
|
|
78
|
+
k as setGlobalConfig
|
|
81
79
|
};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { h as f, r as w, c as y, S as E, f as p, a as d } from "./type-4NUOpogE.js";
|
|
2
|
+
const R = async function(s, o, n) {
|
|
3
|
+
return f(await w(g, s, o, n), s, o, n);
|
|
4
|
+
}, g = async function(s, o, n) {
|
|
5
|
+
const t = await y(s, o, n), r = new URL(t.url), i = t.params;
|
|
6
|
+
i instanceof Object && Object.keys(i).forEach((e) => r.searchParams.set(e, i[e]));
|
|
7
|
+
const a = E.AbortController ? new AbortController() : null;
|
|
8
|
+
function u() {
|
|
9
|
+
a && !a.signal.aborted && a.abort();
|
|
10
|
+
}
|
|
11
|
+
t.abort && t.abort.addEventListener("abort", u);
|
|
12
|
+
const h = t.timeout > 0 ? setTimeout(u, t.timeout) : null, l = t.method === "HEAD";
|
|
13
|
+
return await fetch(r, {
|
|
14
|
+
method: t.method,
|
|
15
|
+
headers: Object.keys(t.headers).length > 0 ? new Headers(t.headers) : void 0,
|
|
16
|
+
body: t.body,
|
|
17
|
+
credentials: t.credentials,
|
|
18
|
+
signal: a == null ? void 0 : a.signal,
|
|
19
|
+
redirect: "follow"
|
|
20
|
+
}).then(async (e) => {
|
|
21
|
+
const c = l ? "" : await e.text().catch((m) => m), b = p([...e.headers.entries()]);
|
|
22
|
+
return c instanceof Error ? {
|
|
23
|
+
url: r.toString(),
|
|
24
|
+
method: t.method,
|
|
25
|
+
status: e.status,
|
|
26
|
+
statusText: e.statusText || d.Unknown,
|
|
27
|
+
body: "",
|
|
28
|
+
headers: b,
|
|
29
|
+
rawError: c
|
|
30
|
+
} : {
|
|
31
|
+
url: r.toString(),
|
|
32
|
+
method: t.method,
|
|
33
|
+
status: e.status,
|
|
34
|
+
statusText: e.statusText,
|
|
35
|
+
headers: b,
|
|
36
|
+
body: c
|
|
37
|
+
};
|
|
38
|
+
}).catch((e) => ({
|
|
39
|
+
url: r.toString(),
|
|
40
|
+
method: t.method,
|
|
41
|
+
status: -1,
|
|
42
|
+
statusText: a != null && a.signal.aborted ? d.Aborted : d.Unknown,
|
|
43
|
+
body: "",
|
|
44
|
+
rawError: e
|
|
45
|
+
})).finally(() => {
|
|
46
|
+
h !== null && clearTimeout(h), t.abort && t.abort.removeEventListener("abort", u);
|
|
47
|
+
});
|
|
48
|
+
};
|
|
49
|
+
export {
|
|
50
|
+
R as f
|
|
51
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const s=require("./type-CzbYPhI4.cjs"),f=async function(a,n,i){return s.handleResponse(await s.retryRequest(w,a,n,i),a,n,i)},w=async function(a,n,i){const t=await s.convertOptions(a,n,i),o=new URL(t.url),u=t.params;u instanceof Object&&Object.keys(u).forEach(e=>o.searchParams.set(e,u[e]));const r=s.Support.AbortController?new AbortController:null;function c(){r&&!r.signal.aborted&&r.abort()}t.abort&&t.abort.addEventListener("abort",c);const h=t.timeout>0?setTimeout(c,t.timeout):null,b=t.method==="HEAD";return await fetch(o,{method:t.method,headers:Object.keys(t.headers).length>0?new Headers(t.headers):void 0,body:t.body,credentials:t.credentials,signal:r==null?void 0:r.signal,redirect:"follow"}).then(async e=>{const d=b?"":await e.text().catch(m=>m),l=s.fromEntries([...e.headers.entries()]);return d instanceof Error?{url:o.toString(),method:t.method,status:e.status,statusText:e.statusText||s.RequestInteralError.Unknown,body:"",headers:l,rawError:d}:{url:o.toString(),method:t.method,status:e.status,statusText:e.statusText,headers:l,body:d}}).catch(e=>({url:o.toString(),method:t.method,status:-1,statusText:r!=null&&r.signal.aborted?s.RequestInteralError.Aborted:s.RequestInteralError.Unknown,body:"",rawError:e})).finally(()=>{h!==null&&clearTimeout(h),t.abort&&t.abort.removeEventListener("abort",c)})};exports.fetchRequest=f;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var y = (e, t, s) =>
|
|
4
|
-
const p = typeof globalThis < "u" ? globalThis : typeof global < "u" ? global : typeof window < "u" ? window : {},
|
|
1
|
+
var H = Object.defineProperty;
|
|
2
|
+
var q = (e, t, s) => t in e ? H(e, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : e[t] = s;
|
|
3
|
+
var y = (e, t, s) => q(e, typeof t != "symbol" ? t + "" : t, s);
|
|
4
|
+
const p = typeof globalThis < "u" ? globalThis : typeof global < "u" ? global : typeof window < "u" ? window : {}, O = {
|
|
5
5
|
fetch: "fetch" in p,
|
|
6
6
|
window: "window" in p,
|
|
7
7
|
FormData: "FormData" in p,
|
|
@@ -10,7 +10,7 @@ const p = typeof globalThis < "u" ? globalThis : typeof global < "u" ? global :
|
|
|
10
10
|
TextDecoder: "TextDecoder" in p,
|
|
11
11
|
AbortController: "AbortController" in p
|
|
12
12
|
};
|
|
13
|
-
function
|
|
13
|
+
function N(e) {
|
|
14
14
|
return e.reduce(
|
|
15
15
|
(t, [s, r]) => (s && (t[s] = r || ""), t),
|
|
16
16
|
{}
|
|
@@ -19,9 +19,9 @@ function I(e) {
|
|
|
19
19
|
function K(e) {
|
|
20
20
|
return new Promise((t) => setTimeout(t, Math.max(0, e)));
|
|
21
21
|
}
|
|
22
|
-
function
|
|
22
|
+
function I() {
|
|
23
23
|
}
|
|
24
|
-
function
|
|
24
|
+
function G(e) {
|
|
25
25
|
const t = e || "CustomError";
|
|
26
26
|
return class extends Error {
|
|
27
27
|
constructor(s) {
|
|
@@ -29,38 +29,37 @@ function W(e) {
|
|
|
29
29
|
value: t,
|
|
30
30
|
enumerable: !1,
|
|
31
31
|
configurable: !0
|
|
32
|
-
}), "setPrototypeOf" in Object && Object.setPrototypeOf(this, new.target.prototype), "captureStackTrace" in Error && Error.captureStackTrace(this, this.constructor);
|
|
32
|
+
}), "setPrototypeOf" in Object && Object.setPrototypeOf(this, new.target.prototype), "captureStackTrace" in Error && typeof Error.captureStackTrace == "function" && Error.captureStackTrace(this, this.constructor);
|
|
33
33
|
}
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
const P = /_\w*/, _ = /-\w*/;
|
|
40
|
-
function A(e) {
|
|
41
|
-
const t = P.test(e) ? e.replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (s, r) => r.toUpperCase()) : _.test(e) ? e.replace(/(?:^-*|-*$)/g, "").replace(/-+(\w)/g, (s, r) => r.toUpperCase()) : e;
|
|
42
|
-
return j(t);
|
|
43
|
-
}
|
|
44
|
-
function M(e) {
|
|
45
|
-
return P.test(e) ? j(e).replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (t, s) => "_" + s.toLowerCase()) : _.test(e) ? j(e.replace(/(?:^-*|-*$)/g, "")).replace(/-+(\w)/g, (t, s) => "_" + s.toLowerCase()) : j(e).replace(/[A-Z]/g, (t) => `_${t.toLowerCase()}`);
|
|
36
|
+
const J = /^(?:https?:)?\/\/.+$/i, V = /^https?:\/\/.+$/i, W = /^\{[\d\D]*\}$/, z = /^\[[\d\D]*\]$/;
|
|
37
|
+
function T(e, t = !1) {
|
|
38
|
+
return t ? J.test(e) : V.test(e);
|
|
46
39
|
}
|
|
47
|
-
|
|
48
|
-
function F(e, t = !1) {
|
|
49
|
-
return t ? G.test(e) : J.test(e);
|
|
50
|
-
}
|
|
51
|
-
function Z(e) {
|
|
40
|
+
function X(e) {
|
|
52
41
|
return e == null;
|
|
53
42
|
}
|
|
54
|
-
function
|
|
55
|
-
if (!e || typeof e != "object")
|
|
56
|
-
return !1;
|
|
43
|
+
function k(e, ...t) {
|
|
44
|
+
if (!e || typeof e != "object") return !1;
|
|
57
45
|
const s = Object.getPrototypeOf(e);
|
|
58
46
|
return s !== Object.prototype && s !== null ? !1 : t.every((r) => r in e);
|
|
59
47
|
}
|
|
60
|
-
function
|
|
61
|
-
return
|
|
48
|
+
function S(e) {
|
|
49
|
+
return W.test(e) || z.test(e);
|
|
50
|
+
}
|
|
51
|
+
function j(e) {
|
|
52
|
+
return e ? e[0].toLowerCase() + e.slice(1) : "";
|
|
53
|
+
}
|
|
54
|
+
const x = /_\w*/, P = /-\w*/;
|
|
55
|
+
function C(e) {
|
|
56
|
+
const t = x.test(e) ? e.replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (s, r) => r.toUpperCase()) : P.test(e) ? e.replace(/(?:^-*|-*$)/g, "").replace(/-+(\w)/g, (s, r) => r.toUpperCase()) : e;
|
|
57
|
+
return j(t);
|
|
62
58
|
}
|
|
63
|
-
function
|
|
59
|
+
function U(e) {
|
|
60
|
+
return x.test(e) ? j(e).replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (t, s) => "_" + s.toLowerCase()) : P.test(e) ? j(e.replace(/(?:^-*|-*$)/g, "")).replace(/-+(\w)/g, (t, s) => "_" + s.toLowerCase()) : j(e).replace(/[A-Z]/g, (t) => `_${t.toLowerCase()}`);
|
|
61
|
+
}
|
|
62
|
+
function E(e, t) {
|
|
64
63
|
try {
|
|
65
64
|
const s = JSON.parse(e);
|
|
66
65
|
return t ? t(s) ? s : null : s;
|
|
@@ -69,25 +68,25 @@ function k(e, t) {
|
|
|
69
68
|
}
|
|
70
69
|
}
|
|
71
70
|
"" + Math.random().toString(32).slice(2);
|
|
72
|
-
function
|
|
73
|
-
return typeof e == "string" ?
|
|
71
|
+
function $(e, t = !1) {
|
|
72
|
+
return typeof e == "string" ? C(e) : Array.isArray(e) ? e.map((s) => !t || typeof s == "string" ? $(s) : typeof s == "object" && s ? $(s, !0) : s) : k(e) ? Object.keys(e).reduce(
|
|
74
73
|
(s, r) => {
|
|
75
|
-
const a =
|
|
76
|
-
return s[n] = t ? e[r] :
|
|
74
|
+
const a = C(String(r)), n = a.charAt(0).toLowerCase() + a.slice(1);
|
|
75
|
+
return s[n] = t ? e[r] : $(e[r]), s;
|
|
77
76
|
},
|
|
78
77
|
{}
|
|
79
78
|
) : e;
|
|
80
79
|
}
|
|
81
|
-
function
|
|
82
|
-
return typeof e == "string" ?
|
|
80
|
+
function R(e, t = !1) {
|
|
81
|
+
return typeof e == "string" ? U(e) : Array.isArray(e) ? e.map((s) => !t || typeof s == "string" ? R(s) : typeof s == "object" && s ? R(s, t) : s) : k(e) ? Object.keys(e).reduce(
|
|
83
82
|
(s, r) => {
|
|
84
|
-
const a =
|
|
85
|
-
return s[a] = t ? e[r] :
|
|
83
|
+
const a = U(String(r));
|
|
84
|
+
return s[a] = t ? e[r] : R(e[r]), s;
|
|
86
85
|
},
|
|
87
86
|
{}
|
|
88
87
|
) : e;
|
|
89
88
|
}
|
|
90
|
-
async function
|
|
89
|
+
async function je(e) {
|
|
91
90
|
return await new Promise(function(t) {
|
|
92
91
|
const s = document.getElementsByTagName("head")[0], r = document.createElement("script");
|
|
93
92
|
r.setAttribute("type", "text/javascript"), r.setAttribute("charset", "utf-8"), r.onload = function() {
|
|
@@ -97,7 +96,7 @@ async function we(e) {
|
|
|
97
96
|
}, r.setAttribute("src", e), s.appendChild(r);
|
|
98
97
|
});
|
|
99
98
|
}
|
|
100
|
-
function
|
|
99
|
+
function Z(e, t = "数据未能正确识别") {
|
|
101
100
|
return typeof e == "function" ? {
|
|
102
101
|
guard: e,
|
|
103
102
|
message: t
|
|
@@ -107,10 +106,10 @@ function z(e, t = "数据未能正确识别") {
|
|
|
107
106
|
};
|
|
108
107
|
}
|
|
109
108
|
function Q(e, t = "") {
|
|
110
|
-
return !t ||
|
|
109
|
+
return !t || T(e, !0) ? L(e) : (L(t) + "/" + e).replace(/\/{2,}/g, "/").replace(/:\//, "://");
|
|
111
110
|
}
|
|
112
|
-
function
|
|
113
|
-
return
|
|
111
|
+
function L(e) {
|
|
112
|
+
return T(e, !0) ? e.startsWith("http") ? e : ("location" in globalThis ? location.protocol : "https:") + e : ("location" in globalThis ? location.origin : "http://127.0.0.1") + "/" + e.replace(/^\/+/, "");
|
|
114
113
|
}
|
|
115
114
|
function Y(e, t) {
|
|
116
115
|
const s = {};
|
|
@@ -123,12 +122,11 @@ function ee(e, t) {
|
|
|
123
122
|
if (t) {
|
|
124
123
|
if (t === !0)
|
|
125
124
|
return e.replace(/\?[^#]*/, "");
|
|
126
|
-
} else
|
|
127
|
-
return e;
|
|
125
|
+
} else return e;
|
|
128
126
|
const s = e.split("#"), r = s[0].split("?"), a = r[0], n = r.length > 1 ? r[1] : "", o = s.length > 1 ? "#" + s[1] : "", c = typeof t == "string" ? [t] : Array.isArray(t) ? t : [];
|
|
129
127
|
return !c.length || !n ? s[0] + o : (c.map((i) => i.replace(/([\\(){}[\]^$+\-*?|])/g, "\\$1")), (a + "?" + n.replace(new RegExp("(?:^|&)(?:" + c.join("|") + ")=[^&$]+", "g"), "").replace(/^&/, "")).replace(/\?$/, "") + o);
|
|
130
128
|
}
|
|
131
|
-
function
|
|
129
|
+
function $e(e, t, s = !1) {
|
|
132
130
|
const r = typeof t == "string" ? t : Object.keys(t).map((o) => `${o}=${encodeURIComponent(t[o])}`).join("&");
|
|
133
131
|
if (!r)
|
|
134
132
|
return e;
|
|
@@ -194,7 +192,7 @@ class re {
|
|
|
194
192
|
t && this.set(t);
|
|
195
193
|
}
|
|
196
194
|
set(t) {
|
|
197
|
-
if (t.baseURL && !/^\/.+/.test(t.baseURL) && !
|
|
195
|
+
if (t.baseURL && !/^\/.+/.test(t.baseURL) && !T(t.baseURL))
|
|
198
196
|
throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"), new Error("BaseURLError");
|
|
199
197
|
Object.assign(this.config, t);
|
|
200
198
|
}
|
|
@@ -210,11 +208,11 @@ class re {
|
|
|
210
208
|
this.config.messageHandler && s && this.config.messageHandler(t, s, r, a);
|
|
211
209
|
}
|
|
212
210
|
}
|
|
213
|
-
const
|
|
211
|
+
const M = "UnexpectResponse";
|
|
214
212
|
function ne(e, t, s, r) {
|
|
215
|
-
if (t.ok && !
|
|
216
|
-
const a =
|
|
217
|
-
return a.guard(t.data) || (s.showMessage(!0, `${e} ${a.message}`, t.code, t.status), console.error(
|
|
213
|
+
if (t.ok && !X(t.data) && r) {
|
|
214
|
+
const a = Z(r, "响应数据未能正确识别");
|
|
215
|
+
return a.guard(t.data) || (s.showMessage(!0, `${e} ${a.message}`, t.code, t.status), console.error(M, e, t.data), t.data = null, t.code = M, t.message = a.message), t;
|
|
218
216
|
}
|
|
219
217
|
return t;
|
|
220
218
|
}
|
|
@@ -293,13 +291,13 @@ class Re {
|
|
|
293
291
|
return n.method = "PATCH", n.body = s || {}, this.guard(t, await this.request(t, n), r || null);
|
|
294
292
|
}
|
|
295
293
|
}
|
|
296
|
-
async function
|
|
294
|
+
async function Oe(e, t, s) {
|
|
297
295
|
var g;
|
|
298
|
-
const r = Object.assign({ method: "GET" }, s), a =
|
|
296
|
+
const r = Object.assign({ method: "GET" }, s), a = O.FormData ? r.body instanceof FormData : !1, n = a && r.method !== "POST" && r.method !== "PUT" ? "POST" : r.method, o = n === "GET" || n === "HEAD" || n === "DELETE";
|
|
299
297
|
o && r.body !== void 0 && (console.warn("request body is invalid with method get, head, delete"), delete r.body);
|
|
300
298
|
const c = Object.assign(
|
|
301
299
|
a || o ? {} : {
|
|
302
|
-
"Content-Type":
|
|
300
|
+
"Content-Type": O.Blob && r.body instanceof Blob ? r.body.type || "application/octet-stream" : "application/json;charset=utf-8"
|
|
303
301
|
},
|
|
304
302
|
r.headers
|
|
305
303
|
), i = r.params || {}, h = {};
|
|
@@ -327,14 +325,14 @@ function ae(e) {
|
|
|
327
325
|
}
|
|
328
326
|
function oe(e) {
|
|
329
327
|
if (e)
|
|
330
|
-
return typeof e == "string" || e instanceof URLSearchParams || e instanceof ArrayBuffer ||
|
|
328
|
+
return typeof e == "string" || e instanceof URLSearchParams || e instanceof ArrayBuffer || O.Blob && e instanceof Blob || O.FormData && e instanceof FormData ? e : JSON.stringify(e);
|
|
331
329
|
}
|
|
332
|
-
const
|
|
330
|
+
const _ = "data", w = "message";
|
|
333
331
|
function ce(e, t, s, r, a) {
|
|
334
332
|
const n = a || r;
|
|
335
|
-
return
|
|
333
|
+
return A(e) ? fe(n.ok || r.ok, e, t, s) : ie(n.failed || r.failed, t, s);
|
|
336
334
|
}
|
|
337
|
-
const
|
|
335
|
+
const ke = function(e) {
|
|
338
336
|
const t = [], s = e.failed || { resolve: "json" };
|
|
339
337
|
switch (t.push("- 当http状态码 <200 或者 >=400 时"), s.resolve) {
|
|
340
338
|
case "body":
|
|
@@ -351,7 +349,7 @@ const Ee = function(e) {
|
|
|
351
349
|
break;
|
|
352
350
|
case "json":
|
|
353
351
|
t.push(
|
|
354
|
-
" 将响应解析为 json,读取 " + (r.dataField ||
|
|
352
|
+
" 将响应解析为 json,读取 " + (r.dataField || _) + " 作为响应数据,读取 " + (r.messageField || w) + " 作为提示消息"
|
|
355
353
|
), r.statusField && t.push(
|
|
356
354
|
" 当 " + r.statusField + " 为 " + (r.statusOKValue || "空值") + " 时是成功提示,否则是错误消息"
|
|
357
355
|
), r.ignoreMessage && t.push(" 并忽略以下消息:" + r.ignoreMessage);
|
|
@@ -369,20 +367,20 @@ function ie(e, t, s) {
|
|
|
369
367
|
};
|
|
370
368
|
switch (r.resolve) {
|
|
371
369
|
case "body":
|
|
372
|
-
a.message =
|
|
370
|
+
a.message = D(s) || s;
|
|
373
371
|
break;
|
|
374
372
|
case "json":
|
|
375
373
|
const { code: n, message: o } = ue(s, r.converter, r.statusField, r.messageField);
|
|
376
|
-
a.code = n || t, a.message =
|
|
374
|
+
a.code = n || t, a.message = D(s) || o;
|
|
377
375
|
break;
|
|
378
376
|
}
|
|
379
377
|
return a;
|
|
380
378
|
}
|
|
381
379
|
function ue(e, t, s, r = w) {
|
|
382
|
-
if (!
|
|
380
|
+
if (!S(e))
|
|
383
381
|
return { message: "" };
|
|
384
|
-
const a =
|
|
385
|
-
return !a || !
|
|
382
|
+
const a = F(E(e), t);
|
|
383
|
+
return !a || !k(a) ? { message: e } : {
|
|
386
384
|
code: s ? v(a, s) : "",
|
|
387
385
|
message: v(a, r) || e
|
|
388
386
|
};
|
|
@@ -398,7 +396,7 @@ function le(e) {
|
|
|
398
396
|
return e ? typeof e == "string" ? e : JSON.stringify(e) : "";
|
|
399
397
|
}
|
|
400
398
|
const he = /<title>([^<]+)<\/title>/i, de = /<message>([^<]+)<\/message>/i;
|
|
401
|
-
function
|
|
399
|
+
function D(e) {
|
|
402
400
|
const t = e.match(he);
|
|
403
401
|
if (t)
|
|
404
402
|
return t[1];
|
|
@@ -415,23 +413,23 @@ function fe(e, t, s, r) {
|
|
|
415
413
|
if (t === 204 || !r)
|
|
416
414
|
return n;
|
|
417
415
|
if (a.resolve === "body")
|
|
418
|
-
return n.data =
|
|
419
|
-
const o =
|
|
420
|
-
if (!o || !
|
|
416
|
+
return n.data = S(r) ? F(E(r), e.converter) : r, n;
|
|
417
|
+
const o = F(E(r), e.converter);
|
|
418
|
+
if (!o || !k(o))
|
|
421
419
|
return n.ok = !1, n.code = "ResponseFormatError", n.message = "响应内容无法格式化为 Object", n;
|
|
422
|
-
const c = a.statusField, i = a.statusOKValue || "", h = a.dataField ||
|
|
420
|
+
const c = a.statusField, i = a.statusOKValue || "", h = a.dataField || _, u = a.messageField || w, l = a.ignoreMessage || "";
|
|
423
421
|
if (c && !(c in o))
|
|
424
422
|
return n.ok = !1, n.code = "ResponseFieldMissing", n.message = "响应内容找不到状态字段 " + c, n;
|
|
425
423
|
const d = c ? o[c] + "" : "";
|
|
426
424
|
return n.ok = c ? d === i : !0, n.code = d || s, n.data = h === !0 ? o : h in o ? o[h] : null, n.message = v(o, u), l && n.message && (Array.isArray(l) && l.includes(n.message) || typeof l == "string" && n.message === l) && (n.message = ""), n;
|
|
427
425
|
}
|
|
428
|
-
function
|
|
426
|
+
function A(e) {
|
|
429
427
|
return e >= 200 && e < 400;
|
|
430
428
|
}
|
|
431
|
-
function
|
|
432
|
-
return t === "camelize" ?
|
|
429
|
+
function F(e, t) {
|
|
430
|
+
return t === "camelize" ? $(e) : t === "snakify" ? R(e) : e;
|
|
433
431
|
}
|
|
434
|
-
const ge =
|
|
432
|
+
const ge = G("APIError");
|
|
435
433
|
function me(e) {
|
|
436
434
|
const t = {};
|
|
437
435
|
for (const a in e.headers)
|
|
@@ -454,9 +452,9 @@ function me(e) {
|
|
|
454
452
|
}
|
|
455
453
|
};
|
|
456
454
|
}
|
|
457
|
-
function
|
|
455
|
+
function Ee(e, t, s, r) {
|
|
458
456
|
var f;
|
|
459
|
-
const a = e.status, n = e.method, o =
|
|
457
|
+
const a = e.status, n = e.method, o = N(
|
|
460
458
|
Object.entries(e.headers || {}).map(([m, g]) => [m.toLowerCase(), g])
|
|
461
459
|
), { ok: c, code: i, data: h, message: u } = ce(
|
|
462
460
|
a,
|
|
@@ -465,7 +463,7 @@ function Oe(e, t, s, r) {
|
|
|
465
463
|
s.get("responseRule"),
|
|
466
464
|
r == null ? void 0 : r.responseRule
|
|
467
465
|
);
|
|
468
|
-
if (!
|
|
466
|
+
if (!A(a)) {
|
|
469
467
|
const m = me({
|
|
470
468
|
url: e.url,
|
|
471
469
|
method: e.method,
|
|
@@ -489,7 +487,7 @@ function Oe(e, t, s, r) {
|
|
|
489
487
|
});
|
|
490
488
|
}
|
|
491
489
|
if (a < 0)
|
|
492
|
-
return
|
|
490
|
+
return B(
|
|
493
491
|
{ ok: !1, status: a, code: e.statusText, headers: {}, message: "", data: null },
|
|
494
492
|
`${n} ${t} ${e.statusText}`,
|
|
495
493
|
n,
|
|
@@ -500,9 +498,9 @@ function Oe(e, t, s, r) {
|
|
|
500
498
|
const l = { ok: c, data: h, code: i, message: u, status: a, headers: o };
|
|
501
499
|
(f = s.get("responseHandler")) == null || f({ ...l }, n, t);
|
|
502
500
|
const d = c ? u : u || e.statusText;
|
|
503
|
-
return
|
|
501
|
+
return B(l, d, n, t, s, r);
|
|
504
502
|
}
|
|
505
|
-
function
|
|
503
|
+
function B(e, t, s, r, a, n) {
|
|
506
504
|
const o = a.get("message"), c = o === !1 || (n == null ? void 0 : n.message) === !1 ? !1 : (n == null ? void 0 : n.message) || o;
|
|
507
505
|
if (c !== !1) {
|
|
508
506
|
const i = typeof c == "function" ? c(e, s, r, t) : t;
|
|
@@ -517,7 +515,7 @@ function ye(e) {
|
|
|
517
515
|
console.error("RequestError", t);
|
|
518
516
|
}
|
|
519
517
|
async function pe(e, t, s, r, a) {
|
|
520
|
-
const n = a || 0, o = Math.max(0, Math.min(10, (r == null ? void 0 : r.maxRetry) ?? s.get("maxRetry") ?? 0)), c = (r == null ? void 0 : r.retryResolve) ?? s.get("retryResolve"), i = s.get("logHandler") ||
|
|
518
|
+
const n = a || 0, o = Math.max(0, Math.min(10, (r == null ? void 0 : r.maxRetry) ?? s.get("maxRetry") ?? 0)), c = (r == null ? void 0 : r.retryResolve) ?? s.get("retryResolve"), i = s.get("logHandler") || I;
|
|
521
519
|
i({
|
|
522
520
|
type: "prepear",
|
|
523
521
|
url: t,
|
|
@@ -540,7 +538,7 @@ async function pe(e, t, s, r, a) {
|
|
|
540
538
|
headers: u.headers,
|
|
541
539
|
cost: d
|
|
542
540
|
});
|
|
543
|
-
const m =
|
|
541
|
+
const m = A(l);
|
|
544
542
|
if (!o || c === "network" && l > 0 || c === "status" && m || Array.isArray(c) && !c.includes(l) || typeof c == "function" && c(u, n) !== !0 || n >= o)
|
|
545
543
|
return u;
|
|
546
544
|
const g = (r == null ? void 0 : r.retryInterval) ?? s.get("retryInterval") ?? 1e3;
|
|
@@ -551,18 +549,18 @@ async function pe(e, t, s, r, a) {
|
|
|
551
549
|
)
|
|
552
550
|
), await pe(e, t, s, r, n + 1);
|
|
553
551
|
}
|
|
554
|
-
|
|
552
|
+
var be = /* @__PURE__ */ ((e) => (e.Aborted = "Aborted", e.Unknown = "Unknown", e.Timeout = "Timeout", e.NotSupport = "NotSupport", e.URLFormatError = "URLFormatError", e))(be || {});
|
|
555
553
|
export {
|
|
556
|
-
|
|
557
|
-
F as K,
|
|
554
|
+
$e as B,
|
|
558
555
|
Re as N,
|
|
559
556
|
re as R,
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
557
|
+
O as S,
|
|
558
|
+
T as X,
|
|
559
|
+
be as a,
|
|
560
|
+
Oe as c,
|
|
561
|
+
N as f,
|
|
562
|
+
ke as g,
|
|
563
|
+
Ee as h,
|
|
564
|
+
je as j,
|
|
565
|
+
pe as r
|
|
568
566
|
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var K=Object.defineProperty;var G=(e,t,s)=>t in e?K(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s;var p=(e,t,s)=>G(e,typeof t!="symbol"?t+"":t,s);const y=typeof globalThis<"u"?globalThis:typeof global<"u"?global:typeof window<"u"?window:{},w={fetch:"fetch"in y,window:"window"in y,FormData:"FormData"in y,Blob:"Blob"in y,wx:"wx"in y,TextDecoder:"TextDecoder"in y,AbortController:"AbortController"in y};function S(e){return e.reduce((t,[s,r])=>(s&&(t[s]=r||""),t),{})}function J(e){return new Promise(t=>setTimeout(t,Math.max(0,e)))}function V(){}function W(e){const t=e||"CustomError";return class extends Error{constructor(s){super(s),Object.defineProperty(this,"name",{value:t,enumerable:!1,configurable:!0}),"setPrototypeOf"in Object&&Object.setPrototypeOf(this,new.target.prototype),"captureStackTrace"in Error&&typeof Error.captureStackTrace=="function"&&Error.captureStackTrace(this,this.constructor)}}}const X=/^(?:https?:)?\/\/.+$/i,z=/^https?:\/\/.+$/i,Z=/^\{[\d\D]*\}$/,Q=/^\[[\d\D]*\]$/;function O(e,t=!1){return t?X.test(e):z.test(e)}function Y(e){return e==null}function k(e,...t){if(!e||typeof e!="object")return!1;const s=Object.getPrototypeOf(e);return s!==Object.prototype&&s!==null?!1:t.every(r=>r in e)}function P(e){return Z.test(e)||Q.test(e)}function R(e){return e?e[0].toLowerCase()+e.slice(1):""}const _=/_\w*/,x=/-\w*/;function A(e){const t=_.test(e)?e.replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(s,r)=>r.toUpperCase()):x.test(e)?e.replace(/(?:^-*|-*$)/g,"").replace(/-+(\w)/g,(s,r)=>r.toUpperCase()):e;return R(t)}function U(e){return _.test(e)?R(e).replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(t,s)=>"_"+s.toLowerCase()):x.test(e)?R(e.replace(/(?:^-*|-*$)/g,"")).replace(/-+(\w)/g,(t,s)=>"_"+s.toLowerCase()):R(e).replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`)}function v(e,t){try{const s=JSON.parse(e);return t?t(s)?s:null:s}catch{return null}}""+Math.random().toString(32).slice(2);function $(e,t=!1){return typeof e=="string"?A(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?$(s):typeof s=="object"&&s?$(s,!0):s):k(e)?Object.keys(e).reduce((s,r)=>{const a=A(String(r)),n=a.charAt(0).toLowerCase()+a.slice(1);return s[n]=t?e[r]:$(e[r]),s},{}):e}function E(e,t=!1){return typeof e=="string"?U(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?E(s):typeof s=="object"&&s?E(s,t):s):k(e)?Object.keys(e).reduce((s,r)=>{const a=U(String(r));return s[a]=t?e[r]:E(e[r]),s},{}):e}async function ee(e){return await new Promise(function(t){const s=document.getElementsByTagName("head")[0],r=document.createElement("script");r.setAttribute("type","text/javascript"),r.setAttribute("charset","utf-8"),r.onload=function(){s.removeChild(r),t(!0)},r.onerror=function(){s.removeChild(r),t(!1)},r.setAttribute("src",e),s.appendChild(r)})}function te(e,t="数据未能正确识别"){return typeof e=="function"?{guard:e,message:t}:{guard:e.guard,message:e.message||t}}function se(e,t=""){return!t||O(e,!0)?L(e):(L(t)+"/"+e).replace(/\/{2,}/g,"/").replace(/:\//,"://")}function L(e){return O(e,!0)?e.startsWith("http")?e:("location"in globalThis?location.protocol:"https:")+e:("location"in globalThis?location.origin:"http://127.0.0.1")+"/"+e.replace(/^\/+/,"")}function re(e,t){const s={};return(e.match(/([^=&#?]+)=[^&#]*/g)||[]).forEach(function(r){const a=r.split("="),n=a[0],o=decodeURIComponent(a[1]||"");s[n]!==void 0?s[n]+=","+o:s[n]=o}),t!==!0?s[t]||"":s}function ne(e,t){if(t){if(t===!0)return e.replace(/\?[^#]*/,"")}else return e;const s=e.split("#"),r=s[0].split("?"),a=r[0],n=r.length>1?r[1]:"",o=s.length>1?"#"+s[1]:"",c=typeof t=="string"?[t]:Array.isArray(t)?t:[];return!c.length||!n?s[0]+o:(c.map(i=>i.replace(/([\\(){}[\]^$+\-*?|])/g,"\\$1")),(a+"?"+n.replace(new RegExp("(?:^|&)(?:"+c.join("|")+")=[^&$]+","g"),"").replace(/^&/,"")).replace(/\?$/,"")+o)}function ae(e,t,s=!1){const r=typeof t=="string"?t:Object.keys(t).map(o=>`${o}=${encodeURIComponent(t[o])}`).join("&");if(!r)return e;const a=e.split("#");s&&(a[0]=ne(a[0],(r.match(/([^=&#?]+)=[^&#]+/g)||[]).map(o=>o.replace(/=.+$/,""))));const n=a[0].indexOf("?")+1?"&":"?";return(a[0]+n+r+(a.length>1?"#"+a[1]:"")).replace(/\?&/,"?")}function oe(e){const t=e.match(/(?:\?|&)([^=]+)(?:&|$)/g);return t?t.join("").replace(/(?:\?|^&+|&+$)/g,"").replace(/&{2}/g,"&").split("&").sort():[]}class ce{constructor(t=500){p(this,"ttl");p(this,"cache");this.cache={},this.ttl=Math.max(t,0)}getKey(t,s){const r=t.replace(/#.+/,""),a=r.replace(/\?.+/g,""),n=Object.assign(re(r,!0),s),o=oe(r),c=Object.keys(n).sort().map(i=>`${i}#${n[i]}`);return`${a}_${c.join(",")}_${o.join(",")}`}updateTTL(t){this.ttl=Math.max(t,0)}get(t){if(this.ttl===0)return null;const s=this.cache[t];return s?s.ttl<Date.now()?(delete this.cache[t],null):s.res:null}set(t,s){this.ttl!==0&&(this.cache[t]={ttl:Date.now()+this.ttl,res:s})}}class q{constructor(t){p(this,"config",{baseURL:"",maxRetry:0,retryInterval:1e3,retryResolve:"network",timeout:1e4,cacheTTL:500,credentials:"same-origin",responseRule:{ok:{resolve:"body"},failed:{resolve:"json",messageField:"message"}}});t&&this.set(t)}set(t){if(t.baseURL&&!/^\/.+/.test(t.baseURL)&&!O(t.baseURL))throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"),new Error("BaseURLError");Object.assign(this.config,t)}get(t){return this.config[t]}getFullUrl(t){return se(t,this.config.baseURL)}showMessage(t,s,r,a){this.config.messageHandler&&s&&this.config.messageHandler(t,s,r,a)}}const D="UnexpectResponse";function ie(e,t,s,r){if(t.ok&&!Y(t.data)&&r){const a=te(r,"响应数据未能正确识别");return a.guard(t.data)||(s.showMessage(!0,`${e} ${a.message}`,t.code,t.status),console.error(D,e,t.data),t.data=null,t.code=D,t.message=a.message),t}return t}class ue{constructor(t,s){p(this,"agent");p(this,"config");p(this,"cache");this.config=new q(s),this.agent=t,this.cache=new ce(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)}async request(t,s){try{return await this.agent(t,this.config,s)}catch(r){return console.error("RequestError",r),{ok:!1,status:-9,code:"Unkonwn",message:String(r),headers:{},data:null}}}async guard(t,s,r){return ie(t,s,this.config,r)}setConfig(t){this.config.set(t),this.cache.updateTTL(this.config.get("cacheTTL"))}getConfig(t){return this.config.get(t)}async head(t,s){const r=Object.assign({},s||null);return r.method="HEAD",this.guard(t,await this.request(t,r),null)}async get(t,s,r){const a=Object.assign({},r||null);a.method="GET";const n=this.cache.getKey(t,a.params),o=this.cache.get(n);if(o)return this.guard(t,await o,s||null);const c=this.request(t,a);return this.cache.set(n,c),this.guard(t,await c,s||null)}async post(t,s,r,a){const n=Object.assign({},a||null);return n.method="POST",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}async del(t,s,r){const a=Object.assign({},r||null);return a.method="DELETE",this.guard(t,await this.request(t,a),s||null)}async put(t,s,r,a){const n=Object.assign({},a||null);return n.method="PUT",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}async patch(t,s,r,a){const n=Object.assign({},a||null);return n.method="PATCH",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}}async function le(e,t,s){var g;const r=Object.assign({method:"GET"},s),a=w.FormData?r.body instanceof FormData:!1,n=a&&r.method!=="POST"&&r.method!=="PUT"?"POST":r.method,o=n==="GET"||n==="HEAD"||n==="DELETE";o&&r.body!==void 0&&(console.warn("request body is invalid with method get, head, delete"),delete r.body);const c=Object.assign(a||o?{}:{"Content-Type":w.Blob&&r.body instanceof Blob?r.body.type||"application/octet-stream":"application/json;charset=utf-8"},r.headers),i=r.params||{},h={};Object.keys(i).forEach(b=>{i[b]!==void 0&&(h[b]=he(i[b]))});const u=t.getFullUrl(e),l=de(r.body),d=r.timeout||t.get("timeout"),f=await async function(){const b=t.get("requestTransformer");if(b)return await b({headers:c,params:h,method:n,url:u,body:l})}(),m=typeof f=="string"&&f?f:u;return(g=t.get("logHandler"))==null||g({type:"ready",url:m,method:n,headers:c,timeout:d,body:l}),{url:m,method:n,body:l,params:h,headers:c,timeout:d,abort:r.abort,credentials:r.credentials||t.get("credentials")}}function he(e){return typeof e=="string"?e:Array.isArray(e)?e.join(","):e+""}function de(e){if(e)return typeof e=="string"||e instanceof URLSearchParams||e instanceof ArrayBuffer||w.Blob&&e instanceof Blob||w.FormData&&e instanceof FormData?e:JSON.stringify(e)}const H="data",j="message";function fe(e,t,s,r,a){const n=a||r;return T(e)?je(n.ok||r.ok,e,t,s):me(n.failed||r.failed,t,s)}const ge=function(e){const t=[],s=e.failed||{resolve:"json"};switch(t.push("- 当http状态码 <200 或者 >=400 时"),s.resolve){case"body":t.push(" 将响应内容格式化为字符串并作为错误消息");break;case"json":t.push(" 将响应解析为json,并读取 "+(s.messageField||j)+" 作为错误消息");break}const r=e.ok||{resolve:"body"};switch(t.push("- 当http状态码 >=200 并且 <400 时"),r.resolve){case"body":t.push(" 将响应尝试解析为 json,并作为数据内容返回");break;case"json":t.push(" 将响应解析为 json,读取 "+(r.dataField||H)+" 作为响应数据,读取 "+(r.messageField||j)+" 作为提示消息"),r.statusField&&t.push(" 当 "+r.statusField+" 为 "+(r.statusOKValue||"空值")+" 时是成功提示,否则是错误消息"),r.ignoreMessage&&t.push(" 并忽略以下消息:"+r.ignoreMessage);break}return t.join(`
|
|
2
|
+
`)};function me(e,t,s){const r=e||{resolve:"json",messageField:j},a={ok:!1,code:t,message:s,data:null};switch(r.resolve){case"body":a.message=M(s)||s;break;case"json":const{code:n,message:o}=pe(s,r.converter,r.statusField,r.messageField);a.code=n||t,a.message=M(s)||o;break}return a}function pe(e,t,s,r=j){if(!P(e))return{message:""};const a=C(v(e),t);return!a||!k(a)?{message:e}:{code:s?F(a,s):"",message:F(a,r)||e}}function F(e,t){const s=Array.isArray(t)?t:[t];for(const r of s)if(r in e)return ye(e[r]);return""}function ye(e){return e?typeof e=="string"?e:JSON.stringify(e):""}const be=/<title>([^<]+)<\/title>/i,we=/<message>([^<]+)<\/message>/i;function M(e){const t=e.match(be);if(t)return t[1];const s=e.match(we);return s?s[1]:""}function je(e,t,s,r){const a=e||{resolve:"body"},n={ok:!0,code:s,message:"",data:null};if(t===204||!r)return n;if(a.resolve==="body")return n.data=P(r)?C(v(r),e.converter):r,n;const o=C(v(r),e.converter);if(!o||!k(o))return n.ok=!1,n.code="ResponseFormatError",n.message="响应内容无法格式化为 Object",n;const c=a.statusField,i=a.statusOKValue||"",h=a.dataField||H,u=a.messageField||j,l=a.ignoreMessage||"";if(c&&!(c in o))return n.ok=!1,n.code="ResponseFieldMissing",n.message="响应内容找不到状态字段 "+c,n;const d=c?o[c]+"":"";return n.ok=c?d===i:!0,n.code=d||s,n.data=h===!0?o:h in o?o[h]:null,n.message=F(o,u),l&&n.message&&(Array.isArray(l)&&l.includes(n.message)||typeof l=="string"&&n.message===l)&&(n.message=""),n}function T(e){return e>=200&&e<400}function C(e,t){return t==="camelize"?$(e):t==="snakify"?E(e):e}const Re=W("APIError");function $e(e){const t={};for(const a in e.headers)(a.startsWith("x-")||a.includes("trace")||a.includes("server")||/\b(?:id|uuid)\b/.test(a))&&(t[a]=e.headers[a]);const s=e.url.replace(/^(?:https?:)?\/*/i,"").replace(/\?.+/,""),r=e.status<0?"unknown":e.status;return{sentryError:new Re(`${s} | ${r}${e.code?` | ${e.code}`:""}`),sentryTags:{...t,status:r,method:e.method,code:e.code||"unknown",message:e.message||"empty"},sentryExtra:{url:e.url,responseBody:e.body||"empty",responseHeaders:e.headers,rawError:e.error}}}function Ee(e,t,s,r){var f;const a=e.status,n=e.method,o=S(Object.entries(e.headers||{}).map(([m,g])=>[m.toLowerCase(),g])),{ok:c,code:i,data:h,message:u}=fe(a,e.statusText,e.body,s.get("responseRule"),r==null?void 0:r.responseRule);if(!T(a)){const m=$e({url:e.url,method:e.method,status:a,code:i,message:u,body:e.body,headers:o,error:e.rawError});(s.get("errorHandler")||Oe)({url:t,method:n,status:a,code:i,message:u,headers:o,rawError:e.rawError,responseBody:e.body,...m})}if(a<0)return B({ok:!1,status:a,code:e.statusText,headers:{},message:"",data:null},`${n} ${t} ${e.statusText}`,n,t,s,r);const l={ok:c,data:h,code:i,message:u,status:a,headers:o};(f=s.get("responseHandler"))==null||f({...l},n,t);const d=c?u:u||e.statusText;return B(l,d,n,t,s,r)}function B(e,t,s,r,a,n){const o=a.get("message"),c=o===!1||(n==null?void 0:n.message)===!1?!1:(n==null?void 0:n.message)||o;if(c!==!1){const i=typeof c=="function"?c(e,s,r,t):t;i instanceof Error?a.showMessage(!0,i.message,e.code,e.status):i&&typeof i=="object"&&"message"in i?a.showMessage(!1,i.message,e.code,e.status):a.showMessage(!e.ok,i,e.code,e.status)}return e}function Oe(e){const t={};for(const s in e)s.startsWith("sentry")||(t[s]=e[s]);console.error("RequestError",t)}async function N(e,t,s,r,a){const n=a||0,o=Math.max(0,Math.min(10,(r==null?void 0:r.maxRetry)??s.get("maxRetry")??0)),c=(r==null?void 0:r.retryResolve)??s.get("retryResolve"),i=s.get("logHandler")||V;i({type:"prepear",url:t,method:(r==null?void 0:r.method)||"GET",retry:n,maxRetry:o,message:n===0?"start":`retry ${n}/${o} start`,headers:r==null?void 0:r.headers,options:r});const h=Date.now(),u=await e(t,s,r),l=u.status,d=Date.now()-h,f=`[cost ${d}][${l}] ${l<0?u.body:""}`;i({type:"finished",url:t,method:u.method,retry:n,maxRetry:o,message:n===0?`finish ${f}`:`retry ${n}/${o} finish ${f}`,response:u,headers:u.headers,cost:d});const m=T(l);if(!o||c==="network"&&l>0||c==="status"&&m||Array.isArray(c)&&!c.includes(l)||typeof c=="function"&&c(u,n)!==!0||n>=o)return u;const g=(r==null?void 0:r.retryInterval)??s.get("retryInterval")??1e3;return await J(Math.max(100,g==="2EB"?Math.pow(2,n)*1e3:typeof g=="function"?g(n+1)||0:g)),await N(e,t,s,r,n+1)}var I=(e=>(e.Aborted="Aborted",e.Unknown="Unknown",e.Timeout="Timeout",e.NotSupport="NotSupport",e.URLFormatError="URLFormatError",e))(I||{});exports.Bt=ae;exports.NetRequestHandler=ue;exports.RequestGlobalConfig=q;exports.RequestInteralError=I;exports.Support=w;exports.X=O;exports.convertOptions=le;exports.fromEntries=S;exports.getResponseRulesDescription=ge;exports.handleResponse=Ee;exports.je=ee;exports.retryRequest=N;
|
package/dist/wx.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./type-CzbYPhI4.cjs"),l=async function(t,r,n){return e.handleResponse(await e.retryRequest(p,t,r,n),t,r,n)},p=async function(t,r,n){const s=await e.convertOptions(t,r,n),u=s.method==="PATCH"?"POST":s.method,a=e.Bt(s.url,s.params);return e.Support.wx?new Promise(d=>{wx.request({url:a,data:s.body,header:s.headers,method:u,dataType:"string",responseType:"text",fail(){d({url:a,method:u,status:-1,statusText:e.RequestInteralError.Unknown,body:""})},success(c){d({url:a,method:u,status:c.statusCode,statusText:c.statusCode+"",headers:{...c.header},body:s.method==="HEAD"?"":f(c.data)})}})}):{url:a,method:u,status:-1,statusText:e.RequestInteralError.NotSupport,body:""}};function f(t){return typeof t=="string"?t:t instanceof ArrayBuffer&&e.Support.TextDecoder?new TextDecoder().decode(t):JSON.stringify(t)}function i(t){return new e.NetRequestHandler(l,t)}const o=i(),y=o.setConfig,g=o.head,h=o.get,q=o.post,x=o.del,R=o.put;exports.NetRequest=i;exports.del=x;exports.get=h;exports.head=g;exports.post=q;exports.put=R;exports.setGlobalConfig=y;
|
package/dist/wx.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
import { h as
|
|
2
|
-
import { v as D } from "./version-DNvZe-sl.js";
|
|
1
|
+
import { h as p, r as f, c as l, B as h, S as i, a as d, N as y } from "./type-4NUOpogE.js";
|
|
3
2
|
const x = async function(t, o, n) {
|
|
4
|
-
return
|
|
5
|
-
},
|
|
6
|
-
const e = await
|
|
7
|
-
return
|
|
3
|
+
return p(await f(m, t, o, n), t, o, n);
|
|
4
|
+
}, m = async function(t, o, n) {
|
|
5
|
+
const e = await l(t, o, n), r = e.method === "PATCH" ? "POST" : e.method, a = h(e.url, e.params);
|
|
6
|
+
return i.wx ? new Promise((c) => {
|
|
8
7
|
wx.request({
|
|
9
|
-
url:
|
|
8
|
+
url: a,
|
|
10
9
|
data: e.body,
|
|
11
10
|
header: e.headers,
|
|
12
11
|
method: r,
|
|
@@ -14,46 +13,45 @@ const x = async function(t, o, n) {
|
|
|
14
13
|
responseType: "text",
|
|
15
14
|
fail() {
|
|
16
15
|
c({
|
|
17
|
-
url:
|
|
16
|
+
url: a,
|
|
18
17
|
method: r,
|
|
19
18
|
status: -1,
|
|
20
|
-
statusText:
|
|
19
|
+
statusText: d.Unknown,
|
|
21
20
|
body: ""
|
|
22
21
|
});
|
|
23
22
|
},
|
|
24
|
-
success(
|
|
23
|
+
success(u) {
|
|
25
24
|
c({
|
|
26
|
-
url:
|
|
25
|
+
url: a,
|
|
27
26
|
method: r,
|
|
28
|
-
status:
|
|
29
|
-
statusText:
|
|
30
|
-
headers: { ...
|
|
31
|
-
body: e.method === "HEAD" ? "" :
|
|
27
|
+
status: u.statusCode,
|
|
28
|
+
statusText: u.statusCode + "",
|
|
29
|
+
headers: { ...u.header },
|
|
30
|
+
body: e.method === "HEAD" ? "" : w(u.data)
|
|
32
31
|
});
|
|
33
32
|
}
|
|
34
33
|
});
|
|
35
34
|
}) : {
|
|
36
|
-
url:
|
|
35
|
+
url: a,
|
|
37
36
|
method: r,
|
|
38
37
|
status: -1,
|
|
39
|
-
statusText:
|
|
38
|
+
statusText: d.NotSupport,
|
|
40
39
|
body: ""
|
|
41
40
|
};
|
|
42
41
|
};
|
|
43
|
-
function m(t) {
|
|
44
|
-
return typeof t == "string" ? t : t instanceof ArrayBuffer && d.TextDecoder ? new TextDecoder().decode(t) : JSON.stringify(t);
|
|
45
|
-
}
|
|
46
42
|
function w(t) {
|
|
47
|
-
return new
|
|
43
|
+
return typeof t == "string" ? t : t instanceof ArrayBuffer && i.TextDecoder ? new TextDecoder().decode(t) : JSON.stringify(t);
|
|
44
|
+
}
|
|
45
|
+
function T(t) {
|
|
46
|
+
return new y(x, t);
|
|
48
47
|
}
|
|
49
|
-
const s =
|
|
48
|
+
const s = T(), q = s.setConfig, R = s.head, b = s.get, S = s.post, C = s.del, N = s.put;
|
|
50
49
|
export {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
D as version
|
|
50
|
+
T as NetRequest,
|
|
51
|
+
C as del,
|
|
52
|
+
b as get,
|
|
53
|
+
R as head,
|
|
54
|
+
S as post,
|
|
55
|
+
N as put,
|
|
56
|
+
q as setGlobalConfig
|
|
59
57
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seayoo-web/request",
|
|
3
|
-
"version": "2.0.
|
|
3
|
+
"version": "2.0.9",
|
|
4
4
|
"description": "requst tools for seayoo web",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -58,16 +58,11 @@
|
|
|
58
58
|
"@vitest/coverage-istanbul": "^3.0.5",
|
|
59
59
|
"happy-dom": "^12.10.3",
|
|
60
60
|
"msw": "^2.7.0",
|
|
61
|
-
"
|
|
62
|
-
"
|
|
63
|
-
},
|
|
64
|
-
"dependencies": {
|
|
65
|
-
"@seayoo-web/utils": "^1.20.0"
|
|
61
|
+
"vitest": "^3.0.5",
|
|
62
|
+
"@seayoo-web/utils": "^2.1.0"
|
|
66
63
|
},
|
|
67
64
|
"scripts": {
|
|
68
|
-
"build": "
|
|
69
|
-
"emit-types": "tsc --emitDeclarationOnly",
|
|
70
|
-
"clear": "rimraf dist && rimraf node_modules",
|
|
65
|
+
"build": "vite build && tsc --emitDeclarationOnly",
|
|
71
66
|
"type-check": "tsc --noEmit",
|
|
72
67
|
"lint": "eslint ./src/**/*.ts",
|
|
73
68
|
"lint:fix": "eslint ./src/**/*.{ts,js} --fix",
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import { type NetRequestAgent, type IRequestGlobalConfig, type IRequestOptions, type IResponseResult, type IRetryRequestOptions, type IBaseRequestOptions } from "./type";
|
|
2
2
|
/** 进度回调函数配置 */
|
|
3
3
|
interface XHRequestOptions extends IBaseRequestOptions {
|
|
4
4
|
onUploadProgress?: (progress: {
|
package/types/inc/type.d.ts
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import type { RequestGlobalConfig } from "./config";
|
|
2
2
|
import type { MaybePromise, TypeGuard, TypeGuardFn } from "@seayoo-web/utils";
|
|
3
3
|
export type IBaseRequestBody = Blob | ArrayBuffer | FormData | URLSearchParams | string;
|
|
4
|
+
export declare const enum RequestInteralError {
|
|
5
|
+
/** 请求被取消 */
|
|
6
|
+
Aborted = "Aborted",
|
|
7
|
+
/** 未知错误,通常是网络错误 */
|
|
8
|
+
Unknown = "Unknown",
|
|
9
|
+
/** 超时 */
|
|
10
|
+
Timeout = "Timeout",
|
|
11
|
+
/** 请求方法不被支持,在微信小程序环境下有效 */
|
|
12
|
+
NotSupport = "NotSupport",
|
|
13
|
+
/** url 格式错误 */
|
|
14
|
+
URLFormatError = "URLFormatError"
|
|
15
|
+
}
|
|
4
16
|
/** 通用网络请求工具的基本配置 */
|
|
5
17
|
export interface IBaseRequestOptions {
|
|
6
18
|
/** 网络请求方法,默认 GET */
|
package/types/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { NetRequestHandler } from "./inc/main";
|
|
2
2
|
import { xhrUpload } from "./inc/request.xhr";
|
|
3
3
|
import type { IRequestGlobalConfig } from "./inc/type";
|
|
4
|
-
export * from "./version";
|
|
5
4
|
export { jsonp, jsonx } from "./inc/jsonp";
|
|
6
5
|
export { getResponseRulesDescription } from "./inc/rule";
|
|
6
|
+
export { RequestInteralError } from "./inc/type";
|
|
7
7
|
export type { IRequestOptions, IRequestGlobalConfig, IResponseRule, IResponseResult, IRequestLog } from "./inc/type";
|
|
8
8
|
/**
|
|
9
9
|
* 基于 xhr 模块的上传工具,支持上传进度
|
package/types/node.d.ts
CHANGED
package/types/wx.d.ts
CHANGED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";const r=require("./version-DgNkLRed.cjs"),f=async function(a,n,i){return r.handleResponse(await r.retryRequest(w,a,n,i),a,n,i)},w=async function(a,n,i){const t=await r.convertOptions(a,n,i),o=new URL(t.url),u=t.params;u instanceof Object&&Object.keys(u).forEach(e=>o.searchParams.set(e,u[e]));const s=r.Support.AbortController?new AbortController:null;function c(){s&&!s.signal.aborted&&s.abort()}t.abort&&t.abort.addEventListener("abort",c);const h=t.timeout>0?setTimeout(c,t.timeout):null,l=t.method==="HEAD";return await fetch(o,{method:t.method,headers:Object.keys(t.headers).length>0?new Headers(t.headers):void 0,body:t.body,credentials:t.credentials,signal:s==null?void 0:s.signal,redirect:"follow"}).then(async e=>{const d=l?"":await e.text().catch(m=>m),b=r.fromEntries([...e.headers.entries()]);return d instanceof Error?{url:o.toString(),method:t.method,status:e.status,statusText:e.statusText||"Unknown",body:"",headers:b,rawError:d}:{url:o.toString(),method:t.method,status:e.status,statusText:e.statusText,headers:b,body:d}}).catch(e=>({url:o.toString(),method:t.method,status:-1,statusText:s!=null&&s.signal.aborted?"Aborted":"Unknown",body:"",rawError:e})).finally(()=>{h!==null&&clearTimeout(h),t.abort&&t.abort.removeEventListener("abort",c)})};exports.fetchRequest=f;
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import { h as l, r as f, c as w, S as y, f as p } from "./version-DNvZe-sl.js";
|
|
2
|
-
const x = async function(o, r, n) {
|
|
3
|
-
return l(await f(E, o, r, n), o, r, n);
|
|
4
|
-
}, E = async function(o, r, n) {
|
|
5
|
-
const t = await w(o, r, n), s = new URL(t.url), i = t.params;
|
|
6
|
-
i instanceof Object && Object.keys(i).forEach((e) => s.searchParams.set(e, i[e]));
|
|
7
|
-
const a = y.AbortController ? new AbortController() : null;
|
|
8
|
-
function c() {
|
|
9
|
-
a && !a.signal.aborted && a.abort();
|
|
10
|
-
}
|
|
11
|
-
t.abort && t.abort.addEventListener("abort", c);
|
|
12
|
-
const d = t.timeout > 0 ? setTimeout(c, t.timeout) : null, b = t.method === "HEAD";
|
|
13
|
-
return await fetch(s, {
|
|
14
|
-
method: t.method,
|
|
15
|
-
headers: Object.keys(t.headers).length > 0 ? new Headers(t.headers) : void 0,
|
|
16
|
-
body: t.body,
|
|
17
|
-
credentials: t.credentials,
|
|
18
|
-
signal: a == null ? void 0 : a.signal,
|
|
19
|
-
redirect: "follow"
|
|
20
|
-
}).then(async (e) => {
|
|
21
|
-
const u = b ? "" : await e.text().catch((m) => m), h = p([...e.headers.entries()]);
|
|
22
|
-
return u instanceof Error ? {
|
|
23
|
-
url: s.toString(),
|
|
24
|
-
method: t.method,
|
|
25
|
-
status: e.status,
|
|
26
|
-
statusText: e.statusText || "Unknown",
|
|
27
|
-
body: "",
|
|
28
|
-
headers: h,
|
|
29
|
-
rawError: u
|
|
30
|
-
} : {
|
|
31
|
-
url: s.toString(),
|
|
32
|
-
method: t.method,
|
|
33
|
-
status: e.status,
|
|
34
|
-
statusText: e.statusText,
|
|
35
|
-
headers: h,
|
|
36
|
-
body: u
|
|
37
|
-
};
|
|
38
|
-
}).catch((e) => ({
|
|
39
|
-
url: s.toString(),
|
|
40
|
-
method: t.method,
|
|
41
|
-
status: -1,
|
|
42
|
-
statusText: a != null && a.signal.aborted ? "Aborted" : "Unknown",
|
|
43
|
-
body: "",
|
|
44
|
-
rawError: e
|
|
45
|
-
})).finally(() => {
|
|
46
|
-
d !== null && clearTimeout(d), t.abort && t.abort.removeEventListener("abort", c);
|
|
47
|
-
});
|
|
48
|
-
};
|
|
49
|
-
export {
|
|
50
|
-
x as f
|
|
51
|
-
};
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";var K=Object.defineProperty;var N=(e,t,s)=>t in e?K(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s;var y=(e,t,s)=>N(e,typeof t!="symbol"?t+"":t,s);const p=typeof globalThis<"u"?globalThis:typeof global<"u"?global:typeof window<"u"?window:{},w={fetch:"fetch"in p,window:"window"in p,FormData:"FormData"in p,Blob:"Blob"in p,wx:"wx"in p,TextDecoder:"TextDecoder"in p,AbortController:"AbortController"in p};function P(e){return e.reduce((t,[s,r])=>(s&&(t[s]=r||""),t),{})}function W(e){return new Promise(t=>setTimeout(t,Math.max(0,e)))}function G(){}function J(e){const t=e||"CustomError";return class extends Error{constructor(s){super(s),Object.defineProperty(this,"name",{value:t,enumerable:!1,configurable:!0}),"setPrototypeOf"in Object&&Object.setPrototypeOf(this,new.target.prototype),"captureStackTrace"in Error&&Error.captureStackTrace(this,this.constructor)}}}function j(e){return e?e[0].toLowerCase()+e.slice(1):""}const _=/_\w*/,x=/-\w*/;function A(e){const t=_.test(e)?e.replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(s,r)=>r.toUpperCase()):x.test(e)?e.replace(/(?:^-*|-*$)/g,"").replace(/-+(\w)/g,(s,r)=>r.toUpperCase()):e;return j(t)}function D(e){return _.test(e)?j(e).replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(t,s)=>"_"+s.toLowerCase()):x.test(e)?j(e.replace(/(?:^-*|-*$)/g,"")).replace(/-+(\w)/g,(t,s)=>"_"+s.toLowerCase()):j(e).replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`)}const V=/^(?:https?:)?\/\/.+$/i,X=/^https?:\/\/.+$/i,Z=/^\{[\d\D]*\}$/,z=/^\[[\d\D]*\]$/;function O(e,t=!1){return t?V.test(e):X.test(e)}function Q(e){return e==null}function v(e,...t){if(!e||typeof e!="object")return!1;const s=Object.getPrototypeOf(e);return s!==Object.prototype&&s!==null?!1:t.every(r=>r in e)}function B(e){return Z.test(e)||z.test(e)}function k(e,t){try{const s=JSON.parse(e);return t?t(s)?s:null:s}catch{return null}}""+Math.random().toString(32).slice(2);function E(e,t=!1){return typeof e=="string"?A(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?E(s):typeof s=="object"&&s?E(s,!0):s):v(e)?Object.keys(e).reduce((s,r)=>{const a=A(String(r)),n=a.charAt(0).toLowerCase()+a.slice(1);return s[n]=t?e[r]:E(e[r]),s},{}):e}function $(e,t=!1){return typeof e=="string"?D(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?$(s):typeof s=="object"&&s?$(s,t):s):v(e)?Object.keys(e).reduce((s,r)=>{const a=D(String(r));return s[a]=t?e[r]:$(e[r]),s},{}):e}async function Y(e){return await new Promise(function(t){const s=document.getElementsByTagName("head")[0],r=document.createElement("script");r.setAttribute("type","text/javascript"),r.setAttribute("charset","utf-8"),r.onload=function(){s.removeChild(r),t(!0)},r.onerror=function(){s.removeChild(r),t(!1)},r.setAttribute("src",e),s.appendChild(r)})}function ee(e,t="数据未能正确识别"){return typeof e=="function"?{guard:e,message:t}:{guard:e.guard,message:e.message||t}}function te(e,t=""){return!t||O(e,!0)?M(e):(M(t)+"/"+e).replace(/\/{2,}/g,"/").replace(/:\//,"://")}function M(e){return O(e,!0)?e.startsWith("http")?e:("location"in globalThis?location.protocol:"https:")+e:("location"in globalThis?location.origin:"http://127.0.0.1")+"/"+e.replace(/^\/+/,"")}function se(e,t){const s={};return(e.match(/([^=&#?]+)=[^&#]*/g)||[]).forEach(function(r){const a=r.split("="),n=a[0],o=decodeURIComponent(a[1]||"");s[n]!==void 0?s[n]+=","+o:s[n]=o}),t!==!0?s[t]||"":s}function re(e,t){if(t){if(t===!0)return e.replace(/\?[^#]*/,"")}else return e;const s=e.split("#"),r=s[0].split("?"),a=r[0],n=r.length>1?r[1]:"",o=s.length>1?"#"+s[1]:"",c=typeof t=="string"?[t]:Array.isArray(t)?t:[];return!c.length||!n?s[0]+o:(c.map(i=>i.replace(/([\\(){}[\]^$+\-*?|])/g,"\\$1")),(a+"?"+n.replace(new RegExp("(?:^|&)(?:"+c.join("|")+")=[^&$]+","g"),"").replace(/^&/,"")).replace(/\?$/,"")+o)}function ne(e,t,s=!1){const r=typeof t=="string"?t:Object.keys(t).map(o=>`${o}=${encodeURIComponent(t[o])}`).join("&");if(!r)return e;const a=e.split("#");s&&(a[0]=re(a[0],(r.match(/([^=&#?]+)=[^&#]+/g)||[]).map(o=>o.replace(/=.+$/,""))));const n=a[0].indexOf("?")+1?"&":"?";return(a[0]+n+r+(a.length>1?"#"+a[1]:"")).replace(/\?&/,"?")}function ae(e){const t=e.match(/(?:\?|&)([^=]+)(?:&|$)/g);return t?t.join("").replace(/(?:\?|^&+|&+$)/g,"").replace(/&{2}/g,"&").split("&").sort():[]}class oe{constructor(t=500){y(this,"ttl");y(this,"cache");this.cache={},this.ttl=Math.max(t,0)}getKey(t,s){const r=t.replace(/#.+/,""),a=r.replace(/\?.+/g,""),n=Object.assign(se(r,!0),s),o=ae(r),c=Object.keys(n).sort().map(i=>`${i}#${n[i]}`);return`${a}_${c.join(",")}_${o.join(",")}`}updateTTL(t){this.ttl=Math.max(t,0)}get(t){if(this.ttl===0)return null;const s=this.cache[t];return s?s.ttl<Date.now()?(delete this.cache[t],null):s.res:null}set(t,s){this.ttl!==0&&(this.cache[t]={ttl:Date.now()+this.ttl,res:s})}}class S{constructor(t){y(this,"config",{baseURL:"",maxRetry:0,retryInterval:1e3,retryResolve:"network",timeout:1e4,cacheTTL:500,credentials:"same-origin",responseRule:{ok:{resolve:"body"},failed:{resolve:"json",messageField:"message"}}});t&&this.set(t)}set(t){if(t.baseURL&&!/^\/.+/.test(t.baseURL)&&!O(t.baseURL))throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"),new Error("BaseURLError");Object.assign(this.config,t)}get(t){return this.config[t]}getFullUrl(t){return te(t,this.config.baseURL)}showMessage(t,s,r,a){this.config.messageHandler&&s&&this.config.messageHandler(t,s,r,a)}}const U="UnexpectResponse";function ce(e,t,s,r){if(t.ok&&!Q(t.data)&&r){const a=ee(r,"响应数据未能正确识别");return a.guard(t.data)||(s.showMessage(!0,`${e} ${a.message}`,t.code,t.status),console.error(U,e,t.data),t.data=null,t.code=U,t.message=a.message),t}return t}class ie{constructor(t,s){y(this,"agent");y(this,"config");y(this,"cache");this.config=new S(s),this.agent=t,this.cache=new oe(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)}async request(t,s){try{return await this.agent(t,this.config,s)}catch(r){return console.error("RequestError",r),{ok:!1,status:-9,code:"Unkonwn",message:String(r),headers:{},data:null}}}async guard(t,s,r){return ce(t,s,this.config,r)}setConfig(t){this.config.set(t),this.cache.updateTTL(this.config.get("cacheTTL"))}getConfig(t){return this.config.get(t)}async head(t,s){const r=Object.assign({},s||null);return r.method="HEAD",this.guard(t,await this.request(t,r),null)}async get(t,s,r){const a=Object.assign({},r||null);a.method="GET";const n=this.cache.getKey(t,a.params),o=this.cache.get(n);if(o)return this.guard(t,await o,s||null);const c=this.request(t,a);return this.cache.set(n,c),this.guard(t,await c,s||null)}async post(t,s,r,a){const n=Object.assign({},a||null);return n.method="POST",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}async del(t,s,r){const a=Object.assign({},r||null);return a.method="DELETE",this.guard(t,await this.request(t,a),s||null)}async put(t,s,r,a){const n=Object.assign({},a||null);return n.method="PUT",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}async patch(t,s,r,a){const n=Object.assign({},a||null);return n.method="PATCH",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}}async function ue(e,t,s){var g;const r=Object.assign({method:"GET"},s),a=w.FormData?r.body instanceof FormData:!1,n=a&&r.method!=="POST"&&r.method!=="PUT"?"POST":r.method,o=n==="GET"||n==="HEAD"||n==="DELETE";o&&r.body!==void 0&&(console.warn("request body is invalid with method get, head, delete"),delete r.body);const c=Object.assign(a||o?{}:{"Content-Type":w.Blob&&r.body instanceof Blob?r.body.type||"application/octet-stream":"application/json;charset=utf-8"},r.headers),i=r.params||{},h={};Object.keys(i).forEach(b=>{i[b]!==void 0&&(h[b]=le(i[b]))});const u=t.getFullUrl(e),l=he(r.body),d=r.timeout||t.get("timeout"),f=await async function(){const b=t.get("requestTransformer");if(b)return await b({headers:c,params:h,method:n,url:u,body:l})}(),m=typeof f=="string"&&f?f:u;return(g=t.get("logHandler"))==null||g({type:"ready",url:m,method:n,headers:c,timeout:d,body:l}),{url:m,method:n,body:l,params:h,headers:c,timeout:d,abort:r.abort,credentials:r.credentials||t.get("credentials")}}function le(e){return typeof e=="string"?e:Array.isArray(e)?e.join(","):e+""}function he(e){if(e)return typeof e=="string"||e instanceof URLSearchParams||e instanceof ArrayBuffer||w.Blob&&e instanceof Blob||w.FormData&&e instanceof FormData?e:JSON.stringify(e)}const H="data",R="message";function de(e,t,s,r,a){const n=a||r;return T(e)?we(n.ok||r.ok,e,t,s):ge(n.failed||r.failed,t,s)}const fe=function(e){const t=[],s=e.failed||{resolve:"json"};switch(t.push("- 当http状态码 <200 或者 >=400 时"),s.resolve){case"body":t.push(" 将响应内容格式化为字符串并作为错误消息");break;case"json":t.push(" 将响应解析为json,并读取 "+(s.messageField||R)+" 作为错误消息");break}const r=e.ok||{resolve:"body"};switch(t.push("- 当http状态码 >=200 并且 <400 时"),r.resolve){case"body":t.push(" 将响应尝试解析为 json,并作为数据内容返回");break;case"json":t.push(" 将响应解析为 json,读取 "+(r.dataField||H)+" 作为响应数据,读取 "+(r.messageField||R)+" 作为提示消息"),r.statusField&&t.push(" 当 "+r.statusField+" 为 "+(r.statusOKValue||"空值")+" 时是成功提示,否则是错误消息"),r.ignoreMessage&&t.push(" 并忽略以下消息:"+r.ignoreMessage);break}return t.join(`
|
|
2
|
-
`)};function ge(e,t,s){const r=e||{resolve:"json",messageField:R},a={ok:!1,code:t,message:s,data:null};switch(r.resolve){case"body":a.message=L(s)||s;break;case"json":const{code:n,message:o}=me(s,r.converter,r.statusField,r.messageField);a.code=n||t,a.message=L(s)||o;break}return a}function me(e,t,s,r=R){if(!B(e))return{message:""};const a=F(k(e),t);return!a||!v(a)?{message:e}:{code:s?C(a,s):"",message:C(a,r)||e}}function C(e,t){const s=Array.isArray(t)?t:[t];for(const r of s)if(r in e)return ye(e[r]);return""}function ye(e){return e?typeof e=="string"?e:JSON.stringify(e):""}const pe=/<title>([^<]+)<\/title>/i,be=/<message>([^<]+)<\/message>/i;function L(e){const t=e.match(pe);if(t)return t[1];const s=e.match(be);return s?s[1]:""}function we(e,t,s,r){const a=e||{resolve:"body"},n={ok:!0,code:s,message:"",data:null};if(t===204||!r)return n;if(a.resolve==="body")return n.data=B(r)?F(k(r),e.converter):r,n;const o=F(k(r),e.converter);if(!o||!v(o))return n.ok=!1,n.code="ResponseFormatError",n.message="响应内容无法格式化为 Object",n;const c=a.statusField,i=a.statusOKValue||"",h=a.dataField||H,u=a.messageField||R,l=a.ignoreMessage||"";if(c&&!(c in o))return n.ok=!1,n.code="ResponseFieldMissing",n.message="响应内容找不到状态字段 "+c,n;const d=c?o[c]+"":"";return n.ok=c?d===i:!0,n.code=d||s,n.data=h===!0?o:h in o?o[h]:null,n.message=C(o,u),l&&n.message&&(Array.isArray(l)&&l.includes(n.message)||typeof l=="string"&&n.message===l)&&(n.message=""),n}function T(e){return e>=200&&e<400}function F(e,t){return t==="camelize"?E(e):t==="snakify"?$(e):e}const Re=J("APIError");function je(e){const t={};for(const a in e.headers)(a.startsWith("x-")||a.includes("trace")||a.includes("server")||/\b(?:id|uuid)\b/.test(a))&&(t[a]=e.headers[a]);const s=e.url.replace(/^(?:https?:)?\/*/i,"").replace(/\?.+/,""),r=e.status<0?"unknown":e.status;return{sentryError:new Re(`${s} | ${r}${e.code?` | ${e.code}`:""}`),sentryTags:{...t,status:r,method:e.method,code:e.code||"unknown",message:e.message||"empty"},sentryExtra:{url:e.url,responseBody:e.body||"empty",responseHeaders:e.headers,rawError:e.error}}}function Ee(e,t,s,r){var f;const a=e.status,n=e.method,o=P(Object.entries(e.headers||{}).map(([m,g])=>[m.toLowerCase(),g])),{ok:c,code:i,data:h,message:u}=de(a,e.statusText,e.body,s.get("responseRule"),r==null?void 0:r.responseRule);if(!T(a)){const m=je({url:e.url,method:e.method,status:a,code:i,message:u,body:e.body,headers:o,error:e.rawError});(s.get("errorHandler")||$e)({url:t,method:n,status:a,code:i,message:u,headers:o,rawError:e.rawError,responseBody:e.body,...m})}if(a<0)return q({ok:!1,status:a,code:e.statusText,headers:{},message:"",data:null},`${n} ${t} ${e.statusText}`,n,t,s,r);const l={ok:c,data:h,code:i,message:u,status:a,headers:o};(f=s.get("responseHandler"))==null||f({...l},n,t);const d=c?u:u||e.statusText;return q(l,d,n,t,s,r)}function q(e,t,s,r,a,n){const o=a.get("message"),c=o===!1||(n==null?void 0:n.message)===!1?!1:(n==null?void 0:n.message)||o;if(c!==!1){const i=typeof c=="function"?c(e,s,r,t):t;i instanceof Error?a.showMessage(!0,i.message,e.code,e.status):i&&typeof i=="object"&&"message"in i?a.showMessage(!1,i.message,e.code,e.status):a.showMessage(!e.ok,i,e.code,e.status)}return e}function $e(e){const t={};for(const s in e)s.startsWith("sentry")||(t[s]=e[s]);console.error("RequestError",t)}async function I(e,t,s,r,a){const n=a||0,o=Math.max(0,Math.min(10,(r==null?void 0:r.maxRetry)??s.get("maxRetry")??0)),c=(r==null?void 0:r.retryResolve)??s.get("retryResolve"),i=s.get("logHandler")||G;i({type:"prepear",url:t,method:(r==null?void 0:r.method)||"GET",retry:n,maxRetry:o,message:n===0?"start":`retry ${n}/${o} start`,headers:r==null?void 0:r.headers,options:r});const h=Date.now(),u=await e(t,s,r),l=u.status,d=Date.now()-h,f=`[cost ${d}][${l}] ${l<0?u.body:""}`;i({type:"finished",url:t,method:u.method,retry:n,maxRetry:o,message:n===0?`finish ${f}`:`retry ${n}/${o} finish ${f}`,response:u,headers:u.headers,cost:d});const m=T(l);if(!o||c==="network"&&l>0||c==="status"&&m||Array.isArray(c)&&!c.includes(l)||typeof c=="function"&&c(u,n)!==!0||n>=o)return u;const g=(r==null?void 0:r.retryInterval)??s.get("retryInterval")??1e3;return await W(Math.max(100,g==="2EB"?Math.pow(2,n)*1e3:typeof g=="function"?g(n+1)||0:g)),await I(e,t,s,r,n+1)}const Oe="2.0.7";exports.Ee=Y;exports.K=O;exports.NetRequestHandler=ie;exports.RequestGlobalConfig=S;exports.Support=w;exports.Wt=ne;exports.convertOptions=ue;exports.fromEntries=P;exports.getResponseRulesDescription=fe;exports.handleResponse=Ee;exports.retryRequest=I;exports.version=Oe;
|