@seayoo-web/request 1.6.1 → 1.6.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/inc/detect.d.ts +5 -0
- package/dist/index.cjs +2 -2
- package/dist/index.js +98 -93
- package/dist/node.cjs +1 -1
- package/dist/node.js +16 -16
- package/dist/version-Dd1gKjL_.cjs +2 -0
- package/dist/{version-D5ZPd9sj.js → version-imlkbI1X.js} +73 -65
- package/dist/version.d.ts +1 -1
- package/dist/wx.cjs +1 -1
- package/dist/wx.js +2 -2
- package/package.json +2 -2
- package/dist/version-yl6932gU.cjs +0 -2
package/dist/inc/detect.d.ts
CHANGED
|
@@ -5,4 +5,9 @@ export declare const Support: {
|
|
|
5
5
|
Blob: boolean;
|
|
6
6
|
wx: boolean;
|
|
7
7
|
TextDecoder: boolean;
|
|
8
|
+
AbortController: boolean;
|
|
8
9
|
};
|
|
10
|
+
/**
|
|
11
|
+
* 简版 Object.fromEntries 以处理某些低版本浏览器和 webview 的问题
|
|
12
|
+
*/
|
|
13
|
+
export declare function fromEntries(kv: [string, string | undefined][]): Record<string, string>;
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
2
|
-
`).forEach(e=>{const r=e.trim();if(!r)return;const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./version-Dd1gKjL_.cjs"),E=async function(n,a,t){const e=await s.convertOptions(n,a,t),r=new URL(e.url),l=e.params;l instanceof Object&&Object.keys(l).forEach(c=>r.searchParams.set(c,l[c]));const o=s.Support.AbortController?new AbortController:null;function i(){o&&!o.signal.aborted&&o.abort()}e.abort&&e.abort.addEventListener("abort",i);const b=e.timeout>0?setTimeout(i,e.timeout):null,p=new Request(r,{method:e.method,headers:new Headers(e.headers),body:e.body,credentials:e.credentials,signal:o==null?void 0:o.signal,redirect:"follow"});return await fetch(p).then(async c=>{const f=s.fromEntries(Object.entries(c.headers).map(([g,h])=>[g.toLowerCase(),Array.isArray(h)?h.join(","):h]));return{url:r.toString(),method:e.method,status:c.status,statusText:c.statusText,headers:f,body:e.method==="HEAD"?"":await c.text()}}).catch(c=>({url:r.toString(),method:e.method,status:-1,statusText:o!=null&&o.signal.aborted?"Aborted":"NetworkError",body:String(c)})).finally(()=>{b!==null&&clearTimeout(b),e.abort&&e.abort.removeEventListener("abort",i)})},m=async function(n,a,t){const e={...await s.convertOptions(n,a,t),onUploadProgress:t==null?void 0:t.onUploadProgress},r=e.method,l=e.onUploadProgress||s.Wt,o=s.Et(e.url,e.params);return await new Promise(i=>{let b=1;function p(w){b=w.total,l({total:w.total,loaded:w.loaded})}let c=null,f=!1;const g=function(){f||(d.abort(),f=!0)};function h(){c!==null&&clearTimeout(c),e.abort&&e.abort.removeEventListener("abort",g)}const d=new XMLHttpRequest;d.open(r,o,!0),d.upload.addEventListener("progress",p),d.addEventListener("load",()=>{h(),l({loaded:b,total:b}),i({url:o,method:r,status:d.status,statusText:d.statusText,headers:T(d),body:r==="HEAD"?"":d.responseText})}),d.addEventListener("error",()=>{h(),i({url:o,method:r,status:-1,statusText:"Failed",body:""})}),d.addEventListener("abort",()=>{h(),i({url:o,method:r,status:-1,statusText:"Aborted",body:""})}),Object.entries(e.headers).forEach(([w,R])=>{d.setRequestHeader(w,R)}),e.credentials==="include"&&(d.withCredentials=!0),d.send(e.body||void 0),e.abort&&e.abort.addEventListener("abort",g),e.timeout>0&&(c=setTimeout(g,e.timeout))})};function T(n){const a={};if(!n)return a;const t=n.getAllResponseHeaders();return t&&t!=="null"&&t.replace(/\r/g,"").split(`
|
|
2
|
+
`).forEach(e=>{const r=e.trim();if(!r)return;const l=r.split(":"),o=l[0].trim();o&&(a[o]=(l[1]||"").trim())}),a}const q=async function(n,a,t){return s.handleResponse(await s.retryRequest(E,n,a,t),n,a,t)},H=async function(n,a,t){return s.handleResponse(await s.retryRequest(m,n,a,t),n,a,t)};async function S(n,a,t,e){const r=t==null?void 0:t.body,l=(t==null?void 0:t.method)==="PUT"?"PUT":"POST";if(a instanceof Blob){const c=new s.RequestGlobalConfig(e),f=await m(n,c,{...t,method:l,body:a});return s.handleResponse(f,n,c,t)}const o=new FormData,i={...a};r instanceof Object&&Object.entries(r).forEach(([c,f])=>{f instanceof Blob?i[c]=f:Array.isArray(f)?f.forEach((g,h)=>{o.append(`${c}[${h}]`,String(g))}):o.append(c,String(f))});for(const c in i)o.append(c,i[c]);const b=new s.RequestGlobalConfig(e),p=await m(n,b,{...t,method:l,body:o});return s.handleResponse(p,n,b,t)}async function x(n,a,t={}){const e=window;"callback"in t||(t.callback="jsonxData"+Math.random().toString(16).slice(2));const r=t.callback+"";if(!n)return null;const l=s.Et(n,t,!0);return new Promise(o=>{e[r]=function(i){if(r in window&&delete e[r],a(i))return i;console.warn("response type check faild",n,i),o(null)},s.he(l).catch(function(){o(null),delete e[r]})})}async function C(n,a,t={}){const e=window;return"var"in t||(t.var="jsonxData"+Math.random().toString(16).slice(2)),n?await s.he(s.Et(n,t,!0)).then(()=>{const r=e[t.var+""];return a(r)?r:(console.warn("response type check faild",n,r),null)}).catch(()=>null):null}const j=async function(n,a,t){return await S(n,a,t,{baseURL:u.getConfig("baseURL"),logHandler:u.getConfig("logHandler"),errorHandler:u.getConfig("errorHandler"),requestTransformer:u.getConfig("requestTransformer"),messageHandler:u.getConfig("messageHandler"),responseHandler:u.getConfig("responseHandler")})};function y(n){if(!s.Support.window)throw new Error("Default Module Only Support In Browser");return s.Support.fetch?new s.NetRequestHandler(q,n):new s.NetRequestHandler(H,n)}const u=y(),L=u.setConfig,v=u.head,U=u.get,A=u.post,O=u.del,P=u.put,D=u.patch;exports.getResponseRulesDescription=s.getResponseRulesDescription;exports.version=s.version;exports.NetRequest=y;exports.del=O;exports.get=U;exports.head=v;exports.jsonp=x;exports.jsonx=C;exports.patch=D;exports.post=A;exports.put=P;exports.setGlobalConfig=L;exports.upload=j;
|
package/dist/index.js
CHANGED
|
@@ -1,86 +1,91 @@
|
|
|
1
|
-
import { c as H, E as
|
|
2
|
-
import { g as
|
|
1
|
+
import { c as H, S as y, f as C, E as R, W as S, h as w, r as x, R as E, a as q, N as T } from "./version-imlkbI1X.js";
|
|
2
|
+
import { g as K, v as Q } from "./version-imlkbI1X.js";
|
|
3
3
|
const U = async function(n, o, e) {
|
|
4
|
-
const t = await H(n, o, e), r = new URL(t.url),
|
|
5
|
-
|
|
6
|
-
const
|
|
4
|
+
const t = await H(n, o, e), r = new URL(t.url), i = t.params;
|
|
5
|
+
i instanceof Object && Object.keys(i).forEach((s) => r.searchParams.set(s, i[s]));
|
|
6
|
+
const a = y.AbortController ? new AbortController() : null;
|
|
7
7
|
function c() {
|
|
8
|
-
|
|
8
|
+
a && !a.signal.aborted && a.abort();
|
|
9
9
|
}
|
|
10
10
|
t.abort && t.abort.addEventListener("abort", c);
|
|
11
|
-
const f = t.timeout > 0 ? setTimeout(c, t.timeout) : null,
|
|
11
|
+
const f = t.timeout > 0 ? setTimeout(c, t.timeout) : null, g = new Request(r, {
|
|
12
12
|
method: t.method,
|
|
13
13
|
headers: new Headers(t.headers),
|
|
14
14
|
body: t.body,
|
|
15
15
|
credentials: t.credentials,
|
|
16
|
-
signal:
|
|
16
|
+
signal: a == null ? void 0 : a.signal,
|
|
17
17
|
redirect: "follow"
|
|
18
18
|
});
|
|
19
|
-
return await fetch(
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
19
|
+
return await fetch(g).then(async (s) => {
|
|
20
|
+
const u = C(
|
|
21
|
+
Object.entries(s.headers).map(([h, b]) => [h.toLowerCase(), Array.isArray(b) ? b.join(",") : b])
|
|
22
|
+
);
|
|
23
|
+
return {
|
|
24
|
+
url: r.toString(),
|
|
25
|
+
method: t.method,
|
|
26
|
+
status: s.status,
|
|
27
|
+
statusText: s.statusText,
|
|
28
|
+
headers: u,
|
|
29
|
+
body: t.method === "HEAD" ? "" : await s.text()
|
|
30
|
+
};
|
|
31
|
+
}).catch((s) => ({
|
|
27
32
|
url: r.toString(),
|
|
28
33
|
method: t.method,
|
|
29
34
|
status: -1,
|
|
30
|
-
statusText:
|
|
31
|
-
body: String(
|
|
35
|
+
statusText: a != null && a.signal.aborted ? "Aborted" : "NetworkError",
|
|
36
|
+
body: String(s)
|
|
32
37
|
})).finally(() => {
|
|
33
38
|
f !== null && clearTimeout(f), t.abort && t.abort.removeEventListener("abort", c);
|
|
34
39
|
});
|
|
35
|
-
},
|
|
40
|
+
}, p = async function(n, o, e) {
|
|
36
41
|
const t = {
|
|
37
42
|
...await H(n, o, e),
|
|
38
43
|
onUploadProgress: e == null ? void 0 : e.onUploadProgress
|
|
39
|
-
}, r = t.method,
|
|
44
|
+
}, r = t.method, i = t.onUploadProgress || S, a = R(t.url, t.params);
|
|
40
45
|
return await new Promise((c) => {
|
|
41
46
|
let f = 1;
|
|
42
|
-
function
|
|
43
|
-
f = m.total,
|
|
47
|
+
function g(m) {
|
|
48
|
+
f = m.total, i({ total: m.total, loaded: m.loaded });
|
|
44
49
|
}
|
|
45
|
-
let
|
|
50
|
+
let s = null, u = !1;
|
|
46
51
|
const h = function() {
|
|
47
|
-
u || (
|
|
52
|
+
u || (d.abort(), u = !0);
|
|
48
53
|
};
|
|
49
|
-
function
|
|
50
|
-
|
|
54
|
+
function b() {
|
|
55
|
+
s !== null && clearTimeout(s), t.abort && t.abort.removeEventListener("abort", h);
|
|
51
56
|
}
|
|
52
|
-
const
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
url:
|
|
57
|
+
const d = new XMLHttpRequest();
|
|
58
|
+
d.open(r, a, !0), d.upload.addEventListener("progress", g), d.addEventListener("load", () => {
|
|
59
|
+
b(), i({ loaded: f, total: f }), c({
|
|
60
|
+
url: a,
|
|
56
61
|
method: r,
|
|
57
|
-
status:
|
|
58
|
-
statusText:
|
|
59
|
-
headers:
|
|
60
|
-
body: r === "HEAD" ? "" :
|
|
62
|
+
status: d.status,
|
|
63
|
+
statusText: d.statusText,
|
|
64
|
+
headers: j(d),
|
|
65
|
+
body: r === "HEAD" ? "" : d.responseText
|
|
61
66
|
});
|
|
62
|
-
}),
|
|
63
|
-
|
|
64
|
-
url:
|
|
67
|
+
}), d.addEventListener("error", () => {
|
|
68
|
+
b(), c({
|
|
69
|
+
url: a,
|
|
65
70
|
method: r,
|
|
66
71
|
status: -1,
|
|
67
72
|
statusText: "Failed",
|
|
68
73
|
body: ""
|
|
69
74
|
});
|
|
70
|
-
}),
|
|
71
|
-
|
|
72
|
-
url:
|
|
75
|
+
}), d.addEventListener("abort", () => {
|
|
76
|
+
b(), c({
|
|
77
|
+
url: a,
|
|
73
78
|
method: r,
|
|
74
79
|
status: -1,
|
|
75
80
|
statusText: "Aborted",
|
|
76
81
|
body: ""
|
|
77
82
|
});
|
|
78
83
|
}), Object.entries(t.headers).forEach(([m, L]) => {
|
|
79
|
-
|
|
80
|
-
}), t.credentials === "include" && (
|
|
84
|
+
d.setRequestHeader(m, L);
|
|
85
|
+
}), t.credentials === "include" && (d.withCredentials = !0), d.send(t.body || void 0), t.abort && t.abort.addEventListener("abort", h), t.timeout > 0 && (s = setTimeout(h, t.timeout));
|
|
81
86
|
});
|
|
82
87
|
};
|
|
83
|
-
function
|
|
88
|
+
function j(n) {
|
|
84
89
|
const o = {};
|
|
85
90
|
if (!n)
|
|
86
91
|
return o;
|
|
@@ -90,92 +95,92 @@ function v(n) {
|
|
|
90
95
|
const r = t.trim();
|
|
91
96
|
if (!r)
|
|
92
97
|
return;
|
|
93
|
-
const
|
|
94
|
-
|
|
98
|
+
const i = r.split(":"), a = i[0].trim();
|
|
99
|
+
a && (o[a] = (i[1] || "").trim());
|
|
95
100
|
}), o;
|
|
96
101
|
}
|
|
97
|
-
const
|
|
102
|
+
const A = async function(n, o, e) {
|
|
98
103
|
return w(await x(U, n, o, e), n, o, e);
|
|
99
|
-
},
|
|
100
|
-
return w(await x(
|
|
104
|
+
}, v = async function(n, o, e) {
|
|
105
|
+
return w(await x(p, n, o, e), n, o, e);
|
|
101
106
|
};
|
|
102
107
|
async function O(n, o, e, t) {
|
|
103
|
-
const r = e == null ? void 0 : e.body,
|
|
108
|
+
const r = e == null ? void 0 : e.body, i = (e == null ? void 0 : e.method) === "PUT" ? "PUT" : "POST";
|
|
104
109
|
if (o instanceof Blob) {
|
|
105
|
-
const
|
|
110
|
+
const s = new E(t), u = await p(n, s, {
|
|
106
111
|
...e,
|
|
107
|
-
method:
|
|
112
|
+
method: i,
|
|
108
113
|
body: o
|
|
109
114
|
});
|
|
110
|
-
return w(u, n,
|
|
115
|
+
return w(u, n, s, e);
|
|
111
116
|
}
|
|
112
|
-
const
|
|
113
|
-
r instanceof Object && Object.entries(r).forEach(([
|
|
114
|
-
u instanceof Blob ? c[
|
|
115
|
-
|
|
116
|
-
}) :
|
|
117
|
+
const a = new FormData(), c = { ...o };
|
|
118
|
+
r instanceof Object && Object.entries(r).forEach(([s, u]) => {
|
|
119
|
+
u instanceof Blob ? c[s] = u : Array.isArray(u) ? u.forEach((h, b) => {
|
|
120
|
+
a.append(`${s}[${b}]`, String(h));
|
|
121
|
+
}) : a.append(s, String(u));
|
|
117
122
|
});
|
|
118
|
-
for (const
|
|
119
|
-
|
|
120
|
-
const f = new
|
|
123
|
+
for (const s in c)
|
|
124
|
+
a.append(s, c[s]);
|
|
125
|
+
const f = new E(t), g = await p(n, f, {
|
|
121
126
|
...e,
|
|
122
|
-
method:
|
|
123
|
-
body:
|
|
127
|
+
method: i,
|
|
128
|
+
body: a
|
|
124
129
|
});
|
|
125
|
-
return w(
|
|
130
|
+
return w(g, n, f, e);
|
|
126
131
|
}
|
|
127
|
-
async function
|
|
132
|
+
async function D(n, o, e = {}) {
|
|
128
133
|
const t = window;
|
|
129
134
|
"callback" in e || (e.callback = "jsonxData" + Math.random().toString(16).slice(2));
|
|
130
135
|
const r = e.callback + "";
|
|
131
136
|
if (!n)
|
|
132
137
|
return null;
|
|
133
|
-
const
|
|
134
|
-
return new Promise((
|
|
138
|
+
const i = R(n, e, !0);
|
|
139
|
+
return new Promise((a) => {
|
|
135
140
|
t[r] = function(c) {
|
|
136
141
|
if (r in window && delete t[r], o(c))
|
|
137
142
|
return c;
|
|
138
|
-
console.warn("response type check faild", n, c),
|
|
139
|
-
}, q(
|
|
140
|
-
|
|
143
|
+
console.warn("response type check faild", n, c), a(null);
|
|
144
|
+
}, q(i).catch(function() {
|
|
145
|
+
a(null), delete t[r];
|
|
141
146
|
});
|
|
142
147
|
});
|
|
143
148
|
}
|
|
144
|
-
async function
|
|
149
|
+
async function M(n, o, e = {}) {
|
|
145
150
|
const t = window;
|
|
146
|
-
return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), n ? await q(
|
|
151
|
+
return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), n ? await q(R(n, e, !0)).then(() => {
|
|
147
152
|
const r = t[e.var + ""];
|
|
148
153
|
return o(r) ? r : (console.warn("response type check faild", n, r), null);
|
|
149
154
|
}).catch(() => null) : null;
|
|
150
155
|
}
|
|
151
|
-
const
|
|
156
|
+
const N = async function(n, o, e) {
|
|
152
157
|
return await O(n, o, e, {
|
|
153
|
-
baseURL:
|
|
154
|
-
logHandler:
|
|
155
|
-
errorHandler:
|
|
156
|
-
requestTransformer:
|
|
157
|
-
messageHandler:
|
|
158
|
-
responseHandler:
|
|
158
|
+
baseURL: l.getConfig("baseURL"),
|
|
159
|
+
logHandler: l.getConfig("logHandler"),
|
|
160
|
+
errorHandler: l.getConfig("errorHandler"),
|
|
161
|
+
requestTransformer: l.getConfig("requestTransformer"),
|
|
162
|
+
messageHandler: l.getConfig("messageHandler"),
|
|
163
|
+
responseHandler: l.getConfig("responseHandler")
|
|
159
164
|
});
|
|
160
165
|
};
|
|
161
166
|
function P(n) {
|
|
162
|
-
if (!
|
|
167
|
+
if (!y.window)
|
|
163
168
|
throw new Error("Default Module Only Support In Browser");
|
|
164
|
-
return
|
|
169
|
+
return y.fetch ? new T(A, n) : new T(v, n);
|
|
165
170
|
}
|
|
166
|
-
const
|
|
171
|
+
const l = P(), F = l.setConfig, B = l.head, X = l.get, G = l.post, I = l.del, W = l.put, $ = l.patch;
|
|
167
172
|
export {
|
|
168
173
|
P as NetRequest,
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
174
|
+
I as del,
|
|
175
|
+
X as get,
|
|
176
|
+
K as getResponseRulesDescription,
|
|
177
|
+
B as head,
|
|
178
|
+
D as jsonp,
|
|
179
|
+
M as jsonx,
|
|
180
|
+
$ as patch,
|
|
181
|
+
G as post,
|
|
182
|
+
W as put,
|
|
183
|
+
F as setGlobalConfig,
|
|
184
|
+
N as upload,
|
|
185
|
+
Q as version
|
|
181
186
|
};
|
package/dist/node.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("node:http"),q=require("node:https"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("node:http"),q=require("node:https"),o=require("./version-Dd1gKjL_.cjs"),R=async function(n,u,c){return o.handleResponse(await o.retryRequest(w,n,u,c),n,u,c)},w=async function(n,u,c){const t=await o.convertOptions(n,u,c);if(!o.U(t.url))return{url:t.url,method:t.method,status:-1,statusText:"URLFormatError",headers:{},body:""};const f=/^https:\/\//i.test(t.url)?q:y,a=new URL(t.url),i=t.params;i instanceof Object&&Object.keys(i).forEach(r=>a.searchParams.set(r,i[r]));const b=t.method==="HEAD";return new Promise(r=>{const d=f.request(a,{headers:t.headers,method:t.method,timeout:t.timeout>0?t.timeout:void 0},function(e){const p=[];e.on("data",h=>p.push(h)),e.on("end",()=>{const h=o.fromEntries(Object.entries(e.headers).map(([g,m])=>[g.toLowerCase(),Array.isArray(m)?m.join(","):m]));r({url:a.toString(),method:t.method,status:e.statusCode||-1,statusText:e.statusMessage||"Unknown",headers:h,body:b?"":Buffer.concat(p).toString("utf-8")})})});d.on("error",e=>{r({url:a.toString(),method:t.method,status:-1,statusText:e.name||"Unknown",body:e.message})}),d.on("timeout",()=>{r({url:a.toString(),method:t.method,status:-1,statusText:"Timeout",body:""})}),t.body&&d.write(t.body),d.end()})};function l(n){return new o.NetRequestHandler(R,n)}const s=l(),S=s.setConfig,T=s.head,U=s.get,j=s.post,C=s.del,O=s.put,x=s.patch;exports.version=o.version;exports.NetRequest=l;exports.del=C;exports.get=U;exports.head=T;exports.patch=x;exports.post=j;exports.put=O;exports.setGlobalConfig=S;
|
package/dist/node.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import l from "node:http";
|
|
2
2
|
import y from "node:https";
|
|
3
|
-
import { h as R, r as g, c as q, U as w,
|
|
4
|
-
import { v as
|
|
5
|
-
const
|
|
3
|
+
import { h as R, r as g, c as q, U as w, f as x, N as U } from "./version-imlkbI1X.js";
|
|
4
|
+
import { v as G } from "./version-imlkbI1X.js";
|
|
5
|
+
const T = async function(s, a, u) {
|
|
6
6
|
return R(await g(j, s, a, u), s, a, u);
|
|
7
7
|
}, j = async function(s, a, u) {
|
|
8
8
|
const t = await q(s, a, u);
|
|
@@ -29,7 +29,7 @@ const U = async function(s, a, u) {
|
|
|
29
29
|
function(e) {
|
|
30
30
|
const h = [];
|
|
31
31
|
e.on("data", (i) => h.push(i)), e.on("end", () => {
|
|
32
|
-
const i =
|
|
32
|
+
const i = x(
|
|
33
33
|
Object.entries(e.headers).map(([b, m]) => [b.toLowerCase(), Array.isArray(m) ? m.join(",") : m])
|
|
34
34
|
);
|
|
35
35
|
n({
|
|
@@ -62,18 +62,18 @@ const U = async function(s, a, u) {
|
|
|
62
62
|
}), t.body && c.write(t.body), c.end();
|
|
63
63
|
});
|
|
64
64
|
};
|
|
65
|
-
function
|
|
66
|
-
return new
|
|
65
|
+
function C(s) {
|
|
66
|
+
return new U(T, s);
|
|
67
67
|
}
|
|
68
|
-
const o =
|
|
68
|
+
const o = C(), O = o.setConfig, S = o.head, A = o.get, H = o.post, k = o.del, P = o.put, B = o.patch;
|
|
69
69
|
export {
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
70
|
+
C as NetRequest,
|
|
71
|
+
k as del,
|
|
72
|
+
A as get,
|
|
73
|
+
S as head,
|
|
74
|
+
B as patch,
|
|
75
|
+
H as post,
|
|
76
|
+
P as put,
|
|
77
|
+
O as setGlobalConfig,
|
|
78
|
+
G as version
|
|
79
79
|
};
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var B=Object.defineProperty;var H=(t,e,n)=>e in t?B(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var f=(t,e,n)=>(H(t,typeof e!="symbol"?e+"":e,n),n);const m=typeof globalThis<"u"?globalThis:typeof global<"u"?global:typeof window<"u"?window:{},b={fetch:"fetch"in m,window:"window"in m,FormData:"FormData"in m,Blob:"Blob"in m,wx:"wx"in m,TextDecoder:"TextDecoder"in m,AbortController:"AbortController"in m};function M(t){return t.reduce((e,[n,s])=>(n&&(e[n]=s||""),e),{})}async function P(t,e,n){var w;const s=Object.assign({method:"GET"},n),r=b.FormData?s.body instanceof FormData:!1,a=r&&s.method!=="POST"&&s.method!=="PUT"?"POST":s.method;(a==="GET"||a==="HEAD"||a==="DELETE")&&s.body!==void 0&&(console.warn("request body is invalid with method get, head, delete"),delete s.body);const o=Object.assign(r?{}:{"Content-Type":b.Blob&&s.body instanceof Blob?s.body.type||"application/octet-stream":"application/json;charset=utf-8"},s.headers),i=s.params||{},c={};Object.keys(i).forEach(h=>{i[h]!==void 0&&(c[h]=S(i[h]))});const d=e.getFullUrl(t),l=q(s.body),u=s.timeout||e.get("timeout"),g=await async function(){var F;const h=e.get("requestTransformer");return h?await h({headers:o,params:c,method:a,url:d,body:l}):await((F=e.get("requestHandler"))==null?void 0:F(o,c,a,t))}(),y=typeof g=="string"&&g?g:d;return(w=e.get("logHandler"))==null||w({type:"ready",url:y,method:a,headers:o,timeout:u,body:l}),{url:y,method:a,body:l,params:c,headers:o,timeout:u,abort:s.abort,credentials:s.credentials||e.get("credentials")}}function S(t){return typeof t=="string"?t:Array.isArray(t)?t.join(","):t+""}function q(t){if(t)return typeof t=="string"||t instanceof URLSearchParams||t instanceof ArrayBuffer||b.Blob&&t instanceof Blob||b.FormData&&t instanceof FormData?t:JSON.stringify(t)}function K(t){return new Promise(e=>setTimeout(e,Math.max(0,t)))}function D(){}function j(t,e){try{const n=JSON.parse(t);return e?e(n)?n:null:n}catch{return null}}""+Math.random().toString(32).slice(2);const I=/^(?:https?:)?\/\/.+$/i,N=/^https?:\/\/.+$/i,G=/^\{[\d\D]*\}$/,W=/^\[[\d\D]*\]$/;function R(t,e=!1){return e?I.test(t):N.test(t)}function J(t){return t==null}function C(t,...e){if(!t||typeof t!="object")return!1;const n=Object.getPrototypeOf(t);return n!==Object.prototype&&n!==null?!1:e.every(s=>s in t)}function A(t){return G.test(t)||W.test(t)}async function _(t){return await new Promise(function(e){const n=document.getElementsByTagName("head")[0],s=document.createElement("script");s.setAttribute("type","text/javascript"),s.setAttribute("charset","utf-8"),s.onload=function(){n.removeChild(s),e(!0)},s.onerror=function(){n.removeChild(s),e(!1)},s.setAttribute("src",t),n.appendChild(s)})}function V(t,e="数据未能正确识别"){return typeof t=="function"?{guard:t,message:e}:{guard:t.guard,message:t.message||e}}function O(t,e=""){return!e||R(t,!0)?T(t):(T(e)+"/"+t).replace(/\/{2,}/g,"/").replace(/:\//,"://")}function T(t){return R(t,!0)?t.startsWith("http")?t:("location"in globalThis?location.protocol:"https:")+t:("location"in globalThis?location.origin:"http://127.0.0.1")+"/"+t.replace(/^\/+/,"")}function z(t,e){if(e){if(e===!0)return t.replace(/\?[^#]*/,"")}else return t;const n=t.split("#"),s=n[0].split("?"),r=s[0],a=s.length>1?s[1]:"",o=n.length>1?"#"+n[1]:"",i=typeof e=="string"?[e]:Array.isArray(e)?e:[];return!i.length||!a?n[0]+o:(i.map(c=>c.replace(/([\\(){}[\]^$+\-*?|])/g,"\\$1")),(r+"?"+a.replace(new RegExp("(?:^|&)(?:"+i.join("|")+")=[^&$]+","g"),"").replace(/^&/,"")).replace(/\?$/,"")+o)}function Q(t,e,n=!1){const s=typeof e=="string"?e:Object.keys(e).map(o=>`${o}=${encodeURIComponent(e[o])}`).join("&");if(!s)return t;const r=t.split("#");n&&(r[0]=z(r[0],(s.match(/([^=&#?]+)=[^&#]+/g)||[]).map(o=>o.replace(/=.+$/,""))));const a=r[0].indexOf("?")+1?"&":"?";return(r[0]+a+s+(r.length>1?"#"+r[1]:"")).replace(/\?&/,"?")}const x="data",p="message";function X(t,e,n,s,r){const a=r||s;return v(t)?ae(a.ok||s.ok,t,e,n):Z(a.failed||s.failed,e,n)}const Y=function(t){const e=[],n=t.failed||{resolve:"json"};switch(e.push("- 当http状态码 <200 或者 >=400 时"),n.resolve){case"body":e.push(" 将响应内容格式化为字符串并作为错误消息");break;case"json":e.push(" 将响应解析为json,并读取 "+(n.messageField||p)+" 作为错误消息");break}const s=t.ok||{resolve:"body"};switch(e.push("- 当http状态码 >=200 并且 <400 时"),s.resolve){case"body":e.push(" 将响应尝试解析为 json,并作为数据内容返回");break;case"json":e.push(" 将响应解析为 json,读取 "+(s.dataField||x)+" 作为响应数据,读取 "+(s.messageField||p)+" 作为提示消息"),s.statusField&&e.push(" 当 "+s.statusField+" 为 "+(s.statusOKValue||"空值")+" 时是成功提示,否则是错误消息"),s.ignoreMessage&&e.push(" 并忽略以下消息:"+s.ignoreMessage);break}return e.join(`
|
|
2
|
+
`)};function Z(t,e,n){const s=t||{resolve:"json",messageField:p},r={ok:!1,code:e,message:n,data:null};switch(s.resolve){case"body":r.message=E(n)||n;break;case"json":const{code:a,message:o}=ee(n,s.statusField,s.messageField);r.code=a||e,r.message=E(n)||o;break}return r}function ee(t,e,n=p){if(!A(t))return{message:""};const s=j(t);return!s||!C(s)?{message:t}:{code:e?$(s,e):"",message:$(s,n)||t}}function $(t,e){const n=Array.isArray(e)?e:[e];for(const s of n)if(s in t)return te(t[s]);return""}function te(t){return t?typeof t=="string"?t:JSON.stringify(t):""}const se=/<title>([^<]+)<\/title>/i,ne=/<message>([^<]+)<\/message>/i;function E(t){const e=t.match(se);if(e)return e[1];const n=t.match(ne);return n?n[1]:""}function ae(t,e,n,s){const r=t||{resolve:"body"},a={ok:!0,code:n,message:"",data:null};if(e===202||e===204||!s)return a;if(r.resolve==="body")return a.data=A(s)?j(s):s,a;const o=j(s);if(!o||!C(o))return a.ok=!1,a.code="ResponseFormatError",a.message="响应内容无法格式化为 Object",a;const i=r.statusField,c=r.statusOKValue||"",d=r.dataField||x,l=r.messageField||p,u=r.ignoreMessage||"";if(i&&!(i in o))return a.ok=!1,a.code="ResponseFieldMissing",a.message="响应内容找不到状态字段 "+i,a;const g=i?o[i]+"":"";return a.ok=i?g===c:!0,a.code=g||n,a.data=d===!0?o:d in o?o[d]:null,a.message=$(o,l),u&&a.message&&(Array.isArray(u)&&u.includes(a.message)||typeof u=="string"&&a.message===u)&&(a.message=""),a}function v(t){return t>=200&&t<400}async function U(t,e,n,s,r){const a=r||0,o=Math.max(0,Math.min(10,(s==null?void 0:s.maxRetry)??n.get("maxRetry")??0)),i=(s==null?void 0:s.retryResolve)??n.get("retryResolve"),c=n.get("logHandler")||D;c({type:"prepear",url:e,method:(s==null?void 0:s.method)||"GET",retry:a,maxRetry:o,message:a===0?"start":`retry ${a}/${o} start`,headers:s==null?void 0:s.headers,options:s});const d=Date.now(),l=await t(e,n,s),u=l.status,g=Date.now()-d,y=`[cost ${g}][${u}] ${u<0?l.body:""}`;c({type:"finished",url:e,method:l.method,retry:a,maxRetry:o,message:a===0?`finish ${y}`:`retry ${a}/${o} finish ${y}`,response:l,headers:l.headers,cost:g});const w=v(u);if(!o||i==="network"&&u>0||i==="status"&&w||Array.isArray(i)&&!i.includes(u)||typeof i=="function"&&i(l,a)!==!0||a>=o)return l;const h=(s==null?void 0:s.retryInterval)??n.get("retryInterval")??1e3;return await K(Math.max(100,h==="2EB"?Math.pow(2,a)*1e3:typeof h=="function"?h(a+1)||0:h)),await U(t,e,n,s,a+1)}function re(t,e,n,s){var o,i;if(t.status<0)return k({ok:!1,status:t.status,code:t.statusText,headers:{},message:"",data:null},`${t.method} ${e} ${t.statusText}`,t.method,e,n,s);v(t.status)||(o=n.get("errorHandler"))==null||o(t.status,t.method,e);const r={...X(t.status,t.statusText,t.body,n.get("responseRule"),s==null?void 0:s.responseRule),status:t.status,headers:M(Object.entries(t.headers||{}).map(([c,d])=>[c.toLowerCase(),d]))};(i=n.get("responseHandler"))==null||i({...r},t.method,e);const a=r.ok?r.message:`${t.method} ${e} [${r.code||t.statusText}] ${r.message||t.statusText}`;return k(r,a,t.method,e,n,s)}function k(t,e,n,s,r,a){const o=r.get("message"),i=o===!1||(a==null?void 0:a.message)===!1?!1:(a==null?void 0:a.message)||o;if(i!==!1){const c=typeof i=="function"?i(t,n,s,e):e;c instanceof Error?r.showMessage(!0,c.message):c&&typeof c=="object"&&"message"in c?r.showMessage(!1,c.message):r.showMessage(!t.ok,c)}return t}class L{constructor(e){f(this,"config",{baseURL:"",maxRetry:0,retryInterval:1e3,retryResolve:"network",timeout:5e3,cacheTTL:500,credentials:"same-origin",responseRule:{ok:{resolve:"body"},failed:{resolve:"json",messageField:"message"}}});e&&this.set(e)}set(e){if(e.baseURL&&!/^\/.+/.test(e.baseURL)&&!R(e.baseURL))throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"),new Error("BaseURLError");Object.assign(this.config,e)}get(e){return this.config[e]}getFullUrl(e){return e.startsWith("/")?O(e):O(e,this.config.baseURL)}showMessage(e,n){this.config.messageHandler&&n&&this.config.messageHandler(e,n)}}class oe{constructor(e=500){f(this,"ttl");f(this,"cache");this.cache={},this.ttl=Math.max(e,0)}getKey(e,n){const s=Object.keys(n||{}).sort().map(r=>`${r}#${n==null?void 0:n[r]}`);return e+s.join(",")}updateTTL(e){this.ttl=Math.max(e,0)}get(e){if(this.ttl===0)return null;const n=this.cache[e];return n?n.ttl<Date.now()?(delete this.cache[e],null):n.res:null}set(e,n){this.ttl!==0&&(this.cache[e]={ttl:Date.now()+this.ttl,res:n})}}function ie(t,e,n,s){if(e.ok&&!J(e.data)&&s){const r=V(s,"响应数据未能正确识别");return r.guard(e.data)||(console.error("ResponseCheckFaild",t,e.data),n.showMessage(!0,`${t} ${r.message}`),e.data=null),e}return e}class ce{constructor(e,n){f(this,"agent");f(this,"config");f(this,"cache");this.config=new L(n),this.agent=e,this.cache=new oe(this.config.get("cacheTTL")),this.setConfig=this.setConfig.bind(this),this.getConfig=this.getConfig.bind(this),this.get=this.get.bind(this),this.post=this.post.bind(this),this.del=this.del.bind(this),this.patch=this.patch.bind(this),this.put=this.put.bind(this),this.head=this.head.bind(this)}async exec(e,n){return await this.agent(e,this.config,n)}async guard(e,n,s){return ie(e,n,this.config,s)}setConfig(e){this.config.set(e),this.cache.updateTTL(this.config.get("cacheTTL"))}getConfig(e){return this.config.get(e)}async head(e,n){const s=Object.assign({},n||null);return s.method="HEAD",this.guard(e,await this.exec(e,s),null)}async get(e,n,s){const r=Object.assign({},s||null);r.method="GET";const a=this.cache.getKey(e,r.params),o=this.cache.get(a);if(o)return this.guard(e,await o,n||null);const i=this.exec(e,r);return this.cache.set(a,i),this.guard(e,await i,n||null)}async post(e,n,s,r){const a=Object.assign({},r||null);return a.method="POST",a.body=n,this.guard(e,await this.exec(e,a),s||null)}async del(e,n,s){const r=Object.assign({},s||null);return r.method="DELETE",this.guard(e,await this.exec(e,r),n||null)}async put(e,n,s,r){const a=Object.assign({},r||null);return a.method="PUT",a.body=n,this.guard(e,await this.exec(e,a),s||null)}async patch(e,n,s,r){const a=Object.assign({},r||null);return a.method="PATCH",a.body=n,this.guard(e,await this.exec(e,a),s||null)}}const ue="1.6.3";exports.Et=Q;exports.NetRequestHandler=ce;exports.RequestGlobalConfig=L;exports.Support=b;exports.U=R;exports.Wt=D;exports.convertOptions=P;exports.fromEntries=M;exports.getResponseRulesDescription=Y;exports.handleResponse=re;exports.he=_;exports.retryRequest=U;exports.version=ue;
|
|
@@ -1,15 +1,22 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var f = (t, e, n) => (
|
|
1
|
+
var A = Object.defineProperty;
|
|
2
|
+
var x = (t, e, n) => e in t ? A(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
|
|
3
|
+
var f = (t, e, n) => (x(t, typeof e != "symbol" ? e + "" : e, n), n);
|
|
4
4
|
const m = typeof globalThis < "u" ? globalThis : typeof global < "u" ? global : typeof window < "u" ? window : {}, w = {
|
|
5
5
|
fetch: "fetch" in m,
|
|
6
6
|
window: "window" in m,
|
|
7
7
|
FormData: "FormData" in m,
|
|
8
8
|
Blob: "Blob" in m,
|
|
9
9
|
wx: "wx" in m,
|
|
10
|
-
TextDecoder: "TextDecoder" in m
|
|
10
|
+
TextDecoder: "TextDecoder" in m,
|
|
11
|
+
AbortController: "AbortController" in m
|
|
11
12
|
};
|
|
12
|
-
|
|
13
|
+
function L(t) {
|
|
14
|
+
return t.reduce(
|
|
15
|
+
(e, [n, s]) => (n && (e[n] = s || ""), e),
|
|
16
|
+
{}
|
|
17
|
+
);
|
|
18
|
+
}
|
|
19
|
+
async function ae(t, e, n) {
|
|
13
20
|
var p;
|
|
14
21
|
const s = Object.assign({ method: "GET" }, n), r = w.FormData ? s.body instanceof FormData : !1, a = r && s.method !== "POST" && s.method !== "PUT" ? "POST" : s.method;
|
|
15
22
|
(a === "GET" || a === "HEAD" || a === "DELETE") && s.body !== void 0 && (console.warn("request body is invalid with method get, head, delete"), delete s.body);
|
|
@@ -20,12 +27,12 @@ async function ne(t, e, n) {
|
|
|
20
27
|
s.headers
|
|
21
28
|
), i = s.params || {}, c = {};
|
|
22
29
|
Object.keys(i).forEach((h) => {
|
|
23
|
-
i[h] !== void 0 && (c[h] =
|
|
30
|
+
i[h] !== void 0 && (c[h] = U(i[h]));
|
|
24
31
|
});
|
|
25
|
-
const d = e.getFullUrl(t), l =
|
|
26
|
-
var
|
|
32
|
+
const d = e.getFullUrl(t), l = B(s.body), u = s.timeout || e.get("timeout"), g = await async function() {
|
|
33
|
+
var v;
|
|
27
34
|
const h = e.get("requestTransformer");
|
|
28
|
-
return h ? await h({ headers: o, params: c, method: a, url: d, body: l }) : await ((
|
|
35
|
+
return h ? await h({ headers: o, params: c, method: a, url: d, body: l }) : await ((v = e.get("requestHandler")) == null ? void 0 : v(o, c, a, t));
|
|
29
36
|
}(), y = typeof g == "string" && g ? g : d;
|
|
30
37
|
return (p = e.get("logHandler")) == null || p({ type: "ready", url: y, method: a, headers: o, timeout: u, body: l }), {
|
|
31
38
|
url: y,
|
|
@@ -38,17 +45,17 @@ async function ne(t, e, n) {
|
|
|
38
45
|
credentials: s.credentials || e.get("credentials")
|
|
39
46
|
};
|
|
40
47
|
}
|
|
41
|
-
function
|
|
48
|
+
function U(t) {
|
|
42
49
|
return typeof t == "string" ? t : Array.isArray(t) ? t.join(",") : t + "";
|
|
43
50
|
}
|
|
44
|
-
function
|
|
51
|
+
function B(t) {
|
|
45
52
|
if (t)
|
|
46
53
|
return typeof t == "string" || t instanceof URLSearchParams || t instanceof ArrayBuffer || w.Blob && t instanceof Blob || w.FormData && t instanceof FormData ? t : JSON.stringify(t);
|
|
47
54
|
}
|
|
48
|
-
function
|
|
55
|
+
function P(t) {
|
|
49
56
|
return new Promise((e) => setTimeout(e, Math.max(0, t)));
|
|
50
57
|
}
|
|
51
|
-
function
|
|
58
|
+
function H() {
|
|
52
59
|
}
|
|
53
60
|
function R(t, e) {
|
|
54
61
|
try {
|
|
@@ -59,11 +66,11 @@ function R(t, e) {
|
|
|
59
66
|
}
|
|
60
67
|
}
|
|
61
68
|
"" + Math.random().toString(32).slice(2);
|
|
62
|
-
const
|
|
69
|
+
const S = /^(?:https?:)?\/\/.+$/i, K = /^https?:\/\/.+$/i, I = /^\{[\d\D]*\}$/, N = /^\[[\d\D]*\]$/;
|
|
63
70
|
function $(t, e = !1) {
|
|
64
|
-
return e ?
|
|
71
|
+
return e ? S.test(t) : K.test(t);
|
|
65
72
|
}
|
|
66
|
-
function
|
|
73
|
+
function q(t) {
|
|
67
74
|
return t == null;
|
|
68
75
|
}
|
|
69
76
|
function M(t, ...e) {
|
|
@@ -73,9 +80,9 @@ function M(t, ...e) {
|
|
|
73
80
|
return n !== Object.prototype && n !== null ? !1 : e.every((s) => s in t);
|
|
74
81
|
}
|
|
75
82
|
function D(t) {
|
|
76
|
-
return
|
|
83
|
+
return I.test(t) || N.test(t);
|
|
77
84
|
}
|
|
78
|
-
async function
|
|
85
|
+
async function re(t) {
|
|
79
86
|
return await new Promise(function(e) {
|
|
80
87
|
const n = document.getElementsByTagName("head")[0], s = document.createElement("script");
|
|
81
88
|
s.setAttribute("type", "text/javascript"), s.setAttribute("charset", "utf-8"), s.onload = function() {
|
|
@@ -85,7 +92,7 @@ async function ae(t) {
|
|
|
85
92
|
}, s.setAttribute("src", t), n.appendChild(s);
|
|
86
93
|
});
|
|
87
94
|
}
|
|
88
|
-
function
|
|
95
|
+
function W(t, e = "数据未能正确识别") {
|
|
89
96
|
return typeof t == "function" ? {
|
|
90
97
|
guard: t,
|
|
91
98
|
message: e
|
|
@@ -94,13 +101,13 @@ function q(t, e = "数据未能正确识别") {
|
|
|
94
101
|
message: t.message || e
|
|
95
102
|
};
|
|
96
103
|
}
|
|
97
|
-
function
|
|
104
|
+
function O(t, e = "") {
|
|
98
105
|
return !e || $(t, !0) ? T(t) : (T(e) + "/" + t).replace(/\/{2,}/g, "/").replace(/:\//, "://");
|
|
99
106
|
}
|
|
100
107
|
function T(t) {
|
|
101
108
|
return $(t, !0) ? t.startsWith("http") ? t : ("location" in globalThis ? location.protocol : "https:") + t : ("location" in globalThis ? location.origin : "http://127.0.0.1") + "/" + t.replace(/^\/+/, "");
|
|
102
109
|
}
|
|
103
|
-
function
|
|
110
|
+
function G(t, e) {
|
|
104
111
|
if (e) {
|
|
105
112
|
if (e === !0)
|
|
106
113
|
return t.replace(/\?[^#]*/, "");
|
|
@@ -109,24 +116,24 @@ function V(t, e) {
|
|
|
109
116
|
const n = t.split("#"), s = n[0].split("?"), r = s[0], a = s.length > 1 ? s[1] : "", o = n.length > 1 ? "#" + n[1] : "", i = typeof e == "string" ? [e] : Array.isArray(e) ? e : [];
|
|
110
117
|
return !i.length || !a ? n[0] + o : (i.map((c) => c.replace(/([\\(){}[\]^$+\-*?|])/g, "\\$1")), (r + "?" + a.replace(new RegExp("(?:^|&)(?:" + i.join("|") + ")=[^&$]+", "g"), "").replace(/^&/, "")).replace(/\?$/, "") + o);
|
|
111
118
|
}
|
|
112
|
-
function
|
|
119
|
+
function oe(t, e, n = !1) {
|
|
113
120
|
const s = typeof e == "string" ? e : Object.keys(e).map((o) => `${o}=${encodeURIComponent(e[o])}`).join("&");
|
|
114
121
|
if (!s)
|
|
115
122
|
return t;
|
|
116
123
|
const r = t.split("#");
|
|
117
|
-
n && (r[0] =
|
|
124
|
+
n && (r[0] = G(
|
|
118
125
|
r[0],
|
|
119
126
|
(s.match(/([^=&#?]+)=[^&#]+/g) || []).map((o) => o.replace(/=.+$/, ""))
|
|
120
127
|
));
|
|
121
128
|
const a = r[0].indexOf("?") + 1 ? "&" : "?";
|
|
122
129
|
return (r[0] + a + s + (r.length > 1 ? "#" + r[1] : "")).replace(/\?&/, "?");
|
|
123
130
|
}
|
|
124
|
-
const
|
|
125
|
-
function
|
|
131
|
+
const C = "data", b = "message";
|
|
132
|
+
function J(t, e, n, s, r) {
|
|
126
133
|
const a = r || s;
|
|
127
|
-
return F(t) ?
|
|
134
|
+
return F(t) ? Y(a.ok || s.ok, t, e, n) : _(a.failed || s.failed, e, n);
|
|
128
135
|
}
|
|
129
|
-
const
|
|
136
|
+
const ie = function(t) {
|
|
130
137
|
const e = [], n = t.failed || { resolve: "json" };
|
|
131
138
|
switch (e.push("- 当http状态码 <200 或者 >=400 时"), n.resolve) {
|
|
132
139
|
case "body":
|
|
@@ -143,14 +150,14 @@ const oe = function(t) {
|
|
|
143
150
|
break;
|
|
144
151
|
case "json":
|
|
145
152
|
e.push(
|
|
146
|
-
" 将响应解析为 json,读取 " + (s.dataField ||
|
|
153
|
+
" 将响应解析为 json,读取 " + (s.dataField || C) + " 作为响应数据,读取 " + (s.messageField || b) + " 作为提示消息"
|
|
147
154
|
), s.statusField && e.push(" 当 " + s.statusField + " 为 " + (s.statusOKValue || "空值") + " 时是成功提示,否则是错误消息"), s.ignoreMessage && e.push(" 并忽略以下消息:" + s.ignoreMessage);
|
|
148
155
|
break;
|
|
149
156
|
}
|
|
150
157
|
return e.join(`
|
|
151
158
|
`);
|
|
152
159
|
};
|
|
153
|
-
function
|
|
160
|
+
function _(t, e, n) {
|
|
154
161
|
const s = t || { resolve: "json", messageField: b }, r = {
|
|
155
162
|
ok: !1,
|
|
156
163
|
code: e,
|
|
@@ -162,13 +169,13 @@ function J(t, e, n) {
|
|
|
162
169
|
r.message = k(n) || n;
|
|
163
170
|
break;
|
|
164
171
|
case "json":
|
|
165
|
-
const { code: a, message: o } =
|
|
172
|
+
const { code: a, message: o } = V(n, s.statusField, s.messageField);
|
|
166
173
|
r.code = a || e, r.message = k(n) || o;
|
|
167
174
|
break;
|
|
168
175
|
}
|
|
169
176
|
return r;
|
|
170
177
|
}
|
|
171
|
-
function
|
|
178
|
+
function V(t, e, n = b) {
|
|
172
179
|
if (!D(t))
|
|
173
180
|
return { message: "" };
|
|
174
181
|
const s = R(t);
|
|
@@ -181,21 +188,21 @@ function j(t, e) {
|
|
|
181
188
|
const n = Array.isArray(e) ? e : [e];
|
|
182
189
|
for (const s of n)
|
|
183
190
|
if (s in t)
|
|
184
|
-
return
|
|
191
|
+
return z(t[s]);
|
|
185
192
|
return "";
|
|
186
193
|
}
|
|
187
|
-
function
|
|
194
|
+
function z(t) {
|
|
188
195
|
return t ? typeof t == "string" ? t : JSON.stringify(t) : "";
|
|
189
196
|
}
|
|
190
|
-
const
|
|
197
|
+
const Q = /<title>([^<]+)<\/title>/i, X = /<message>([^<]+)<\/message>/i;
|
|
191
198
|
function k(t) {
|
|
192
|
-
const e = t.match(
|
|
199
|
+
const e = t.match(Q);
|
|
193
200
|
if (e)
|
|
194
201
|
return e[1];
|
|
195
|
-
const n = t.match(
|
|
202
|
+
const n = t.match(X);
|
|
196
203
|
return n ? n[1] : "";
|
|
197
204
|
}
|
|
198
|
-
function
|
|
205
|
+
function Y(t, e, n, s) {
|
|
199
206
|
const r = t || { resolve: "body" }, a = {
|
|
200
207
|
ok: !0,
|
|
201
208
|
code: n,
|
|
@@ -209,7 +216,7 @@ function X(t, e, n, s) {
|
|
|
209
216
|
const o = R(s);
|
|
210
217
|
if (!o || !M(o))
|
|
211
218
|
return a.ok = !1, a.code = "ResponseFormatError", a.message = "响应内容无法格式化为 Object", a;
|
|
212
|
-
const i = r.statusField, c = r.statusOKValue || "", d = r.dataField ||
|
|
219
|
+
const i = r.statusField, c = r.statusOKValue || "", d = r.dataField || C, l = r.messageField || b, u = r.ignoreMessage || "";
|
|
213
220
|
if (i && !(i in o))
|
|
214
221
|
return a.ok = !1, a.code = "ResponseFieldMissing", a.message = "响应内容找不到状态字段 " + i, a;
|
|
215
222
|
const g = i ? o[i] + "" : "";
|
|
@@ -218,8 +225,8 @@ function X(t, e, n, s) {
|
|
|
218
225
|
function F(t) {
|
|
219
226
|
return t >= 200 && t < 400;
|
|
220
227
|
}
|
|
221
|
-
async function
|
|
222
|
-
const a = r || 0, o = Math.max(0, Math.min(10, (s == null ? void 0 : s.maxRetry) ?? n.get("maxRetry") ?? 0)), i = (s == null ? void 0 : s.retryResolve) ?? n.get("retryResolve"), c = n.get("logHandler") ||
|
|
228
|
+
async function Z(t, e, n, s, r) {
|
|
229
|
+
const a = r || 0, o = Math.max(0, Math.min(10, (s == null ? void 0 : s.maxRetry) ?? n.get("maxRetry") ?? 0)), i = (s == null ? void 0 : s.retryResolve) ?? n.get("retryResolve"), c = n.get("logHandler") || H;
|
|
223
230
|
c({
|
|
224
231
|
type: "prepear",
|
|
225
232
|
url: e,
|
|
@@ -246,14 +253,14 @@ async function Y(t, e, n, s, r) {
|
|
|
246
253
|
if (!o || i === "network" && u > 0 || i === "status" && p || Array.isArray(i) && !i.includes(u) || typeof i == "function" && i(l, a) !== !0 || a >= o)
|
|
247
254
|
return l;
|
|
248
255
|
const h = (s == null ? void 0 : s.retryInterval) ?? n.get("retryInterval") ?? 1e3;
|
|
249
|
-
return await
|
|
256
|
+
return await P(
|
|
250
257
|
Math.max(
|
|
251
258
|
100,
|
|
252
259
|
h === "2EB" ? Math.pow(2, a) * 1e3 : typeof h == "function" ? h(a + 1) || 0 : h
|
|
253
260
|
)
|
|
254
|
-
), await
|
|
261
|
+
), await Z(t, e, n, s, a + 1);
|
|
255
262
|
}
|
|
256
|
-
function
|
|
263
|
+
function ce(t, e, n, s) {
|
|
257
264
|
var o, i;
|
|
258
265
|
if (t.status < 0)
|
|
259
266
|
return E(
|
|
@@ -266,9 +273,9 @@ function ie(t, e, n, s) {
|
|
|
266
273
|
);
|
|
267
274
|
F(t.status) || (o = n.get("errorHandler")) == null || o(t.status, t.method, e);
|
|
268
275
|
const r = {
|
|
269
|
-
...
|
|
276
|
+
...J(t.status, t.statusText, t.body, n.get("responseRule"), s == null ? void 0 : s.responseRule),
|
|
270
277
|
status: t.status,
|
|
271
|
-
headers:
|
|
278
|
+
headers: L(Object.entries(t.headers || {}).map(([c, d]) => [c.toLowerCase(), d]))
|
|
272
279
|
};
|
|
273
280
|
(i = n.get("responseHandler")) == null || i({ ...r }, t.method, e);
|
|
274
281
|
const a = r.ok ? r.message : `${t.method} ${e} [${r.code || t.statusText}] ${r.message || t.statusText}`;
|
|
@@ -282,7 +289,7 @@ function E(t, e, n, s, r, a) {
|
|
|
282
289
|
}
|
|
283
290
|
return t;
|
|
284
291
|
}
|
|
285
|
-
class
|
|
292
|
+
class ee {
|
|
286
293
|
constructor(e) {
|
|
287
294
|
// 保存的配置需要部分字段强制设置默认值
|
|
288
295
|
f(this, "config", {
|
|
@@ -315,14 +322,14 @@ class Z {
|
|
|
315
322
|
}
|
|
316
323
|
/** 基于 baseURL 返回完整的 url 地址, 如果 url 地址以 / 开头则表示忽略 baseURL 的值 */
|
|
317
324
|
getFullUrl(e) {
|
|
318
|
-
return e.startsWith("/") ?
|
|
325
|
+
return e.startsWith("/") ? O(e) : O(e, this.config.baseURL);
|
|
319
326
|
}
|
|
320
327
|
/** 提示消息 */
|
|
321
328
|
showMessage(e, n) {
|
|
322
329
|
this.config.messageHandler && n && this.config.messageHandler(e, n);
|
|
323
330
|
}
|
|
324
331
|
}
|
|
325
|
-
class
|
|
332
|
+
class te {
|
|
326
333
|
constructor(e = 500) {
|
|
327
334
|
f(this, "ttl");
|
|
328
335
|
f(this, "cache");
|
|
@@ -348,19 +355,19 @@ class ee {
|
|
|
348
355
|
});
|
|
349
356
|
}
|
|
350
357
|
}
|
|
351
|
-
function
|
|
352
|
-
if (e.ok && !
|
|
353
|
-
const r =
|
|
358
|
+
function se(t, e, n, s) {
|
|
359
|
+
if (e.ok && !q(e.data) && s) {
|
|
360
|
+
const r = W(s, "响应数据未能正确识别");
|
|
354
361
|
return r.guard(e.data) || (console.error("ResponseCheckFaild", t, e.data), n.showMessage(!0, `${t} ${r.message}`), e.data = null), e;
|
|
355
362
|
}
|
|
356
363
|
return e;
|
|
357
364
|
}
|
|
358
|
-
class
|
|
365
|
+
class ue {
|
|
359
366
|
constructor(e, n) {
|
|
360
367
|
f(this, "agent");
|
|
361
368
|
f(this, "config");
|
|
362
369
|
f(this, "cache");
|
|
363
|
-
this.config = new
|
|
370
|
+
this.config = new ee(n), this.agent = e, this.cache = new te(this.config.get("cacheTTL")), this.setConfig = this.setConfig.bind(this), this.getConfig = this.getConfig.bind(this), this.get = this.get.bind(this), this.post = this.post.bind(this), this.del = this.del.bind(this), this.patch = this.patch.bind(this), this.put = this.put.bind(this), this.head = this.head.bind(this);
|
|
364
371
|
}
|
|
365
372
|
/**
|
|
366
373
|
* 执行网络请求
|
|
@@ -372,7 +379,7 @@ class ce {
|
|
|
372
379
|
* 检查响应的数据类型
|
|
373
380
|
*/
|
|
374
381
|
async guard(e, n, s) {
|
|
375
|
-
return
|
|
382
|
+
return se(e, n, this.config, s);
|
|
376
383
|
}
|
|
377
384
|
/**
|
|
378
385
|
* 修改默认请求配置: baesURL / timeout / credentials / errorHandler / messageHandler / responseHandler / logHandler / responseRule
|
|
@@ -419,18 +426,19 @@ class ce {
|
|
|
419
426
|
return a.method = "PATCH", a.body = n, this.guard(e, await this.exec(e, a), s || null);
|
|
420
427
|
}
|
|
421
428
|
}
|
|
422
|
-
const
|
|
429
|
+
const le = "1.6.3";
|
|
423
430
|
export {
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
431
|
+
oe as E,
|
|
432
|
+
ue as N,
|
|
433
|
+
ee as R,
|
|
427
434
|
w as S,
|
|
428
435
|
$ as U,
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
ie as
|
|
434
|
-
|
|
435
|
-
|
|
436
|
+
H as W,
|
|
437
|
+
re as a,
|
|
438
|
+
ae as c,
|
|
439
|
+
L as f,
|
|
440
|
+
ie as g,
|
|
441
|
+
ce as h,
|
|
442
|
+
Z as r,
|
|
443
|
+
le as v
|
|
436
444
|
};
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const version = "1.6.
|
|
1
|
+
export declare const version = "1.6.3";
|
package/dist/wx.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./version-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./version-Dd1gKjL_.cjs"),p=async function(t,r,n){return e.handleResponse(await e.retryRequest(l,t,r,n),t,r,n)},l=async function(t,r,n){const o=await e.convertOptions(t,r,n),u=o.method==="PATCH"?"POST":o.method,a=e.Et(o.url,o.params);return e.Support.wx?new Promise(d=>{wx.request({url:a,data:o.body,header:o.headers,method:u,dataType:"string",responseType:"text",fail(){d({url:a,method:u,status:-1,statusText:"NetworkError",body:""})},success(c){d({url:a,method:u,status:c.statusCode,statusText:c.statusCode+"",headers:{...c.header},body:o.method==="HEAD"?"":f(c.data)})}})}):{url:a,method:u,status:-1,statusText:"NotSupport",body:"NotFound namespace of wx"}};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(p,t)}const s=i(),y=s.setConfig,g=s.head,h=s.get,x=s.post,w=s.del,T=s.put;exports.version=e.version;exports.NetRequest=i;exports.del=w;exports.get=h;exports.head=g;exports.post=x;exports.put=T;exports.setGlobalConfig=y;
|
package/dist/wx.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { h as i, r as p, c as f, E as l, S as d, N as x } from "./version-
|
|
2
|
-
import { v as A } from "./version-
|
|
1
|
+
import { h as i, r as p, c as f, E as l, S as d, N as x } from "./version-imlkbI1X.js";
|
|
2
|
+
import { v as A } from "./version-imlkbI1X.js";
|
|
3
3
|
const h = async function(t, o, r) {
|
|
4
4
|
return i(await p(y, t, o, r), t, o, r);
|
|
5
5
|
}, y = async function(t, o, r) {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seayoo-web/request",
|
|
3
|
-
"version": "1.6.
|
|
3
|
+
"version": "1.6.3",
|
|
4
4
|
"description": "requst tools for seayoo web",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -82,6 +82,6 @@
|
|
|
82
82
|
"vitest": "^1.4.0"
|
|
83
83
|
},
|
|
84
84
|
"dependencies": {
|
|
85
|
-
"@seayoo-web/utils": "^1.16.
|
|
85
|
+
"@seayoo-web/utils": "^1.16.4"
|
|
86
86
|
}
|
|
87
87
|
}
|
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";var L=Object.defineProperty;var B=(t,e,n)=>e in t?L(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var f=(t,e,n)=>(B(t,typeof e!="symbol"?e+"":e,n),n);const m=typeof globalThis<"u"?globalThis:typeof global<"u"?global:typeof window<"u"?window:{},b={fetch:"fetch"in m,window:"window"in m,FormData:"FormData"in m,Blob:"Blob"in m,wx:"wx"in m,TextDecoder:"TextDecoder"in m};async function H(t,e,n){var w;const s=Object.assign({method:"GET"},n),r=b.FormData?s.body instanceof FormData:!1,a=r&&s.method!=="POST"&&s.method!=="PUT"?"POST":s.method;(a==="GET"||a==="HEAD"||a==="DELETE")&&s.body!==void 0&&(console.warn("request body is invalid with method get, head, delete"),delete s.body);const o=Object.assign(r?{}:{"Content-Type":b.Blob&&s.body instanceof Blob?s.body.type||"application/octet-stream":"application/json;charset=utf-8"},s.headers),i=s.params||{},c={};Object.keys(i).forEach(h=>{i[h]!==void 0&&(c[h]=P(i[h]))});const d=e.getFullUrl(t),l=S(s.body),u=s.timeout||e.get("timeout"),g=await async function(){var v;const h=e.get("requestTransformer");return h?await h({headers:o,params:c,method:a,url:d,body:l}):await((v=e.get("requestHandler"))==null?void 0:v(o,c,a,t))}(),y=typeof g=="string"&&g?g:d;return(w=e.get("logHandler"))==null||w({type:"ready",url:y,method:a,headers:o,timeout:u,body:l}),{url:y,method:a,body:l,params:c,headers:o,timeout:u,abort:s.abort,credentials:s.credentials||e.get("credentials")}}function P(t){return typeof t=="string"?t:Array.isArray(t)?t.join(","):t+""}function S(t){if(t)return typeof t=="string"||t instanceof URLSearchParams||t instanceof ArrayBuffer||b.Blob&&t instanceof Blob||b.FormData&&t instanceof FormData?t:JSON.stringify(t)}function q(t){return new Promise(e=>setTimeout(e,Math.max(0,t)))}function M(){}function j(t,e){try{const n=JSON.parse(t);return e?e(n)?n:null:n}catch{return null}}""+Math.random().toString(32).slice(2);const K=/^(?:https?:)?\/\/.+$/i,I=/^https?:\/\/.+$/i,N=/^\{[\d\D]*\}$/,V=/^\[[\d\D]*\]$/;function R(t,e=!1){return e?K.test(t):I.test(t)}function G(t){return t==null}function D(t,...e){if(!t||typeof t!="object")return!1;const n=Object.getPrototypeOf(t);return n!==Object.prototype&&n!==null?!1:e.every(s=>s in t)}function C(t){return N.test(t)||V.test(t)}async function J(t){return await new Promise(function(e){const n=document.getElementsByTagName("head")[0],s=document.createElement("script");s.setAttribute("type","text/javascript"),s.setAttribute("charset","utf-8"),s.onload=function(){n.removeChild(s),e(!0)},s.onerror=function(){n.removeChild(s),e(!1)},s.setAttribute("src",t),n.appendChild(s)})}function _(t,e="数据未能正确识别"){return typeof t=="function"?{guard:t,message:e}:{guard:t.guard,message:t.message||e}}function F(t,e=""){return!e||R(t,!0)?T(t):(T(e)+"/"+t).replace(/\/{2,}/g,"/").replace(/:\//,"://")}function T(t){return R(t,!0)?t.startsWith("http")?t:("location"in globalThis?location.protocol:"https:")+t:("location"in globalThis?location.origin:"http://127.0.0.1")+"/"+t.replace(/^\/+/,"")}function W(t,e){if(e){if(e===!0)return t.replace(/\?[^#]*/,"")}else return t;const n=t.split("#"),s=n[0].split("?"),r=s[0],a=s.length>1?s[1]:"",o=n.length>1?"#"+n[1]:"",i=typeof e=="string"?[e]:Array.isArray(e)?e:[];return!i.length||!a?n[0]+o:(i.map(c=>c.replace(/([\\(){}[\]^$+\-*?|])/g,"\\$1")),(r+"?"+a.replace(new RegExp("(?:^|&)(?:"+i.join("|")+")=[^&$]+","g"),"").replace(/^&/,"")).replace(/\?$/,"")+o)}function z(t,e,n=!1){const s=typeof e=="string"?e:Object.keys(e).map(o=>`${o}=${encodeURIComponent(e[o])}`).join("&");if(!s)return t;const r=t.split("#");n&&(r[0]=W(r[0],(s.match(/([^=&#?]+)=[^&#]+/g)||[]).map(o=>o.replace(/=.+$/,""))));const a=r[0].indexOf("?")+1?"&":"?";return(r[0]+a+s+(r.length>1?"#"+r[1]:"")).replace(/\?&/,"?")}const x="data",p="message";function Q(t,e,n,s,r){const a=r||s;return $(t)?ne(a.ok||s.ok,t,e,n):Y(a.failed||s.failed,e,n)}const X=function(t){const e=[],n=t.failed||{resolve:"json"};switch(e.push("- 当http状态码 <200 或者 >=400 时"),n.resolve){case"body":e.push(" 将响应内容格式化为字符串并作为错误消息");break;case"json":e.push(" 将响应解析为json,并读取 "+(n.messageField||p)+" 作为错误消息");break}const s=t.ok||{resolve:"body"};switch(e.push("- 当http状态码 >=200 并且 <400 时"),s.resolve){case"body":e.push(" 将响应尝试解析为 json,并作为数据内容返回");break;case"json":e.push(" 将响应解析为 json,读取 "+(s.dataField||x)+" 作为响应数据,读取 "+(s.messageField||p)+" 作为提示消息"),s.statusField&&e.push(" 当 "+s.statusField+" 为 "+(s.statusOKValue||"空值")+" 时是成功提示,否则是错误消息"),s.ignoreMessage&&e.push(" 并忽略以下消息:"+s.ignoreMessage);break}return e.join(`
|
|
2
|
-
`)};function Y(t,e,n){const s=t||{resolve:"json",messageField:p},r={ok:!1,code:e,message:n,data:null};switch(s.resolve){case"body":r.message=k(n)||n;break;case"json":const{code:a,message:o}=Z(n,s.statusField,s.messageField);r.code=a||e,r.message=k(n)||o;break}return r}function Z(t,e,n=p){if(!C(t))return{message:""};const s=j(t);return!s||!D(s)?{message:t}:{code:e?O(s,e):"",message:O(s,n)||t}}function O(t,e){const n=Array.isArray(e)?e:[e];for(const s of n)if(s in t)return ee(t[s]);return""}function ee(t){return t?typeof t=="string"?t:JSON.stringify(t):""}const te=/<title>([^<]+)<\/title>/i,se=/<message>([^<]+)<\/message>/i;function k(t){const e=t.match(te);if(e)return e[1];const n=t.match(se);return n?n[1]:""}function ne(t,e,n,s){const r=t||{resolve:"body"},a={ok:!0,code:n,message:"",data:null};if(e===202||e===204||!s)return a;if(r.resolve==="body")return a.data=C(s)?j(s):s,a;const o=j(s);if(!o||!D(o))return a.ok=!1,a.code="ResponseFormatError",a.message="响应内容无法格式化为 Object",a;const i=r.statusField,c=r.statusOKValue||"",d=r.dataField||x,l=r.messageField||p,u=r.ignoreMessage||"";if(i&&!(i in o))return a.ok=!1,a.code="ResponseFieldMissing",a.message="响应内容找不到状态字段 "+i,a;const g=i?o[i]+"":"";return a.ok=i?g===c:!0,a.code=g||n,a.data=d===!0?o:d in o?o[d]:null,a.message=O(o,l),u&&a.message&&(Array.isArray(u)&&u.includes(a.message)||typeof u=="string"&&a.message===u)&&(a.message=""),a}function $(t){return t>=200&&t<400}async function A(t,e,n,s,r){const a=r||0,o=Math.max(0,Math.min(10,(s==null?void 0:s.maxRetry)??n.get("maxRetry")??0)),i=(s==null?void 0:s.retryResolve)??n.get("retryResolve"),c=n.get("logHandler")||M;c({type:"prepear",url:e,method:(s==null?void 0:s.method)||"GET",retry:a,maxRetry:o,message:a===0?"start":`retry ${a}/${o} start`,headers:s==null?void 0:s.headers,options:s});const d=Date.now(),l=await t(e,n,s),u=l.status,g=Date.now()-d,y=`[cost ${g}][${u}] ${u<0?l.body:""}`;c({type:"finished",url:e,method:l.method,retry:a,maxRetry:o,message:a===0?`finish ${y}`:`retry ${a}/${o} finish ${y}`,response:l,headers:l.headers,cost:g});const w=$(u);if(!o||i==="network"&&u>0||i==="status"&&w||Array.isArray(i)&&!i.includes(u)||typeof i=="function"&&i(l,a)!==!0||a>=o)return l;const h=(s==null?void 0:s.retryInterval)??n.get("retryInterval")??1e3;return await q(Math.max(100,h==="2EB"?Math.pow(2,a)*1e3:typeof h=="function"?h(a+1)||0:h)),await A(t,e,n,s,a+1)}function ae(t,e,n,s){var o,i;if(t.status<0)return E({ok:!1,status:t.status,code:t.statusText,headers:{},message:"",data:null},`${t.method} ${e} ${t.statusText}`,t.method,e,n,s);$(t.status)||(o=n.get("errorHandler"))==null||o(t.status,t.method,e);const r={...Q(t.status,t.statusText,t.body,n.get("responseRule"),s==null?void 0:s.responseRule),status:t.status,headers:Object.fromEntries(Object.entries(t.headers||{}).map(([c,d])=>[c.toLowerCase(),d]))};(i=n.get("responseHandler"))==null||i({...r},t.method,e);const a=r.ok?r.message:`${t.method} ${e} [${r.code||t.statusText}] ${r.message||t.statusText}`;return E(r,a,t.method,e,n,s)}function E(t,e,n,s,r,a){const o=r.get("message"),i=o===!1||(a==null?void 0:a.message)===!1?!1:(a==null?void 0:a.message)||o;if(i!==!1){const c=typeof i=="function"?i(t,n,s,e):e;c instanceof Error?r.showMessage(!0,c.message):c&&typeof c=="object"&&"message"in c?r.showMessage(!1,c.message):r.showMessage(!t.ok,c)}return t}class U{constructor(e){f(this,"config",{baseURL:"",maxRetry:0,retryInterval:1e3,retryResolve:"network",timeout:5e3,cacheTTL:500,credentials:"same-origin",responseRule:{ok:{resolve:"body"},failed:{resolve:"json",messageField:"message"}}});e&&this.set(e)}set(e){if(e.baseURL&&!/^\/.+/.test(e.baseURL)&&!R(e.baseURL))throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"),new Error("BaseURLError");Object.assign(this.config,e)}get(e){return this.config[e]}getFullUrl(e){return e.startsWith("/")?F(e):F(e,this.config.baseURL)}showMessage(e,n){this.config.messageHandler&&n&&this.config.messageHandler(e,n)}}class re{constructor(e=500){f(this,"ttl");f(this,"cache");this.cache={},this.ttl=Math.max(e,0)}getKey(e,n){const s=Object.keys(n||{}).sort().map(r=>`${r}#${n==null?void 0:n[r]}`);return e+s.join(",")}updateTTL(e){this.ttl=Math.max(e,0)}get(e){if(this.ttl===0)return null;const n=this.cache[e];return n?n.ttl<Date.now()?(delete this.cache[e],null):n.res:null}set(e,n){this.ttl!==0&&(this.cache[e]={ttl:Date.now()+this.ttl,res:n})}}function oe(t,e,n,s){if(e.ok&&!G(e.data)&&s){const r=_(s,"响应数据未能正确识别");return r.guard(e.data)||(console.error("ResponseCheckFaild",t,e.data),n.showMessage(!0,`${t} ${r.message}`),e.data=null),e}return e}class ie{constructor(e,n){f(this,"agent");f(this,"config");f(this,"cache");this.config=new U(n),this.agent=e,this.cache=new re(this.config.get("cacheTTL")),this.setConfig=this.setConfig.bind(this),this.getConfig=this.getConfig.bind(this),this.get=this.get.bind(this),this.post=this.post.bind(this),this.del=this.del.bind(this),this.patch=this.patch.bind(this),this.put=this.put.bind(this),this.head=this.head.bind(this)}async exec(e,n){return await this.agent(e,this.config,n)}async guard(e,n,s){return oe(e,n,this.config,s)}setConfig(e){this.config.set(e),this.cache.updateTTL(this.config.get("cacheTTL"))}getConfig(e){return this.config.get(e)}async head(e,n){const s=Object.assign({},n||null);return s.method="HEAD",this.guard(e,await this.exec(e,s),null)}async get(e,n,s){const r=Object.assign({},s||null);r.method="GET";const a=this.cache.getKey(e,r.params),o=this.cache.get(a);if(o)return this.guard(e,await o,n||null);const i=this.exec(e,r);return this.cache.set(a,i),this.guard(e,await i,n||null)}async post(e,n,s,r){const a=Object.assign({},r||null);return a.method="POST",a.body=n,this.guard(e,await this.exec(e,a),s||null)}async del(e,n,s){const r=Object.assign({},s||null);return r.method="DELETE",this.guard(e,await this.exec(e,r),n||null)}async put(e,n,s,r){const a=Object.assign({},r||null);return a.method="PUT",a.body=n,this.guard(e,await this.exec(e,a),s||null)}async patch(e,n,s,r){const a=Object.assign({},r||null);return a.method="PATCH",a.body=n,this.guard(e,await this.exec(e,a),s||null)}}const ce="1.6.1";exports.Et=z;exports.NetRequestHandler=ie;exports.RequestGlobalConfig=U;exports.Support=b;exports.U=R;exports.Vt=M;exports.convertOptions=H;exports.getResponseRulesDescription=X;exports.handleResponse=ae;exports.he=J;exports.retryRequest=A;exports.version=ce;
|