@seayoo-web/request 2.0.10 → 2.1.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/README.md +7 -1
- package/dist/index.cjs +2 -2
- package/dist/index.js +65 -64
- package/dist/node.cjs +1 -1
- package/dist/node.js +26 -26
- package/dist/{request.fetch-DR4KeRZ0.js → request.fetch-3tu4c4YP.js} +15 -24
- package/dist/request.fetch-WuNmpi3J.cjs +1 -0
- package/dist/retry-C2OSRA9r.cjs +2 -0
- package/dist/{type-CRH6iqXf.js → retry-DUbMIN1-.js} +100 -100
- package/dist/wx.cjs +1 -1
- package/dist/wx.js +21 -21
- package/package.json +2 -2
- package/types/inc/config.d.ts +1 -1
- package/types/inc/guard.d.ts +1 -1
- package/types/inc/main.d.ts +1 -1
- package/types/inc/type.d.ts +7 -1
- package/dist/request.fetch-K4MJcDmZ.cjs +0 -1
- package/dist/type-CrucJEBR.cjs +0 -2
- package/types/inc/detect.d.ts +0 -13
- package/types/version.d.ts +0 -1
package/README.md
CHANGED
|
@@ -138,6 +138,12 @@ setConfig({ timeout: 10000 })
|
|
|
138
138
|
|
|
139
139
|
说明:用于控制 get 请求的缓冲时效,单位 ms,默认 500,设置为 0 则禁用缓冲
|
|
140
140
|
|
|
141
|
+
### defaultTypeGuardMessage
|
|
142
|
+
|
|
143
|
+
类型:string
|
|
144
|
+
|
|
145
|
+
说明:默认的类型守卫错误提示,仅支持全局配置,默认 “响应数据未能正确识别“,单个 api 可通过传入完整的类型守卫({ guard, message })来定制提示消息。
|
|
146
|
+
|
|
141
147
|
### message
|
|
142
148
|
|
|
143
149
|
类型:false | ((
|
|
@@ -288,7 +294,7 @@ status 表示网络错误或者http 状态码错误时重试;
|
|
|
288
294
|
|
|
289
295
|
类型:null | ((isError: boolean, message: string, code: string, status:number) => void)
|
|
290
296
|
|
|
291
|
-
说明:全局默认提示函数,isError 表示需要提示的消息是否为错误消息,message 为提示内容,code
|
|
297
|
+
说明:全局默认提示函数,isError 表示需要提示的消息是否为错误消息,message 为提示内容,code 为状态描述码或内置错误码(可导出 RequestInteralError 进行对比),status 为 http 状态码
|
|
292
298
|
|
|
293
299
|
### logHandler
|
|
294
300
|
|
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./
|
|
2
|
-
`).forEach(n=>{const r=n.trim();if(!r)return;const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./retry-C2OSRA9r.cjs"),q=require("./request.fetch-WuNmpi3J.cjs"),y=async function(t,s,e){return o.handleResponse(await o.retryRequest(R,t,s,e),t,s,e)};async function m(t,s,e,n){const r=e==null?void 0:e.body,d=(e==null?void 0:e.method)==="PUT"?"PUT":"POST";if(s instanceof Blob){const u=new o.RequestGlobalConfig(n),g=await R(t,u,{...e,method:d,body:s});return o.handleResponse(g,t,u,e)}const c=new FormData,f={...s};r instanceof Object&&Object.entries(r).forEach(([u,g])=>{g instanceof Blob?f[u]=g:Array.isArray(g)?g.forEach((a,i)=>{c.append(`${u}[${i}]`,String(a))}):c.append(u,String(g))});for(const u in f)c.append(u,f[u]);const b=new o.RequestGlobalConfig(n),w=await R(t,b,{...e,method:d,body:c});return o.handleResponse(w,t,b,e)}const R=async function(t,s,e){const n=await o.convertOptions(t,s,e),r=n.method,d=e==null?void 0:e.onUploadProgress,c=o.Zt(n.url,n.params);return await new Promise(f=>{let b=null,w=!1;const u=function(){w||(a.abort(),w=!0)};function g(){b!==null&&clearTimeout(b),n.abort&&n.abort.removeEventListener("abort",u)}const a=new XMLHttpRequest;if(a.open(r,c,!0),d){let i=1;a.upload.addEventListener("progress",h=>{i=h.total,d({total:h.total,loaded:h.loaded})}),a.addEventListener("load",()=>{d({loaded:i,total:i})})}a.addEventListener("load",()=>{const i=a.status;g(),f({url:c,method:r,status:i,statusText:a.statusText,headers:E(a),body:r==="HEAD"||i===204?"":a.responseText})}),a.addEventListener("error",i=>{g(),f({url:c,method:r,status:a.status||-1,statusText:a.statusText||o.RequestInteralError.NetworkError,body:"",rawError:i})},!0),a.addEventListener("abort",()=>{g(),f({url:c,method:r,status:-1,statusText:o.RequestInteralError.Aborted,body:""})}),Object.entries(n.headers).forEach(([i,h])=>{a.setRequestHeader(i,h)}),n.credentials==="include"&&(a.withCredentials=!0),a.send(n.body||void 0),n.abort&&n.abort.addEventListener("abort",u),n.timeout>0&&(b=setTimeout(u,n.timeout))})};function E(t){const s={};if(!t)return s;const e=t.getAllResponseHeaders();return e&&e!=="null"&&e.replace(/\r/g,"").split(`
|
|
2
|
+
`).forEach(n=>{const r=n.trim();if(!r)return;const d=r.split(":"),c=d[0].trim();c&&(s[c]=(d[1]||"").trim())}),s}async function H(t,s,e={}){const n=window;"callback"in e||(e.callback="jsonxData"+Math.random().toString(16).slice(2));const r=e.callback+"";if(!t)return null;const d=o.Zt(t,e,!0);return new Promise(c=>{n[r]=function(f){if(r in window&&delete n[r],s(f))return f;console.warn("response type check faild",t,f),c(null)},o.Ie(d).catch(function(){c(null),delete n[r]})})}async function T(t,s,e={}){const n=window;return"var"in e||(e.var="jsonxData"+Math.random().toString(16).slice(2)),t?await o.Ie(o.Zt(t,e,!0)).then(()=>{const r=n[e.var+""];return s(r)?r:(console.warn("response type check faild",t,r),null)}).catch(()=>null):null}const x=async function(t,s,e){return await m(t,s,e,{baseURL:l.getConfig("baseURL"),logHandler:l.getConfig("logHandler"),errorHandler:l.getConfig("errorHandler"),requestTransformer:l.getConfig("requestTransformer"),messageHandler:l.getConfig("messageHandler"),responseHandler:l.getConfig("responseHandler")})};function p(t){if(!o.lt.window)throw new Error("Default Module Only Support In Browser");return o.lt.fetch?new o.NetRequestHandler(q.fetchRequest,t):new o.NetRequestHandler(y,t)}const l=p(),C=l.setConfig,j=l.request,L=l.head,S=l.get,k=l.post,v=l.del,D=l.put,U=l.patch;exports.RequestInteralError=o.RequestInteralError;exports.getResponseRulesDescription=o.getResponseRulesDescription;exports.NetRequest=p;exports.del=v;exports.get=S;exports.head=L;exports.jsonp=H;exports.jsonx=T;exports.patch=U;exports.post=k;exports.put=D;exports.request=j;exports.setGlobalConfig=C;exports.upload=x;
|
package/dist/index.js
CHANGED
|
@@ -1,89 +1,90 @@
|
|
|
1
|
-
import { h as w, r as T, R as y, c as x,
|
|
2
|
-
import { g as z } from "./
|
|
3
|
-
import { f as C } from "./request.fetch-
|
|
1
|
+
import { h as w, r as T, R as y, c as x, Z as p, a as R, I as q, l as E, N as H } from "./retry-DUbMIN1-.js";
|
|
2
|
+
import { g as z } from "./retry-DUbMIN1-.js";
|
|
3
|
+
import { f as C } from "./request.fetch-3tu4c4YP.js";
|
|
4
4
|
const L = async function(t, a, e) {
|
|
5
5
|
return w(await T(m, t, a, e), t, a, e);
|
|
6
6
|
};
|
|
7
|
-
async function
|
|
8
|
-
const r = e == null ? void 0 : e.body,
|
|
7
|
+
async function k(t, a, e, n) {
|
|
8
|
+
const r = e == null ? void 0 : e.body, d = (e == null ? void 0 : e.method) === "PUT" ? "PUT" : "POST";
|
|
9
9
|
if (a instanceof Blob) {
|
|
10
|
-
const l = new y(n),
|
|
10
|
+
const l = new y(n), f = await m(t, l, {
|
|
11
11
|
...e,
|
|
12
|
-
method:
|
|
12
|
+
method: d,
|
|
13
13
|
body: a
|
|
14
14
|
});
|
|
15
|
-
return w(
|
|
15
|
+
return w(f, t, l, e);
|
|
16
16
|
}
|
|
17
|
-
const s = new FormData(),
|
|
18
|
-
r instanceof Object && Object.entries(r).forEach(([l,
|
|
19
|
-
|
|
20
|
-
s.append(`${l}[${
|
|
21
|
-
}) : s.append(l, String(
|
|
17
|
+
const s = new FormData(), u = { ...a };
|
|
18
|
+
r instanceof Object && Object.entries(r).forEach(([l, f]) => {
|
|
19
|
+
f instanceof Blob ? u[l] = f : Array.isArray(f) ? f.forEach((o, i) => {
|
|
20
|
+
s.append(`${l}[${i}]`, String(o));
|
|
21
|
+
}) : s.append(l, String(f));
|
|
22
22
|
});
|
|
23
|
-
for (const l in
|
|
24
|
-
s.append(l,
|
|
23
|
+
for (const l in u)
|
|
24
|
+
s.append(l, u[l]);
|
|
25
25
|
const b = new y(n), h = await m(t, b, {
|
|
26
26
|
...e,
|
|
27
|
-
method:
|
|
27
|
+
method: d,
|
|
28
28
|
body: s
|
|
29
29
|
});
|
|
30
30
|
return w(h, t, b, e);
|
|
31
31
|
}
|
|
32
32
|
const m = async function(t, a, e) {
|
|
33
|
-
const n = await x(t, a, e), r = n.method,
|
|
34
|
-
return await new Promise((
|
|
33
|
+
const n = await x(t, a, e), r = n.method, d = e == null ? void 0 : e.onUploadProgress, s = p(n.url, n.params);
|
|
34
|
+
return await new Promise((u) => {
|
|
35
35
|
let b = null, h = !1;
|
|
36
36
|
const l = function() {
|
|
37
37
|
h || (o.abort(), h = !0);
|
|
38
38
|
};
|
|
39
|
-
function
|
|
39
|
+
function f() {
|
|
40
40
|
b !== null && clearTimeout(b), n.abort && n.abort.removeEventListener("abort", l);
|
|
41
41
|
}
|
|
42
42
|
const o = new XMLHttpRequest();
|
|
43
|
-
if (o.open(r, s, !0),
|
|
44
|
-
let
|
|
43
|
+
if (o.open(r, s, !0), d) {
|
|
44
|
+
let i = 1;
|
|
45
45
|
o.upload.addEventListener("progress", (g) => {
|
|
46
|
-
|
|
46
|
+
i = g.total, d({ total: g.total, loaded: g.loaded });
|
|
47
47
|
}), o.addEventListener("load", () => {
|
|
48
|
-
|
|
48
|
+
d({ loaded: i, total: i });
|
|
49
49
|
});
|
|
50
50
|
}
|
|
51
51
|
o.addEventListener("load", () => {
|
|
52
|
-
|
|
52
|
+
const i = o.status;
|
|
53
|
+
f(), u({
|
|
53
54
|
url: s,
|
|
54
55
|
method: r,
|
|
55
|
-
status:
|
|
56
|
+
status: i,
|
|
56
57
|
statusText: o.statusText,
|
|
57
|
-
headers:
|
|
58
|
-
body: r === "HEAD" ? "" : o.responseText
|
|
58
|
+
headers: S(o),
|
|
59
|
+
body: r === "HEAD" || i === 204 ? "" : o.responseText
|
|
59
60
|
});
|
|
60
61
|
}), o.addEventListener(
|
|
61
62
|
"error",
|
|
62
|
-
(
|
|
63
|
-
|
|
63
|
+
(i) => {
|
|
64
|
+
f(), u({
|
|
64
65
|
url: s,
|
|
65
66
|
method: r,
|
|
66
|
-
status: -1,
|
|
67
|
-
statusText: R.
|
|
67
|
+
status: o.status || -1,
|
|
68
|
+
statusText: o.statusText || R.NetworkError,
|
|
68
69
|
body: "",
|
|
69
|
-
rawError:
|
|
70
|
+
rawError: i
|
|
70
71
|
});
|
|
71
72
|
},
|
|
72
73
|
!0
|
|
73
74
|
), o.addEventListener("abort", () => {
|
|
74
|
-
|
|
75
|
+
f(), u({
|
|
75
76
|
url: s,
|
|
76
77
|
method: r,
|
|
77
78
|
status: -1,
|
|
78
79
|
statusText: R.Aborted,
|
|
79
80
|
body: ""
|
|
80
81
|
});
|
|
81
|
-
}), Object.entries(n.headers).forEach(([
|
|
82
|
-
o.setRequestHeader(
|
|
82
|
+
}), Object.entries(n.headers).forEach(([i, g]) => {
|
|
83
|
+
o.setRequestHeader(i, g);
|
|
83
84
|
}), n.credentials === "include" && (o.withCredentials = !0), o.send(n.body || void 0), n.abort && n.abort.addEventListener("abort", l), n.timeout > 0 && (b = setTimeout(l, n.timeout));
|
|
84
85
|
});
|
|
85
86
|
};
|
|
86
|
-
function
|
|
87
|
+
function S(t) {
|
|
87
88
|
const a = {};
|
|
88
89
|
if (!t)
|
|
89
90
|
return a;
|
|
@@ -93,36 +94,36 @@ function U(t) {
|
|
|
93
94
|
const r = n.trim();
|
|
94
95
|
if (!r)
|
|
95
96
|
return;
|
|
96
|
-
const
|
|
97
|
-
s && (a[s] = (
|
|
97
|
+
const d = r.split(":"), s = d[0].trim();
|
|
98
|
+
s && (a[s] = (d[1] || "").trim());
|
|
98
99
|
}), a;
|
|
99
100
|
}
|
|
100
|
-
async function
|
|
101
|
+
async function D(t, a, e = {}) {
|
|
101
102
|
const n = window;
|
|
102
103
|
"callback" in e || (e.callback = "jsonxData" + Math.random().toString(16).slice(2));
|
|
103
104
|
const r = e.callback + "";
|
|
104
105
|
if (!t)
|
|
105
106
|
return null;
|
|
106
|
-
const
|
|
107
|
+
const d = p(t, e, !0);
|
|
107
108
|
return new Promise((s) => {
|
|
108
|
-
n[r] = function(
|
|
109
|
-
if (r in window && delete n[r], a(
|
|
110
|
-
return
|
|
111
|
-
console.warn("response type check faild", t,
|
|
112
|
-
},
|
|
109
|
+
n[r] = function(u) {
|
|
110
|
+
if (r in window && delete n[r], a(u))
|
|
111
|
+
return u;
|
|
112
|
+
console.warn("response type check faild", t, u), s(null);
|
|
113
|
+
}, q(d).catch(function() {
|
|
113
114
|
s(null), delete n[r];
|
|
114
115
|
});
|
|
115
116
|
});
|
|
116
117
|
}
|
|
117
|
-
async function
|
|
118
|
+
async function O(t, a, e = {}) {
|
|
118
119
|
const n = window;
|
|
119
|
-
return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), t ? await
|
|
120
|
+
return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), t ? await q(p(t, e, !0)).then(() => {
|
|
120
121
|
const r = n[e.var + ""];
|
|
121
122
|
return a(r) ? r : (console.warn("response type check faild", t, r), null);
|
|
122
123
|
}).catch(() => null) : null;
|
|
123
124
|
}
|
|
124
|
-
const
|
|
125
|
-
return await
|
|
125
|
+
const P = async function(t, a, e) {
|
|
126
|
+
return await k(t, a, e, {
|
|
126
127
|
baseURL: c.getConfig("baseURL"),
|
|
127
128
|
logHandler: c.getConfig("logHandler"),
|
|
128
129
|
errorHandler: c.getConfig("errorHandler"),
|
|
@@ -131,25 +132,25 @@ const O = async function(t, a, e) {
|
|
|
131
132
|
responseHandler: c.getConfig("responseHandler")
|
|
132
133
|
});
|
|
133
134
|
};
|
|
134
|
-
function
|
|
135
|
-
if (!
|
|
135
|
+
function U(t) {
|
|
136
|
+
if (!E.window)
|
|
136
137
|
throw new Error("Default Module Only Support In Browser");
|
|
137
|
-
return
|
|
138
|
+
return E.fetch ? new H(C, t) : new H(L, t);
|
|
138
139
|
}
|
|
139
|
-
const c =
|
|
140
|
+
const c = U(), A = c.setConfig, M = c.request, N = c.head, I = c.get, B = c.post, X = c.del, F = c.put, G = c.patch;
|
|
140
141
|
export {
|
|
141
|
-
|
|
142
|
+
U as NetRequest,
|
|
142
143
|
R as RequestInteralError,
|
|
143
|
-
|
|
144
|
-
|
|
144
|
+
X as del,
|
|
145
|
+
I as get,
|
|
145
146
|
z as getResponseRulesDescription,
|
|
146
147
|
N as head,
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
148
|
+
D as jsonp,
|
|
149
|
+
O as jsonx,
|
|
150
|
+
G as patch,
|
|
151
|
+
B as post,
|
|
152
|
+
F as put,
|
|
153
|
+
M as request,
|
|
154
|
+
A as setGlobalConfig,
|
|
155
|
+
P as upload
|
|
155
156
|
};
|
package/dist/node.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./retry-C2OSRA9r.cjs"),b=require("./request.fetch-WuNmpi3J.cjs"),y=require("node:http"),g=require("node:https"),w=async function(s,u,c){return e.handleResponse(await e.retryRequest(E,s,u,c),s,u,c)},E=async function(s,u,c){const t=await e.convertOptions(s,u,c);if(!e.tt(t.url))return{url:t.url,method:t.method,status:-1,statusText:e.RequestInteralError.URLFormatError,headers:{},body:""};const p=/^https:\/\//i.test(t.url)?g: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=p.request(a,{headers:t.headers,method:t.method,timeout:t.timeout>0?t.timeout:void 0},function(o){const m=[];o.on("data",h=>m.push(h)),o.on("end",()=>{const h=e.fromEntries(Object.entries(o.headers).map(([R,l])=>[R.toLowerCase(),Array.isArray(l)?l.join(","):l]));n({url:a.toString(),method:t.method,status:o.statusCode||-1,statusText:o.statusMessage||e.RequestInteralError.Unknown,headers:h,body:f||o.statusCode===204?"":Buffer.concat(m).toString("utf-8")})})});d.on("error",o=>{n({url:a.toString(),method:t.method,status:-1,statusText:e.RequestInteralError.Unknown,body:"",rawError:o})}),d.on("timeout",()=>{n({url:a.toString(),method:t.method,status:-1,statusText:e.RequestInteralError.Timeout,body:""})}),t.body&&d.write(t.body),d.end()})};function q(s){return e.lt.fetch?new e.NetRequestHandler(b.fetchRequest,s):new e.NetRequestHandler(w,s)}const r=q(),C=r.setConfig,S=r.head,T=r.get,j=r.post,N=r.del,O=r.put,U=r.patch;exports.NetRequest=q;exports.del=N;exports.get=T;exports.head=S;exports.patch=U;exports.post=j;exports.put=O;exports.setGlobalConfig=C;
|
package/dist/node.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import { h as y, r as q, c as w,
|
|
2
|
-
import { f as
|
|
3
|
-
import
|
|
4
|
-
import
|
|
5
|
-
const
|
|
6
|
-
return y(await q(L,
|
|
7
|
-
}, L = async function(
|
|
8
|
-
const t = await w(
|
|
1
|
+
import { h as y, r as q, c as w, t as g, a as d, f as E, N as p, l as x } from "./retry-DUbMIN1-.js";
|
|
2
|
+
import { f as C } from "./request.fetch-3tu4c4YP.js";
|
|
3
|
+
import T from "node:http";
|
|
4
|
+
import U from "node:https";
|
|
5
|
+
const j = async function(o, a, u) {
|
|
6
|
+
return y(await q(L, o, a, u), o, a, u);
|
|
7
|
+
}, L = async function(o, a, u) {
|
|
8
|
+
const t = await w(o, a, u);
|
|
9
9
|
if (!g(t.url))
|
|
10
10
|
return {
|
|
11
11
|
url: t.url,
|
|
@@ -15,7 +15,7 @@ const C = async function(e, a, u) {
|
|
|
15
15
|
headers: {},
|
|
16
16
|
body: ""
|
|
17
17
|
};
|
|
18
|
-
const l = /^https:\/\//i.test(t.url) ?
|
|
18
|
+
const l = /^https:\/\//i.test(t.url) ? U : T, n = new URL(t.url), i = t.params;
|
|
19
19
|
i instanceof Object && Object.keys(i).forEach((r) => n.searchParams.set(r, i[r]));
|
|
20
20
|
const R = t.method === "HEAD";
|
|
21
21
|
return new Promise((r) => {
|
|
@@ -26,31 +26,31 @@ const C = async function(e, a, u) {
|
|
|
26
26
|
method: t.method,
|
|
27
27
|
timeout: t.timeout > 0 ? t.timeout : void 0
|
|
28
28
|
},
|
|
29
|
-
function(
|
|
29
|
+
function(e) {
|
|
30
30
|
const f = [];
|
|
31
|
-
|
|
31
|
+
e.on("data", (m) => f.push(m)), e.on("end", () => {
|
|
32
32
|
const m = E(
|
|
33
|
-
Object.entries(
|
|
33
|
+
Object.entries(e.headers).map(([b, h]) => [b.toLowerCase(), Array.isArray(h) ? h.join(",") : h])
|
|
34
34
|
);
|
|
35
35
|
r({
|
|
36
36
|
url: n.toString(),
|
|
37
37
|
method: t.method,
|
|
38
|
-
status:
|
|
39
|
-
statusText:
|
|
38
|
+
status: e.statusCode || -1,
|
|
39
|
+
statusText: e.statusMessage || d.Unknown,
|
|
40
40
|
headers: m,
|
|
41
|
-
body: R ? "" : Buffer.concat(f).toString("utf-8")
|
|
41
|
+
body: R || e.statusCode === 204 ? "" : Buffer.concat(f).toString("utf-8")
|
|
42
42
|
});
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
45
|
);
|
|
46
|
-
c.on("error", (
|
|
46
|
+
c.on("error", (e) => {
|
|
47
47
|
r({
|
|
48
48
|
url: n.toString(),
|
|
49
49
|
method: t.method,
|
|
50
50
|
status: -1,
|
|
51
51
|
statusText: d.Unknown,
|
|
52
52
|
body: "",
|
|
53
|
-
rawError:
|
|
53
|
+
rawError: e
|
|
54
54
|
});
|
|
55
55
|
}), c.on("timeout", () => {
|
|
56
56
|
r({
|
|
@@ -63,17 +63,17 @@ const C = async function(e, a, u) {
|
|
|
63
63
|
}), t.body && c.write(t.body), c.end();
|
|
64
64
|
});
|
|
65
65
|
};
|
|
66
|
-
function N(
|
|
67
|
-
return x.fetch ? new p(
|
|
66
|
+
function N(o) {
|
|
67
|
+
return x.fetch ? new p(C, o) : new p(j, o);
|
|
68
68
|
}
|
|
69
|
-
const s = N(), k = s.setConfig,
|
|
69
|
+
const s = N(), k = s.setConfig, P = s.head, B = s.get, D = s.post, F = s.del, G = s.put, I = s.patch;
|
|
70
70
|
export {
|
|
71
71
|
N as NetRequest,
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
72
|
+
F as del,
|
|
73
|
+
B as get,
|
|
74
|
+
P as head,
|
|
75
|
+
I as patch,
|
|
76
|
+
D as post,
|
|
77
|
+
G as put,
|
|
78
78
|
k as setGlobalConfig
|
|
79
79
|
};
|
|
@@ -1,16 +1,16 @@
|
|
|
1
|
-
import { h as
|
|
2
|
-
const
|
|
3
|
-
return
|
|
4
|
-
},
|
|
5
|
-
const t = await y(r,
|
|
6
|
-
i instanceof Object && Object.keys(i).forEach((e) =>
|
|
1
|
+
import { h as m, r as w, c as y, l as E, f as R, a as u } from "./retry-DUbMIN1-.js";
|
|
2
|
+
const x = async function(r, s, o) {
|
|
3
|
+
return m(await w(g, r, s, o), r, s, o);
|
|
4
|
+
}, g = async function(r, s, o) {
|
|
5
|
+
const t = await y(r, s, o), n = new URL(t.url), i = t.params;
|
|
6
|
+
i instanceof Object && Object.keys(i).forEach((e) => n.searchParams.set(e, i[e]));
|
|
7
7
|
const a = E.AbortController ? new AbortController() : null;
|
|
8
8
|
function c() {
|
|
9
9
|
a && !a.signal.aborted && a.abort();
|
|
10
10
|
}
|
|
11
11
|
t.abort && t.abort.addEventListener("abort", c);
|
|
12
12
|
const h = t.timeout > 0 ? setTimeout(c, t.timeout) : null, l = t.method === "HEAD";
|
|
13
|
-
return await fetch(
|
|
13
|
+
return await fetch(n, {
|
|
14
14
|
method: t.method,
|
|
15
15
|
headers: Object.keys(t.headers).length > 0 ? new Headers(t.headers) : void 0,
|
|
16
16
|
body: t.body,
|
|
@@ -18,28 +18,19 @@ const T = async function(r, o, n) {
|
|
|
18
18
|
signal: a == null ? void 0 : a.signal,
|
|
19
19
|
redirect: "follow"
|
|
20
20
|
}).then(async (e) => {
|
|
21
|
-
const
|
|
22
|
-
|
|
23
|
-
url: s.toString(),
|
|
24
|
-
method: t.method,
|
|
25
|
-
status: e.status,
|
|
26
|
-
statusText: e.statusText || d.Unknown,
|
|
27
|
-
body: "",
|
|
28
|
-
headers: b,
|
|
29
|
-
rawError: u
|
|
30
|
-
} : {
|
|
31
|
-
url: s.toString(),
|
|
21
|
+
const b = {
|
|
22
|
+
url: n.toString(),
|
|
32
23
|
method: t.method,
|
|
33
24
|
status: e.status,
|
|
34
25
|
statusText: e.statusText,
|
|
35
|
-
headers:
|
|
36
|
-
|
|
37
|
-
};
|
|
26
|
+
headers: R([...e.headers.entries()])
|
|
27
|
+
}, d = l || e.status === 204 ? "" : await e.text().catch((f) => f);
|
|
28
|
+
return d instanceof Error ? { ...b, body: "", statusText: u.Unknown, rawError: d } : { ...b, body: d };
|
|
38
29
|
}).catch((e) => ({
|
|
39
|
-
url:
|
|
30
|
+
url: n.toString(),
|
|
40
31
|
method: t.method,
|
|
41
32
|
status: -1,
|
|
42
|
-
statusText: a != null && a.signal.aborted ?
|
|
33
|
+
statusText: a != null && a.signal.aborted ? u.Aborted : u.NetworkError,
|
|
43
34
|
body: "",
|
|
44
35
|
rawError: e
|
|
45
36
|
})).finally(() => {
|
|
@@ -47,5 +38,5 @@ const T = async function(r, o, n) {
|
|
|
47
38
|
});
|
|
48
39
|
};
|
|
49
40
|
export {
|
|
50
|
-
|
|
41
|
+
x as f
|
|
51
42
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";const s=require("./retry-C2OSRA9r.cjs"),m=async function(a,o,n){return s.handleResponse(await s.retryRequest(w,a,o,n),a,o,n)},w=async function(a,o,n){const t=await s.convertOptions(a,o,n),i=new URL(t.url),c=t.params;c instanceof Object&&Object.keys(c).forEach(e=>i.searchParams.set(e,c[e]));const r=s.lt.AbortController?new AbortController:null;function u(){r&&!r.signal.aborted&&r.abort()}t.abort&&t.abort.addEventListener("abort",u);const h=t.timeout>0?setTimeout(u,t.timeout):null,b=t.method==="HEAD";return await fetch(i,{method:t.method,headers:Object.keys(t.headers).length>0?new Headers(t.headers):void 0,body:t.body,credentials:t.credentials,signal:r==null?void 0:r.signal,redirect:"follow"}).then(async e=>{const l={url:i.toString(),method:t.method,status:e.status,statusText:e.statusText,headers:s.fromEntries([...e.headers.entries()])},d=b||e.status===204?"":await e.text().catch(f=>f);return d instanceof Error?{...l,body:"",statusText:s.RequestInteralError.Unknown,rawError:d}:{...l,body:d}}).catch(e=>({url:i.toString(),method:t.method,status:-1,statusText:r!=null&&r.signal.aborted?s.RequestInteralError.Aborted:s.RequestInteralError.NetworkError,body:"",rawError:e})).finally(()=>{h!==null&&clearTimeout(h),t.abort&&t.abort.removeEventListener("abort",u)})};exports.fetchRequest=m;
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
"use strict";var G=Object.defineProperty;var J=(e,t,s)=>t in e?G(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s;var y=(e,t,s)=>J(e,typeof t!="symbol"?t+"":t,s);const h=typeof globalThis<"u"?globalThis:typeof global<"u"?global:typeof self<"u"?self:typeof window<"u"?window:{},R={wx:"wx"in h&&D(h.wx),fetch:"fetch"in h&&w(h.fetch),window:"window"in h&&D(h.window),Blob:"Blob"in h&&w(h.Blob),FormData:"FormData"in h&&w(h.FormData),TextDecoder:"TextDecoder"in h&&w(h.TextDecoder),AbortController:"AbortController"in h&&w(h.AbortController)};function w(e){return typeof e=="function"}function D(e){return typeof e=="object"&&e!==null}function W(e){return new Promise(t=>setTimeout(t,Math.max(0,e)))}function V(){}function Z(e){const t=e||"CustomError";return class extends Error{constructor(s){super(s),Object.defineProperty(this,"name",{value:t,enumerable:!1,configurable:!0}),"setPrototypeOf"in Object&&Object.setPrototypeOf(this,new.target.prototype),"captureStackTrace"in Error&&typeof Error.captureStackTrace=="function"&&Error.captureStackTrace(this,this.constructor)}}}const Q=/^(?:https?:)?\/\/.+$/i,z=/^https?:\/\/.+$/i,Y=/^\{[\d\D]*\}$/,X=/^\[[\d\D]*\]$/;function O(e,t=!1){return t?Q.test(e):z.test(e)}function ee(e){return e==null}function v(e,...t){if(!e||typeof e!="object")return!1;const s=Object.getPrototypeOf(e);return s!==Object.prototype&&s!==null?!1:t.every(r=>r in e)}function _(e){return Y.test(e)||X.test(e)}function $(e){return e?e[0].toLowerCase()+e.slice(1):""}const B=/_\w*/,H=/-\w*/;function M(e){const t=B.test(e)?e.replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(s,r)=>r.toUpperCase()):H.test(e)?e.replace(/(?:^-*|-*$)/g,"").replace(/-+(\w)/g,(s,r)=>r.toUpperCase()):e;return $(t)}function L(e){return B.test(e)?$(e).replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(t,s)=>"_"+s.toLowerCase()):H.test(e)?$(e.replace(/(?:^-*|-*$)/g,"")).replace(/-+(\w)/g,(t,s)=>"_"+s.toLowerCase()):$(e).replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`)}function T(e,t){try{const s=JSON.parse(e);return t?t(s)?s:null:s}catch{return null}}""+Math.random().toString(32).slice(2);function k(e,t=!1){return typeof e=="string"?M(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?k(s):typeof s=="object"&&s?k(s,!0):s):v(e)?Object.keys(e).reduce((s,r)=>{const o=M(String(r)),n=o.charAt(0).toLowerCase()+o.slice(1);return s[n]=t?e[r]:k(e[r]),s},{}):e}function E(e,t=!1){return typeof e=="string"?L(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?E(s):typeof s=="object"&&s?E(s,t):s):v(e)?Object.keys(e).reduce((s,r)=>{const o=L(String(r));return s[o]=t?e[r]:E(e[r]),s},{}):e}async function te(e){return await new Promise(function(t){const s=document.getElementsByTagName("head")[0],r=document.createElement("script");r.setAttribute("type","text/javascript"),r.setAttribute("charset","utf-8"),r.onload=function(){s.removeChild(r),t(!0)},r.onerror=function(){s.removeChild(r),t(!1)},r.setAttribute("src",e),s.appendChild(r)})}function se(e,t="数据未能正确识别"){return typeof e=="function"?{guard:e,message:t}:{guard:e.guard,message:e.message||t}}function re(e,t=""){return!t||O(e,!0)?x(e):(x(t)+"/"+e).replace(/\/{2,}/g,"/").replace(/:\//,"://")}function x(e){return O(e,!0)?e.startsWith("http")?e:("location"in globalThis?location.protocol:"https:")+e:("location"in globalThis?location.origin:"http://127.0.0.1")+"/"+e.replace(/^\/+/,"")}function ne(e,t){const s={};return(e.match(/([^=&#?]+)=[^&#]*/g)||[]).forEach(function(r){const o=r.split("="),n=o[0],a=decodeURIComponent(o[1]||"");s[n]!==void 0?s[n]+=","+a:s[n]=a}),t!==!0?s[t]||"":s}function oe(e,t){if(t){if(t===!0)return e.replace(/\?[^#]*/,"")}else return e;const s=e.split("#"),r=s[0].split("?"),o=r[0],n=r.length>1?r[1]:"",a=s.length>1?"#"+s[1]:"",c=typeof t=="string"?[t]:Array.isArray(t)?t:[];return!c.length||!n?s[0]+a:(c.map(i=>i.replace(/([\\(){}[\]^$+\-*?|])/g,"\\$1")),(o+"?"+n.replace(new RegExp("(?:^|&)(?:"+c.join("|")+")=[^&$]+","g"),"").replace(/^&/,"")).replace(/\?$/,"")+a)}function ae(e,t,s=!1){const r=typeof t=="string"?t:Object.keys(t).map(a=>`${a}=${encodeURIComponent(t[a])}`).join("&");if(!r)return e;const o=e.split("#");s&&(o[0]=oe(o[0],(r.match(/([^=&#?]+)=[^&#]+/g)||[]).map(a=>a.replace(/=.+$/,""))));const n=o[0].indexOf("?")+1?"&":"?";return(o[0]+n+r+(o.length>1?"#"+o[1]:"")).replace(/\?&/,"?")}function ce(e){const t=e.match(/(?:\?|&)([^=]+)(?:&|$)/g);return t?t.join("").replace(/(?:\?|^&+|&+$)/g,"").replace(/&{2}/g,"&").split("&").sort():[]}class ie{constructor(t=500){y(this,"ttl");y(this,"cache");this.cache={},this.ttl=Math.max(t,0)}getKey(t,s){const r=t.replace(/#.+/,""),o=r.replace(/\?.+/g,""),n=Object.assign(ne(r,!0),s),a=ce(r),c=Object.keys(n).sort().map(i=>`${i}#${n[i]}`);return`${o}_${c.join(",")}_${a.join(",")}`}updateTTL(t){this.ttl=Math.max(t,0)}get(t){if(this.ttl===0)return null;const s=this.cache[t];return s?s.ttl<Date.now()?(delete this.cache[t],null):s.res:null}set(t,s){this.ttl!==0&&(this.cache[t]={ttl:Date.now()+this.ttl,res:s})}}class q{constructor(t){y(this,"config",{baseURL:"",maxRetry:0,retryInterval:1e3,retryResolve:"network",timeout:1e4,cacheTTL:500,credentials:"same-origin",defaultTypeGuardMessage:"响应数据未能正确识别",responseRule:{ok:{resolve:"body"},failed:{resolve:"json",messageField:"message"}}});t&&this.set(t)}set(t){if(t.baseURL&&!/^\/.+/.test(t.baseURL)&&!O(t.baseURL))throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"),new Error("BaseURLError");Object.assign(this.config,t)}get(t){return this.config[t]}getFullUrl(t){return re(t,this.config.baseURL)}showMessage(t,s,r,o){this.config.messageHandler&&s&&this.config.messageHandler(t,s,r,o)}}var C=(e=>(e.UnexpectResponse="UnexpectResponse",e.Aborted="Aborted",e.Unknown="Unknown",e.NetworkError="NetworkError",e.Timeout="Timeout",e.NotSupport="NotSupport",e.URLFormatError="URLFormatError",e))(C||{});function ue(e,t,s,r){if(t.ok&&!ee(t.data)&&r){const o=se(r,s.get("defaultTypeGuardMessage"));return o.guard(t.data)||(t.code=C.UnexpectResponse,s.showMessage(!0,`${e} ${o.message}`,t.code,t.status),console.error(t.code,e,t.data),t.data=null,t.message=o.message),t}return t}class le{constructor(t,s){y(this,"agent");y(this,"config");y(this,"cache");this.config=new q(s),this.agent=t,this.cache=new ie(this.config.get("cacheTTL")),this.setConfig=this.setConfig.bind(this),this.getConfig=this.getConfig.bind(this),this.request=this.request.bind(this),this.get=this.get.bind(this),this.post=this.post.bind(this),this.del=this.del.bind(this),this.patch=this.patch.bind(this),this.put=this.put.bind(this),this.head=this.head.bind(this)}async request(t,s){try{return await this.agent(t,this.config,s)}catch(r){return console.error("RequestError",r),{ok:!1,status:-9,code:"Unkonwn",message:String(r),headers:{},data:null}}}async guard(t,s,r){return ue(t,s,this.config,r)}setConfig(t){this.config.set(t),this.cache.updateTTL(this.config.get("cacheTTL"))}getConfig(t){return this.config.get(t)}async head(t,s){const r=Object.assign({},s||null);return r.method="HEAD",this.guard(t,await this.request(t,r),null)}async get(t,s,r){const o=Object.assign({},r||null);o.method="GET";const n=this.cache.getKey(t,o.params),a=this.cache.get(n);if(a)return this.guard(t,await a,s||null);const c=this.request(t,o);return this.cache.set(n,c),this.guard(t,await c,s||null)}async post(t,s,r,o){const n=Object.assign({},o||null);return n.method="POST",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}async del(t,s,r){const o=Object.assign({},r||null);return o.method="DELETE",this.guard(t,await this.request(t,o),s||null)}async put(t,s,r,o){const n=Object.assign({},o||null);return n.method="PUT",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}async patch(t,s,r,o){const n=Object.assign({},o||null);return n.method="PATCH",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}}async function he(e,t,s){var m;const r=Object.assign({method:"GET"},s),o=R.FormData?r.body instanceof FormData:!1,n=o&&r.method!=="POST"&&r.method!=="PUT"?"POST":r.method,a=n==="GET"||n==="HEAD"||n==="DELETE";a&&r.body!==void 0&&(console.warn("request body is invalid with method get, head, delete"),delete r.body);const c=Object.assign(o||a?{}:{"Content-Type":R.Blob&&r.body instanceof Blob?r.body.type||"application/octet-stream":"application/json;charset=utf-8"},r.headers),i=r.params||{},d={};Object.keys(i).forEach(b=>{i[b]!==void 0&&(d[b]=de(i[b]))});const u=t.getFullUrl(e),l=fe(r.body),f=r.timeout||t.get("timeout"),g=await async function(){const b=t.get("requestTransformer");if(b)return await b({headers:c,params:d,method:n,url:u,body:l})}(),p=typeof g=="string"&&g?g:u;return(m=t.get("logHandler"))==null||m({type:"ready",url:p,method:n,headers:c,timeout:f,body:l}),{url:p,method:n,body:l,params:d,headers:c,timeout:f,abort:r.abort,credentials:r.credentials||t.get("credentials")||"same-origin"}}function de(e){return typeof e=="string"?e:Array.isArray(e)?e.join(","):e+""}function fe(e){if(e)return typeof e=="string"||e instanceof URLSearchParams||e instanceof ArrayBuffer||R.Blob&&e instanceof Blob||R.FormData&&e instanceof FormData?e:JSON.stringify(e)}const N="data",j="message";function ge(e,t,s,r,o){const n=o||r;return U(e)?je(n.ok||r.ok,e,t,s):pe(n.failed||r.failed,t,s)}const me=function(e){const t=[],s=e.failed||{resolve:"json"};switch(t.push("- 当http状态码 <200 或者 >=400 时"),s.resolve){case"body":t.push(" 将响应内容格式化为字符串并作为错误消息");break;case"json":t.push(" 将响应解析为json,并读取 "+(s.messageField||j)+" 作为错误消息");break}const r=e.ok||{resolve:"body"};switch(t.push("- 当http状态码 >=200 并且 <400 时"),r.resolve){case"body":t.push(" 将响应尝试解析为 json,并作为数据内容返回");break;case"json":t.push(" 将响应解析为 json,读取 "+(r.dataField||N)+" 作为响应数据,读取 "+(r.messageField||j)+" 作为提示消息"),r.statusField&&t.push(" 当 "+r.statusField+" 为 "+(r.statusOKValue||"空值")+" 时是成功提示,否则是错误消息"),r.ignoreMessage&&t.push(" 并忽略以下消息:"+r.ignoreMessage);break}return t.join(`
|
|
2
|
+
`)};function pe(e,t,s){const r=e||{resolve:"json",messageField:j},o={ok:!1,code:t,message:s,data:null};switch(r.resolve){case"body":o.message=P(s)||s;break;case"json":const{code:n,message:a}=ye(s,r.converter,r.statusField,r.messageField);o.code=n||t,o.message=P(s)||a;break}return o}function ye(e,t,s,r=j){if(!_(e))return{message:""};const o=A(T(e),t);return!o||!v(o)?{message:e}:{code:s?F(o,s):"",message:F(o,r)||e}}function F(e,t){const s=Array.isArray(t)?t:[t];for(const r of s)if(r in e)return be(e[r]);return""}function be(e){return e?typeof e=="string"?e:JSON.stringify(e):""}const we=/<title>([^<]+)<\/title>/i,Re=/<message>([^<]+)<\/message>/i;function P(e){const t=e.match(we);if(t)return t[1];const s=e.match(Re);return s?s[1]:""}function je(e,t,s,r){const o=e||{resolve:"body"},n={ok:!0,code:s,message:"",data:null};if(t===204||!r)return n;if(o.resolve==="body")return n.data=_(r)?A(T(r),e.converter):r,n;const a=A(T(r),e.converter);if(!a||!v(a))return n.ok=!1,n.code="ResponseFormatError",n.message="响应内容无法格式化为 Object",n;const c=o.statusField,i=o.statusOKValue||"",d=o.dataField||N,u=o.messageField||j,l=o.ignoreMessage||"";if(c&&!(c in a))return n.ok=!1,n.code="ResponseFieldMissing",n.message="响应内容找不到状态字段 "+c,n;const f=c?a[c]+"":"";return n.ok=c?f===i:!0,n.code=f||s,n.data=d===!0?a:d in a?a[d]:null,n.message=F(a,u),l&&n.message&&(Array.isArray(l)&&l.includes(n.message)||typeof l=="string"&&n.message===l)&&(n.message=""),n}function U(e){return e>=200&&e<400}function A(e,t){return t==="camelize"?k(e):t==="snakify"?E(e):e}const $e=Z("APIError");function ke(e){const t={};for(const o in e.headers)(o.startsWith("x-")||o.includes("trace")||o.includes("server")||/\b(?:id|uuid)\b/.test(o))&&(t[o]=e.headers[o]);const s=e.url.replace(/^(?:https?:)?\/*/i,"").replace(/\?.+/,""),r=e.status<0?"unknown":e.status;return{sentryError:new $e(`${s} | ${r}${e.code?` | ${e.code}`:""}`),sentryTags:{...t,status:r,method:e.method,code:e.code||"unknown",message:e.message||"empty"},sentryExtra:{url:e.url,responseBody:e.body||"empty",responseHeaders:e.headers,rawError:e.error}}}function I(e){return e.reduce((t,[s,r])=>(s&&(t[s]=r||""),t),{})}function Ee(e,t,s,r){var g;const o=e.status,n=e.method,a=I(Object.entries(e.headers||{}).map(([p,m])=>[p.toLowerCase(),m])),{ok:c,code:i,data:d,message:u}=ge(o,e.statusText,e.body,s.get("responseRule"),r==null?void 0:r.responseRule);if(!U(o)){const p=ke({url:e.url,method:e.method,status:o,code:i,message:u,body:e.body,headers:a,error:e.rawError});(s.get("errorHandler")||Oe)({url:t,method:n,status:o,code:i,message:u,headers:a,rawError:e.rawError,responseBody:e.body,...p})}if(o<0)return S({ok:!1,status:o,code:e.statusText,headers:{},message:"",data:null},`${n} ${t} ${e.statusText}`,n,t,s,r);const l={ok:c,data:d,code:i,message:u,status:o,headers:a};(g=s.get("responseHandler"))==null||g({...l},n,t);const f=c?u:u||e.statusText;return S(l,f,n,t,s,r)}function S(e,t,s,r,o,n){const a=o.get("message"),c=a===!1||(n==null?void 0:n.message)===!1?!1:(n==null?void 0:n.message)||a;if(c!==!1){const i=typeof c=="function"?c(e,s,r,t):t;i instanceof Error?o.showMessage(!0,i.message,e.code,e.status):i&&typeof i=="object"&&"message"in i?o.showMessage(!1,i.message,e.code,e.status):o.showMessage(!e.ok,i,e.code,e.status)}return e}function Oe(e){const t={};for(const s in e)s.startsWith("sentry")||(t[s]=e[s]);console.error("RequestError",t)}async function K(e,t,s,r,o){const n=o||0,a=Math.max(0,Math.min(10,(r==null?void 0:r.maxRetry)??s.get("maxRetry")??0)),c=(r==null?void 0:r.retryResolve)??s.get("retryResolve"),i=s.get("logHandler")||V;i({type:"prepear",url:t,method:(r==null?void 0:r.method)||"GET",retry:n,maxRetry:a,message:n===0?"start":`retry ${n}/${a} start`,headers:r==null?void 0:r.headers,options:r});const d=Date.now(),u=await e(t,s,r),l=u.status,f=Date.now()-d,g=`[cost ${f}][${l}] ${l<0?u.body:""}`;i({type:"finished",url:t,method:u.method,retry:n,maxRetry:a,message:n===0?`finish ${g}`:`retry ${n}/${a} finish ${g}`,response:u,headers:u.headers,cost:f});const p=U(l);if(!a||c==="network"&&l>0||c==="status"&&p||Array.isArray(c)&&!c.includes(l)||typeof c=="function"&&c(u,n)!==!0||n>=a)return u;const m=(r==null?void 0:r.retryInterval)??s.get("retryInterval")??1e3;return await W(Math.max(100,m==="2EB"?Math.pow(2,n)*1e3:typeof m=="function"?m(n+1)||0:m)),await K(e,t,s,r,n+1)}exports.Ie=te;exports.NetRequestHandler=le;exports.RequestGlobalConfig=q;exports.RequestInteralError=C;exports.Zt=ae;exports.convertOptions=he;exports.fromEntries=I;exports.getResponseRulesDescription=me;exports.handleResponse=Ee;exports.lt=R;exports.retryRequest=K;exports.tt=O;
|
|
@@ -1,10 +1,10 @@
|
|
|
1
|
-
var
|
|
2
|
-
var
|
|
3
|
-
var
|
|
4
|
-
const h = typeof globalThis < "u" ? globalThis : typeof global < "u" ? global : typeof self < "u" ? self : typeof window < "u" ? window : {},
|
|
5
|
-
wx: "wx" in h &&
|
|
1
|
+
var q = Object.defineProperty;
|
|
2
|
+
var K = (e, t, s) => t in e ? q(e, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : e[t] = s;
|
|
3
|
+
var y = (e, t, s) => K(e, typeof t != "symbol" ? t + "" : t, s);
|
|
4
|
+
const h = typeof globalThis < "u" ? globalThis : typeof global < "u" ? global : typeof self < "u" ? self : typeof window < "u" ? window : {}, E = {
|
|
5
|
+
wx: "wx" in h && U(h.wx),
|
|
6
6
|
fetch: "fetch" in h && w(h.fetch),
|
|
7
|
-
window: "window" in h &&
|
|
7
|
+
window: "window" in h && U(h.window),
|
|
8
8
|
Blob: "Blob" in h && w(h.Blob),
|
|
9
9
|
FormData: "FormData" in h && w(h.FormData),
|
|
10
10
|
TextDecoder: "TextDecoder" in h && w(h.TextDecoder),
|
|
@@ -13,7 +13,7 @@ const h = typeof globalThis < "u" ? globalThis : typeof global < "u" ? global :
|
|
|
13
13
|
function w(e) {
|
|
14
14
|
return typeof e == "function";
|
|
15
15
|
}
|
|
16
|
-
function
|
|
16
|
+
function U(e) {
|
|
17
17
|
return typeof e == "object" && e !== null;
|
|
18
18
|
}
|
|
19
19
|
function I(e) {
|
|
@@ -33,33 +33,33 @@ function J(e) {
|
|
|
33
33
|
}
|
|
34
34
|
};
|
|
35
35
|
}
|
|
36
|
-
const
|
|
36
|
+
const W = /^(?:https?:)?\/\/.+$/i, V = /^https?:\/\/.+$/i, Z = /^\{[\d\D]*\}$/, Q = /^\[[\d\D]*\]$/;
|
|
37
37
|
function A(e, t = !1) {
|
|
38
|
-
return t ?
|
|
38
|
+
return t ? W.test(e) : V.test(e);
|
|
39
39
|
}
|
|
40
|
-
function
|
|
40
|
+
function z(e) {
|
|
41
41
|
return e == null;
|
|
42
42
|
}
|
|
43
|
-
function
|
|
43
|
+
function O(e, ...t) {
|
|
44
44
|
if (!e || typeof e != "object") return !1;
|
|
45
45
|
const s = Object.getPrototypeOf(e);
|
|
46
46
|
return s !== Object.prototype && s !== null ? !1 : t.every((r) => r in e);
|
|
47
47
|
}
|
|
48
|
-
function
|
|
49
|
-
return
|
|
48
|
+
function S(e) {
|
|
49
|
+
return Z.test(e) || Q.test(e);
|
|
50
50
|
}
|
|
51
51
|
function $(e) {
|
|
52
52
|
return e ? e[0].toLowerCase() + e.slice(1) : "";
|
|
53
53
|
}
|
|
54
|
-
const _ = /_\w*/,
|
|
55
|
-
function
|
|
56
|
-
const t = _.test(e) ? e.replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (s, r) => r.toUpperCase()) :
|
|
54
|
+
const _ = /_\w*/, B = /-\w*/;
|
|
55
|
+
function D(e) {
|
|
56
|
+
const t = _.test(e) ? e.replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (s, r) => r.toUpperCase()) : B.test(e) ? e.replace(/(?:^-*|-*$)/g, "").replace(/-+(\w)/g, (s, r) => r.toUpperCase()) : e;
|
|
57
57
|
return $(t);
|
|
58
58
|
}
|
|
59
59
|
function M(e) {
|
|
60
|
-
return _.test(e) ? $(e).replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (t, s) => "_" + s.toLowerCase()) :
|
|
60
|
+
return _.test(e) ? $(e).replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (t, s) => "_" + s.toLowerCase()) : B.test(e) ? $(e.replace(/(?:^-*|-*$)/g, "")).replace(/-+(\w)/g, (t, s) => "_" + s.toLowerCase()) : $(e).replace(/[A-Z]/g, (t) => `_${t.toLowerCase()}`);
|
|
61
61
|
}
|
|
62
|
-
function
|
|
62
|
+
function T(e, t) {
|
|
63
63
|
try {
|
|
64
64
|
const s = JSON.parse(e);
|
|
65
65
|
return t ? t(s) ? s : null : s;
|
|
@@ -69,24 +69,24 @@ function v(e, t) {
|
|
|
69
69
|
}
|
|
70
70
|
"" + Math.random().toString(32).slice(2);
|
|
71
71
|
function R(e, t = !1) {
|
|
72
|
-
return typeof e == "string" ?
|
|
72
|
+
return typeof e == "string" ? D(e) : Array.isArray(e) ? e.map((s) => !t || typeof s == "string" ? R(s) : typeof s == "object" && s ? R(s, !0) : s) : O(e) ? Object.keys(e).reduce(
|
|
73
73
|
(s, r) => {
|
|
74
|
-
const a =
|
|
74
|
+
const a = D(String(r)), n = a.charAt(0).toLowerCase() + a.slice(1);
|
|
75
75
|
return s[n] = t ? e[r] : R(e[r]), s;
|
|
76
76
|
},
|
|
77
77
|
{}
|
|
78
78
|
) : e;
|
|
79
79
|
}
|
|
80
|
-
function
|
|
81
|
-
return typeof e == "string" ? M(e) : Array.isArray(e) ? e.map((s) => !t || typeof s == "string" ?
|
|
80
|
+
function k(e, t = !1) {
|
|
81
|
+
return typeof e == "string" ? M(e) : Array.isArray(e) ? e.map((s) => !t || typeof s == "string" ? k(s) : typeof s == "object" && s ? k(s, t) : s) : O(e) ? Object.keys(e).reduce(
|
|
82
82
|
(s, r) => {
|
|
83
83
|
const a = M(String(r));
|
|
84
|
-
return s[a] = t ? e[r] :
|
|
84
|
+
return s[a] = t ? e[r] : k(e[r]), s;
|
|
85
85
|
},
|
|
86
86
|
{}
|
|
87
87
|
) : e;
|
|
88
88
|
}
|
|
89
|
-
async function
|
|
89
|
+
async function $e(e) {
|
|
90
90
|
return await new Promise(function(t) {
|
|
91
91
|
const s = document.getElementsByTagName("head")[0], r = document.createElement("script");
|
|
92
92
|
r.setAttribute("type", "text/javascript"), r.setAttribute("charset", "utf-8"), r.onload = function() {
|
|
@@ -96,7 +96,7 @@ async function Re(e) {
|
|
|
96
96
|
}, r.setAttribute("src", e), s.appendChild(r);
|
|
97
97
|
});
|
|
98
98
|
}
|
|
99
|
-
function
|
|
99
|
+
function Y(e, t = "数据未能正确识别") {
|
|
100
100
|
return typeof e == "function" ? {
|
|
101
101
|
guard: e,
|
|
102
102
|
message: t
|
|
@@ -126,7 +126,7 @@ function te(e, t) {
|
|
|
126
126
|
const s = e.split("#"), r = s[0].split("?"), a = r[0], n = r.length > 1 ? r[1] : "", o = s.length > 1 ? "#" + s[1] : "", c = typeof t == "string" ? [t] : Array.isArray(t) ? t : [];
|
|
127
127
|
return !c.length || !n ? s[0] + o : (c.map((i) => i.replace(/([\\(){}[\]^$+\-*?|])/g, "\\$1")), (a + "?" + n.replace(new RegExp("(?:^|&)(?:" + c.join("|") + ")=[^&$]+", "g"), "").replace(/^&/, "")).replace(/\?$/, "") + o);
|
|
128
128
|
}
|
|
129
|
-
function
|
|
129
|
+
function Re(e, t, s = !1) {
|
|
130
130
|
const r = typeof t == "string" ? t : Object.keys(t).map((o) => `${o}=${encodeURIComponent(t[o])}`).join("&");
|
|
131
131
|
if (!r)
|
|
132
132
|
return e;
|
|
@@ -144,8 +144,8 @@ function se(e) {
|
|
|
144
144
|
}
|
|
145
145
|
class re {
|
|
146
146
|
constructor(t = 500) {
|
|
147
|
-
|
|
148
|
-
|
|
147
|
+
y(this, "ttl");
|
|
148
|
+
y(this, "cache");
|
|
149
149
|
this.cache = {}, this.ttl = Math.max(t, 0);
|
|
150
150
|
}
|
|
151
151
|
getKey(t, s) {
|
|
@@ -171,7 +171,7 @@ class re {
|
|
|
171
171
|
class ne {
|
|
172
172
|
constructor(t) {
|
|
173
173
|
// 保存的配置需要部分字段强制设置默认值
|
|
174
|
-
|
|
174
|
+
y(this, "config", {
|
|
175
175
|
baseURL: "",
|
|
176
176
|
maxRetry: 0,
|
|
177
177
|
retryInterval: 1e3,
|
|
@@ -179,6 +179,7 @@ class ne {
|
|
|
179
179
|
timeout: 1e4,
|
|
180
180
|
cacheTTL: 500,
|
|
181
181
|
credentials: "same-origin",
|
|
182
|
+
defaultTypeGuardMessage: "响应数据未能正确识别",
|
|
182
183
|
responseRule: {
|
|
183
184
|
ok: {
|
|
184
185
|
resolve: "body"
|
|
@@ -208,19 +209,19 @@ class ne {
|
|
|
208
209
|
this.config.messageHandler && s && this.config.messageHandler(t, s, r, a);
|
|
209
210
|
}
|
|
210
211
|
}
|
|
211
|
-
|
|
212
|
+
var H = /* @__PURE__ */ ((e) => (e.UnexpectResponse = "UnexpectResponse", e.Aborted = "Aborted", e.Unknown = "Unknown", e.NetworkError = "NetworkError", e.Timeout = "Timeout", e.NotSupport = "NotSupport", e.URLFormatError = "URLFormatError", e))(H || {});
|
|
212
213
|
function ae(e, t, s, r) {
|
|
213
|
-
if (t.ok && !
|
|
214
|
-
const a =
|
|
215
|
-
return a.guard(t.data) || (s.showMessage(!0, `${e} ${a.message}`, t.code, t.status), console.error(
|
|
214
|
+
if (t.ok && !z(t.data) && r) {
|
|
215
|
+
const a = Y(r, s.get("defaultTypeGuardMessage"));
|
|
216
|
+
return a.guard(t.data) || (t.code = H.UnexpectResponse, s.showMessage(!0, `${e} ${a.message}`, t.code, t.status), console.error(t.code, e, t.data), t.data = null, t.message = a.message), t;
|
|
216
217
|
}
|
|
217
218
|
return t;
|
|
218
219
|
}
|
|
219
|
-
class
|
|
220
|
+
class ke {
|
|
220
221
|
constructor(t, s) {
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
222
|
+
y(this, "agent");
|
|
223
|
+
y(this, "config");
|
|
224
|
+
y(this, "cache");
|
|
224
225
|
this.config = new ne(s), this.agent = t, this.cache = new re(this.config.get("cacheTTL")), this.setConfig = this.setConfig.bind(this), this.getConfig = this.getConfig.bind(this), this.request = this.request.bind(this), this.get = this.get.bind(this), this.post = this.post.bind(this), this.del = this.del.bind(this), this.patch = this.patch.bind(this), this.put = this.put.bind(this), this.head = this.head.bind(this);
|
|
225
226
|
}
|
|
226
227
|
/**
|
|
@@ -291,33 +292,33 @@ class Oe {
|
|
|
291
292
|
return n.method = "PATCH", n.body = s || {}, this.guard(t, await this.request(t, n), r || null);
|
|
292
293
|
}
|
|
293
294
|
}
|
|
294
|
-
async function
|
|
295
|
+
async function Ee(e, t, s) {
|
|
295
296
|
var m;
|
|
296
|
-
const r = Object.assign({ method: "GET" }, s), a =
|
|
297
|
+
const r = Object.assign({ method: "GET" }, s), a = E.FormData ? r.body instanceof FormData : !1, n = a && r.method !== "POST" && r.method !== "PUT" ? "POST" : r.method, o = n === "GET" || n === "HEAD" || n === "DELETE";
|
|
297
298
|
o && r.body !== void 0 && (console.warn("request body is invalid with method get, head, delete"), delete r.body);
|
|
298
299
|
const c = Object.assign(
|
|
299
300
|
a || o ? {} : {
|
|
300
|
-
"Content-Type":
|
|
301
|
+
"Content-Type": E.Blob && r.body instanceof Blob ? r.body.type || "application/octet-stream" : "application/json;charset=utf-8"
|
|
301
302
|
},
|
|
302
303
|
r.headers
|
|
303
|
-
), i = r.params || {},
|
|
304
|
+
), i = r.params || {}, d = {};
|
|
304
305
|
Object.keys(i).forEach((b) => {
|
|
305
|
-
i[b] !== void 0 && (
|
|
306
|
+
i[b] !== void 0 && (d[b] = oe(i[b]));
|
|
306
307
|
});
|
|
307
|
-
const u = t.getFullUrl(e), l = ce(r.body),
|
|
308
|
+
const u = t.getFullUrl(e), l = ce(r.body), f = r.timeout || t.get("timeout"), g = await async function() {
|
|
308
309
|
const b = t.get("requestTransformer");
|
|
309
310
|
if (b)
|
|
310
|
-
return await b({ headers: c, params:
|
|
311
|
-
}(),
|
|
312
|
-
return (m = t.get("logHandler")) == null || m({ type: "ready", url:
|
|
313
|
-
url:
|
|
311
|
+
return await b({ headers: c, params: d, method: n, url: u, body: l });
|
|
312
|
+
}(), p = typeof g == "string" && g ? g : u;
|
|
313
|
+
return (m = t.get("logHandler")) == null || m({ type: "ready", url: p, method: n, headers: c, timeout: f, body: l }), {
|
|
314
|
+
url: p,
|
|
314
315
|
method: n,
|
|
315
316
|
body: l,
|
|
316
|
-
params:
|
|
317
|
+
params: d,
|
|
317
318
|
headers: c,
|
|
318
|
-
timeout:
|
|
319
|
+
timeout: f,
|
|
319
320
|
abort: r.abort,
|
|
320
|
-
credentials: r.credentials || t.get("credentials")
|
|
321
|
+
credentials: r.credentials || t.get("credentials") || "same-origin"
|
|
321
322
|
};
|
|
322
323
|
}
|
|
323
324
|
function oe(e) {
|
|
@@ -325,14 +326,14 @@ function oe(e) {
|
|
|
325
326
|
}
|
|
326
327
|
function ce(e) {
|
|
327
328
|
if (e)
|
|
328
|
-
return typeof e == "string" || e instanceof URLSearchParams || e instanceof ArrayBuffer ||
|
|
329
|
+
return typeof e == "string" || e instanceof URLSearchParams || e instanceof ArrayBuffer || E.Blob && e instanceof Blob || E.FormData && e instanceof FormData ? e : JSON.stringify(e);
|
|
329
330
|
}
|
|
330
|
-
const
|
|
331
|
+
const N = "data", j = "message";
|
|
331
332
|
function ie(e, t, s, r, a) {
|
|
332
333
|
const n = a || r;
|
|
333
334
|
return C(e) ? ge(n.ok || r.ok, e, t, s) : ue(n.failed || r.failed, t, s);
|
|
334
335
|
}
|
|
335
|
-
const
|
|
336
|
+
const Oe = function(e) {
|
|
336
337
|
const t = [], s = e.failed || { resolve: "json" };
|
|
337
338
|
switch (t.push("- 当http状态码 <200 或者 >=400 时"), s.resolve) {
|
|
338
339
|
case "body":
|
|
@@ -349,7 +350,7 @@ const ve = function(e) {
|
|
|
349
350
|
break;
|
|
350
351
|
case "json":
|
|
351
352
|
t.push(
|
|
352
|
-
" 将响应解析为 json,读取 " + (r.dataField ||
|
|
353
|
+
" 将响应解析为 json,读取 " + (r.dataField || N) + " 作为响应数据,读取 " + (r.messageField || j) + " 作为提示消息"
|
|
353
354
|
), r.statusField && t.push(
|
|
354
355
|
" 当 " + r.statusField + " 为 " + (r.statusOKValue || "空值") + " 时是成功提示,否则是错误消息"
|
|
355
356
|
), r.ignoreMessage && t.push(" 并忽略以下消息:" + r.ignoreMessage);
|
|
@@ -367,25 +368,25 @@ function ue(e, t, s) {
|
|
|
367
368
|
};
|
|
368
369
|
switch (r.resolve) {
|
|
369
370
|
case "body":
|
|
370
|
-
a.message =
|
|
371
|
+
a.message = x(s) || s;
|
|
371
372
|
break;
|
|
372
373
|
case "json":
|
|
373
374
|
const { code: n, message: o } = le(s, r.converter, r.statusField, r.messageField);
|
|
374
|
-
a.code = n || t, a.message =
|
|
375
|
+
a.code = n || t, a.message = x(s) || o;
|
|
375
376
|
break;
|
|
376
377
|
}
|
|
377
378
|
return a;
|
|
378
379
|
}
|
|
379
380
|
function le(e, t, s, r = j) {
|
|
380
|
-
if (!
|
|
381
|
+
if (!S(e))
|
|
381
382
|
return { message: "" };
|
|
382
|
-
const a = F(
|
|
383
|
-
return !a || !
|
|
384
|
-
code: s ?
|
|
385
|
-
message:
|
|
383
|
+
const a = F(T(e), t);
|
|
384
|
+
return !a || !O(a) ? { message: e } : {
|
|
385
|
+
code: s ? v(a, s) : "",
|
|
386
|
+
message: v(a, r) || e
|
|
386
387
|
};
|
|
387
388
|
}
|
|
388
|
-
function
|
|
389
|
+
function v(e, t) {
|
|
389
390
|
const s = Array.isArray(t) ? t : [t];
|
|
390
391
|
for (const r of s)
|
|
391
392
|
if (r in e)
|
|
@@ -395,12 +396,12 @@ function T(e, t) {
|
|
|
395
396
|
function he(e) {
|
|
396
397
|
return e ? typeof e == "string" ? e : JSON.stringify(e) : "";
|
|
397
398
|
}
|
|
398
|
-
const
|
|
399
|
-
function
|
|
400
|
-
const t = e.match(
|
|
399
|
+
const de = /<title>([^<]+)<\/title>/i, fe = /<message>([^<]+)<\/message>/i;
|
|
400
|
+
function x(e) {
|
|
401
|
+
const t = e.match(de);
|
|
401
402
|
if (t)
|
|
402
403
|
return t[1];
|
|
403
|
-
const s = e.match(
|
|
404
|
+
const s = e.match(fe);
|
|
404
405
|
return s ? s[1] : "";
|
|
405
406
|
}
|
|
406
407
|
function ge(e, t, s, r) {
|
|
@@ -413,24 +414,24 @@ function ge(e, t, s, r) {
|
|
|
413
414
|
if (t === 204 || !r)
|
|
414
415
|
return n;
|
|
415
416
|
if (a.resolve === "body")
|
|
416
|
-
return n.data =
|
|
417
|
-
const o = F(
|
|
418
|
-
if (!o || !
|
|
417
|
+
return n.data = S(r) ? F(T(r), e.converter) : r, n;
|
|
418
|
+
const o = F(T(r), e.converter);
|
|
419
|
+
if (!o || !O(o))
|
|
419
420
|
return n.ok = !1, n.code = "ResponseFormatError", n.message = "响应内容无法格式化为 Object", n;
|
|
420
|
-
const c = a.statusField, i = a.statusOKValue || "",
|
|
421
|
+
const c = a.statusField, i = a.statusOKValue || "", d = a.dataField || N, u = a.messageField || j, l = a.ignoreMessage || "";
|
|
421
422
|
if (c && !(c in o))
|
|
422
423
|
return n.ok = !1, n.code = "ResponseFieldMissing", n.message = "响应内容找不到状态字段 " + c, n;
|
|
423
|
-
const
|
|
424
|
-
return n.ok = c ?
|
|
424
|
+
const f = c ? o[c] + "" : "";
|
|
425
|
+
return n.ok = c ? f === i : !0, n.code = f || s, n.data = d === !0 ? o : d in o ? o[d] : null, n.message = v(o, u), l && n.message && (Array.isArray(l) && l.includes(n.message) || typeof l == "string" && n.message === l) && (n.message = ""), n;
|
|
425
426
|
}
|
|
426
427
|
function C(e) {
|
|
427
428
|
return e >= 200 && e < 400;
|
|
428
429
|
}
|
|
429
430
|
function F(e, t) {
|
|
430
|
-
return t === "camelize" ? R(e) : t === "snakify" ?
|
|
431
|
+
return t === "camelize" ? R(e) : t === "snakify" ? k(e) : e;
|
|
431
432
|
}
|
|
432
433
|
const me = J("APIError");
|
|
433
|
-
function
|
|
434
|
+
function pe(e) {
|
|
434
435
|
const t = {};
|
|
435
436
|
for (const a in e.headers)
|
|
436
437
|
(a.startsWith("x-") || a.includes("trace") || a.includes("server") || /\b(?:id|uuid)\b/.test(a)) && (t[a] = e.headers[a]);
|
|
@@ -452,7 +453,7 @@ function ye(e) {
|
|
|
452
453
|
}
|
|
453
454
|
};
|
|
454
455
|
}
|
|
455
|
-
function
|
|
456
|
+
function ye(e) {
|
|
456
457
|
return e.reduce(
|
|
457
458
|
(t, [s, r]) => (s && (t[s] = r || ""), t),
|
|
458
459
|
{}
|
|
@@ -460,9 +461,9 @@ function pe(e) {
|
|
|
460
461
|
}
|
|
461
462
|
function Te(e, t, s, r) {
|
|
462
463
|
var g;
|
|
463
|
-
const a = e.status, n = e.method, o =
|
|
464
|
-
Object.entries(e.headers || {}).map(([
|
|
465
|
-
), { ok: c, code: i, data:
|
|
464
|
+
const a = e.status, n = e.method, o = ye(
|
|
465
|
+
Object.entries(e.headers || {}).map(([p, m]) => [p.toLowerCase(), m])
|
|
466
|
+
), { ok: c, code: i, data: d, message: u } = ie(
|
|
466
467
|
a,
|
|
467
468
|
e.statusText,
|
|
468
469
|
e.body,
|
|
@@ -470,7 +471,7 @@ function Te(e, t, s, r) {
|
|
|
470
471
|
r == null ? void 0 : r.responseRule
|
|
471
472
|
);
|
|
472
473
|
if (!C(a)) {
|
|
473
|
-
const
|
|
474
|
+
const p = pe({
|
|
474
475
|
url: e.url,
|
|
475
476
|
method: e.method,
|
|
476
477
|
status: a,
|
|
@@ -489,11 +490,11 @@ function Te(e, t, s, r) {
|
|
|
489
490
|
headers: o,
|
|
490
491
|
rawError: e.rawError,
|
|
491
492
|
responseBody: e.body,
|
|
492
|
-
...
|
|
493
|
+
...p
|
|
493
494
|
});
|
|
494
495
|
}
|
|
495
496
|
if (a < 0)
|
|
496
|
-
return
|
|
497
|
+
return P(
|
|
497
498
|
{ ok: !1, status: a, code: e.statusText, headers: {}, message: "", data: null },
|
|
498
499
|
`${n} ${t} ${e.statusText}`,
|
|
499
500
|
n,
|
|
@@ -501,12 +502,12 @@ function Te(e, t, s, r) {
|
|
|
501
502
|
s,
|
|
502
503
|
r
|
|
503
504
|
);
|
|
504
|
-
const l = { ok: c, data:
|
|
505
|
+
const l = { ok: c, data: d, code: i, message: u, status: a, headers: o };
|
|
505
506
|
(g = s.get("responseHandler")) == null || g({ ...l }, n, t);
|
|
506
|
-
const
|
|
507
|
-
return
|
|
507
|
+
const f = c ? u : u || e.statusText;
|
|
508
|
+
return P(l, f, n, t, s, r);
|
|
508
509
|
}
|
|
509
|
-
function
|
|
510
|
+
function P(e, t, s, r, a, n) {
|
|
510
511
|
const o = a.get("message"), c = o === !1 || (n == null ? void 0 : n.message) === !1 ? !1 : (n == null ? void 0 : n.message) || o;
|
|
511
512
|
if (c !== !1) {
|
|
512
513
|
const i = typeof c == "function" ? c(e, s, r, t) : t;
|
|
@@ -532,7 +533,7 @@ async function we(e, t, s, r, a) {
|
|
|
532
533
|
headers: r == null ? void 0 : r.headers,
|
|
533
534
|
options: r
|
|
534
535
|
});
|
|
535
|
-
const
|
|
536
|
+
const d = Date.now(), u = await e(t, s, r), l = u.status, f = Date.now() - d, g = `[cost ${f}][${l}] ${l < 0 ? u.body : ""}`;
|
|
536
537
|
i({
|
|
537
538
|
type: "finished",
|
|
538
539
|
url: t,
|
|
@@ -542,10 +543,10 @@ async function we(e, t, s, r, a) {
|
|
|
542
543
|
message: n === 0 ? `finish ${g}` : `retry ${n}/${o} finish ${g}`,
|
|
543
544
|
response: u,
|
|
544
545
|
headers: u.headers,
|
|
545
|
-
cost:
|
|
546
|
+
cost: f
|
|
546
547
|
});
|
|
547
|
-
const
|
|
548
|
-
if (!o || c === "network" && l > 0 || c === "status" &&
|
|
548
|
+
const p = C(l);
|
|
549
|
+
if (!o || c === "network" && l > 0 || c === "status" && p || Array.isArray(c) && !c.includes(l) || typeof c == "function" && c(u, n) !== !0 || n >= o)
|
|
549
550
|
return u;
|
|
550
551
|
const m = (r == null ? void 0 : r.retryInterval) ?? s.get("retryInterval") ?? 1e3;
|
|
551
552
|
return await I(
|
|
@@ -555,18 +556,17 @@ async function we(e, t, s, r, a) {
|
|
|
555
556
|
)
|
|
556
557
|
), await we(e, t, s, r, n + 1);
|
|
557
558
|
}
|
|
558
|
-
var je = /* @__PURE__ */ ((e) => (e.Aborted = "Aborted", e.Unknown = "Unknown", e.Timeout = "Timeout", e.NotSupport = "NotSupport", e.URLFormatError = "URLFormatError", e))(je || {});
|
|
559
559
|
export {
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
Re as M,
|
|
563
|
-
Oe as N,
|
|
560
|
+
$e as I,
|
|
561
|
+
ke as N,
|
|
564
562
|
ne as R,
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
563
|
+
Re as Z,
|
|
564
|
+
H as a,
|
|
565
|
+
Ee as c,
|
|
566
|
+
ye as f,
|
|
567
|
+
Oe as g,
|
|
570
568
|
Te as h,
|
|
571
|
-
|
|
569
|
+
E as l,
|
|
570
|
+
we as r,
|
|
571
|
+
A as t
|
|
572
572
|
};
|
package/dist/wx.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./retry-C2OSRA9r.cjs"),l=async function(t,n,u){return e.handleResponse(await e.retryRequest(f,t,n,u),t,n,u)},f=async function(t,n,u){const s=await e.convertOptions(t,n,u),a=s.method==="PATCH"?"POST":s.method,c=e.Zt(s.url,s.params);return e.lt.wx?new Promise(d=>{wx.request({url:c,data:s.body,header:s.headers,method:a,dataType:"string",responseType:"text",fail(){d({url:c,method:a,status:-1,statusText:e.RequestInteralError.Unknown,body:""})},success(r){d({url:c,method:a,status:r.statusCode,statusText:r.statusCode+"",headers:{...r.header},body:s.method==="HEAD"||r.statusCode===204?"":p(r.data)})}})}):{url:c,method:a,status:-1,statusText:e.RequestInteralError.NotSupport,body:""}};function p(t){return typeof t=="string"?t:t instanceof ArrayBuffer&&e.lt.TextDecoder?new TextDecoder().decode(t):JSON.stringify(t)}function i(t){return new e.NetRequestHandler(l,t)}const o=i(),y=o.setConfig,g=o.head,h=o.get,q=o.post,x=o.del,R=o.put;exports.NetRequest=i;exports.del=x;exports.get=h;exports.head=g;exports.post=q;exports.put=R;exports.setGlobalConfig=y;
|
package/dist/wx.js
CHANGED
|
@@ -1,39 +1,39 @@
|
|
|
1
|
-
import { h as
|
|
2
|
-
const x = async function(t,
|
|
3
|
-
return
|
|
4
|
-
}, m = async function(t,
|
|
5
|
-
const e = await
|
|
1
|
+
import { h as l, r as f, c as p, Z as h, l as i, a as d, N as y } from "./retry-DUbMIN1-.js";
|
|
2
|
+
const x = async function(t, n, r) {
|
|
3
|
+
return l(await f(m, t, n, r), t, n, r);
|
|
4
|
+
}, m = async function(t, n, r) {
|
|
5
|
+
const e = await p(t, n, r), a = e.method === "PATCH" ? "POST" : e.method, u = h(e.url, e.params);
|
|
6
6
|
return i.wx ? new Promise((c) => {
|
|
7
7
|
wx.request({
|
|
8
|
-
url:
|
|
8
|
+
url: u,
|
|
9
9
|
data: e.body,
|
|
10
10
|
header: e.headers,
|
|
11
|
-
method:
|
|
11
|
+
method: a,
|
|
12
12
|
dataType: "string",
|
|
13
13
|
responseType: "text",
|
|
14
14
|
fail() {
|
|
15
15
|
c({
|
|
16
|
-
url:
|
|
17
|
-
method:
|
|
16
|
+
url: u,
|
|
17
|
+
method: a,
|
|
18
18
|
status: -1,
|
|
19
19
|
statusText: d.Unknown,
|
|
20
20
|
body: ""
|
|
21
21
|
});
|
|
22
22
|
},
|
|
23
|
-
success(
|
|
23
|
+
success(o) {
|
|
24
24
|
c({
|
|
25
|
-
url:
|
|
26
|
-
method:
|
|
27
|
-
status:
|
|
28
|
-
statusText:
|
|
29
|
-
headers: { ...
|
|
30
|
-
body: e.method === "HEAD" ? "" : w(
|
|
25
|
+
url: u,
|
|
26
|
+
method: a,
|
|
27
|
+
status: o.statusCode,
|
|
28
|
+
statusText: o.statusCode + "",
|
|
29
|
+
headers: { ...o.header },
|
|
30
|
+
body: e.method === "HEAD" || o.statusCode === 204 ? "" : w(o.data)
|
|
31
31
|
});
|
|
32
32
|
}
|
|
33
33
|
});
|
|
34
34
|
}) : {
|
|
35
|
-
url:
|
|
36
|
-
method:
|
|
35
|
+
url: u,
|
|
36
|
+
method: a,
|
|
37
37
|
status: -1,
|
|
38
38
|
statusText: d.NotSupport,
|
|
39
39
|
body: ""
|
|
@@ -45,13 +45,13 @@ function w(t) {
|
|
|
45
45
|
function T(t) {
|
|
46
46
|
return new y(x, t);
|
|
47
47
|
}
|
|
48
|
-
const s = T(), q = s.setConfig, R = s.head, b = s.get, C = s.post,
|
|
48
|
+
const s = T(), q = s.setConfig, R = s.head, b = s.get, C = s.post, N = s.del, S = s.put;
|
|
49
49
|
export {
|
|
50
50
|
T as NetRequest,
|
|
51
|
-
|
|
51
|
+
N as del,
|
|
52
52
|
b as get,
|
|
53
53
|
R as head,
|
|
54
54
|
C as post,
|
|
55
|
-
|
|
55
|
+
S as put,
|
|
56
56
|
q as setGlobalConfig
|
|
57
57
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seayoo-web/request",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.1.1",
|
|
4
4
|
"description": "requst tools for seayoo web",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
"happy-dom": "^12.10.3",
|
|
60
60
|
"msw": "^2.7.0",
|
|
61
61
|
"vitest": "^3.0.5",
|
|
62
|
-
"@seayoo-web/utils": "^
|
|
62
|
+
"@seayoo-web/utils": "^3.0.8"
|
|
63
63
|
},
|
|
64
64
|
"scripts": {
|
|
65
65
|
"build": "vite build && tsc --emitDeclarationOnly",
|
package/types/inc/config.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ export declare class RequestGlobalConfig {
|
|
|
4
4
|
private config;
|
|
5
5
|
constructor(config?: IRequestGlobalConfig);
|
|
6
6
|
set(config: Partial<IRequestGlobalConfig>): void;
|
|
7
|
-
get<T extends keyof IRequestGlobalConfig>(key: T): SomeRequired<IRequestGlobalConfig, "timeout" | "credentials" | "baseURL" | "maxRetry" | "cacheTTL" | "responseRule" | "retryInterval" | "retryResolve">[T];
|
|
7
|
+
get<T extends keyof IRequestGlobalConfig>(key: T): SomeRequired<IRequestGlobalConfig, "timeout" | "credentials" | "baseURL" | "maxRetry" | "cacheTTL" | "responseRule" | "retryInterval" | "retryResolve" | "defaultTypeGuardMessage">[T];
|
|
8
8
|
/** 基于 baseURL 返回完整的 url 地址 */
|
|
9
9
|
getFullUrl(url: string): string;
|
|
10
10
|
/** 提示消息 */
|
package/types/inc/guard.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
+
import { type IResponseResult, type TypeGuardParam } from "./type";
|
|
1
2
|
import type { RequestGlobalConfig } from "./config";
|
|
2
|
-
import type { IResponseResult, TypeGuardParam } from "./type";
|
|
3
3
|
export declare function checkTypedDataResult<T>(url: string, result: IResponseResult, config: RequestGlobalConfig, typeGard: TypeGuardParam<T> | null): IResponseResult<T | null | unknown>;
|
package/types/inc/main.d.ts
CHANGED
|
@@ -21,7 +21,7 @@ export declare class NetRequestHandler {
|
|
|
21
21
|
/**
|
|
22
22
|
* 读取默认的请求配置
|
|
23
23
|
*/
|
|
24
|
-
getConfig<T extends keyof IRequestGlobalConfig>(key: T): import("@seayoo-web/utils").SomeRequired<IRequestGlobalConfig, "timeout" | "credentials" | "baseURL" | "maxRetry" | "cacheTTL" | "responseRule" | "retryInterval" | "retryResolve">[T];
|
|
24
|
+
getConfig<T extends keyof IRequestGlobalConfig>(key: T): import("@seayoo-web/utils").SomeRequired<IRequestGlobalConfig, "timeout" | "credentials" | "baseURL" | "maxRetry" | "cacheTTL" | "responseRule" | "retryInterval" | "retryResolve" | "defaultTypeGuardMessage">[T];
|
|
25
25
|
/**
|
|
26
26
|
* 发送一个 HEAD 请求,并且不处理响应 body
|
|
27
27
|
*/
|
package/types/inc/type.d.ts
CHANGED
|
@@ -2,10 +2,14 @@ import type { RequestGlobalConfig } from "./config";
|
|
|
2
2
|
import type { MaybePromise, TypeGuard, TypeGuardFn } from "@seayoo-web/utils";
|
|
3
3
|
export type IBaseRequestBody = Blob | ArrayBuffer | FormData | URLSearchParams | string;
|
|
4
4
|
export declare const enum RequestInteralError {
|
|
5
|
+
/** 响应数据校验失败 */
|
|
6
|
+
UnexpectResponse = "UnexpectResponse",
|
|
5
7
|
/** 请求被取消 */
|
|
6
8
|
Aborted = "Aborted",
|
|
7
|
-
/**
|
|
9
|
+
/** 未知错误 */
|
|
8
10
|
Unknown = "Unknown",
|
|
11
|
+
/** 网络错误 */
|
|
12
|
+
NetworkError = "NetworkError",
|
|
9
13
|
/** 超时 */
|
|
10
14
|
Timeout = "Timeout",
|
|
11
15
|
/** 请求方法不被支持,在微信小程序环境下有效 */
|
|
@@ -83,6 +87,8 @@ export type IRequestGlobalConfig = {
|
|
|
83
87
|
baseURL?: string;
|
|
84
88
|
/** 用于 get 请求的缓存时长,单位 ms,建议不小于 100 */
|
|
85
89
|
cacheTTL?: number;
|
|
90
|
+
/** 接口响应错误时的默认提示消息 */
|
|
91
|
+
defaultTypeGuardMessage?: string;
|
|
86
92
|
/**
|
|
87
93
|
* 全局 request 转化函数,headers 和 params 为引用型数据可直接修改,如果返回字符串,则替换原来的 url 进行请求
|
|
88
94
|
*/
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
"use strict";const s=require("./type-CrucJEBR.cjs"),f=async function(o,n,i){return s.handleResponse(await s.retryRequest(w,o,n,i),o,n,i)},w=async function(o,n,i){const t=await s.convertOptions(o,n,i),a=new URL(t.url),u=t.params;u instanceof Object&&Object.keys(u).forEach(e=>a.searchParams.set(e,u[e]));const r=s.Kt.AbortController?new AbortController:null;function c(){r&&!r.signal.aborted&&r.abort()}t.abort&&t.abort.addEventListener("abort",c);const h=t.timeout>0?setTimeout(c,t.timeout):null,b=t.method==="HEAD";return await fetch(a,{method:t.method,headers:Object.keys(t.headers).length>0?new Headers(t.headers):void 0,body:t.body,credentials:t.credentials,signal:r==null?void 0:r.signal,redirect:"follow"}).then(async e=>{const d=b?"":await e.text().catch(m=>m),l=s.fromEntries([...e.headers.entries()]);return d instanceof Error?{url:a.toString(),method:t.method,status:e.status,statusText:e.statusText||s.RequestInteralError.Unknown,body:"",headers:l,rawError:d}:{url:a.toString(),method:t.method,status:e.status,statusText:e.statusText,headers:l,body:d}}).catch(e=>({url:a.toString(),method:t.method,status:-1,statusText:r!=null&&r.signal.aborted?s.RequestInteralError.Aborted:s.RequestInteralError.Unknown,body:"",rawError:e})).finally(()=>{h!==null&&clearTimeout(h),t.abort&&t.abort.removeEventListener("abort",c)})};exports.fetchRequest=f;
|
package/dist/type-CrucJEBR.cjs
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
"use strict";var J=Object.defineProperty;var V=(e,t,s)=>t in e?J(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s;var p=(e,t,s)=>V(e,typeof t!="symbol"?t+"":t,s);const h=typeof globalThis<"u"?globalThis:typeof global<"u"?global:typeof self<"u"?self:typeof window<"u"?window:{},R={wx:"wx"in h&&D(h.wx),fetch:"fetch"in h&&w(h.fetch),window:"window"in h&&D(h.window),Blob:"Blob"in h&&w(h.Blob),FormData:"FormData"in h&&w(h.FormData),TextDecoder:"TextDecoder"in h&&w(h.TextDecoder),AbortController:"AbortController"in h&&w(h.AbortController)};function w(e){return typeof e=="function"}function D(e){return typeof e=="object"&&e!==null}function W(e){return new Promise(t=>setTimeout(t,Math.max(0,e)))}function Y(){}function z(e){const t=e||"CustomError";return class extends Error{constructor(s){super(s),Object.defineProperty(this,"name",{value:t,enumerable:!1,configurable:!0}),"setPrototypeOf"in Object&&Object.setPrototypeOf(this,new.target.prototype),"captureStackTrace"in Error&&typeof Error.captureStackTrace=="function"&&Error.captureStackTrace(this,this.constructor)}}}const Z=/^(?:https?:)?\/\/.+$/i,Q=/^https?:\/\/.+$/i,X=/^\{[\d\D]*\}$/,ee=/^\[[\d\D]*\]$/;function k(e,t=!1){return t?Z.test(e):Q.test(e)}function te(e){return e==null}function v(e,...t){if(!e||typeof e!="object")return!1;const s=Object.getPrototypeOf(e);return s!==Object.prototype&&s!==null?!1:t.every(r=>r in e)}function B(e){return X.test(e)||ee.test(e)}function $(e){return e?e[0].toLowerCase()+e.slice(1):""}const _=/_\w*/,q=/-\w*/;function U(e){const t=_.test(e)?e.replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(s,r)=>r.toUpperCase()):q.test(e)?e.replace(/(?:^-*|-*$)/g,"").replace(/-+(\w)/g,(s,r)=>r.toUpperCase()):e;return $(t)}function M(e){return _.test(e)?$(e).replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(t,s)=>"_"+s.toLowerCase()):q.test(e)?$(e.replace(/(?:^-*|-*$)/g,"")).replace(/-+(\w)/g,(t,s)=>"_"+s.toLowerCase()):$(e).replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`)}function T(e,t){try{const s=JSON.parse(e);return t?t(s)?s:null:s}catch{return null}}""+Math.random().toString(32).slice(2);function E(e,t=!1){return typeof e=="string"?U(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?E(s):typeof s=="object"&&s?E(s,!0):s):v(e)?Object.keys(e).reduce((s,r)=>{const o=U(String(r)),n=o.charAt(0).toLowerCase()+o.slice(1);return s[n]=t?e[r]:E(e[r]),s},{}):e}function O(e,t=!1){return typeof e=="string"?M(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?O(s):typeof s=="object"&&s?O(s,t):s):v(e)?Object.keys(e).reduce((s,r)=>{const o=M(String(r));return s[o]=t?e[r]:O(e[r]),s},{}):e}async function se(e){return await new Promise(function(t){const s=document.getElementsByTagName("head")[0],r=document.createElement("script");r.setAttribute("type","text/javascript"),r.setAttribute("charset","utf-8"),r.onload=function(){s.removeChild(r),t(!0)},r.onerror=function(){s.removeChild(r),t(!1)},r.setAttribute("src",e),s.appendChild(r)})}function re(e,t="数据未能正确识别"){return typeof e=="function"?{guard:e,message:t}:{guard:e.guard,message:e.message||t}}function ne(e,t=""){return!t||k(e,!0)?L(e):(L(t)+"/"+e).replace(/\/{2,}/g,"/").replace(/:\//,"://")}function L(e){return k(e,!0)?e.startsWith("http")?e:("location"in globalThis?location.protocol:"https:")+e:("location"in globalThis?location.origin:"http://127.0.0.1")+"/"+e.replace(/^\/+/,"")}function oe(e,t){const s={};return(e.match(/([^=&#?]+)=[^&#]*/g)||[]).forEach(function(r){const o=r.split("="),n=o[0],a=decodeURIComponent(o[1]||"");s[n]!==void 0?s[n]+=","+a:s[n]=a}),t!==!0?s[t]||"":s}function ae(e,t){if(t){if(t===!0)return e.replace(/\?[^#]*/,"")}else return e;const s=e.split("#"),r=s[0].split("?"),o=r[0],n=r.length>1?r[1]:"",a=s.length>1?"#"+s[1]:"",c=typeof t=="string"?[t]:Array.isArray(t)?t:[];return!c.length||!n?s[0]+a:(c.map(i=>i.replace(/([\\(){}[\]^$+\-*?|])/g,"\\$1")),(o+"?"+n.replace(new RegExp("(?:^|&)(?:"+c.join("|")+")=[^&$]+","g"),"").replace(/^&/,"")).replace(/\?$/,"")+a)}function ce(e,t,s=!1){const r=typeof t=="string"?t:Object.keys(t).map(a=>`${a}=${encodeURIComponent(t[a])}`).join("&");if(!r)return e;const o=e.split("#");s&&(o[0]=ae(o[0],(r.match(/([^=&#?]+)=[^&#]+/g)||[]).map(a=>a.replace(/=.+$/,""))));const n=o[0].indexOf("?")+1?"&":"?";return(o[0]+n+r+(o.length>1?"#"+o[1]:"")).replace(/\?&/,"?")}function ie(e){const t=e.match(/(?:\?|&)([^=]+)(?:&|$)/g);return t?t.join("").replace(/(?:\?|^&+|&+$)/g,"").replace(/&{2}/g,"&").split("&").sort():[]}class ue{constructor(t=500){p(this,"ttl");p(this,"cache");this.cache={},this.ttl=Math.max(t,0)}getKey(t,s){const r=t.replace(/#.+/,""),o=r.replace(/\?.+/g,""),n=Object.assign(oe(r,!0),s),a=ie(r),c=Object.keys(n).sort().map(i=>`${i}#${n[i]}`);return`${o}_${c.join(",")}_${a.join(",")}`}updateTTL(t){this.ttl=Math.max(t,0)}get(t){if(this.ttl===0)return null;const s=this.cache[t];return s?s.ttl<Date.now()?(delete this.cache[t],null):s.res:null}set(t,s){this.ttl!==0&&(this.cache[t]={ttl:Date.now()+this.ttl,res:s})}}class H{constructor(t){p(this,"config",{baseURL:"",maxRetry:0,retryInterval:1e3,retryResolve:"network",timeout:1e4,cacheTTL:500,credentials:"same-origin",responseRule:{ok:{resolve:"body"},failed:{resolve:"json",messageField:"message"}}});t&&this.set(t)}set(t){if(t.baseURL&&!/^\/.+/.test(t.baseURL)&&!k(t.baseURL))throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"),new Error("BaseURLError");Object.assign(this.config,t)}get(t){return this.config[t]}getFullUrl(t){return ne(t,this.config.baseURL)}showMessage(t,s,r,o){this.config.messageHandler&&s&&this.config.messageHandler(t,s,r,o)}}const P="UnexpectResponse";function le(e,t,s,r){if(t.ok&&!te(t.data)&&r){const o=re(r,"响应数据未能正确识别");return o.guard(t.data)||(s.showMessage(!0,`${e} ${o.message}`,t.code,t.status),console.error(P,e,t.data),t.data=null,t.code=P,t.message=o.message),t}return t}class he{constructor(t,s){p(this,"agent");p(this,"config");p(this,"cache");this.config=new H(s),this.agent=t,this.cache=new ue(this.config.get("cacheTTL")),this.setConfig=this.setConfig.bind(this),this.getConfig=this.getConfig.bind(this),this.request=this.request.bind(this),this.get=this.get.bind(this),this.post=this.post.bind(this),this.del=this.del.bind(this),this.patch=this.patch.bind(this),this.put=this.put.bind(this),this.head=this.head.bind(this)}async request(t,s){try{return await this.agent(t,this.config,s)}catch(r){return console.error("RequestError",r),{ok:!1,status:-9,code:"Unkonwn",message:String(r),headers:{},data:null}}}async guard(t,s,r){return le(t,s,this.config,r)}setConfig(t){this.config.set(t),this.cache.updateTTL(this.config.get("cacheTTL"))}getConfig(t){return this.config.get(t)}async head(t,s){const r=Object.assign({},s||null);return r.method="HEAD",this.guard(t,await this.request(t,r),null)}async get(t,s,r){const o=Object.assign({},r||null);o.method="GET";const n=this.cache.getKey(t,o.params),a=this.cache.get(n);if(a)return this.guard(t,await a,s||null);const c=this.request(t,o);return this.cache.set(n,c),this.guard(t,await c,s||null)}async post(t,s,r,o){const n=Object.assign({},o||null);return n.method="POST",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}async del(t,s,r){const o=Object.assign({},r||null);return o.method="DELETE",this.guard(t,await this.request(t,o),s||null)}async put(t,s,r,o){const n=Object.assign({},o||null);return n.method="PUT",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}async patch(t,s,r,o){const n=Object.assign({},o||null);return n.method="PATCH",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}}async function fe(e,t,s){var m;const r=Object.assign({method:"GET"},s),o=R.FormData?r.body instanceof FormData:!1,n=o&&r.method!=="POST"&&r.method!=="PUT"?"POST":r.method,a=n==="GET"||n==="HEAD"||n==="DELETE";a&&r.body!==void 0&&(console.warn("request body is invalid with method get, head, delete"),delete r.body);const c=Object.assign(o||a?{}:{"Content-Type":R.Blob&&r.body instanceof Blob?r.body.type||"application/octet-stream":"application/json;charset=utf-8"},r.headers),i=r.params||{},f={};Object.keys(i).forEach(b=>{i[b]!==void 0&&(f[b]=de(i[b]))});const u=t.getFullUrl(e),l=ge(r.body),d=r.timeout||t.get("timeout"),g=await async function(){const b=t.get("requestTransformer");if(b)return await b({headers:c,params:f,method:n,url:u,body:l})}(),y=typeof g=="string"&&g?g:u;return(m=t.get("logHandler"))==null||m({type:"ready",url:y,method:n,headers:c,timeout:d,body:l}),{url:y,method:n,body:l,params:f,headers:c,timeout:d,abort:r.abort,credentials:r.credentials||t.get("credentials")}}function de(e){return typeof e=="string"?e:Array.isArray(e)?e.join(","):e+""}function ge(e){if(e)return typeof e=="string"||e instanceof URLSearchParams||e instanceof ArrayBuffer||R.Blob&&e instanceof Blob||R.FormData&&e instanceof FormData?e:JSON.stringify(e)}const K="data",j="message";function me(e,t,s,r,o){const n=o||r;return C(e)?$e(n.ok||r.ok,e,t,s):pe(n.failed||r.failed,t,s)}const ye=function(e){const t=[],s=e.failed||{resolve:"json"};switch(t.push("- 当http状态码 <200 或者 >=400 时"),s.resolve){case"body":t.push(" 将响应内容格式化为字符串并作为错误消息");break;case"json":t.push(" 将响应解析为json,并读取 "+(s.messageField||j)+" 作为错误消息");break}const r=e.ok||{resolve:"body"};switch(t.push("- 当http状态码 >=200 并且 <400 时"),r.resolve){case"body":t.push(" 将响应尝试解析为 json,并作为数据内容返回");break;case"json":t.push(" 将响应解析为 json,读取 "+(r.dataField||K)+" 作为响应数据,读取 "+(r.messageField||j)+" 作为提示消息"),r.statusField&&t.push(" 当 "+r.statusField+" 为 "+(r.statusOKValue||"空值")+" 时是成功提示,否则是错误消息"),r.ignoreMessage&&t.push(" 并忽略以下消息:"+r.ignoreMessage);break}return t.join(`
|
|
2
|
-
`)};function pe(e,t,s){const r=e||{resolve:"json",messageField:j},o={ok:!1,code:t,message:s,data:null};switch(r.resolve){case"body":o.message=x(s)||s;break;case"json":const{code:n,message:a}=be(s,r.converter,r.statusField,r.messageField);o.code=n||t,o.message=x(s)||a;break}return o}function be(e,t,s,r=j){if(!B(e))return{message:""};const o=A(T(e),t);return!o||!v(o)?{message:e}:{code:s?F(o,s):"",message:F(o,r)||e}}function F(e,t){const s=Array.isArray(t)?t:[t];for(const r of s)if(r in e)return we(e[r]);return""}function we(e){return e?typeof e=="string"?e:JSON.stringify(e):""}const Re=/<title>([^<]+)<\/title>/i,je=/<message>([^<]+)<\/message>/i;function x(e){const t=e.match(Re);if(t)return t[1];const s=e.match(je);return s?s[1]:""}function $e(e,t,s,r){const o=e||{resolve:"body"},n={ok:!0,code:s,message:"",data:null};if(t===204||!r)return n;if(o.resolve==="body")return n.data=B(r)?A(T(r),e.converter):r,n;const a=A(T(r),e.converter);if(!a||!v(a))return n.ok=!1,n.code="ResponseFormatError",n.message="响应内容无法格式化为 Object",n;const c=o.statusField,i=o.statusOKValue||"",f=o.dataField||K,u=o.messageField||j,l=o.ignoreMessage||"";if(c&&!(c in a))return n.ok=!1,n.code="ResponseFieldMissing",n.message="响应内容找不到状态字段 "+c,n;const d=c?a[c]+"":"";return n.ok=c?d===i:!0,n.code=d||s,n.data=f===!0?a:f in a?a[f]:null,n.message=F(a,u),l&&n.message&&(Array.isArray(l)&&l.includes(n.message)||typeof l=="string"&&n.message===l)&&(n.message=""),n}function C(e){return e>=200&&e<400}function A(e,t){return t==="camelize"?E(e):t==="snakify"?O(e):e}const Ee=z("APIError");function Oe(e){const t={};for(const o in e.headers)(o.startsWith("x-")||o.includes("trace")||o.includes("server")||/\b(?:id|uuid)\b/.test(o))&&(t[o]=e.headers[o]);const s=e.url.replace(/^(?:https?:)?\/*/i,"").replace(/\?.+/,""),r=e.status<0?"unknown":e.status;return{sentryError:new Ee(`${s} | ${r}${e.code?` | ${e.code}`:""}`),sentryTags:{...t,status:r,method:e.method,code:e.code||"unknown",message:e.message||"empty"},sentryExtra:{url:e.url,responseBody:e.body||"empty",responseHeaders:e.headers,rawError:e.error}}}function N(e){return e.reduce((t,[s,r])=>(s&&(t[s]=r||""),t),{})}function ke(e,t,s,r){var g;const o=e.status,n=e.method,a=N(Object.entries(e.headers||{}).map(([y,m])=>[y.toLowerCase(),m])),{ok:c,code:i,data:f,message:u}=me(o,e.statusText,e.body,s.get("responseRule"),r==null?void 0:r.responseRule);if(!C(o)){const y=Oe({url:e.url,method:e.method,status:o,code:i,message:u,body:e.body,headers:a,error:e.rawError});(s.get("errorHandler")||ve)({url:t,method:n,status:o,code:i,message:u,headers:a,rawError:e.rawError,responseBody:e.body,...y})}if(o<0)return S({ok:!1,status:o,code:e.statusText,headers:{},message:"",data:null},`${n} ${t} ${e.statusText}`,n,t,s,r);const l={ok:c,data:f,code:i,message:u,status:o,headers:a};(g=s.get("responseHandler"))==null||g({...l},n,t);const d=c?u:u||e.statusText;return S(l,d,n,t,s,r)}function S(e,t,s,r,o,n){const a=o.get("message"),c=a===!1||(n==null?void 0:n.message)===!1?!1:(n==null?void 0:n.message)||a;if(c!==!1){const i=typeof c=="function"?c(e,s,r,t):t;i instanceof Error?o.showMessage(!0,i.message,e.code,e.status):i&&typeof i=="object"&&"message"in i?o.showMessage(!1,i.message,e.code,e.status):o.showMessage(!e.ok,i,e.code,e.status)}return e}function ve(e){const t={};for(const s in e)s.startsWith("sentry")||(t[s]=e[s]);console.error("RequestError",t)}async function I(e,t,s,r,o){const n=o||0,a=Math.max(0,Math.min(10,(r==null?void 0:r.maxRetry)??s.get("maxRetry")??0)),c=(r==null?void 0:r.retryResolve)??s.get("retryResolve"),i=s.get("logHandler")||Y;i({type:"prepear",url:t,method:(r==null?void 0:r.method)||"GET",retry:n,maxRetry:a,message:n===0?"start":`retry ${n}/${a} start`,headers:r==null?void 0:r.headers,options:r});const f=Date.now(),u=await e(t,s,r),l=u.status,d=Date.now()-f,g=`[cost ${d}][${l}] ${l<0?u.body:""}`;i({type:"finished",url:t,method:u.method,retry:n,maxRetry:a,message:n===0?`finish ${g}`:`retry ${n}/${a} finish ${g}`,response:u,headers:u.headers,cost:d});const y=C(l);if(!a||c==="network"&&l>0||c==="status"&&y||Array.isArray(c)&&!c.includes(l)||typeof c=="function"&&c(u,n)!==!0||n>=a)return u;const m=(r==null?void 0:r.retryInterval)??s.get("retryInterval")??1e3;return await W(Math.max(100,m==="2EB"?Math.pow(2,n)*1e3:typeof m=="function"?m(n+1)||0:m)),await I(e,t,s,r,n+1)}var G=(e=>(e.Aborted="Aborted",e.Unknown="Unknown",e.Timeout="Timeout",e.NotSupport="NotSupport",e.URLFormatError="URLFormatError",e))(G||{});exports.Dt=ce;exports.Kt=R;exports.Me=se;exports.NetRequestHandler=he;exports.RequestGlobalConfig=H;exports.RequestInteralError=G;exports.Y=k;exports.convertOptions=fe;exports.fromEntries=N;exports.getResponseRulesDescription=ye;exports.handleResponse=ke;exports.retryRequest=I;
|
package/types/inc/detect.d.ts
DELETED
|
@@ -1,13 +0,0 @@
|
|
|
1
|
-
export declare const Support: {
|
|
2
|
-
fetch: boolean;
|
|
3
|
-
window: boolean;
|
|
4
|
-
FormData: boolean;
|
|
5
|
-
Blob: boolean;
|
|
6
|
-
wx: boolean;
|
|
7
|
-
TextDecoder: boolean;
|
|
8
|
-
AbortController: boolean;
|
|
9
|
-
};
|
|
10
|
-
/**
|
|
11
|
-
* 简版 Object.fromEntries 以处理某些低版本浏览器和 webview 的问题
|
|
12
|
-
*/
|
|
13
|
-
export declare function fromEntries(kv: [string, string | undefined][]): Record<string, string>;
|
package/types/version.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const version = "2.0.7";
|