@seayoo-web/request 1.6.0 → 1.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/inc/detect.d.ts +9 -0
- package/dist/index.cjs +2 -2
- package/dist/index.js +83 -83
- package/dist/node.cjs +1 -1
- package/dist/node.js +9 -9
- package/dist/version-D6D6EXmg.cjs +2 -0
- package/dist/version-D9usx_Q7.js +437 -0
- package/dist/version.d.ts +1 -1
- package/dist/wx.cjs +1 -1
- package/dist/wx.js +17 -17
- package/package.json +8 -8
- package/dist/version-CLBYgaXw.js +0 -427
- package/dist/version-DJIdoURF.cjs +0 -2
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("./version-
|
|
2
|
-
`).forEach(e=>{const r=e.trim();if(!r)return;const i=r.split(":"),
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("./version-D6D6EXmg.cjs"),E=async function(n,s,t){const e=await c.convertOptions(n,s,t),r=new URL(e.url),i=e.params;i instanceof Object&&Object.keys(i).forEach(a=>r.searchParams.set(a,i[a]));const o=c.Support.AbortController?new AbortController:null;function l(){o&&!o.signal.aborted&&o.abort()}e.abort&&e.abort.addEventListener("abort",l);const b=e.timeout>0?setTimeout(l,e.timeout):null,h=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(h).then(async a=>({url:r.toString(),method:e.method,status:a.status,statusText:a.statusText,headers:Object.fromEntries(a.headers.entries()),body:e.method==="HEAD"?"":await a.text()})).catch(a=>({url:r.toString(),method:e.method,status:-1,statusText:o!=null&&o.signal.aborted?"Aborted":"NetworkError",body:String(a)})).finally(()=>{b!==null&&clearTimeout(b),e.abort&&e.abort.removeEventListener("abort",l)})},m=async function(n,s,t){const e={...await c.convertOptions(n,s,t),onUploadProgress:t==null?void 0:t.onUploadProgress},r=e.method,i=e.onUploadProgress||c.Wt,o=c.Et(e.url,e.params);return await new Promise(l=>{let b=1;function h(w){b=w.total,i({total:w.total,loaded:w.loaded})}let a=null,f=!1;const g=function(){f||(d.abort(),f=!0)};function p(){a!==null&&clearTimeout(a),e.abort&&e.abort.removeEventListener("abort",g)}const d=new XMLHttpRequest;d.open(r,o,!0),d.upload.addEventListener("progress",h),d.addEventListener("load",()=>{p(),i({loaded:b,total:b}),l({url:o,method:r,status:d.status,statusText:d.statusText,headers:T(d),body:r==="HEAD"?"":d.responseText})}),d.addEventListener("error",()=>{p(),l({url:o,method:r,status:-1,statusText:"Failed",body:""})}),d.addEventListener("abort",()=>{p(),l({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&&(a=setTimeout(g,e.timeout))})};function T(n){const s={};if(!n)return s;const t=n.getAllResponseHeaders();return t&&t!=="null"&&t.replace(/\r/g,"").split(`
|
|
2
|
+
`).forEach(e=>{const r=e.trim();if(!r)return;const i=r.split(":"),o=i[0].trim();o&&(s[o]=(i[1]||"").trim())}),s}const q=async function(n,s,t){return c.handleResponse(await c.retryRequest(E,n,s,t),n,s,t)},H=async function(n,s,t){return c.handleResponse(await c.retryRequest(m,n,s,t),n,s,t)};async function S(n,s,t,e){const r=t==null?void 0:t.body,i=(t==null?void 0:t.method)==="PUT"?"PUT":"POST";if(s instanceof Blob){const a=new c.RequestGlobalConfig(e),f=await m(n,a,{...t,method:i,body:s});return c.handleResponse(f,n,a,t)}const o=new FormData,l={...s};r instanceof Object&&Object.entries(r).forEach(([a,f])=>{f instanceof Blob?l[a]=f:Array.isArray(f)?f.forEach((g,p)=>{o.append(`${a}[${p}]`,String(g))}):o.append(a,String(f))});for(const a in l)o.append(a,l[a]);const b=new c.RequestGlobalConfig(e),h=await m(n,b,{...t,method:i,body:o});return c.handleResponse(h,n,b,t)}async function x(n,s,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 i=c.Et(n,t,!0);return new Promise(o=>{e[r]=function(l){if(r in window&&delete e[r],s(l))return l;console.warn("response type check faild",n,l),o(null)},c.he(i).catch(function(){o(null),delete e[r]})})}async function v(n,s,t={}){const e=window;return"var"in t||(t.var="jsonxData"+Math.random().toString(16).slice(2)),n?await c.he(c.Et(n,t,!0)).then(()=>{const r=e[t.var+""];return s(r)?r:(console.warn("response type check faild",n,r),null)}).catch(()=>null):null}const C=async function(n,s,t){return await S(n,s,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(!c.Support.window)throw new Error("Default Module Only Support In Browser");return c.Support.fetch?new c.NetRequestHandler(q,n):new c.NetRequestHandler(H,n)}const u=y(),j=u.setConfig,L=u.head,U=u.get,O=u.post,P=u.del,A=u.put,D=u.patch;exports.getResponseRulesDescription=c.getResponseRulesDescription;exports.version=c.version;exports.NetRequest=y;exports.del=P;exports.get=U;exports.head=L;exports.jsonp=x;exports.jsonx=v;exports.patch=D;exports.post=O;exports.put=A;exports.setGlobalConfig=j;exports.upload=C;
|
package/dist/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { c as
|
|
2
|
-
import { g as J, v as K } from "./version-
|
|
1
|
+
import { c as H, S as y, E as R, W as S, h as w, r as x, R as E, a as q, N as T } from "./version-D9usx_Q7.js";
|
|
2
|
+
import { g as J, v as K } from "./version-D9usx_Q7.js";
|
|
3
3
|
const C = async function(n, o, e) {
|
|
4
|
-
const t = await
|
|
5
|
-
i instanceof Object && Object.keys(i).forEach((
|
|
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
11
|
const f = t.timeout > 0 ? setTimeout(c, t.timeout) : null, b = new Request(r, {
|
|
@@ -13,71 +13,71 @@ const C = async function(n, o, e) {
|
|
|
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(b).then(async (
|
|
19
|
+
return await fetch(b).then(async (s) => ({
|
|
20
20
|
url: r.toString(),
|
|
21
21
|
method: t.method,
|
|
22
|
-
status:
|
|
23
|
-
statusText:
|
|
24
|
-
headers: Object.fromEntries(
|
|
25
|
-
body: t.method === "HEAD" ? "" : await
|
|
26
|
-
})).catch((
|
|
22
|
+
status: s.status,
|
|
23
|
+
statusText: s.statusText,
|
|
24
|
+
headers: Object.fromEntries(s.headers.entries()),
|
|
25
|
+
body: t.method === "HEAD" ? "" : await s.text()
|
|
26
|
+
})).catch((s) => ({
|
|
27
27
|
url: r.toString(),
|
|
28
28
|
method: t.method,
|
|
29
29
|
status: -1,
|
|
30
|
-
statusText:
|
|
31
|
-
body: String(
|
|
30
|
+
statusText: a != null && a.signal.aborted ? "Aborted" : "NetworkError",
|
|
31
|
+
body: String(s)
|
|
32
32
|
})).finally(() => {
|
|
33
33
|
f !== null && clearTimeout(f), t.abort && t.abort.removeEventListener("abort", c);
|
|
34
34
|
});
|
|
35
|
-
},
|
|
35
|
+
}, p = async function(n, o, e) {
|
|
36
36
|
const t = {
|
|
37
|
-
...await
|
|
37
|
+
...await H(n, o, e),
|
|
38
38
|
onUploadProgress: e == null ? void 0 : e.onUploadProgress
|
|
39
|
-
}, r = t.method, i = t.onUploadProgress ||
|
|
39
|
+
}, r = t.method, i = t.onUploadProgress || S, a = R(t.url, t.params);
|
|
40
40
|
return await new Promise((c) => {
|
|
41
41
|
let f = 1;
|
|
42
|
-
function b(
|
|
43
|
-
f =
|
|
42
|
+
function b(m) {
|
|
43
|
+
f = m.total, i({ total: m.total, loaded: m.loaded });
|
|
44
44
|
}
|
|
45
|
-
let
|
|
45
|
+
let s = null, u = !1;
|
|
46
46
|
const h = function() {
|
|
47
|
-
u || (
|
|
47
|
+
u || (d.abort(), u = !0);
|
|
48
48
|
};
|
|
49
49
|
function g() {
|
|
50
|
-
|
|
50
|
+
s !== null && clearTimeout(s), t.abort && t.abort.removeEventListener("abort", h);
|
|
51
51
|
}
|
|
52
|
-
const
|
|
53
|
-
|
|
52
|
+
const d = new XMLHttpRequest();
|
|
53
|
+
d.open(r, a, !0), d.upload.addEventListener("progress", b), d.addEventListener("load", () => {
|
|
54
54
|
g(), i({ loaded: f, total: f }), c({
|
|
55
|
-
url:
|
|
55
|
+
url: a,
|
|
56
56
|
method: r,
|
|
57
|
-
status:
|
|
58
|
-
statusText:
|
|
59
|
-
headers: U(
|
|
60
|
-
body: r === "HEAD" ? "" :
|
|
57
|
+
status: d.status,
|
|
58
|
+
statusText: d.statusText,
|
|
59
|
+
headers: U(d),
|
|
60
|
+
body: r === "HEAD" ? "" : d.responseText
|
|
61
61
|
});
|
|
62
|
-
}),
|
|
62
|
+
}), d.addEventListener("error", () => {
|
|
63
63
|
g(), c({
|
|
64
|
-
url:
|
|
64
|
+
url: a,
|
|
65
65
|
method: r,
|
|
66
66
|
status: -1,
|
|
67
67
|
statusText: "Failed",
|
|
68
68
|
body: ""
|
|
69
69
|
});
|
|
70
|
-
}),
|
|
70
|
+
}), d.addEventListener("abort", () => {
|
|
71
71
|
g(), c({
|
|
72
|
-
url:
|
|
72
|
+
url: a,
|
|
73
73
|
method: r,
|
|
74
74
|
status: -1,
|
|
75
75
|
statusText: "Aborted",
|
|
76
76
|
body: ""
|
|
77
77
|
});
|
|
78
|
-
}), Object.entries(t.headers).forEach(([
|
|
79
|
-
|
|
80
|
-
}), t.credentials === "include" && (
|
|
78
|
+
}), Object.entries(t.headers).forEach(([m, L]) => {
|
|
79
|
+
d.setRequestHeader(m, L);
|
|
80
|
+
}), 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
81
|
});
|
|
82
82
|
};
|
|
83
83
|
function U(n) {
|
|
@@ -90,39 +90,39 @@ function U(n) {
|
|
|
90
90
|
const r = t.trim();
|
|
91
91
|
if (!r)
|
|
92
92
|
return;
|
|
93
|
-
const i = r.split(":"),
|
|
94
|
-
|
|
93
|
+
const i = r.split(":"), a = i[0].trim();
|
|
94
|
+
a && (o[a] = (i[1] || "").trim());
|
|
95
95
|
}), o;
|
|
96
96
|
}
|
|
97
97
|
const v = async function(n, o, e) {
|
|
98
|
-
return
|
|
99
|
-
},
|
|
100
|
-
return
|
|
98
|
+
return w(await x(C, n, o, e), n, o, e);
|
|
99
|
+
}, j = async function(n, o, e) {
|
|
100
|
+
return w(await x(p, n, o, e), n, o, e);
|
|
101
101
|
};
|
|
102
|
-
async function
|
|
102
|
+
async function A(n, o, e, t) {
|
|
103
103
|
const r = e == null ? void 0 : e.body, i = (e == null ? void 0 : e.method) === "PUT" ? "PUT" : "POST";
|
|
104
104
|
if (o instanceof Blob) {
|
|
105
|
-
const
|
|
105
|
+
const s = new E(t), u = await p(n, s, {
|
|
106
106
|
...e,
|
|
107
107
|
method: i,
|
|
108
108
|
body: o
|
|
109
109
|
});
|
|
110
|
-
return
|
|
110
|
+
return w(u, n, s, e);
|
|
111
111
|
}
|
|
112
|
-
const
|
|
113
|
-
r instanceof Object && Object.entries(r).forEach(([
|
|
114
|
-
u instanceof Blob ? c[
|
|
115
|
-
|
|
116
|
-
}) :
|
|
112
|
+
const a = new FormData(), c = { ...o };
|
|
113
|
+
r instanceof Object && Object.entries(r).forEach(([s, u]) => {
|
|
114
|
+
u instanceof Blob ? c[s] = u : Array.isArray(u) ? u.forEach((h, g) => {
|
|
115
|
+
a.append(`${s}[${g}]`, String(h));
|
|
116
|
+
}) : a.append(s, String(u));
|
|
117
117
|
});
|
|
118
|
-
for (const
|
|
119
|
-
|
|
120
|
-
const f = new
|
|
118
|
+
for (const s in c)
|
|
119
|
+
a.append(s, c[s]);
|
|
120
|
+
const f = new E(t), b = await p(n, f, {
|
|
121
121
|
...e,
|
|
122
122
|
method: i,
|
|
123
|
-
body:
|
|
123
|
+
body: a
|
|
124
124
|
});
|
|
125
|
-
return
|
|
125
|
+
return w(b, n, f, e);
|
|
126
126
|
}
|
|
127
127
|
async function k(n, o, e = {}) {
|
|
128
128
|
const t = window;
|
|
@@ -130,52 +130,52 @@ async function k(n, o, e = {}) {
|
|
|
130
130
|
const r = e.callback + "";
|
|
131
131
|
if (!n)
|
|
132
132
|
return null;
|
|
133
|
-
const i =
|
|
134
|
-
return new Promise((
|
|
133
|
+
const i = R(n, e, !0);
|
|
134
|
+
return new Promise((a) => {
|
|
135
135
|
t[r] = function(c) {
|
|
136
136
|
if (r in window && delete t[r], o(c))
|
|
137
137
|
return c;
|
|
138
|
-
console.warn("response type check faild", n, c),
|
|
139
|
-
},
|
|
140
|
-
|
|
138
|
+
console.warn("response type check faild", n, c), a(null);
|
|
139
|
+
}, q(i).catch(function() {
|
|
140
|
+
a(null), delete t[r];
|
|
141
141
|
});
|
|
142
142
|
});
|
|
143
143
|
}
|
|
144
|
-
async function
|
|
144
|
+
async function D(n, o, e = {}) {
|
|
145
145
|
const t = window;
|
|
146
|
-
return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), n ? await
|
|
146
|
+
return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), n ? await q(R(n, e, !0)).then(() => {
|
|
147
147
|
const r = t[e.var + ""];
|
|
148
148
|
return o(r) ? r : (console.warn("response type check faild", n, r), null);
|
|
149
149
|
}).catch(() => null) : null;
|
|
150
150
|
}
|
|
151
|
-
const
|
|
152
|
-
return await
|
|
153
|
-
baseURL:
|
|
154
|
-
logHandler:
|
|
155
|
-
errorHandler:
|
|
156
|
-
requestTransformer:
|
|
157
|
-
messageHandler:
|
|
158
|
-
responseHandler:
|
|
151
|
+
const M = async function(n, o, e) {
|
|
152
|
+
return await A(n, o, e, {
|
|
153
|
+
baseURL: l.getConfig("baseURL"),
|
|
154
|
+
logHandler: l.getConfig("logHandler"),
|
|
155
|
+
errorHandler: l.getConfig("errorHandler"),
|
|
156
|
+
requestTransformer: l.getConfig("requestTransformer"),
|
|
157
|
+
messageHandler: l.getConfig("messageHandler"),
|
|
158
|
+
responseHandler: l.getConfig("responseHandler")
|
|
159
159
|
});
|
|
160
160
|
};
|
|
161
161
|
function O(n) {
|
|
162
|
-
if (
|
|
163
|
-
|
|
164
|
-
|
|
162
|
+
if (!y.window)
|
|
163
|
+
throw new Error("Default Module Only Support In Browser");
|
|
164
|
+
return y.fetch ? new T(v, n) : new T(j, n);
|
|
165
165
|
}
|
|
166
|
-
const
|
|
166
|
+
const l = O(), N = l.setConfig, F = l.head, B = l.get, X = l.post, G = l.del, I = l.put, W = l.patch;
|
|
167
167
|
export {
|
|
168
168
|
O as NetRequest,
|
|
169
|
-
|
|
170
|
-
|
|
169
|
+
G as del,
|
|
170
|
+
B as get,
|
|
171
171
|
J as getResponseRulesDescription,
|
|
172
|
-
|
|
172
|
+
F as head,
|
|
173
173
|
k as jsonp,
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
174
|
+
D as jsonx,
|
|
175
|
+
W as patch,
|
|
176
|
+
X as post,
|
|
177
|
+
I as put,
|
|
178
|
+
N as setGlobalConfig,
|
|
179
|
+
M as upload,
|
|
180
180
|
K as version
|
|
181
181
|
};
|
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"),r=require("./version-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("node:http"),q=require("node:https"),r=require("./version-D6D6EXmg.cjs"),R=async function(s,u,c){return r.handleResponse(await r.retryRequest(w,s,u,c),s,u,c)},w=async function(s,u,c){const t=await r.convertOptions(s,u,c);if(!r.U(t.url))return{url:t.url,method:t.method,status:-1,statusText:"URLFormatError",headers:{},body:""};const b=/^https:\/\//i.test(t.url)?q:y,a=new URL(t.url),i=t.params;i instanceof Object&&Object.keys(i).forEach(n=>a.searchParams.set(n,i[n]));const f=t.method==="HEAD";return new Promise(n=>{const d=b.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=Object.fromEntries(Object.entries(e.headers).map(([g,m])=>[g.toLowerCase(),Array.isArray(m)?m.join(","):m]));n({url:a.toString(),method:t.method,status:e.statusCode||-1,statusText:e.statusMessage||"Unknown",headers:h,body:f?"":Buffer.concat(p).toString("utf-8")})})});d.on("error",e=>{n({url:a.toString(),method:t.method,status:-1,statusText:e.name||"Unknown",body:e.message})}),d.on("timeout",()=>{n({url:a.toString(),method:t.method,status:-1,statusText:"Timeout",body:""})}),t.body&&d.write(t.body),d.end()})};function l(s){return new r.NetRequestHandler(R,s)}const o=l(),j=o.setConfig,O=o.head,S=o.get,T=o.post,U=o.del,C=o.put,x=o.patch;exports.version=r.version;exports.NetRequest=l;exports.del=U;exports.get=S;exports.head=O;exports.patch=x;exports.post=T;exports.put=C;exports.setGlobalConfig=j;
|
package/dist/node.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
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,
|
|
4
|
-
import { v as F } from "./version-
|
|
5
|
-
const
|
|
6
|
-
return R(await g(
|
|
7
|
-
},
|
|
3
|
+
import { h as R, r as g, c as q, U as w, N as x } from "./version-D9usx_Q7.js";
|
|
4
|
+
import { v as F } from "./version-D9usx_Q7.js";
|
|
5
|
+
const U = async function(s, a, u) {
|
|
6
|
+
return R(await g(j, s, a, u), s, a, u);
|
|
7
|
+
}, j = async function(s, a, u) {
|
|
8
8
|
const t = await q(s, a, u);
|
|
9
9
|
if (!w(t.url))
|
|
10
10
|
return {
|
|
@@ -62,12 +62,12 @@ const j = async function(s, a, u) {
|
|
|
62
62
|
}), t.body && c.write(t.body), c.end();
|
|
63
63
|
});
|
|
64
64
|
};
|
|
65
|
-
function
|
|
66
|
-
return new x(
|
|
65
|
+
function O(s) {
|
|
66
|
+
return new x(U, s);
|
|
67
67
|
}
|
|
68
|
-
const o =
|
|
68
|
+
const o = O(), L = o.setConfig, N = o.head, S = o.get, A = o.post, H = o.del, k = o.put, P = o.patch;
|
|
69
69
|
export {
|
|
70
|
-
|
|
70
|
+
O as NetRequest,
|
|
71
71
|
H as del,
|
|
72
72
|
S as get,
|
|
73
73
|
N as head,
|
|
@@ -0,0 +1,2 @@
|
|
|
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,AbortController:"AbortController"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]*\}$/,G=/^\[[\d\D]*\]$/;function R(t,e=!1){return e?K.test(t):I.test(t)}function W(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)||G.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 V(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]=V(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 A="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||A)+" 作为响应数据,读取 "+(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||A,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 x(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 x(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&&!W(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.2";exports.Et=z;exports.NetRequestHandler=ie;exports.RequestGlobalConfig=U;exports.Support=b;exports.U=R;exports.Wt=M;exports.convertOptions=H;exports.getResponseRulesDescription=X;exports.handleResponse=ae;exports.he=J;exports.retryRequest=x;exports.version=ce;
|