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