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