@seayoo-web/request 1.6.5 → 1.6.7
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 +89 -90
- package/dist/node.cjs +1 -1
- package/dist/node.js +2 -2
- package/dist/{version-D6u1dag6.js → version-CG8SvMVf.js} +38 -39
- package/dist/version-DQX6nwEA.cjs +2 -0
- package/dist/version.d.ts +1 -1
- package/dist/wx.cjs +1 -1
- package/dist/wx.js +2 -2
- package/package.json +1 -1
- package/dist/version-D0wlS8hW.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(e=>{const r=e.trim();if(!r)return;const i=r.split(":"),o=i[0].trim();o&&(
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("./version-DQX6nwEA.cjs"),y=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,g=new Request(r,{method:e.method,headers:Object.keys(e.headers).length>0?new Headers(e.headers):void 0,body:e.body,credentials:e.credentials,signal:o==null?void 0:o.signal,redirect:"follow"});return await fetch(g).then(async a=>({url:r.toString(),method:e.method,status:a.status,statusText:a.statusText,headers:c.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),r=e.method,i=t==null?void 0:t.onUploadProgress,o=c.Et(e.url,e.params);return await new Promise(l=>{let b=null,g=!1;const a=function(){g||(d.abort(),g=!0)};function f(){b!==null&&clearTimeout(b),e.abort&&e.abort.removeEventListener("abort",a)}const d=new XMLHttpRequest;if(d.open(r,o,!0),i){let h=1;d.upload.addEventListener("progress",w=>{h=w.total,i({total:w.total,loaded:w.loaded})}),d.addEventListener("load",()=>{f(),i({loaded:h,total:h}),l({url:o,method:r,status:d.status,statusText:d.statusText,headers:R(d),body:r==="HEAD"?"":d.responseText})})}d.addEventListener("error",()=>{f(),l({url:o,method:r,status:-1,statusText:"Failed",body:""})}),d.addEventListener("abort",()=>{f(),l({url:o,method:r,status:-1,statusText:"Aborted",body:""})}),Object.entries(e.headers).forEach(([h,w])=>{d.setRequestHeader(h,w)}),e.credentials==="include"&&(d.withCredentials=!0),d.send(e.body||void 0),e.abort&&e.abort.addEventListener("abort",a),e.timeout>0&&(b=setTimeout(a,e.timeout))})};function R(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 E=async function(n,s,t){return c.handleResponse(await c.retryRequest(y,n,s,t),n,s,t)},T=async function(n,s,t){return c.handleResponse(await c.retryRequest(m,n,s,t),n,s,t)};async function q(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((d,h)=>{o.append(`${a}[${h}]`,String(d))}):o.append(a,String(f))});for(const a in l)o.append(a,l[a]);const b=new c.RequestGlobalConfig(e),g=await m(n,b,{...t,method:i,body:o});return c.handleResponse(g,n,b,t)}async function H(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 S(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 v=async function(n,s,t){return await q(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 p(n){if(!c.Support.window)throw new Error("Default Module Only Support In Browser");return c.Support.fetch?new c.NetRequestHandler(E,n):new c.NetRequestHandler(T,n)}const u=p(),x=u.setConfig,C=u.head,j=u.get,L=u.post,O=u.del,U=u.put,k=u.patch;exports.getResponseRulesDescription=c.getResponseRulesDescription;exports.version=c.version;exports.NetRequest=p;exports.del=O;exports.get=j;exports.head=C;exports.jsonp=H;exports.jsonx=S;exports.patch=k;exports.post=L;exports.put=U;exports.setGlobalConfig=x;exports.upload=v;
|
package/dist/index.js
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import { c as
|
|
2
|
-
import { g as
|
|
3
|
-
const
|
|
4
|
-
const t = await
|
|
5
|
-
|
|
6
|
-
const a =
|
|
7
|
-
function
|
|
1
|
+
import { c as T, S as w, f as q, E as p, h as m, r as H, R, a as x, N as E } from "./version-CG8SvMVf.js";
|
|
2
|
+
import { g as z, v as J } from "./version-CG8SvMVf.js";
|
|
3
|
+
const L = async function(n, o, e) {
|
|
4
|
+
const t = await T(n, o, e), r = new URL(t.url), c = t.params;
|
|
5
|
+
c instanceof Object && Object.keys(c).forEach((s) => r.searchParams.set(s, c[s]));
|
|
6
|
+
const a = w.AbortController ? new AbortController() : null;
|
|
7
|
+
function i() {
|
|
8
8
|
a && !a.signal.aborted && a.abort();
|
|
9
9
|
}
|
|
10
|
-
t.abort && t.abort.addEventListener("abort",
|
|
11
|
-
const f = t.timeout > 0 ? setTimeout(
|
|
10
|
+
t.abort && t.abort.addEventListener("abort", i);
|
|
11
|
+
const f = t.timeout > 0 ? setTimeout(i, t.timeout) : null, h = new Request(r, {
|
|
12
12
|
method: t.method,
|
|
13
|
-
headers: new Headers(t.headers),
|
|
13
|
+
headers: Object.keys(t.headers).length > 0 ? new Headers(t.headers) : void 0,
|
|
14
14
|
body: t.body,
|
|
15
15
|
credentials: t.credentials,
|
|
16
16
|
signal: a == null ? void 0 : a.signal,
|
|
17
17
|
redirect: "follow"
|
|
18
18
|
});
|
|
19
|
-
return await fetch(
|
|
19
|
+
return await fetch(h).then(async (s) => ({
|
|
20
20
|
url: r.toString(),
|
|
21
21
|
method: t.method,
|
|
22
22
|
status: s.status,
|
|
23
23
|
statusText: s.statusText,
|
|
24
|
-
headers:
|
|
24
|
+
headers: q([...s.headers.entries()]),
|
|
25
25
|
body: t.method === "HEAD" ? "" : await s.text()
|
|
26
26
|
})).catch((s) => ({
|
|
27
27
|
url: r.toString(),
|
|
@@ -30,37 +30,36 @@ const U = async function(n, o, e) {
|
|
|
30
30
|
statusText: a != null && a.signal.aborted ? "Aborted" : "NetworkError",
|
|
31
31
|
body: String(s)
|
|
32
32
|
})).finally(() => {
|
|
33
|
-
f !== null && clearTimeout(f), t.abort && t.abort.removeEventListener("abort",
|
|
33
|
+
f !== null && clearTimeout(f), t.abort && t.abort.removeEventListener("abort", i);
|
|
34
34
|
});
|
|
35
|
-
},
|
|
36
|
-
const t =
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
let f = 1;
|
|
42
|
-
function b(m) {
|
|
43
|
-
f = m.total, i({ total: m.total, loaded: m.loaded });
|
|
44
|
-
}
|
|
45
|
-
let s = null, u = !1;
|
|
46
|
-
const h = function() {
|
|
47
|
-
u || (d.abort(), u = !0);
|
|
35
|
+
}, y = async function(n, o, e) {
|
|
36
|
+
const t = await T(n, o, e), r = t.method, c = e == null ? void 0 : e.onUploadProgress, a = p(t.url, t.params);
|
|
37
|
+
return await new Promise((i) => {
|
|
38
|
+
let f = null, h = !1;
|
|
39
|
+
const s = function() {
|
|
40
|
+
h || (d.abort(), h = !0);
|
|
48
41
|
};
|
|
49
|
-
function
|
|
50
|
-
|
|
42
|
+
function u() {
|
|
43
|
+
f !== null && clearTimeout(f), t.abort && t.abort.removeEventListener("abort", s);
|
|
51
44
|
}
|
|
52
45
|
const d = new XMLHttpRequest();
|
|
53
|
-
d.open(r, a, !0),
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
46
|
+
if (d.open(r, a, !0), c) {
|
|
47
|
+
let b = 1;
|
|
48
|
+
d.upload.addEventListener("progress", (g) => {
|
|
49
|
+
b = g.total, c({ total: g.total, loaded: g.loaded });
|
|
50
|
+
}), d.addEventListener("load", () => {
|
|
51
|
+
u(), c({ loaded: b, total: b }), i({
|
|
52
|
+
url: a,
|
|
53
|
+
method: r,
|
|
54
|
+
status: d.status,
|
|
55
|
+
statusText: d.statusText,
|
|
56
|
+
headers: S(d),
|
|
57
|
+
body: r === "HEAD" ? "" : d.responseText
|
|
58
|
+
});
|
|
61
59
|
});
|
|
62
|
-
}
|
|
63
|
-
|
|
60
|
+
}
|
|
61
|
+
d.addEventListener("error", () => {
|
|
62
|
+
u(), i({
|
|
64
63
|
url: a,
|
|
65
64
|
method: r,
|
|
66
65
|
status: -1,
|
|
@@ -68,19 +67,19 @@ const U = async function(n, o, e) {
|
|
|
68
67
|
body: ""
|
|
69
68
|
});
|
|
70
69
|
}), d.addEventListener("abort", () => {
|
|
71
|
-
|
|
70
|
+
u(), i({
|
|
72
71
|
url: a,
|
|
73
72
|
method: r,
|
|
74
73
|
status: -1,
|
|
75
74
|
statusText: "Aborted",
|
|
76
75
|
body: ""
|
|
77
76
|
});
|
|
78
|
-
}), Object.entries(t.headers).forEach(([
|
|
79
|
-
d.setRequestHeader(
|
|
80
|
-
}), t.credentials === "include" && (d.withCredentials = !0), d.send(t.body || void 0), t.abort && t.abort.addEventListener("abort",
|
|
77
|
+
}), Object.entries(t.headers).forEach(([b, g]) => {
|
|
78
|
+
d.setRequestHeader(b, g);
|
|
79
|
+
}), t.credentials === "include" && (d.withCredentials = !0), d.send(t.body || void 0), t.abort && t.abort.addEventListener("abort", s), t.timeout > 0 && (f = setTimeout(s, t.timeout));
|
|
81
80
|
});
|
|
82
81
|
};
|
|
83
|
-
function
|
|
82
|
+
function S(n) {
|
|
84
83
|
const o = {};
|
|
85
84
|
if (!n)
|
|
86
85
|
return o;
|
|
@@ -90,66 +89,66 @@ function v(n) {
|
|
|
90
89
|
const r = t.trim();
|
|
91
90
|
if (!r)
|
|
92
91
|
return;
|
|
93
|
-
const
|
|
94
|
-
a && (o[a] = (
|
|
92
|
+
const c = r.split(":"), a = c[0].trim();
|
|
93
|
+
a && (o[a] = (c[1] || "").trim());
|
|
95
94
|
}), o;
|
|
96
95
|
}
|
|
97
|
-
const
|
|
98
|
-
return
|
|
99
|
-
},
|
|
100
|
-
return
|
|
96
|
+
const v = async function(n, o, e) {
|
|
97
|
+
return m(await H(L, n, o, e), n, o, e);
|
|
98
|
+
}, C = async function(n, o, e) {
|
|
99
|
+
return m(await H(y, n, o, e), n, o, e);
|
|
101
100
|
};
|
|
102
|
-
async function
|
|
103
|
-
const r = e == null ? void 0 : e.body,
|
|
101
|
+
async function j(n, o, e, t) {
|
|
102
|
+
const r = e == null ? void 0 : e.body, c = (e == null ? void 0 : e.method) === "PUT" ? "PUT" : "POST";
|
|
104
103
|
if (o instanceof Blob) {
|
|
105
|
-
const s = new
|
|
104
|
+
const s = new R(t), u = await y(n, s, {
|
|
106
105
|
...e,
|
|
107
|
-
method:
|
|
106
|
+
method: c,
|
|
108
107
|
body: o
|
|
109
108
|
});
|
|
110
|
-
return
|
|
109
|
+
return m(u, n, s, e);
|
|
111
110
|
}
|
|
112
|
-
const a = new FormData(),
|
|
111
|
+
const a = new FormData(), i = { ...o };
|
|
113
112
|
r instanceof Object && Object.entries(r).forEach(([s, u]) => {
|
|
114
|
-
u instanceof Blob ?
|
|
115
|
-
a.append(`${s}[${
|
|
113
|
+
u instanceof Blob ? i[s] = u : Array.isArray(u) ? u.forEach((d, b) => {
|
|
114
|
+
a.append(`${s}[${b}]`, String(d));
|
|
116
115
|
}) : a.append(s, String(u));
|
|
117
116
|
});
|
|
118
|
-
for (const s in
|
|
119
|
-
a.append(s,
|
|
120
|
-
const f = new
|
|
117
|
+
for (const s in i)
|
|
118
|
+
a.append(s, i[s]);
|
|
119
|
+
const f = new R(t), h = await y(n, f, {
|
|
121
120
|
...e,
|
|
122
|
-
method:
|
|
121
|
+
method: c,
|
|
123
122
|
body: a
|
|
124
123
|
});
|
|
125
|
-
return
|
|
124
|
+
return m(h, n, f, e);
|
|
126
125
|
}
|
|
127
|
-
async function
|
|
126
|
+
async function A(n, o, e = {}) {
|
|
128
127
|
const t = window;
|
|
129
128
|
"callback" in e || (e.callback = "jsonxData" + Math.random().toString(16).slice(2));
|
|
130
129
|
const r = e.callback + "";
|
|
131
130
|
if (!n)
|
|
132
131
|
return null;
|
|
133
|
-
const
|
|
132
|
+
const c = p(n, e, !0);
|
|
134
133
|
return new Promise((a) => {
|
|
135
|
-
t[r] = function(
|
|
136
|
-
if (r in window && delete t[r], o(
|
|
137
|
-
return
|
|
138
|
-
console.warn("response type check faild", n,
|
|
139
|
-
},
|
|
134
|
+
t[r] = function(i) {
|
|
135
|
+
if (r in window && delete t[r], o(i))
|
|
136
|
+
return i;
|
|
137
|
+
console.warn("response type check faild", n, i), a(null);
|
|
138
|
+
}, x(c).catch(function() {
|
|
140
139
|
a(null), delete t[r];
|
|
141
140
|
});
|
|
142
141
|
});
|
|
143
142
|
}
|
|
144
|
-
async function
|
|
143
|
+
async function O(n, o, e = {}) {
|
|
145
144
|
const t = window;
|
|
146
|
-
return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), n ? await
|
|
145
|
+
return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), n ? await x(p(n, e, !0)).then(() => {
|
|
147
146
|
const r = t[e.var + ""];
|
|
148
147
|
return o(r) ? r : (console.warn("response type check faild", n, r), null);
|
|
149
148
|
}).catch(() => null) : null;
|
|
150
149
|
}
|
|
151
|
-
const
|
|
152
|
-
return await
|
|
150
|
+
const D = async function(n, o, e) {
|
|
151
|
+
return await j(n, o, e, {
|
|
153
152
|
baseURL: l.getConfig("baseURL"),
|
|
154
153
|
logHandler: l.getConfig("logHandler"),
|
|
155
154
|
errorHandler: l.getConfig("errorHandler"),
|
|
@@ -158,24 +157,24 @@ const N = async function(n, o, e) {
|
|
|
158
157
|
responseHandler: l.getConfig("responseHandler")
|
|
159
158
|
});
|
|
160
159
|
};
|
|
161
|
-
function
|
|
162
|
-
if (!
|
|
160
|
+
function U(n) {
|
|
161
|
+
if (!w.window)
|
|
163
162
|
throw new Error("Default Module Only Support In Browser");
|
|
164
|
-
return
|
|
163
|
+
return w.fetch ? new E(v, n) : new E(C, n);
|
|
165
164
|
}
|
|
166
|
-
const l =
|
|
165
|
+
const l = U(), P = l.setConfig, M = l.head, N = l.get, F = l.post, B = l.del, X = l.put, G = l.patch;
|
|
167
166
|
export {
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
167
|
+
U as NetRequest,
|
|
168
|
+
B as del,
|
|
169
|
+
N as get,
|
|
170
|
+
z as getResponseRulesDescription,
|
|
171
|
+
M as head,
|
|
172
|
+
A as jsonp,
|
|
173
|
+
O as jsonx,
|
|
174
|
+
G as patch,
|
|
175
|
+
F as post,
|
|
176
|
+
X as put,
|
|
177
|
+
P as setGlobalConfig,
|
|
178
|
+
D as upload,
|
|
179
|
+
J as version
|
|
181
180
|
};
|
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"),o=require("./version-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("node:http"),q=require("node:https"),o=require("./version-DQX6nwEA.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,7 +1,7 @@
|
|
|
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, f as x, N as U } from "./version-
|
|
4
|
-
import { v as G } from "./version-
|
|
3
|
+
import { h as R, r as g, c as q, U as w, f as x, N as U } from "./version-CG8SvMVf.js";
|
|
4
|
+
import { v as G } from "./version-CG8SvMVf.js";
|
|
5
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) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
var
|
|
2
|
-
var x = (t, e, n) => e in t ?
|
|
1
|
+
var C = Object.defineProperty;
|
|
2
|
+
var x = (t, e, n) => e in t ? C(t, e, { enumerable: !0, configurable: !0, writable: !0, value: n }) : t[e] = n;
|
|
3
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,
|
|
@@ -10,7 +10,7 @@ const m = typeof globalThis < "u" ? globalThis : typeof global < "u" ? global :
|
|
|
10
10
|
TextDecoder: "TextDecoder" in m,
|
|
11
11
|
AbortController: "AbortController" in m
|
|
12
12
|
};
|
|
13
|
-
function
|
|
13
|
+
function L(t) {
|
|
14
14
|
return t.reduce(
|
|
15
15
|
(e, [n, s]) => (n && (e[n] = s || ""), e),
|
|
16
16
|
{}
|
|
@@ -21,13 +21,13 @@ async function ae(t, e, n) {
|
|
|
21
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;
|
|
22
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);
|
|
23
23
|
const o = Object.assign(
|
|
24
|
-
r ? {} : {
|
|
24
|
+
r || a === "GET" || a === "HEAD" || a === "DELETE" ? {} : {
|
|
25
25
|
"Content-Type": w.Blob && s.body instanceof Blob ? s.body.type || "application/octet-stream" : "application/json;charset=utf-8"
|
|
26
26
|
},
|
|
27
27
|
s.headers
|
|
28
28
|
), i = s.params || {}, c = {};
|
|
29
29
|
Object.keys(i).forEach((h) => {
|
|
30
|
-
i[h] !== void 0 && (c[h] =
|
|
30
|
+
i[h] !== void 0 && (c[h] = U(i[h]));
|
|
31
31
|
});
|
|
32
32
|
const d = e.getFullUrl(t), l = B(s.body), u = s.timeout || e.get("timeout"), g = await async function() {
|
|
33
33
|
var v;
|
|
@@ -45,17 +45,17 @@ async function ae(t, e, n) {
|
|
|
45
45
|
credentials: s.credentials || e.get("credentials")
|
|
46
46
|
};
|
|
47
47
|
}
|
|
48
|
-
function
|
|
48
|
+
function U(t) {
|
|
49
49
|
return typeof t == "string" ? t : Array.isArray(t) ? t.join(",") : t + "";
|
|
50
50
|
}
|
|
51
51
|
function B(t) {
|
|
52
52
|
if (t)
|
|
53
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);
|
|
54
54
|
}
|
|
55
|
-
function
|
|
55
|
+
function H(t) {
|
|
56
56
|
return new Promise((e) => setTimeout(e, Math.max(0, t)));
|
|
57
57
|
}
|
|
58
|
-
function
|
|
58
|
+
function P() {
|
|
59
59
|
}
|
|
60
60
|
function R(t, e) {
|
|
61
61
|
try {
|
|
@@ -73,13 +73,13 @@ function $(t, e = !1) {
|
|
|
73
73
|
function N(t) {
|
|
74
74
|
return t == null;
|
|
75
75
|
}
|
|
76
|
-
function
|
|
76
|
+
function D(t, ...e) {
|
|
77
77
|
if (!t || typeof t != "object")
|
|
78
78
|
return !1;
|
|
79
79
|
const n = Object.getPrototypeOf(t);
|
|
80
80
|
return n !== Object.prototype && n !== null ? !1 : e.every((s) => s in t);
|
|
81
81
|
}
|
|
82
|
-
function
|
|
82
|
+
function M(t) {
|
|
83
83
|
return q.test(t) || I.test(t);
|
|
84
84
|
}
|
|
85
85
|
async function re(t) {
|
|
@@ -92,7 +92,7 @@ async function re(t) {
|
|
|
92
92
|
}, s.setAttribute("src", t), n.appendChild(s);
|
|
93
93
|
});
|
|
94
94
|
}
|
|
95
|
-
function
|
|
95
|
+
function G(t, e = "数据未能正确识别") {
|
|
96
96
|
return typeof t == "function" ? {
|
|
97
97
|
guard: t,
|
|
98
98
|
message: e
|
|
@@ -101,13 +101,13 @@ function W(t, e = "数据未能正确识别") {
|
|
|
101
101
|
message: t.message || e
|
|
102
102
|
};
|
|
103
103
|
}
|
|
104
|
-
function
|
|
105
|
-
return !e || $(t, !0) ?
|
|
104
|
+
function E(t, e = "") {
|
|
105
|
+
return !e || $(t, !0) ? O(t) : (O(e) + "/" + t).replace(/\/{2,}/g, "/").replace(/:\//, "://");
|
|
106
106
|
}
|
|
107
|
-
function
|
|
107
|
+
function O(t) {
|
|
108
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(/^\/+/, "");
|
|
109
109
|
}
|
|
110
|
-
function
|
|
110
|
+
function J(t, e) {
|
|
111
111
|
if (e) {
|
|
112
112
|
if (e === !0)
|
|
113
113
|
return t.replace(/\?[^#]*/, "");
|
|
@@ -121,15 +121,15 @@ function oe(t, e, n = !1) {
|
|
|
121
121
|
if (!s)
|
|
122
122
|
return t;
|
|
123
123
|
const r = t.split("#");
|
|
124
|
-
n && (r[0] =
|
|
124
|
+
n && (r[0] = J(
|
|
125
125
|
r[0],
|
|
126
126
|
(s.match(/([^=&#?]+)=[^&#]+/g) || []).map((o) => o.replace(/=.+$/, ""))
|
|
127
127
|
));
|
|
128
128
|
const a = r[0].indexOf("?") + 1 ? "&" : "?";
|
|
129
129
|
return (r[0] + a + s + (r.length > 1 ? "#" + r[1] : "")).replace(/\?&/, "?");
|
|
130
130
|
}
|
|
131
|
-
const
|
|
132
|
-
function
|
|
131
|
+
const A = "data", b = "message";
|
|
132
|
+
function W(t, e, n, s, r) {
|
|
133
133
|
const a = r || s;
|
|
134
134
|
return F(t) ? Y(a.ok || s.ok, t, e, n) : _(a.failed || s.failed, e, n);
|
|
135
135
|
}
|
|
@@ -150,7 +150,7 @@ const ie = function(t) {
|
|
|
150
150
|
break;
|
|
151
151
|
case "json":
|
|
152
152
|
e.push(
|
|
153
|
-
" 将响应解析为 json,读取 " + (s.dataField ||
|
|
153
|
+
" 将响应解析为 json,读取 " + (s.dataField || A) + " 作为响应数据,读取 " + (s.messageField || b) + " 作为提示消息"
|
|
154
154
|
), s.statusField && e.push(" 当 " + s.statusField + " 为 " + (s.statusOKValue || "空值") + " 时是成功提示,否则是错误消息"), s.ignoreMessage && e.push(" 并忽略以下消息:" + s.ignoreMessage);
|
|
155
155
|
break;
|
|
156
156
|
}
|
|
@@ -166,20 +166,20 @@ function _(t, e, n) {
|
|
|
166
166
|
};
|
|
167
167
|
switch (s.resolve) {
|
|
168
168
|
case "body":
|
|
169
|
-
r.message =
|
|
169
|
+
r.message = T(n) || n;
|
|
170
170
|
break;
|
|
171
171
|
case "json":
|
|
172
172
|
const { code: a, message: o } = V(n, s.statusField, s.messageField);
|
|
173
|
-
r.code = a || e, r.message =
|
|
173
|
+
r.code = a || e, r.message = T(n) || o;
|
|
174
174
|
break;
|
|
175
175
|
}
|
|
176
176
|
return r;
|
|
177
177
|
}
|
|
178
178
|
function V(t, e, n = b) {
|
|
179
|
-
if (!
|
|
179
|
+
if (!M(t))
|
|
180
180
|
return { message: "" };
|
|
181
181
|
const s = R(t);
|
|
182
|
-
return !s || !
|
|
182
|
+
return !s || !D(s) ? { message: t } : {
|
|
183
183
|
code: e ? j(s, e) : "",
|
|
184
184
|
message: j(s, n) || t
|
|
185
185
|
};
|
|
@@ -195,7 +195,7 @@ function z(t) {
|
|
|
195
195
|
return t ? typeof t == "string" ? t : JSON.stringify(t) : "";
|
|
196
196
|
}
|
|
197
197
|
const Q = /<title>([^<]+)<\/title>/i, X = /<message>([^<]+)<\/message>/i;
|
|
198
|
-
function
|
|
198
|
+
function T(t) {
|
|
199
199
|
const e = t.match(Q);
|
|
200
200
|
if (e)
|
|
201
201
|
return e[1];
|
|
@@ -212,11 +212,11 @@ function Y(t, e, n, s) {
|
|
|
212
212
|
if (e === 202 || e === 204 || !s)
|
|
213
213
|
return a;
|
|
214
214
|
if (r.resolve === "body")
|
|
215
|
-
return a.data =
|
|
215
|
+
return a.data = M(s) ? R(s) : s, a;
|
|
216
216
|
const o = R(s);
|
|
217
|
-
if (!o || !
|
|
217
|
+
if (!o || !D(o))
|
|
218
218
|
return a.ok = !1, a.code = "ResponseFormatError", a.message = "响应内容无法格式化为 Object", a;
|
|
219
|
-
const i = r.statusField, c = r.statusOKValue || "", d = r.dataField ||
|
|
219
|
+
const i = r.statusField, c = r.statusOKValue || "", d = r.dataField || A, l = r.messageField || b, u = r.ignoreMessage || "";
|
|
220
220
|
if (i && !(i in o))
|
|
221
221
|
return a.ok = !1, a.code = "ResponseFieldMissing", a.message = "响应内容找不到状态字段 " + i, a;
|
|
222
222
|
const g = i ? o[i] + "" : "";
|
|
@@ -226,7 +226,7 @@ function F(t) {
|
|
|
226
226
|
return t >= 200 && t < 400;
|
|
227
227
|
}
|
|
228
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") ||
|
|
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") || P;
|
|
230
230
|
c({
|
|
231
231
|
type: "prepear",
|
|
232
232
|
url: e,
|
|
@@ -253,7 +253,7 @@ async function Z(t, e, n, s, r) {
|
|
|
253
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)
|
|
254
254
|
return l;
|
|
255
255
|
const h = (s == null ? void 0 : s.retryInterval) ?? n.get("retryInterval") ?? 1e3;
|
|
256
|
-
return await
|
|
256
|
+
return await H(
|
|
257
257
|
Math.max(
|
|
258
258
|
100,
|
|
259
259
|
h === "2EB" ? Math.pow(2, a) * 1e3 : typeof h == "function" ? h(a + 1) || 0 : h
|
|
@@ -263,7 +263,7 @@ async function Z(t, e, n, s, r) {
|
|
|
263
263
|
function ce(t, e, n, s) {
|
|
264
264
|
var o, i;
|
|
265
265
|
if (t.status < 0)
|
|
266
|
-
return
|
|
266
|
+
return k(
|
|
267
267
|
{ ok: !1, status: t.status, code: t.statusText, headers: {}, message: "", data: null },
|
|
268
268
|
`${t.method} ${e} ${t.statusText}`,
|
|
269
269
|
t.method,
|
|
@@ -273,15 +273,15 @@ function ce(t, e, n, s) {
|
|
|
273
273
|
);
|
|
274
274
|
F(t.status) || (o = n.get("errorHandler")) == null || o(t.status, t.method, e);
|
|
275
275
|
const r = {
|
|
276
|
-
...
|
|
276
|
+
...W(t.status, t.statusText, t.body, n.get("responseRule"), s == null ? void 0 : s.responseRule),
|
|
277
277
|
status: t.status,
|
|
278
|
-
headers:
|
|
278
|
+
headers: L(Object.entries(t.headers || {}).map(([c, d]) => [c.toLowerCase(), d]))
|
|
279
279
|
};
|
|
280
280
|
(i = n.get("responseHandler")) == null || i({ ...r }, t.method, e);
|
|
281
281
|
const a = r.ok ? r.message : `${t.method} ${e} [${r.code || t.statusText}] ${r.message || t.statusText}`;
|
|
282
|
-
return
|
|
282
|
+
return k(r, a, t.method, e, n, s);
|
|
283
283
|
}
|
|
284
|
-
function
|
|
284
|
+
function k(t, e, n, s, r, a) {
|
|
285
285
|
const o = r.get("message"), i = o === !1 || (a == null ? void 0 : a.message) === !1 ? !1 : (a == null ? void 0 : a.message) || o;
|
|
286
286
|
if (i !== !1) {
|
|
287
287
|
const c = typeof i == "function" ? i(t, n, s, e) : e;
|
|
@@ -322,7 +322,7 @@ class ee {
|
|
|
322
322
|
}
|
|
323
323
|
/** 基于 baseURL 返回完整的 url 地址, 如果 url 地址以 / 开头则表示忽略 baseURL 的值 */
|
|
324
324
|
getFullUrl(e) {
|
|
325
|
-
return e.startsWith("/") ?
|
|
325
|
+
return e.startsWith("/") ? E(e) : E(e, this.config.baseURL);
|
|
326
326
|
}
|
|
327
327
|
/** 提示消息 */
|
|
328
328
|
showMessage(e, n) {
|
|
@@ -357,7 +357,7 @@ class te {
|
|
|
357
357
|
}
|
|
358
358
|
function se(t, e, n, s) {
|
|
359
359
|
if (e.ok && !N(e.data) && s) {
|
|
360
|
-
const r =
|
|
360
|
+
const r = G(s, "响应数据未能正确识别");
|
|
361
361
|
return r.guard(e.data) || (console.error("ResponseCheckFaild", t, e.data), n.showMessage(!0, `${t} ${r.message}`), e.data = null), e;
|
|
362
362
|
}
|
|
363
363
|
return e;
|
|
@@ -437,17 +437,16 @@ class ue {
|
|
|
437
437
|
return a.method = "PATCH", a.body = n, this.guard(e, await this.exec(e, a), s || null);
|
|
438
438
|
}
|
|
439
439
|
}
|
|
440
|
-
const le = "1.6.
|
|
440
|
+
const le = "1.6.7";
|
|
441
441
|
export {
|
|
442
442
|
oe as E,
|
|
443
443
|
ue as N,
|
|
444
444
|
ee as R,
|
|
445
445
|
w as S,
|
|
446
446
|
$ as U,
|
|
447
|
-
H as W,
|
|
448
447
|
re as a,
|
|
449
448
|
ae as c,
|
|
450
|
-
|
|
449
|
+
L as f,
|
|
451
450
|
ie as g,
|
|
452
451
|
ce as h,
|
|
453
452
|
Z as r,
|
|
@@ -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};function D(t){return t.reduce((e,[n,s])=>(n&&(e[n]=s||""),e),{})}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||a==="GET"||a==="HEAD"||a==="DELETE"?{}:{"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 E;const h=e.get("requestTransformer");return h?await h({headers:o,params:c,method:a,url:d,body:l}):await((E=e.get("requestHandler"))==null?void 0:E(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 K(){}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]*\}$/,J=/^\[[\d\D]*\]$/;function R(t,e=!1){return e?I.test(t):N.test(t)}function W(t){return t==null}function M(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 G.test(t)||J.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 F(t,e=""){return!e||R(t,!0)?O(t):(O(e)+"/"+t).replace(/\/{2,}/g,"/").replace(/:\//,"://")}function O(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 A="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||A)+" 作为响应数据,读取 "+(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=T(n)||n;break;case"json":const{code:a,message:o}=ee(n,s.statusField,s.messageField);r.code=a||e,r.message=T(n)||o;break}return r}function ee(t,e,n=p){if(!C(t))return{message:""};const s=j(t);return!s||!M(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 T(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=C(s)?j(s):s,a;const o=j(s);if(!o||!M(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,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")||K;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 q(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:D(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 x{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 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&&!W(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 x(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){try{return await this.agent(e,this.config,n)}catch(s){return console.error("RequestError",s),{ok:!1,status:-9,code:"Unkonwn",message:String(s),headers:{},data:null}}}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.7";exports.Et=Q;exports.NetRequestHandler=ce;exports.RequestGlobalConfig=x;exports.Support=b;exports.U=R;exports.convertOptions=H;exports.fromEntries=D;exports.getResponseRulesDescription=Y;exports.handleResponse=re;exports.he=_;exports.retryRequest=U;exports.version=ue;
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const version = "1.6.
|
|
1
|
+
export declare const version = "1.6.7";
|
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-DQX6nwEA.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-CG8SvMVf.js";
|
|
2
|
+
import { v as A } from "./version-CG8SvMVf.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,2 +0,0 @@
|
|
|
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 U="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||U)+" 作为响应数据,读取 "+(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=k(n)||n;break;case"json":const{code:a,message:o}=ee(n,s.statusField,s.messageField);r.code=a||e,r.message=k(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 k(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||U,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 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")||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 x(t,e,n,s,a+1)}function re(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);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 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 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){try{return await this.agent(e,this.config,n)}catch(s){return console.error("RequestError",s),{ok:!1,status:-9,code:"Unkonwn",message:String(s),headers:{},data:null}}}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.5";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=x;exports.version=ue;
|