@seayoo-web/request 3.1.2 → 3.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +1 -1
- package/dist/index.js +3 -3
- package/dist/node.cjs +1 -1
- package/dist/node.js +15 -12
- package/dist/{request.fetch-BuFwMw3Q.cjs → request.fetch-COXrFv2v.cjs} +1 -1
- package/dist/{request.fetch-7NzBdH3R.js → request.fetch-Cuk4gO1S.js} +1 -1
- package/dist/{retry-6b-kUUKa.cjs → retry-8pExkGke.cjs} +3 -3
- package/dist/{retry-Bx4jZvgG.js → retry-_AqXIa5P.js} +105 -90
- package/dist/wx.cjs +1 -1
- package/dist/wx.js +13 -10
- package/package.json +1 -1
- package/types/inc/type.d.ts +9 -9
- package/types/node.d.ts +4 -1
- package/types/wx.d.ts +4 -1
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./retry-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const o=require("./retry-8pExkGke.cjs"),q=require("./request.fetch-COXrFv2v.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.ge(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.RequestInternalError.NetworkError,body:"",rawError:i})},!0),a.addEventListener("abort",()=>{g(),f({url:c,method:r,status:0,statusText:o.RequestInternalError.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
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.ge(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 failed",t,f),c(null)},o.ce(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.ce(o.ge(t,e,!0)).then(()=>{const r=n[e.var+""];return s(r)?r:(console.warn("response type check failed",t,r),null)}).catch(()=>null):null}const j=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.jt.window)throw new Error("Default Module Only Support In Browser");return o.jt.fetch?new o.NetRequestHandler(q.fetchRequest,t):new o.NetRequestHandler(y,t)}const l=p(),x=l.setConfig,C=l.request,L=l.head,S=l.get,k=l.post,v=l.del,D=l.put,U=l.patch;exports.RequestInternalError=o.RequestInternalError;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=C;exports.setGlobalConfig=x;exports.upload=j;
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { h as w, r as T, R as y, c as x, g as p, a as R, b as q, j as E, N as H } from "./retry-
|
|
2
|
-
import { d as J } from "./retry-
|
|
3
|
-
import { f as C } from "./request.fetch-
|
|
1
|
+
import { h as w, r as T, R as y, c as x, g as p, a as R, b as q, j as E, N as H } from "./retry-_AqXIa5P.js";
|
|
2
|
+
import { d as J } from "./retry-_AqXIa5P.js";
|
|
3
|
+
import { f as C } from "./request.fetch-Cuk4gO1S.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
|
};
|
package/dist/node.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./retry-
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./retry-8pExkGke.cjs"),y=require("./request.fetch-COXrFv2v.cjs"),b=require("node:http"),g=require("node:https"),E=async function(r,a,c){return e.handleResponse(await e.retryRequest(w,r,a,c),r,a,c)},w=async function(r,a,c){const t=await e.convertOptions(r,a,c);if(!e.At(t.url))return{url:t.url,method:t.method,status:-1,statusText:e.RequestInternalError.URLFormatError,headers:{},body:""};const R=/^https:\/\//i.test(t.url)?g:b,u=new URL(t.url),i=t.params;i instanceof Object&&Object.keys(i).forEach(n=>u.searchParams.set(n,i[n]));const q=t.method==="HEAD";return new Promise(n=>{const d=R.request(u,{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(([f,l])=>[f.toLowerCase(),Array.isArray(l)?l.join(","):l]));n({url:u.toString(),method:t.method,status:o.statusCode||-1,statusText:o.statusMessage||e.RequestInternalError.Unknown,headers:h,body:q||o.statusCode===204?"":Buffer.concat(m).toString("utf-8")})})});d.on("error",o=>{n({url:u.toString(),method:t.method,status:-1,statusText:e.RequestInternalError.NetworkError,body:"",rawError:o})}),d.on("timeout",()=>{n({url:u.toString(),method:t.method,status:-1,statusText:e.RequestInternalError.Timeout,body:""})}),t.body&&d.write(t.body),d.end()})};function p(r){return e.jt.fetch?new e.NetRequestHandler(y.fetchRequest,r):new e.NetRequestHandler(E,r)}const s=p(),j=s.setConfig,C=s.head,I=s.get,N=s.post,S=s.del,T=s.put,O=s.patch;exports.RequestInternalError=e.RequestInternalError;exports.getResponseRulesDescription=e.getResponseRulesDescription;exports.NetRequest=p;exports.del=S;exports.get=I;exports.head=C;exports.patch=O;exports.post=N;exports.put=T;exports.setGlobalConfig=j;
|
package/dist/node.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { h as b, r as q, c as w, A as g, a as d, f as E, N as
|
|
2
|
-
import {
|
|
1
|
+
import { h as b, r as q, c as w, A as g, a as d, f as E, N as f, j } from "./retry-_AqXIa5P.js";
|
|
2
|
+
import { d as J } from "./retry-_AqXIa5P.js";
|
|
3
|
+
import { f as x } from "./request.fetch-Cuk4gO1S.js";
|
|
3
4
|
import A from "node:http";
|
|
4
5
|
import C from "node:https";
|
|
5
6
|
const N = async function(o, a, u) {
|
|
@@ -15,11 +16,11 @@ const N = async function(o, a, u) {
|
|
|
15
16
|
headers: {},
|
|
16
17
|
body: ""
|
|
17
18
|
};
|
|
18
|
-
const
|
|
19
|
+
const R = /^https:\/\//i.test(t.url) ? C : A, n = new URL(t.url), i = t.params;
|
|
19
20
|
i instanceof Object && Object.keys(i).forEach((r) => n.searchParams.set(r, i[r]));
|
|
20
|
-
const
|
|
21
|
+
const l = t.method === "HEAD";
|
|
21
22
|
return new Promise((r) => {
|
|
22
|
-
const c =
|
|
23
|
+
const c = R.request(
|
|
23
24
|
n,
|
|
24
25
|
{
|
|
25
26
|
headers: t.headers,
|
|
@@ -27,8 +28,8 @@ const N = async function(o, a, u) {
|
|
|
27
28
|
timeout: t.timeout > 0 ? t.timeout : void 0
|
|
28
29
|
},
|
|
29
30
|
function(e) {
|
|
30
|
-
const
|
|
31
|
-
e.on("data", (m) =>
|
|
31
|
+
const p = [];
|
|
32
|
+
e.on("data", (m) => p.push(m)), e.on("end", () => {
|
|
32
33
|
const m = E(
|
|
33
34
|
Object.entries(e.headers).map(([y, h]) => [y.toLowerCase(), Array.isArray(h) ? h.join(",") : h])
|
|
34
35
|
);
|
|
@@ -38,7 +39,7 @@ const N = async function(o, a, u) {
|
|
|
38
39
|
status: e.statusCode || -1,
|
|
39
40
|
statusText: e.statusMessage || d.Unknown,
|
|
40
41
|
headers: m,
|
|
41
|
-
body:
|
|
42
|
+
body: l || e.statusCode === 204 ? "" : Buffer.concat(p).toString("utf-8")
|
|
42
43
|
});
|
|
43
44
|
});
|
|
44
45
|
}
|
|
@@ -64,16 +65,18 @@ const N = async function(o, a, u) {
|
|
|
64
65
|
});
|
|
65
66
|
};
|
|
66
67
|
function L(o) {
|
|
67
|
-
return j.fetch ? new
|
|
68
|
+
return j.fetch ? new f(x, o) : new f(N, o);
|
|
68
69
|
}
|
|
69
|
-
const s = L(), k = s.setConfig, B = s.head,
|
|
70
|
+
const s = L(), k = s.setConfig, B = s.head, D = s.get, P = s.post, F = s.del, G = s.put, I = s.patch;
|
|
70
71
|
export {
|
|
71
72
|
L as NetRequest,
|
|
73
|
+
d as RequestInternalError,
|
|
72
74
|
F as del,
|
|
73
|
-
|
|
75
|
+
D as get,
|
|
76
|
+
J as getResponseRulesDescription,
|
|
74
77
|
B as head,
|
|
75
78
|
I as patch,
|
|
76
|
-
|
|
79
|
+
P as post,
|
|
77
80
|
G as put,
|
|
78
81
|
k as setGlobalConfig
|
|
79
82
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";const s=require("./retry-
|
|
1
|
+
"use strict";const s=require("./retry-8pExkGke.cjs"),m=async function(a,n,i){return s.handleResponse(await s.retryRequest(w,a,n,i),a,n,i)},w=async function(a,n,i){const t=await s.convertOptions(a,n,i),c=new URL(t.url),u=t.params;u instanceof Object&&Object.keys(u).forEach(e=>c.searchParams.set(e,u[e]));const r=s.jt.AbortController?new AbortController:null;function d(){r&&!r.signal.aborted&&r.abort()}t.abort&&t.abort.addEventListener("abort",d);const h=t.timeout>0?setTimeout(d,t.timeout):null,l=t.method==="HEAD";return await fetch(c,{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 o={url:c.toString(),method:t.method,status:e.status,statusText:e.statusText,headers:s.fromEntries([...e.headers.entries()])},b=l||e.status===204?"":await e.text().catch(f=>f);return b instanceof Error?{...o,body:"",statusText:s.RequestInternalError.Unknown,rawError:b}:{...o,body:b}}).catch(e=>{const o=r==null?void 0:r.signal.aborted;return{url:c.toString(),method:t.method,status:o?0:-1,statusText:o?s.RequestInternalError.Aborted:s.RequestInternalError.NetworkError,body:"",rawError:e}}).finally(()=>{h!==null&&clearTimeout(h),t.abort&&t.abort.removeEventListener("abort",d)})};exports.fetchRequest=m;
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { h as m, r as w, c as y, j as E, f as R, a as b } from "./retry-
|
|
1
|
+
import { h as m, r as w, c as y, j as E, f as R, a as b } from "./retry-_AqXIa5P.js";
|
|
2
2
|
const x = async function(s, o, n) {
|
|
3
3
|
return m(await w(g, s, o, n), s, o, n);
|
|
4
4
|
}, g = async function(s, o, n) {
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
"use strict";var dt=Object.defineProperty;var _t=(e,t,s)=>t in e?dt(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s;var v=(e,t,s)=>_t(e,typeof t!="symbol"?t+"":t,s);var mt=Object.defineProperty,gt=(e,t,s)=>t in e?mt(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s,o=(e,t,s)=>gt(e,typeof t!="symbol"?t+"":t,s);const u=typeof globalThis<"u"?globalThis:typeof self<"u"?self:typeof window<"u"?window:Function("return this")(),j={wx:"wx"in u&&O(u.wx)&&"getSystemInfo"in u.wx&&_(u.wx.getSystemInfo),fetch:"fetch"in u&&_(u.fetch),window:"window"in u&&O(u.window),requestAnimationFrame:"requestAnimationFrame"in u&&_(u.requestAnimationFrame),localStorage:"localStorage"in u&&O(u.localStorage)&&"getItem"in u.localStorage&&_(u.localStorage.getItem),sessionStorage:"sessionStorage"in u&&O(u.sessionStorage)&&"getItem"in u.sessionStorage&&_(u.sessionStorage.getItem),File:"File"in u&&_(u.File),Blob:"Blob"in u&&_(u.Blob),Proxy:"Proxy"in u&&_(u.Proxy),Request:"Request"in u&&_(u.Request),FormData:"FormData"in u&&_(u.FormData),TextDecoder:"TextDecoder"in u&&_(u.TextDecoder),ResizeObserver:"ResizeObserver"in u&&_(u.ResizeObserver),AbortController:"AbortController"in u&&_(u.AbortController),URLSearchParams:"URLSearchParams"in u&&_(u.URLSearchParams)};function _(e){return typeof e=="function"}function O(e){return typeof e=="object"&&e!==null}function pt(e){return new Promise(t=>setTimeout(t,Math.max(0,e)))}function rt(){}function nt(e,t){const s={log:console.log.bind(console),error:console.error.bind(console),warn:console.warn.bind(console)},r=`[${e}]`;return["log","error","warn"].forEach(n=>{const i=console[n];s[n]=(...a)=>{i.apply(console,[r,...a])}}),s}function bt(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 yt=/^(?:https?:)?\/\/.+$/i,wt=/^https?:\/\/.+$/i,kt=/^\{[\d\D]*\}$/,vt=/^\[[\d\D]*\]$/;function $t(e){return typeof e=="string"}function U(e,t=!1){return t?yt.test(e):wt.test(e)}function jt(e){return e==null}function it(e,...t){return e!==null&&typeof e=="object"&&!(e instanceof Map)&&!(e instanceof Set)&&!(e instanceof WeakMap)&&!(e instanceof WeakSet)&&!Array.isArray(e)&&t.every(s=>s in e)}function b(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 q(e,t){return Array.isArray(e)&&e.every(s=>t(s))}function C(e){if(typeof e!="number"||Number.isNaN(e)||!Number.isFinite(e)||Number.isInteger(e)&&!Number.isSafeInteger(e))return!1;if(e===0)return!0;const t=Math.abs(e);return t>=Number.EPSILON&&t<=Number.MAX_SAFE_INTEGER}function ot(e){return kt.test(e)||vt.test(e)}function Rt(e,t=20){const s=Math.max(t,10),r=[...e];return r.length<=s?e:r.slice(0,s-3).join("")+"..."}function E(e){return e?e[0].toLowerCase()+e.slice(1):""}const at=/_\w*/,lt=/-\w*/;function X(e){const t=at.test(e)?e.replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(s,r)=>r.toUpperCase()):lt.test(e)?e.replace(/(?:^-*|-*$)/g,"").replace(/-+(\w)/g,(s,r)=>r.toUpperCase()):e;return E(t)}function Y(e){return at.test(e)?E(e).replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(t,s)=>"_"+s.toLowerCase()):lt.test(e)?E(e.replace(/(?:^-*|-*$)/g,"")).replace(/-+(\w)/g,(t,s)=>"_"+s.toLowerCase()):E(e).replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`)}function P(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 A(e,t=!1){return typeof e=="string"?X(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?A(s):typeof s=="object"&&s?A(s,!0):s):b(e)?Object.keys(e).reduce((s,r)=>{const n=X(String(r)),i=n.charAt(0).toLowerCase()+n.slice(1);return s[i]=t?e[r]:A(e[r]),s},{}):e}function F(e,t=!1){return typeof e=="string"?Y(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?F(s):typeof s=="object"&&s?F(s,t):s):b(e)?Object.keys(e).reduce((s,r)=>{const n=Y(String(r));return s[n]=t?e[r]:F(e[r]),s},{}):e}nt("LoadUtils");async function Nt(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 Q(e){return Object.getOwnPropertyNames(e).forEach(function(t){delete e[t]}),e}const g=nt("Validator");function x(e){return e?`"${Rt(e).replace(/"/g,'\\"')}"`:"empty string"}function L(e){return typeof e=="object"&&e&&"constructor"in e&&typeof e.constructor=="function"?e.constructor.name||"ClassInstance":Object.prototype.toString.call(e).replace(/(?:.+ |]$)/g,"")}function M(e){return typeof e=="bigint"}function l(e){return new Error(`${e?`${e}()`:"method"} is not allowed after locked`)}class B{constructor(){o(this,"_ps",{}),o(this,"_allow",[]),o(this,"_reject",[]),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1)}pattern(t,s){if(this._locked)throw l("pattern");return this._ps[`#${t||"custom"}`]=s,this}url(t=!1){if(this._locked)throw l("url");return this._ps.url=t?/^(?:https?:)?\/\/.+$/i:/^https?:\/\/.+$/i,this}dataUri(){if(this._locked)throw l("dataUri");return this._ps.dataUri=/^data:[a-z]+\/[a-z]+;base64,[\w+/=]+$/,this}enum(...t){if(this._locked)throw l("enum");const s=t[0];return q(t,$t)?(this._allow=t,Q(this._ps)):b(s)&&(this._allow=Object.values(s).map(r=>`${r}`),Q(this._ps)),this}disallow(...t){return this._reject.push(...t),this}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){const t=new B;return t._ps={...this._ps},t._allow=[...this._allow],t._reject=[...this._reject],t._locked=!1,t}validate(t,s=g.warn){if(t===void 0)return this._opt||(s("should be a string, but got undefined"),!1);if(t===null)return this._null||(s("should be a string, but got null"),!1);if(typeof t!="string")return s(`should be a string, but got ${typeof t}`),!1;if(this._allow.length>0)return this._allow.includes(t)||(s(`${x(t)} is not allowed, just support ${this._allow.map(x).join(", ")}`),!1);if(this._reject.length>0&&this._reject.some(n=>typeof n=="string"?n===t:n.test(t)))return s(`${x(t)} is not allowed`),!1;const r=Object.keys(this._ps);return r.length>0&&!r.some(n=>{const i=this._ps[n];return i instanceof RegExp?i.test(t):i(t)})?(s(`${x(t)} is not match pattern ${r.join(" | ")}`),!1):!0}}class H{constructor(){o(this,"_int",!1),o(this,"_min",-1/0),o(this,"_max",1/0),o(this,"_allow",[]),o(this,"_reject",[]),o(this,"_infinite",!1),o(this,"_unsafe",!1),o(this,"_nan",!1),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1)}int(){if(this._locked)throw l("int");return this._int=!0,this}min(t){if(this._locked)throw l("min");return Number.isNaN(t)||(this._min=t),this}max(t){if(this._locked)throw l("max");return Number.isNaN(t)||(this._max=t),this}allowNaN(){if(this._locked)throw l("allowNaN");return this._nan=!0,this}allowInfinity(){if(this._locked)throw l("allowInfinity");return this._infinite=!0,this._unsafe=!0,this}unsafe(){if(this._locked)throw l("unsafe");return this._unsafe=!0,this}enum(...t){if(this._locked)throw l("enum");const s=t[0];return q(t,C)?(this._allow=t,this._reject=[]):b(s)&&(this._allow=Object.values(s).map(r=>C(r)?r:null).filter(r=>r!==null),this._reject=[]),this}disallow(...t){if(this._locked)throw l("disallow");return this._reject.push(...t),this}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){const t=new H;return t._int=this._int,t._min=this._min,t._max=this._max,t._allow=[...this._allow],t._reject=[...this._reject],t._infinite=this._infinite,t._unsafe=this._unsafe,t._nan=this._nan,t._locked=!1,t}validate(t,s=g.warn){return t===void 0?this._opt||(s("should be a number, but got undefined"),!1):t===null?this._null||(s("should be a number, but got null"),!1):typeof t!="number"?(s(`should be a number, but got ${typeof t}`),!1):Number.isNaN(t)?this._nan||(s("should be a number, but got NaN"),!1):!Number.isFinite(t)&&!this._infinite?(s("should be a number, but got Infinity"),!1):this._allow.length>0?this._allow.includes(t)||(s(`${t} is not allowed, just support ${this._allow.join(", ")}`),!1):this._reject.length>0&&this._reject.includes(t)?(s(`${t} is not allowed`),!1):this._int&&Math.ceil(t)!==t?(s(`${t} is not an integer`),!1):!C(t)&&!this._unsafe?(s(`${t} is not a safe number`),!1):t>=this._min&&t<=this._max||(s(`${t} is not in range [${this._min}, ${this._max}]`),!1)}}class G{constructor(){o(this,"_min",null),o(this,"_max",null),o(this,"_allow",[]),o(this,"_reject",[]),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1)}min(t){if(this._locked)throw l("min");return this._min=t,this}max(t){if(this._locked)throw l("max");return this._max=t,this}enum(...t){if(this._locked)throw l("enum");const s=t[0];return q(t,M)?(this._allow=t,this._reject=[]):b(s)&&(this._allow=Object.values(s).map(r=>M(r)?r:null).filter(r=>r!==null),this._reject=[]),this}disallow(...t){if(this._locked)throw l("disallow");return this._reject.push(...t),this}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){const t=new G;return t._min=this._min,t._max=this._max,t._allow=[...this._allow],t._reject=[...this._reject],t._locked=!1,t}validate(t,s=g.warn){return t===void 0?this._opt||(s("should be a bigint, but got undefined"),!1):t===null?this._null||(s("should be a bigint, but got null"),!1):M(t)?this._allow.length>0?this._allow.includes(t)||(s(`${t} is not allowed, just support ${this._allow.join(", ")}`),!1):this._reject.length>0&&this._reject.includes(t)?(s(`${t} is not allowed`),!1):(this._min!==null?t>=this._min:!0)&&(this._max!==null?t<=this._max:!0)||(s(`${t} is not in range [${this._min===null?-1/0:this._min}, ${this._max===null?1/0:this._max}]`),!1):(s(`should be a bigint, but got ${typeof t}`),!1)}}class z{constructor(){o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1)}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){return new z}validate(t,s=g.warn){return t===void 0?this._opt||(s("should be a boolean, but got undefined"),!1):t===null?this._null||(s("should be a boolean, but got null"),!1):typeof t!="boolean"?(s(`should be a boolean, but got ${typeof t}`),!1):!0}}class R{constructor(t){o(this,"_shape"),o(this,"_plain",!1),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1),this._shape=t}plain(){if(this._locked)throw l("plain");return this._plain=!0,this}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}get shape(){return{...this._shape}}clone(){const t=new R(this._shape);return t._plain=this._plain,t._locked=!1,t}match(t,s){return t in this._shape?this._shape[t].validate(s,rt):!1}validate(t,s=g.warn){return t===void 0?this._opt||(s("should be a object, but got undefined"),!1):t===null?this._null||(s("should be a object, but got null"),!1):it(t)?this._plain&&!b(t)?(s(`should be a plain object, but got ${L(t)}`),!1):Object.keys(this._shape).every(r=>this._shape[r].validate(t[r],(...n)=>{s(`[.${String(r)}]`,...n)})):(s(`should be a object, but got ${L(t)}`),!1)}}class K{constructor(t){o(this,"_val"),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1),this._val=t}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){return new K(this._val)}validate(t,s=g.warn){return t===void 0?this._opt||(s("should be a record, but got undefined"),!1):t===null?this._null||(s("should be a record, but got null"),!1):b(t)?Object.keys(t).every(r=>this._val.validate(t[r],(...n)=>{s(`[:${r}]`,...n)})):(s(`should be a record, but got ${L(t)}`),!1)}}class W{constructor(t){o(this,"_val"),o(this,"_min",0),o(this,"_max",1/0),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1),this._val=t}min(t){if(this._locked)throw l("min");return this._min=t,this}max(t){if(this._locked)throw l("max");return this._max=t,this}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){const t=new W(this._val);return t._min=this._min,t._max=this._max,t._locked=!1,t}validate(t,s=g.warn){return t===void 0?this._opt||(s("should be a array, but got undefined"),!1):t===null?this._null||(s("should be a array, but got null"),!1):Array.isArray(t)?t.length<this._min||t.length>this._max?(this._min===this._max?s(`should be a array with length ${this._min}`):s(`should be a array with length between ${this._min} and ${this._max}`),!1):t.every((r,n)=>this._val.validate(r,(...i)=>{s(`[${n}]`,...i)})):(s(`should be a array, but got ${typeof t}`),!1)}}class J{constructor(...t){o(this,"_vs"),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1),this._vs=t}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){return new J(...this._vs)}validate(t,s=g.warn){return t===void 0?this._opt||(s("should be a tuple, but got undefined"),!1):t===null?this._null||(s("should be a tuple, but got null"),!1):Array.isArray(t)?t.length!==this._vs.length?(s(`should be a tuple with length ${this._vs.length}, but got ${t.length}`),!1):this._vs.every((r,n)=>r.validate(t[n],(...i)=>{s(`[idx:${n}]`,...i)})):(s(`should be a tuple, but got ${typeof t}`),!1)}}class V{constructor(...t){o(this,"_vs"),o(this,"_key",""),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1),this._vs=t}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}key(t){if(this._locked)throw l("key");return this._vs.every(s=>s instanceof R)?this._key=t:g.warn("union type key can only be used when all validators are ObjectValidator"),this}satisfies(){return this}lock(){return this._locked=!0,this}get validators(){return[...this._vs]}clone(){const t=new V(...this._vs);return t._key=this._key,t._locked=!1,t}validate(t,s=g.warn){if(t===void 0)return this._opt||(s("should be a union, but got undefined"),!1);if(t===null)return this._null||(s("should be a union, but got null"),!1);if(this._key&&it(t)&&t){const n=t[this._key];if(n===void 0)return s(`key field "${this._key}" is not found`,t),!1;const i=this._vs.find(a=>a instanceof R&&a.match(this._key,n));return i?i.validate(t,s):(s(`key field "${this._key}" value is match union definition`,n),!1)}const r=this._vs.some(n=>n.validate(t,rt));return r||s("value is not match union definition",t),r}}class S{constructor(t,s){o(this,"_name",""),o(this,"_guard"),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1),this._name=t,this._guard=s}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){return new S(this._name,this._guard)}validate(t,s=g.warn){const r=this._name||"custom type";return t===void 0?this._opt||(s(`should be ${r}, but got undefined`),!1):t===null?this._null||(s(`should be ${r}, but got null`),!1):this._guard(t)||(s(`custom validation${this._name?`(${this._name})`:""} failed`),!1)}}function Ot(e,t){if(typeof e=="string"){if(!t)throw new Error("custom type guard must be defined");return new S(e,t)}return new S("",e)}const xt={shape(e){return e},define(e){return e},guard(e){return function(t){return e.validate(t)}},object(e){return new R(e)},record(e){return new K(e)},array(e){return new W(e)},tuple(...e){return new J(...e)},union(...e){return new V(...e)},string(){return new B},bool(){return new z},number(){return new H},bigint(){return new G},custom:Ot,unknown(){return{validate(e){return!0}}},never(){return{validate(e){return!1}}}},Et=e=>b(e)&&Object.keys(e).length===0;xt.custom("EmptyObject",Et);function At(e,t="数据未能正确识别"){return typeof e=="function"?{guard:e,message:t}:{guard:e.guard,message:e.message||t}}function Ft(e,t=""){return!t||U(e,!0)?tt(e):(tt(t)+"/"+e).replace(/\/{2,}/g,"/").replace(/:\//,"://")}function tt(e){return U(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 St(e,t){const s={};return(e.match(/([^=&#?]+)=[^&#]*/g)||[]).forEach(function(r){const n=r.split("="),i=n[0],a=decodeURIComponent(n[1]||"");s[i]!==void 0?s[i]+=","+a:s[i]=a}),t!==!0?s[t]||"":s}function Ut(e,t){if(t){if(t===!0)return e.replace(/\?[^#]*/,"")}else return e;const s=e.split("#"),r=s[0].split("?"),n=r[0],i=r.length>1?r[1]:"",a=s.length>1?"#"+s[1]:"",h=typeof t=="string"?[t]:Array.isArray(t)?t:[];return!h.length||!i?s[0]+a:(h.map(c=>c.replace(/([\\(){}[\]^$+\-*?|])/g,"\\$1")),(n+"?"+i.replace(new RegExp("(?:^|&)(?:"+h.join("|")+")=[^&$]+","g"),"").replace(/^&/,"")).replace(/\?$/,"")+a)}function Tt(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 n=e.split("#");s&&(n[0]=Ut(n[0],(r.match(/([^=&#?]+)=[^&#]+/g)||[]).map(a=>a.replace(/=.+$/,""))));const i=n[0].indexOf("?")+1?"&":"?";return(n[0]+i+r+(n.length>1?"#"+n[1]:"")).replace(/\?&/,"?")}function Ct(e){const t=e.match(/(?:\?|&)([^=]+)(?:&|$)/g);return t?t.join("").replace(/(?:\?|^&+|&+$)/g,"").replace(/&{2}/g,"&").split("&").sort():[]}class Mt{constructor(t=500){v(this,"ttl");v(this,"cache");this.cache={},this.ttl=Math.max(t,0)}getKey(t,s){const r=t.replace(/#.+/,""),n=r.replace(/\?.+/g,""),i=Object.assign(St(r,!0),s),a=Ct(r),h=Object.keys(i).sort().map(c=>`${c}#${i[c]}`);return`${n}_${h.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 ut{constructor(t){v(this,"config",{baseURL:"/",maxRetry:0,retryInterval:100,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)&&!U(t.baseURL))throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"),new Error("BaseURLError");Object.assign(this.config,t)}get(t){return this.config[t]}getFullUrl(t){return Ft(t,this.config.baseURL)}showMessage(t,s,r,n){this.config.messageHandler&&s&&this.config.messageHandler(t,s,r,n)}}var T=(e=>(e.UnexpectResponse="UnexpectResponse",e.Aborted="Aborted",e.Unknown="Unknown",e.NetworkError="NetworkError",e.Timeout="Timeout",e.NotSupport="NotSupport",e.URLFormatError="URLFormatError",e))(T||{});function Pt(e,t,s,r){if(t.ok&&!jt(t.data)&&r){const n=At(r,s.get("defaultTypeGuardMessage"));return n.guard(t.data)||(t.code=T.UnexpectResponse,s.showMessage(!0,`${e} ${n.message}`,t.code,t.status),console.error(t.code,e,t.data),t.data=null,t.message=n.message),t}return t}class Lt{constructor(t,s){v(this,"agent");v(this,"config");v(this,"cache");this.config=new ut(s),this.agent=t,this.cache=new Mt(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:T.Unknown,message:r instanceof Error?`${r.message}
|
|
2
|
-
${r.stack||""}`:String(r),headers:{},data:null}}}async guard(t,s,r){return Pt(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 n=Object.assign({},r||null);n.method="GET";const i=this.cache.getKey(t,n.params),a=this.cache.get(i);if(a)return this.guard(t,await a,s||null);const h=this.request(t,n);return this.cache.set(i,h),this.guard(t,await h,s||null)}async post(t,s,r,n){const i=Object.assign({},n||null);return i.method="POST",i.body=s||{},this.guard(t,await this.request(t,i),r||null)}async del(t,s,r){const n=Object.assign({},r||null);return n.method="DELETE",this.guard(t,await this.request(t,n),s||null)}async put(t,s,r,n){const i=Object.assign({},n||null);return i.method="PUT",i.body=s||{},this.guard(t,await this.request(t,i),r||null)}async patch(t,s,r,n){const i=Object.assign({},n||null);return i.method="PATCH",i.body=s||{},this.guard(t,await this.request(t,i),r||null)}}async function
|
|
3
|
-
`)};function Gt(e,t,s){const r=e||{resolve:"json",messageField:
|
|
1
|
+
"use strict";var dt=Object.defineProperty;var _t=(e,t,s)=>t in e?dt(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s;var v=(e,t,s)=>_t(e,typeof t!="symbol"?t+"":t,s);var mt=Object.defineProperty,gt=(e,t,s)=>t in e?mt(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s,o=(e,t,s)=>gt(e,typeof t!="symbol"?t+"":t,s);const u=typeof globalThis<"u"?globalThis:typeof self<"u"?self:typeof window<"u"?window:Function("return this")(),j={wx:"wx"in u&&N(u.wx)&&"getSystemInfo"in u.wx&&_(u.wx.getSystemInfo),fetch:"fetch"in u&&_(u.fetch),window:"window"in u&&N(u.window),requestAnimationFrame:"requestAnimationFrame"in u&&_(u.requestAnimationFrame),localStorage:"localStorage"in u&&N(u.localStorage)&&"getItem"in u.localStorage&&_(u.localStorage.getItem),sessionStorage:"sessionStorage"in u&&N(u.sessionStorage)&&"getItem"in u.sessionStorage&&_(u.sessionStorage.getItem),File:"File"in u&&_(u.File),Blob:"Blob"in u&&_(u.Blob),Proxy:"Proxy"in u&&_(u.Proxy),Request:"Request"in u&&_(u.Request),FormData:"FormData"in u&&_(u.FormData),TextDecoder:"TextDecoder"in u&&_(u.TextDecoder),ResizeObserver:"ResizeObserver"in u&&_(u.ResizeObserver),AbortController:"AbortController"in u&&_(u.AbortController),URLSearchParams:"URLSearchParams"in u&&_(u.URLSearchParams)};function _(e){return typeof e=="function"}function N(e){return typeof e=="object"&&e!==null}function pt(e){return new Promise(t=>setTimeout(t,Math.max(0,e)))}function rt(){}function nt(e,t){const s={log:console.log.bind(console),error:console.error.bind(console),warn:console.warn.bind(console)},r=`[${e}]`;return["log","error","warn"].forEach(n=>{const i=console[n];s[n]=(...a)=>{i.apply(console,[r,...a])}}),s}function bt(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 yt=/^(?:https?:)?\/\/.+$/i,wt=/^https?:\/\/.+$/i,kt=/^\{[\d\D]*\}$/,vt=/^\[[\d\D]*\]$/;function $t(e){return typeof e=="string"}function U(e,t=!1){return t?yt.test(e):wt.test(e)}function jt(e){return e==null}function it(e,...t){return e!==null&&typeof e=="object"&&!(e instanceof Map)&&!(e instanceof Set)&&!(e instanceof WeakMap)&&!(e instanceof WeakSet)&&!Array.isArray(e)&&t.every(s=>s in e)}function b(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 q(e,t){return Array.isArray(e)&&e.every(s=>t(s))}function T(e){if(typeof e!="number"||Number.isNaN(e)||!Number.isFinite(e)||Number.isInteger(e)&&!Number.isSafeInteger(e))return!1;if(e===0)return!0;const t=Math.abs(e);return t>=Number.EPSILON&&t<=Number.MAX_SAFE_INTEGER}function ot(e){return kt.test(e)||vt.test(e)}function Rt(e,t=20){const s=Math.max(t,10),r=[...e];return r.length<=s?e:r.slice(0,s-3).join("")+"..."}function x(e){return e?e[0].toLowerCase()+e.slice(1):""}const at=/_\w*/,lt=/-\w*/;function X(e){const t=at.test(e)?e.replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(s,r)=>r.toUpperCase()):lt.test(e)?e.replace(/(?:^-*|-*$)/g,"").replace(/-+(\w)/g,(s,r)=>r.toUpperCase()):e;return x(t)}function Y(e){return at.test(e)?x(e).replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(t,s)=>"_"+s.toLowerCase()):lt.test(e)?x(e.replace(/(?:^-*|-*$)/g,"")).replace(/-+(\w)/g,(t,s)=>"_"+s.toLowerCase()):x(e).replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`)}function M(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 A(e,t=!1){return typeof e=="string"?X(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?A(s):typeof s=="object"&&s?A(s,!0):s):b(e)?Object.keys(e).reduce((s,r)=>{const n=X(String(r)),i=n.charAt(0).toLowerCase()+n.slice(1);return s[i]=t?e[r]:A(e[r]),s},{}):e}function F(e,t=!1){return typeof e=="string"?Y(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?F(s):typeof s=="object"&&s?F(s,t):s):b(e)?Object.keys(e).reduce((s,r)=>{const n=Y(String(r));return s[n]=t?e[r]:F(e[r]),s},{}):e}nt("LoadUtils");async function Et(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 Q(e){return Object.getOwnPropertyNames(e).forEach(function(t){delete e[t]}),e}const g=nt("Validator");function O(e){return e?`"${Rt(e).replace(/"/g,'\\"')}"`:"empty string"}function P(e){return typeof e=="object"&&e&&"constructor"in e&&typeof e.constructor=="function"?e.constructor.name||"ClassInstance":Object.prototype.toString.call(e).replace(/(?:.+ |]$)/g,"")}function C(e){return typeof e=="bigint"}function l(e){return new Error(`${e?`${e}()`:"method"} is not allowed after locked`)}class D{constructor(){o(this,"_ps",{}),o(this,"_allow",[]),o(this,"_reject",[]),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1)}pattern(t,s){if(this._locked)throw l("pattern");return this._ps[`#${t||"custom"}`]=s,this}url(t=!1){if(this._locked)throw l("url");return this._ps.url=t?/^(?:https?:)?\/\/.+$/i:/^https?:\/\/.+$/i,this}dataUri(){if(this._locked)throw l("dataUri");return this._ps.dataUri=/^data:[a-z]+\/[a-z]+;base64,[\w+/=]+$/,this}enum(...t){if(this._locked)throw l("enum");const s=t[0];return q(t,$t)?(this._allow=t,Q(this._ps)):b(s)&&(this._allow=Object.values(s).map(r=>`${r}`),Q(this._ps)),this}disallow(...t){return this._reject.push(...t),this}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){const t=new D;return t._ps={...this._ps},t._allow=[...this._allow],t._reject=[...this._reject],t._locked=!1,t}validate(t,s=g.warn){if(t===void 0)return this._opt||(s("should be a string, but got undefined"),!1);if(t===null)return this._null||(s("should be a string, but got null"),!1);if(typeof t!="string")return s(`should be a string, but got ${typeof t}`),!1;if(this._allow.length>0)return this._allow.includes(t)||(s(`${O(t)} is not allowed, just support ${this._allow.map(O).join(", ")}`),!1);if(this._reject.length>0&&this._reject.some(n=>typeof n=="string"?n===t:n.test(t)))return s(`${O(t)} is not allowed`),!1;const r=Object.keys(this._ps);return r.length>0&&!r.some(n=>{const i=this._ps[n];return i instanceof RegExp?i.test(t):i(t)})?(s(`${O(t)} is not match pattern ${r.join(" | ")}`),!1):!0}}class B{constructor(){o(this,"_int",!1),o(this,"_min",-1/0),o(this,"_max",1/0),o(this,"_allow",[]),o(this,"_reject",[]),o(this,"_infinite",!1),o(this,"_unsafe",!1),o(this,"_nan",!1),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1)}int(){if(this._locked)throw l("int");return this._int=!0,this}min(t){if(this._locked)throw l("min");return Number.isNaN(t)||(this._min=t),this}max(t){if(this._locked)throw l("max");return Number.isNaN(t)||(this._max=t),this}allowNaN(){if(this._locked)throw l("allowNaN");return this._nan=!0,this}allowInfinity(){if(this._locked)throw l("allowInfinity");return this._infinite=!0,this._unsafe=!0,this}unsafe(){if(this._locked)throw l("unsafe");return this._unsafe=!0,this}enum(...t){if(this._locked)throw l("enum");const s=t[0];return q(t,T)?(this._allow=t,this._reject=[]):b(s)&&(this._allow=Object.values(s).map(r=>T(r)?r:null).filter(r=>r!==null),this._reject=[]),this}disallow(...t){if(this._locked)throw l("disallow");return this._reject.push(...t),this}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){const t=new B;return t._int=this._int,t._min=this._min,t._max=this._max,t._allow=[...this._allow],t._reject=[...this._reject],t._infinite=this._infinite,t._unsafe=this._unsafe,t._nan=this._nan,t._locked=!1,t}validate(t,s=g.warn){return t===void 0?this._opt||(s("should be a number, but got undefined"),!1):t===null?this._null||(s("should be a number, but got null"),!1):typeof t!="number"?(s(`should be a number, but got ${typeof t}`),!1):Number.isNaN(t)?this._nan||(s("should be a number, but got NaN"),!1):!Number.isFinite(t)&&!this._infinite?(s("should be a number, but got Infinity"),!1):this._allow.length>0?this._allow.includes(t)||(s(`${t} is not allowed, just support ${this._allow.join(", ")}`),!1):this._reject.length>0&&this._reject.includes(t)?(s(`${t} is not allowed`),!1):this._int&&Math.ceil(t)!==t?(s(`${t} is not an integer`),!1):!T(t)&&!this._unsafe?(s(`${t} is not a safe number`),!1):t>=this._min&&t<=this._max||(s(`${t} is not in range [${this._min}, ${this._max}]`),!1)}}class H{constructor(){o(this,"_min",null),o(this,"_max",null),o(this,"_allow",[]),o(this,"_reject",[]),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1)}min(t){if(this._locked)throw l("min");return this._min=t,this}max(t){if(this._locked)throw l("max");return this._max=t,this}enum(...t){if(this._locked)throw l("enum");const s=t[0];return q(t,C)?(this._allow=t,this._reject=[]):b(s)&&(this._allow=Object.values(s).map(r=>C(r)?r:null).filter(r=>r!==null),this._reject=[]),this}disallow(...t){if(this._locked)throw l("disallow");return this._reject.push(...t),this}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){const t=new H;return t._min=this._min,t._max=this._max,t._allow=[...this._allow],t._reject=[...this._reject],t._locked=!1,t}validate(t,s=g.warn){return t===void 0?this._opt||(s("should be a bigint, but got undefined"),!1):t===null?this._null||(s("should be a bigint, but got null"),!1):C(t)?this._allow.length>0?this._allow.includes(t)||(s(`${t} is not allowed, just support ${this._allow.join(", ")}`),!1):this._reject.length>0&&this._reject.includes(t)?(s(`${t} is not allowed`),!1):(this._min!==null?t>=this._min:!0)&&(this._max!==null?t<=this._max:!0)||(s(`${t} is not in range [${this._min===null?-1/0:this._min}, ${this._max===null?1/0:this._max}]`),!1):(s(`should be a bigint, but got ${typeof t}`),!1)}}class G{constructor(){o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1)}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){return new G}validate(t,s=g.warn){return t===void 0?this._opt||(s("should be a boolean, but got undefined"),!1):t===null?this._null||(s("should be a boolean, but got null"),!1):typeof t!="boolean"?(s(`should be a boolean, but got ${typeof t}`),!1):!0}}class R{constructor(t){o(this,"_shape"),o(this,"_plain",!1),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1),this._shape=t}plain(){if(this._locked)throw l("plain");return this._plain=!0,this}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}get shape(){return{...this._shape}}clone(){const t=new R(this._shape);return t._plain=this._plain,t._locked=!1,t}match(t,s){return t in this._shape?this._shape[t].validate(s,rt):!1}validate(t,s=g.warn){return t===void 0?this._opt||(s("should be a object, but got undefined"),!1):t===null?this._null||(s("should be a object, but got null"),!1):it(t)?this._plain&&!b(t)?(s(`should be a plain object, but got ${P(t)}`),!1):Object.keys(this._shape).every(r=>this._shape[r].validate(t[r],(...n)=>{s(`[.${String(r)}]`,...n)})):(s(`should be a object, but got ${P(t)}`),!1)}}class z{constructor(t){o(this,"_val"),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1),this._val=t}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){return new z(this._val)}validate(t,s=g.warn){return t===void 0?this._opt||(s("should be a record, but got undefined"),!1):t===null?this._null||(s("should be a record, but got null"),!1):b(t)?Object.keys(t).every(r=>this._val.validate(t[r],(...n)=>{s(`[:${r}]`,...n)})):(s(`should be a record, but got ${P(t)}`),!1)}}class K{constructor(t){o(this,"_val"),o(this,"_min",0),o(this,"_max",1/0),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1),this._val=t}min(t){if(this._locked)throw l("min");return this._min=t,this}max(t){if(this._locked)throw l("max");return this._max=t,this}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){const t=new K(this._val);return t._min=this._min,t._max=this._max,t._locked=!1,t}validate(t,s=g.warn){return t===void 0?this._opt||(s("should be a array, but got undefined"),!1):t===null?this._null||(s("should be a array, but got null"),!1):Array.isArray(t)?t.length<this._min||t.length>this._max?(this._min===this._max?s(`should be a array with length ${this._min}`):s(`should be a array with length between ${this._min} and ${this._max}`),!1):t.every((r,n)=>this._val.validate(r,(...i)=>{s(`[${n}]`,...i)})):(s(`should be a array, but got ${typeof t}`),!1)}}class W{constructor(...t){o(this,"_vs"),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1),this._vs=t}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){return new W(...this._vs)}validate(t,s=g.warn){return t===void 0?this._opt||(s("should be a tuple, but got undefined"),!1):t===null?this._null||(s("should be a tuple, but got null"),!1):Array.isArray(t)?t.length!==this._vs.length?(s(`should be a tuple with length ${this._vs.length}, but got ${t.length}`),!1):this._vs.every((r,n)=>r.validate(t[n],(...i)=>{s(`[idx:${n}]`,...i)})):(s(`should be a tuple, but got ${typeof t}`),!1)}}class J{constructor(...t){o(this,"_vs"),o(this,"_key",""),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1),this._vs=t}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}key(t){if(this._locked)throw l("key");return this._vs.every(s=>s instanceof R)?this._key=t:g.warn("union type key can only be used when all validators are ObjectValidator"),this}satisfies(){return this}lock(){return this._locked=!0,this}get validators(){return[...this._vs]}clone(){const t=new J(...this._vs);return t._key=this._key,t._locked=!1,t}validate(t,s=g.warn){if(t===void 0)return this._opt||(s("should be a union, but got undefined"),!1);if(t===null)return this._null||(s("should be a union, but got null"),!1);if(this._key&&it(t)&&t){const n=t[this._key];if(n===void 0)return s(`key field "${this._key}" is not found`,t),!1;const i=this._vs.find(a=>a instanceof R&&a.match(this._key,n));return i?i.validate(t,s):(s(`key field "${this._key}" value is match union definition`,n),!1)}const r=this._vs.some(n=>n.validate(t,rt));return r||s("value is not match union definition",t),r}}class S{constructor(t,s){o(this,"_name",""),o(this,"_guard"),o(this,"_locked",!1),o(this,"_opt",!1),o(this,"_null",!1),this._name=t,this._guard=s}optional(){if(this._locked)throw l("optional");return this._opt=!0,this}maybeNull(){if(this._locked)throw l("maybeNull");return this._null=!0,this}lock(){return this._locked=!0,this}clone(){return new S(this._name,this._guard)}validate(t,s=g.warn){const r=this._name||"custom type";return t===void 0?this._opt||(s(`should be ${r}, but got undefined`),!1):t===null?this._null||(s(`should be ${r}, but got null`),!1):this._guard(t)||(s(`custom validation${this._name?`(${this._name})`:""} failed`),!1)}}function Nt(e,t){if(typeof e=="string"){if(!t)throw new Error("custom type guard must be defined");return new S(e,t)}return new S("",e)}const Ot={shape(e){return e},define(e){return e},guard(e){return function(t){return e.validate(t)}},object(e){return new R(e)},record(e){return new z(e)},array(e){return new K(e)},tuple(...e){return new W(...e)},union(...e){return new J(...e)},string(){return new D},bool(){return new G},number(){return new B},bigint(){return new H},custom:Nt,unknown(){return{validate(e){return!0}}},never(){return{validate(e){return!1}}}},xt=e=>b(e)&&Object.keys(e).length===0;Ot.custom("EmptyObject",xt);function At(e,t="数据未能正确识别"){return typeof e=="function"?{guard:e,message:t}:{guard:e.guard,message:e.message||t}}function Ft(e,t=""){return!t||U(e,!0)?tt(e):(tt(t)+"/"+e).replace(/\/{2,}/g,"/").replace(/:\//,"://")}function tt(e){return U(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 St(e,t){const s={};return(e.match(/([^=&#?]+)=[^&#]*/g)||[]).forEach(function(r){const n=r.split("="),i=n[0],a=decodeURIComponent(n[1]||"");s[i]!==void 0?s[i]+=","+a:s[i]=a}),t!==!0?s[t]||"":s}function Ut(e,t){if(t){if(t===!0)return e.replace(/\?[^#]*/,"")}else return e;const s=e.split("#"),r=s[0].split("?"),n=r[0],i=r.length>1?r[1]:"",a=s.length>1?"#"+s[1]:"",h=typeof t=="string"?[t]:Array.isArray(t)?t:[];return!h.length||!i?s[0]+a:(h.map(c=>c.replace(/([\\(){}[\]^$+\-*?|])/g,"\\$1")),(n+"?"+i.replace(new RegExp("(?:^|&)(?:"+h.join("|")+")=[^&$]+","g"),"").replace(/^&/,"")).replace(/\?$/,"")+a)}function Tt(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 n=e.split("#");s&&(n[0]=Ut(n[0],(r.match(/([^=&#?]+)=[^&#]+/g)||[]).map(a=>a.replace(/=.+$/,""))));const i=n[0].indexOf("?")+1?"&":"?";return(n[0]+i+r+(n.length>1?"#"+n[1]:"")).replace(/\?&/,"?")}function Ct(e){const t=e.match(/(?:\?|&)([^=]+)(?:&|$)/g);return t?t.join("").replace(/(?:\?|^&+|&+$)/g,"").replace(/&{2}/g,"&").split("&").sort():[]}class Mt{constructor(t=500){v(this,"ttl");v(this,"cache");this.cache={},this.ttl=Math.max(t,0)}getKey(t,s){const r=t.replace(/#.+/,""),n=r.replace(/\?.+/g,""),i=Object.assign(St(r,!0),s),a=Ct(r),h=Object.keys(i).sort().map(c=>`${c}#${i[c]}`);return`${n}_${h.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 ut{constructor(t){v(this,"config",{baseURL:"/",maxRetry:0,retryInterval:100,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)&&!U(t.baseURL))throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"),new Error("BaseURLError");Object.assign(this.config,t)}get(t){return this.config[t]}getFullUrl(t){return Ft(t,this.config.baseURL)}showMessage(t,s,r,n){this.config.messageHandler&&s&&this.config.messageHandler(t,s,r,n)}}const V={UnexpectResponse:"UnexpectResponse",Aborted:"Aborted",Unknown:"Unknown",NetworkError:"NetworkError",Timeout:"Timeout",NotSupport:"NotSupport",URLFormatError:"URLFormatError"};function Pt(e,t,s,r){if(t.ok&&!jt(t.data)&&r){const n=At(r,s.get("defaultTypeGuardMessage"));return n.guard(t.data)||(t.code=V.UnexpectResponse,s.showMessage(!0,`${e} ${n.message}`,t.code,t.status),console.error(t.code,e,t.data),t.data=null,t.message=n.message),t}return t}class Lt{constructor(t,s){v(this,"agent");v(this,"config");v(this,"cache");this.config=new ut(s),this.agent=t,this.cache=new Mt(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:V.Unknown,message:r instanceof Error?`${r.message}
|
|
2
|
+
${r.stack||""}`:String(r),headers:{},data:null}}}async guard(t,s,r){return Pt(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 n=Object.assign({},r||null);n.method="GET";const i=this.cache.getKey(t,n.params),a=this.cache.get(i);if(a)return this.guard(t,await a,s||null);const h=this.request(t,n);return this.cache.set(i,h),this.guard(t,await h,s||null)}async post(t,s,r,n){const i=Object.assign({},n||null);return i.method="POST",i.body=s||{},this.guard(t,await this.request(t,i),r||null)}async del(t,s,r){const n=Object.assign({},r||null);return n.method="DELETE",this.guard(t,await this.request(t,n),s||null)}async put(t,s,r,n){const i=Object.assign({},n||null);return i.method="PUT",i.body=s||{},this.guard(t,await this.request(t,i),r||null)}async patch(t,s,r,n){const i=Object.assign({},n||null);return i.method="PATCH",i.body=s||{},this.guard(t,await this.request(t,i),r||null)}}async function It(e,t,s){var w;const r=Object.assign({method:"GET"},s),n=j.FormData?r.body instanceof FormData:!1,i=n&&r.method!=="POST"&&r.method!=="PUT"?"POST":r.method,a=i==="GET"||i==="HEAD"||i==="DELETE";a&&r.body!==void 0&&(console.warn("request body is invalid with method get, head, delete"),delete r.body);const h=Object.assign(n||a?{}:{"Content-Type":j.Blob&&r.body instanceof Blob?r.body.type||"application/octet-stream":"application/json;charset=utf-8"},r.headers),c=r.params||{},m={};Object.keys(c).forEach($=>{c[$]!==void 0&&(m[$]=qt(c[$]))});const f=t.getFullUrl(e),d=Dt(r.body),p=r.timeout||t.get("timeout"),y=await async function(){const $=t.get("requestTransformer");if($)return await $({headers:h,params:m,method:i,url:f,body:d})}(),k=typeof y=="string"&&y?y:f;return(w=t.get("logHandler"))==null||w({type:"ready",url:k,method:i,headers:h,timeout:p,body:d}),{url:k,method:i,body:d,params:m,headers:h,timeout:p,abort:r.abort,credentials:r.credentials||t.get("credentials")}}function qt(e){return typeof e=="string"?e:Array.isArray(e)?e.join(","):e+""}function Dt(e){if(e)return typeof e=="string"||j.URLSearchParams&&e instanceof URLSearchParams||e instanceof ArrayBuffer||j.Blob&&e instanceof Blob||j.FormData&&e instanceof FormData?e:JSON.stringify(e)}const ht="data",E="message";function Bt(e,t,s,r,n){const i=n||r;return Z(e)?Vt(i.ok||r.ok,e,t,s):Gt(i.failed||r.failed,t,s)}const Ht=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||E)+" 作为错误消息");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||ht)+" 作为响应数据,读取 "+(r.messageField||E)+" 作为提示消息"),r.statusField&&t.push(" 当 "+r.statusField+" 为 "+(r.statusOKValue||"空值")+" 时是成功提示,否则是错误消息"),r.ignoreMessage&&t.push(" 并忽略以下消息:"+r.ignoreMessage);break}return t.join(`
|
|
3
|
+
`)};function Gt(e,t,s){const r=e||{resolve:"json",messageField:E},n={ok:!1,code:t,message:s,data:null};switch(r.resolve){case"body":n.message=et(s)||s;break;case"json":const{code:i,message:a}=zt(s,r.converter,r.statusField,r.messageField);n.code=i||t,n.message=et(s)||a;break}return n}function zt(e,t,s,r=E){if(!ot(e))return{message:""};const n=I(M(e),t);return!n||!b(n)?{message:e}:{code:s?L(n,s):"",message:L(n,r)||e}}function L(e,t){const s=Array.isArray(t)?t:[t];for(const r of s)if(r in e)return Kt(e[r]);return""}function Kt(e){return e?typeof e=="string"?e:JSON.stringify(e):""}const Wt=/<title>([^<]+)<\/title>/i,Jt=/<message>([^<]+)<\/message>/i;function et(e){const t=e.match(Wt);if(t)return t[1];const s=e.match(Jt);return s?s[1]:""}function Vt(e,t,s,r){const n=e||{resolve:"body"},i={ok:!0,code:s,message:"",data:null};if(t===204||!r)return i;if(n.resolve==="body")return i.data=ot(r)?I(M(r),e.converter):r,i;const a=I(M(r),e.converter);if(!a||!b(a))return i.ok=!1,i.code="ResponseFormatError",i.message="响应内容无法格式化为 Object",i;const h=n.statusField,c=n.statusOKValue||"",m=n.dataField||ht,f=n.messageField||E,d=n.ignoreMessage||"";if(h&&!(h in a))return i.ok=!1,i.code="ResponseFieldMissing",i.message="响应内容找不到状态字段 "+h,i;const p=h?a[h]+"":"";return i.ok=h?p===c:!0,i.code=p||s,i.data=m===!0?a:m in a?a[m]:null,i.message=L(a,f),d&&i.message&&(Array.isArray(d)&&d.includes(i.message)||typeof d=="string"&&i.message===d)&&(i.message=""),i}function Z(e){return e>=200&&e<400}function I(e,t){return t==="camelize"?A(e):t==="snakify"?F(e):e}const Zt=bt("APIError");function Xt(e){const t={};for(const n in e.headers)(n.startsWith("x-")||n.includes("trace")||n.includes("server")||/\b(?:id|uuid)\b/.test(n))&&(t[n]=e.headers[n]);const s=e.url.replace(/^(?:https?:)?\/*/i,"").replace(/\?.+/,""),r=e.status<0?"unknown":e.status;return{sentryError:new Zt(`${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 ct(e){return e.reduce((t,[s,r])=>(s&&(t[s]=r||""),t),{})}function Yt(e,t,s,r){var y;const n=e.status,i=e.method,a=ct(Object.entries(e.headers||{}).map(([k,w])=>[k.toLowerCase(),w])),{ok:h,code:c,data:m,message:f}=Bt(n,e.statusText,e.body,s.get("responseRule"),r==null?void 0:r.responseRule);if(!Z(n)){const k=Xt({url:e.url,method:e.method,status:n,code:c,message:f,body:e.body,headers:a,error:e.rawError});(s.get("errorHandler")||Qt)({url:t,method:i,status:n,code:c,message:f,headers:a,rawError:e.rawError,responseBody:e.body,...k})}if(n<0)return st({ok:!1,status:n,code:e.statusText,headers:{},message:"",data:null},`${i} ${t} ${e.statusText}`,i,t,s,r);const d={ok:h,data:m,code:c,message:f,status:n,headers:a};(y=s.get("responseHandler"))==null||y({...d},i,t);const p=h?f:f||e.statusText;return st(d,p,i,t,s,r)}function st(e,t,s,r,n,i){const a=n.get("message"),h=a===!1||(i==null?void 0:i.message)===!1?!1:(i==null?void 0:i.message)||a;if(h!==!1){const c=typeof h=="function"?h(e,s,r,t):t;c instanceof Error?n.showMessage(!0,c.message,e.code,e.status):c&&typeof c=="object"&&"message"in c?n.showMessage(!1,c.message,e.code,e.status):n.showMessage(!e.ok,c,e.code,e.status)}return e}function Qt(e){const t={};for(const s in e)s.startsWith("sentry")||(t[s]=e[s]);console.error("RequestError",t)}async function ft(e,t,s,r,n){const i=n||0,a=Math.max(0,Math.min(10,(r==null?void 0:r.maxRetry)??s.get("maxRetry")??0)),h=(r==null?void 0:r.retryResolve)??s.get("retryResolve"),c=s.get("logHandler");c==null||c({type:"prepare",url:t,method:(r==null?void 0:r.method)||"GET",retry:i,maxRetry:a,message:i===0?"start":`retry ${i}/${a} start`,headers:r==null?void 0:r.headers,options:r});const m=Date.now(),f=await e(t,s,r),d=f.status,p=Date.now()-m,y=`[cost ${p}][${d}] ${d<0?f.body:""}`;c==null||c({type:"finished",url:t,method:f.method,retry:i,maxRetry:a,message:i===0?`finish ${y}`:`retry ${i}/${a} finish ${y}`,response:f,headers:f.headers,cost:p});const k=Z(d);if(!a||i>=a||h==="network"&&d>0||h==="status"&&k||Array.isArray(h)&&!h.includes(d)||typeof h=="function"&&h(f,i)!==!0)return f;const w=(r==null?void 0:r.retryInterval)??s.get("retryInterval")??100;return await pt(Math.max(100,w==="2EB"?Math.pow(2,i)*100:typeof w=="function"?w(i+1,{url:t,status:d,method:f.method})||0:w)),await ft(e,t,s,r,i+1)}exports.At=U;exports.NetRequestHandler=Lt;exports.RequestGlobalConfig=ut;exports.RequestInternalError=V;exports.ce=Et;exports.convertOptions=It;exports.fromEntries=ct;exports.ge=Tt;exports.getResponseRulesDescription=Ht;exports.handleResponse=Yt;exports.jt=j;exports.retryRequest=ft;
|
|
@@ -3,12 +3,12 @@ var ct = (e, t, s) => t in e ? ht(e, t, { enumerable: !0, configurable: !0, writ
|
|
|
3
3
|
var v = (e, t, s) => ct(e, typeof t != "symbol" ? t + "" : t, s);
|
|
4
4
|
var ft = Object.defineProperty, dt = (e, t, s) => t in e ? ft(e, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : e[t] = s, o = (e, t, s) => dt(e, typeof t != "symbol" ? t + "" : t, s);
|
|
5
5
|
const u = typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")(), j = {
|
|
6
|
-
wx: "wx" in u &&
|
|
6
|
+
wx: "wx" in u && x(u.wx) && "getSystemInfo" in u.wx && _(u.wx.getSystemInfo),
|
|
7
7
|
fetch: "fetch" in u && _(u.fetch),
|
|
8
|
-
window: "window" in u &&
|
|
8
|
+
window: "window" in u && x(u.window),
|
|
9
9
|
requestAnimationFrame: "requestAnimationFrame" in u && _(u.requestAnimationFrame),
|
|
10
|
-
localStorage: "localStorage" in u &&
|
|
11
|
-
sessionStorage: "sessionStorage" in u &&
|
|
10
|
+
localStorage: "localStorage" in u && x(u.localStorage) && "getItem" in u.localStorage && _(u.localStorage.getItem),
|
|
11
|
+
sessionStorage: "sessionStorage" in u && x(u.sessionStorage) && "getItem" in u.sessionStorage && _(u.sessionStorage.getItem),
|
|
12
12
|
File: "File" in u && _(u.File),
|
|
13
13
|
Blob: "Blob" in u && _(u.Blob),
|
|
14
14
|
Proxy: "Proxy" in u && _(u.Proxy),
|
|
@@ -22,15 +22,15 @@ const u = typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : type
|
|
|
22
22
|
function _(e) {
|
|
23
23
|
return typeof e == "function";
|
|
24
24
|
}
|
|
25
|
-
function
|
|
25
|
+
function x(e) {
|
|
26
26
|
return typeof e == "object" && e !== null;
|
|
27
27
|
}
|
|
28
28
|
function _t(e) {
|
|
29
29
|
return new Promise((t) => setTimeout(t, Math.max(0, e)));
|
|
30
30
|
}
|
|
31
|
-
function
|
|
31
|
+
function st() {
|
|
32
32
|
}
|
|
33
|
-
function
|
|
33
|
+
function rt(e, t) {
|
|
34
34
|
const s = {
|
|
35
35
|
log: console.log.bind(console),
|
|
36
36
|
error: console.error.bind(console),
|
|
@@ -59,13 +59,13 @@ const gt = /^(?:https?:)?\/\/.+$/i, pt = /^https?:\/\/.+$/i, bt = /^\{[\d\D]*\}$
|
|
|
59
59
|
function wt(e) {
|
|
60
60
|
return typeof e == "string";
|
|
61
61
|
}
|
|
62
|
-
function
|
|
62
|
+
function I(e, t = !1) {
|
|
63
63
|
return t ? gt.test(e) : pt.test(e);
|
|
64
64
|
}
|
|
65
65
|
function kt(e) {
|
|
66
66
|
return e == null;
|
|
67
67
|
}
|
|
68
|
-
function
|
|
68
|
+
function nt(e, ...t) {
|
|
69
69
|
return e !== null && typeof e == "object" && !(e instanceof Map) && !(e instanceof Set) && !(e instanceof WeakMap) && !(e instanceof WeakSet) && !Array.isArray(e) && t.every((s) => s in e);
|
|
70
70
|
}
|
|
71
71
|
function b(e, ...t) {
|
|
@@ -73,7 +73,7 @@ function b(e, ...t) {
|
|
|
73
73
|
const s = Object.getPrototypeOf(e);
|
|
74
74
|
return s !== Object.prototype && s !== null ? !1 : t.every((r) => r in e);
|
|
75
75
|
}
|
|
76
|
-
function
|
|
76
|
+
function D(e, t) {
|
|
77
77
|
return Array.isArray(e) && e.every((s) => t(s));
|
|
78
78
|
}
|
|
79
79
|
function U(e) {
|
|
@@ -84,23 +84,23 @@ function U(e) {
|
|
|
84
84
|
const t = Math.abs(e);
|
|
85
85
|
return t >= Number.EPSILON && t <= Number.MAX_SAFE_INTEGER;
|
|
86
86
|
}
|
|
87
|
-
function
|
|
87
|
+
function it(e) {
|
|
88
88
|
return bt.test(e) || yt.test(e);
|
|
89
89
|
}
|
|
90
90
|
function vt(e, t = 20) {
|
|
91
91
|
const s = Math.max(t, 10), r = [...e];
|
|
92
92
|
return r.length <= s ? e : r.slice(0, s - 3).join("") + "...";
|
|
93
93
|
}
|
|
94
|
-
function
|
|
94
|
+
function O(e) {
|
|
95
95
|
return e ? e[0].toLowerCase() + e.slice(1) : "";
|
|
96
96
|
}
|
|
97
|
-
const
|
|
98
|
-
function
|
|
99
|
-
const t =
|
|
100
|
-
return
|
|
97
|
+
const ot = /_\w*/, at = /-\w*/;
|
|
98
|
+
function Z(e) {
|
|
99
|
+
const t = ot.test(e) ? e.replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (s, r) => r.toUpperCase()) : at.test(e) ? e.replace(/(?:^-*|-*$)/g, "").replace(/-+(\w)/g, (s, r) => r.toUpperCase()) : e;
|
|
100
|
+
return O(t);
|
|
101
101
|
}
|
|
102
|
-
function
|
|
103
|
-
return
|
|
102
|
+
function X(e) {
|
|
103
|
+
return ot.test(e) ? O(e).replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (t, s) => "_" + s.toLowerCase()) : at.test(e) ? O(e.replace(/(?:^-*|-*$)/g, "")).replace(/-+(\w)/g, (t, s) => "_" + s.toLowerCase()) : O(e).replace(/[A-Z]/g, (t) => `_${t.toLowerCase()}`);
|
|
104
104
|
}
|
|
105
105
|
function M(e, t) {
|
|
106
106
|
try {
|
|
@@ -112,24 +112,24 @@ function M(e, t) {
|
|
|
112
112
|
}
|
|
113
113
|
"" + Math.random().toString(32).slice(2);
|
|
114
114
|
function A(e, t = !1) {
|
|
115
|
-
return typeof e == "string" ?
|
|
115
|
+
return typeof e == "string" ? Z(e) : Array.isArray(e) ? e.map((s) => !t || typeof s == "string" ? A(s) : typeof s == "object" && s ? A(s, !0) : s) : b(e) ? Object.keys(e).reduce(
|
|
116
116
|
(s, r) => {
|
|
117
|
-
const n =
|
|
117
|
+
const n = Z(String(r)), i = n.charAt(0).toLowerCase() + n.slice(1);
|
|
118
118
|
return s[i] = t ? e[r] : A(e[r]), s;
|
|
119
119
|
},
|
|
120
120
|
{}
|
|
121
121
|
) : e;
|
|
122
122
|
}
|
|
123
123
|
function F(e, t = !1) {
|
|
124
|
-
return typeof e == "string" ?
|
|
124
|
+
return typeof e == "string" ? X(e) : Array.isArray(e) ? e.map((s) => !t || typeof s == "string" ? F(s) : typeof s == "object" && s ? F(s, t) : s) : b(e) ? Object.keys(e).reduce(
|
|
125
125
|
(s, r) => {
|
|
126
|
-
const n =
|
|
126
|
+
const n = X(String(r));
|
|
127
127
|
return s[n] = t ? e[r] : F(e[r]), s;
|
|
128
128
|
},
|
|
129
129
|
{}
|
|
130
130
|
) : e;
|
|
131
131
|
}
|
|
132
|
-
|
|
132
|
+
rt("LoadUtils");
|
|
133
133
|
async function Vt(e) {
|
|
134
134
|
return await new Promise(function(t) {
|
|
135
135
|
const s = document.getElementsByTagName("head")[0], r = document.createElement("script");
|
|
@@ -140,13 +140,13 @@ async function Vt(e) {
|
|
|
140
140
|
}, r.setAttribute("src", e), s.appendChild(r);
|
|
141
141
|
});
|
|
142
142
|
}
|
|
143
|
-
function
|
|
143
|
+
function Y(e) {
|
|
144
144
|
return Object.getOwnPropertyNames(e).forEach(function(t) {
|
|
145
145
|
delete e[t];
|
|
146
146
|
}), e;
|
|
147
147
|
}
|
|
148
|
-
const g =
|
|
149
|
-
function
|
|
148
|
+
const g = rt("Validator");
|
|
149
|
+
function E(e) {
|
|
150
150
|
return e ? `"${vt(e).replace(/"/g, '\\"')}"` : "empty string";
|
|
151
151
|
}
|
|
152
152
|
function C(e) {
|
|
@@ -158,7 +158,7 @@ function T(e) {
|
|
|
158
158
|
function l(e) {
|
|
159
159
|
return new Error(`${e ? `${e}()` : "method"} is not allowed after locked`);
|
|
160
160
|
}
|
|
161
|
-
class
|
|
161
|
+
class q {
|
|
162
162
|
constructor() {
|
|
163
163
|
o(this, "_ps", {}), o(this, "_allow", []), o(this, "_reject", []), o(this, "_locked", !1), o(this, "_opt", !1), o(this, "_null", !1);
|
|
164
164
|
}
|
|
@@ -186,7 +186,7 @@ class B {
|
|
|
186
186
|
enum(...t) {
|
|
187
187
|
if (this._locked) throw l("enum");
|
|
188
188
|
const s = t[0];
|
|
189
|
-
return
|
|
189
|
+
return D(t, wt) ? (this._allow = t, Y(this._ps)) : b(s) && (this._allow = Object.values(s).map((r) => `${r}`), Y(this._ps)), this;
|
|
190
190
|
}
|
|
191
191
|
disallow(...t) {
|
|
192
192
|
return this._reject.push(...t), this;
|
|
@@ -203,7 +203,7 @@ class B {
|
|
|
203
203
|
return this._locked = !0, this;
|
|
204
204
|
}
|
|
205
205
|
clone() {
|
|
206
|
-
const t = new
|
|
206
|
+
const t = new q();
|
|
207
207
|
return t._ps = { ...this._ps }, t._allow = [...this._allow], t._reject = [...this._reject], t._locked = !1, t;
|
|
208
208
|
}
|
|
209
209
|
validate(t, s = g.warn) {
|
|
@@ -214,17 +214,17 @@ class B {
|
|
|
214
214
|
if (typeof t != "string")
|
|
215
215
|
return s(`should be a string, but got ${typeof t}`), !1;
|
|
216
216
|
if (this._allow.length > 0)
|
|
217
|
-
return this._allow.includes(t) || (s(`${
|
|
217
|
+
return this._allow.includes(t) || (s(`${E(t)} is not allowed, just support ${this._allow.map(E).join(", ")}`), !1);
|
|
218
218
|
if (this._reject.length > 0 && this._reject.some((n) => typeof n == "string" ? n === t : n.test(t)))
|
|
219
|
-
return s(`${
|
|
219
|
+
return s(`${E(t)} is not allowed`), !1;
|
|
220
220
|
const r = Object.keys(this._ps);
|
|
221
221
|
return r.length > 0 && !r.some((n) => {
|
|
222
222
|
const i = this._ps[n];
|
|
223
223
|
return i instanceof RegExp ? i.test(t) : i(t);
|
|
224
|
-
}) ? (s(`${
|
|
224
|
+
}) ? (s(`${E(t)} is not match pattern ${r.join(" | ")}`), !1) : !0;
|
|
225
225
|
}
|
|
226
226
|
}
|
|
227
|
-
class
|
|
227
|
+
class B {
|
|
228
228
|
constructor() {
|
|
229
229
|
o(this, "_int", !1), o(this, "_min", -1 / 0), o(this, "_max", 1 / 0), o(this, "_allow", []), o(this, "_reject", []), o(this, "_infinite", !1), o(this, "_unsafe", !1), o(this, "_nan", !1), o(this, "_locked", !1), o(this, "_opt", !1), o(this, "_null", !1);
|
|
230
230
|
}
|
|
@@ -258,7 +258,7 @@ class q {
|
|
|
258
258
|
enum(...t) {
|
|
259
259
|
if (this._locked) throw l("enum");
|
|
260
260
|
const s = t[0];
|
|
261
|
-
return
|
|
261
|
+
return D(t, U) ? (this._allow = t, this._reject = []) : b(s) && (this._allow = Object.values(s).map((r) => U(r) ? r : null).filter((r) => r !== null), this._reject = []), this;
|
|
262
262
|
}
|
|
263
263
|
disallow(...t) {
|
|
264
264
|
if (this._locked) throw l("disallow");
|
|
@@ -276,7 +276,7 @@ class q {
|
|
|
276
276
|
return this._locked = !0, this;
|
|
277
277
|
}
|
|
278
278
|
clone() {
|
|
279
|
-
const t = new
|
|
279
|
+
const t = new B();
|
|
280
280
|
return t._int = this._int, t._min = this._min, t._max = this._max, t._allow = [...this._allow], t._reject = [...this._reject], t._infinite = this._infinite, t._unsafe = this._unsafe, t._nan = this._nan, t._locked = !1, t;
|
|
281
281
|
}
|
|
282
282
|
validate(t, s = g.warn) {
|
|
@@ -298,7 +298,7 @@ class H {
|
|
|
298
298
|
enum(...t) {
|
|
299
299
|
if (this._locked) throw l("enum");
|
|
300
300
|
const s = t[0];
|
|
301
|
-
return
|
|
301
|
+
return D(t, T) ? (this._allow = t, this._reject = []) : b(s) && (this._allow = Object.values(s).map((r) => T(r) ? r : null).filter((r) => r !== null), this._reject = []), this;
|
|
302
302
|
}
|
|
303
303
|
disallow(...t) {
|
|
304
304
|
if (this._locked) throw l("disallow");
|
|
@@ -347,7 +347,7 @@ class z {
|
|
|
347
347
|
return t === void 0 ? this._opt || (s("should be a boolean, but got undefined"), !1) : t === null ? this._null || (s("should be a boolean, but got null"), !1) : typeof t != "boolean" ? (s(`should be a boolean, but got ${typeof t}`), !1) : !0;
|
|
348
348
|
}
|
|
349
349
|
}
|
|
350
|
-
class
|
|
350
|
+
class R {
|
|
351
351
|
constructor(t) {
|
|
352
352
|
o(this, "_shape"), o(this, "_plain", !1), o(this, "_locked", !1), o(this, "_opt", !1), o(this, "_null", !1), this._shape = t;
|
|
353
353
|
}
|
|
@@ -373,17 +373,17 @@ class N {
|
|
|
373
373
|
return { ...this._shape };
|
|
374
374
|
}
|
|
375
375
|
clone() {
|
|
376
|
-
const t = new
|
|
376
|
+
const t = new R(this._shape);
|
|
377
377
|
return t._plain = this._plain, t._locked = !1, t;
|
|
378
378
|
}
|
|
379
379
|
/**
|
|
380
380
|
* 检查指定的 field / value 是否可以通过校验
|
|
381
381
|
*/
|
|
382
382
|
match(t, s) {
|
|
383
|
-
return t in this._shape ? this._shape[t].validate(s,
|
|
383
|
+
return t in this._shape ? this._shape[t].validate(s, st) : !1;
|
|
384
384
|
}
|
|
385
385
|
validate(t, s = g.warn) {
|
|
386
|
-
return t === void 0 ? this._opt || (s("should be a object, but got undefined"), !1) : t === null ? this._null || (s("should be a object, but got null"), !1) :
|
|
386
|
+
return t === void 0 ? this._opt || (s("should be a object, but got undefined"), !1) : t === null ? this._null || (s("should be a object, but got null"), !1) : nt(t) ? this._plain && !b(t) ? (s(`should be a plain object, but got ${C(t)}`), !1) : Object.keys(this._shape).every((r) => this._shape[r].validate(t[r], (...n) => {
|
|
387
387
|
s(`[.${String(r)}]`, ...n);
|
|
388
388
|
})) : (s(`should be a object, but got ${C(t)}`), !1);
|
|
389
389
|
}
|
|
@@ -494,7 +494,7 @@ class J {
|
|
|
494
494
|
*/
|
|
495
495
|
key(t) {
|
|
496
496
|
if (this._locked) throw l("key");
|
|
497
|
-
return this._vs.every((s) => s instanceof
|
|
497
|
+
return this._vs.every((s) => s instanceof R) ? this._key = t : g.warn("union type key can only be used when all validators are ObjectValidator"), this;
|
|
498
498
|
}
|
|
499
499
|
satisfies() {
|
|
500
500
|
return this;
|
|
@@ -514,16 +514,16 @@ class J {
|
|
|
514
514
|
return this._opt || (s("should be a union, but got undefined"), !1);
|
|
515
515
|
if (t === null)
|
|
516
516
|
return this._null || (s("should be a union, but got null"), !1);
|
|
517
|
-
if (this._key &&
|
|
517
|
+
if (this._key && nt(t) && t) {
|
|
518
518
|
const n = t[this._key];
|
|
519
519
|
if (n === void 0)
|
|
520
520
|
return s(`key field "${this._key}" is not found`, t), !1;
|
|
521
521
|
const i = this._vs.find(
|
|
522
|
-
(a) => a instanceof
|
|
522
|
+
(a) => a instanceof R && a.match(this._key, n)
|
|
523
523
|
);
|
|
524
524
|
return i ? i.validate(t, s) : (s(`key field "${this._key}" value is match union definition`, n), !1);
|
|
525
525
|
}
|
|
526
|
-
const r = this._vs.some((n) => n.validate(t,
|
|
526
|
+
const r = this._vs.some((n) => n.validate(t, st));
|
|
527
527
|
return r || s("value is not match union definition", t), r;
|
|
528
528
|
}
|
|
529
529
|
}
|
|
@@ -581,7 +581,7 @@ const jt = {
|
|
|
581
581
|
},
|
|
582
582
|
/** 对象结构验证 */
|
|
583
583
|
object(e) {
|
|
584
|
-
return new
|
|
584
|
+
return new R(e);
|
|
585
585
|
},
|
|
586
586
|
/**
|
|
587
587
|
* record 数据验证,不指定 key 即 string Record。
|
|
@@ -605,7 +605,7 @@ const jt = {
|
|
|
605
605
|
},
|
|
606
606
|
/** 字符串类型验证 */
|
|
607
607
|
string() {
|
|
608
|
-
return new
|
|
608
|
+
return new q();
|
|
609
609
|
},
|
|
610
610
|
/** 布尔型类型验证 */
|
|
611
611
|
bool() {
|
|
@@ -613,7 +613,7 @@ const jt = {
|
|
|
613
613
|
},
|
|
614
614
|
/** 数字验证 */
|
|
615
615
|
number() {
|
|
616
|
-
return new
|
|
616
|
+
return new B();
|
|
617
617
|
},
|
|
618
618
|
/** bigint 验证 */
|
|
619
619
|
bigint() {
|
|
@@ -641,9 +641,9 @@ const jt = {
|
|
|
641
641
|
}
|
|
642
642
|
};
|
|
643
643
|
}
|
|
644
|
-
},
|
|
645
|
-
jt.custom("EmptyObject",
|
|
646
|
-
function
|
|
644
|
+
}, Rt = (e) => b(e) && Object.keys(e).length === 0;
|
|
645
|
+
jt.custom("EmptyObject", Rt);
|
|
646
|
+
function Nt(e, t = "数据未能正确识别") {
|
|
647
647
|
return typeof e == "function" ? {
|
|
648
648
|
guard: e,
|
|
649
649
|
message: t
|
|
@@ -652,20 +652,20 @@ function xt(e, t = "数据未能正确识别") {
|
|
|
652
652
|
message: e.message || t
|
|
653
653
|
};
|
|
654
654
|
}
|
|
655
|
-
function
|
|
656
|
-
return !t ||
|
|
655
|
+
function xt(e, t = "") {
|
|
656
|
+
return !t || I(e, !0) ? Q(e) : (Q(t) + "/" + e).replace(/\/{2,}/g, "/").replace(/:\//, "://");
|
|
657
657
|
}
|
|
658
|
-
function
|
|
659
|
-
return
|
|
658
|
+
function Q(e) {
|
|
659
|
+
return I(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(/^\/+/, "");
|
|
660
660
|
}
|
|
661
|
-
function
|
|
661
|
+
function Et(e, t) {
|
|
662
662
|
const s = {};
|
|
663
663
|
return (e.match(/([^=&#?]+)=[^&#]*/g) || []).forEach(function(r) {
|
|
664
664
|
const n = r.split("="), i = n[0], a = decodeURIComponent(n[1] || "");
|
|
665
665
|
s[i] !== void 0 ? s[i] += "," + a : s[i] = a;
|
|
666
666
|
}), t !== !0 ? s[t] || "" : s;
|
|
667
667
|
}
|
|
668
|
-
function
|
|
668
|
+
function Ot(e, t) {
|
|
669
669
|
if (t) {
|
|
670
670
|
if (t === !0)
|
|
671
671
|
return e.replace(/\?[^#]*/, "");
|
|
@@ -678,7 +678,7 @@ function Zt(e, t, s = !1) {
|
|
|
678
678
|
if (!r)
|
|
679
679
|
return e;
|
|
680
680
|
const n = e.split("#");
|
|
681
|
-
s && (n[0] =
|
|
681
|
+
s && (n[0] = Ot(
|
|
682
682
|
n[0],
|
|
683
683
|
(r.match(/([^=&#?]+)=[^&#]+/g) || []).map((a) => a.replace(/=.+$/, ""))
|
|
684
684
|
));
|
|
@@ -696,7 +696,7 @@ class Ft {
|
|
|
696
696
|
this.cache = {}, this.ttl = Math.max(t, 0);
|
|
697
697
|
}
|
|
698
698
|
getKey(t, s) {
|
|
699
|
-
const r = t.replace(/#.+/, ""), n = r.replace(/\?.+/g, ""), i = Object.assign(
|
|
699
|
+
const r = t.replace(/#.+/, ""), n = r.replace(/\?.+/g, ""), i = Object.assign(Et(r, !0), s), a = At(r), h = Object.keys(i).sort().map((c) => `${c}#${i[c]}`);
|
|
700
700
|
return `${n}_${h.join(",")}_${a.join(",")}`;
|
|
701
701
|
}
|
|
702
702
|
updateTTL(t) {
|
|
@@ -740,7 +740,7 @@ class St {
|
|
|
740
740
|
t && this.set(t);
|
|
741
741
|
}
|
|
742
742
|
set(t) {
|
|
743
|
-
if (t.baseURL && !/^\/.+/.test(t.baseURL) && !
|
|
743
|
+
if (t.baseURL && !/^\/.+/.test(t.baseURL) && !I(t.baseURL))
|
|
744
744
|
throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"), new Error("BaseURLError");
|
|
745
745
|
Object.assign(this.config, t);
|
|
746
746
|
}
|
|
@@ -749,18 +749,33 @@ class St {
|
|
|
749
749
|
}
|
|
750
750
|
/** 基于 baseURL 返回完整的 url 地址 */
|
|
751
751
|
getFullUrl(t) {
|
|
752
|
-
return
|
|
752
|
+
return xt(t, this.config.baseURL);
|
|
753
753
|
}
|
|
754
754
|
/** 提示消息 */
|
|
755
755
|
showMessage(t, s, r, n) {
|
|
756
756
|
this.config.messageHandler && s && this.config.messageHandler(t, s, r, n);
|
|
757
757
|
}
|
|
758
758
|
}
|
|
759
|
-
|
|
759
|
+
const lt = {
|
|
760
|
+
/** 响应数据校验失败 */
|
|
761
|
+
UnexpectResponse: "UnexpectResponse",
|
|
762
|
+
/** 请求被取消 */
|
|
763
|
+
Aborted: "Aborted",
|
|
764
|
+
/** 未知错误 */
|
|
765
|
+
Unknown: "Unknown",
|
|
766
|
+
/** 网络错误 */
|
|
767
|
+
NetworkError: "NetworkError",
|
|
768
|
+
/** 超时 */
|
|
769
|
+
Timeout: "Timeout",
|
|
770
|
+
/** 请求方法不被支持,在微信小程序环境下有效 */
|
|
771
|
+
NotSupport: "NotSupport",
|
|
772
|
+
/** url 格式错误 */
|
|
773
|
+
URLFormatError: "URLFormatError"
|
|
774
|
+
};
|
|
760
775
|
function Ut(e, t, s, r) {
|
|
761
776
|
if (t.ok && !kt(t.data) && r) {
|
|
762
|
-
const n =
|
|
763
|
-
return n.guard(t.data) || (t.code =
|
|
777
|
+
const n = Nt(r, s.get("defaultTypeGuardMessage"));
|
|
778
|
+
return n.guard(t.data) || (t.code = lt.UnexpectResponse, s.showMessage(!0, `${e} ${n.message}`, t.code, t.status), console.error(t.code, e, t.data), t.data = null, t.message = n.message), t;
|
|
764
779
|
}
|
|
765
780
|
return t;
|
|
766
781
|
}
|
|
@@ -781,7 +796,7 @@ class Xt {
|
|
|
781
796
|
return console.error("RequestError", r), {
|
|
782
797
|
ok: !1,
|
|
783
798
|
status: -9,
|
|
784
|
-
code:
|
|
799
|
+
code: lt.Unknown,
|
|
785
800
|
message: r instanceof Error ? `${r.message}
|
|
786
801
|
${r.stack || ""}` : String(r),
|
|
787
802
|
headers: {},
|
|
@@ -876,10 +891,10 @@ function Mt(e) {
|
|
|
876
891
|
if (e)
|
|
877
892
|
return typeof e == "string" || j.URLSearchParams && e instanceof URLSearchParams || e instanceof ArrayBuffer || j.Blob && e instanceof Blob || j.FormData && e instanceof FormData ? e : JSON.stringify(e);
|
|
878
893
|
}
|
|
879
|
-
const ut = "data",
|
|
894
|
+
const ut = "data", N = "message";
|
|
880
895
|
function Ct(e, t, s, r, n) {
|
|
881
896
|
const i = n || r;
|
|
882
|
-
return
|
|
897
|
+
return V(e) ? Bt(i.ok || r.ok, e, t, s) : Pt(i.failed || r.failed, t, s);
|
|
883
898
|
}
|
|
884
899
|
const Qt = function(e) {
|
|
885
900
|
const t = [], s = e.failed || { resolve: "json" };
|
|
@@ -888,7 +903,7 @@ const Qt = function(e) {
|
|
|
888
903
|
t.push(" 将响应内容格式化为字符串并作为错误消息");
|
|
889
904
|
break;
|
|
890
905
|
case "json":
|
|
891
|
-
t.push(" 将响应解析为json,并读取 " + (s.messageField ||
|
|
906
|
+
t.push(" 将响应解析为json,并读取 " + (s.messageField || N) + " 作为错误消息");
|
|
892
907
|
break;
|
|
893
908
|
}
|
|
894
909
|
const r = e.ok || { resolve: "body" };
|
|
@@ -898,7 +913,7 @@ const Qt = function(e) {
|
|
|
898
913
|
break;
|
|
899
914
|
case "json":
|
|
900
915
|
t.push(
|
|
901
|
-
" 将响应解析为 json,读取 " + (r.dataField || ut) + " 作为响应数据,读取 " + (r.messageField ||
|
|
916
|
+
" 将响应解析为 json,读取 " + (r.dataField || ut) + " 作为响应数据,读取 " + (r.messageField || N) + " 作为提示消息"
|
|
902
917
|
), r.statusField && t.push(
|
|
903
918
|
" 当 " + r.statusField + " 为 " + (r.statusOKValue || "空值") + " 时是成功提示,否则是错误消息"
|
|
904
919
|
), r.ignoreMessage && t.push(" 并忽略以下消息:" + r.ignoreMessage);
|
|
@@ -908,7 +923,7 @@ const Qt = function(e) {
|
|
|
908
923
|
`);
|
|
909
924
|
};
|
|
910
925
|
function Pt(e, t, s) {
|
|
911
|
-
const r = e || { resolve: "json", messageField:
|
|
926
|
+
const r = e || { resolve: "json", messageField: N }, n = {
|
|
912
927
|
ok: !1,
|
|
913
928
|
code: t,
|
|
914
929
|
message: s,
|
|
@@ -916,17 +931,17 @@ function Pt(e, t, s) {
|
|
|
916
931
|
};
|
|
917
932
|
switch (r.resolve) {
|
|
918
933
|
case "body":
|
|
919
|
-
n.message =
|
|
934
|
+
n.message = tt(s) || s;
|
|
920
935
|
break;
|
|
921
936
|
case "json":
|
|
922
937
|
const { code: i, message: a } = Lt(s, r.converter, r.statusField, r.messageField);
|
|
923
|
-
n.code = i || t, n.message =
|
|
938
|
+
n.code = i || t, n.message = tt(s) || a;
|
|
924
939
|
break;
|
|
925
940
|
}
|
|
926
941
|
return n;
|
|
927
942
|
}
|
|
928
|
-
function Lt(e, t, s, r =
|
|
929
|
-
if (!
|
|
943
|
+
function Lt(e, t, s, r = N) {
|
|
944
|
+
if (!it(e))
|
|
930
945
|
return { message: "" };
|
|
931
946
|
const n = L(M(e), t);
|
|
932
947
|
return !n || !b(n) ? { message: e } : {
|
|
@@ -938,21 +953,21 @@ function P(e, t) {
|
|
|
938
953
|
const s = Array.isArray(t) ? t : [t];
|
|
939
954
|
for (const r of s)
|
|
940
955
|
if (r in e)
|
|
941
|
-
return
|
|
956
|
+
return It(e[r]);
|
|
942
957
|
return "";
|
|
943
958
|
}
|
|
944
|
-
function
|
|
959
|
+
function It(e) {
|
|
945
960
|
return e ? typeof e == "string" ? e : JSON.stringify(e) : "";
|
|
946
961
|
}
|
|
947
|
-
const
|
|
948
|
-
function
|
|
949
|
-
const t = e.match(
|
|
962
|
+
const Dt = /<title>([^<]+)<\/title>/i, qt = /<message>([^<]+)<\/message>/i;
|
|
963
|
+
function tt(e) {
|
|
964
|
+
const t = e.match(Dt);
|
|
950
965
|
if (t)
|
|
951
966
|
return t[1];
|
|
952
|
-
const s = e.match(
|
|
967
|
+
const s = e.match(qt);
|
|
953
968
|
return s ? s[1] : "";
|
|
954
969
|
}
|
|
955
|
-
function
|
|
970
|
+
function Bt(e, t, s, r) {
|
|
956
971
|
const n = e || { resolve: "body" }, i = {
|
|
957
972
|
ok: !0,
|
|
958
973
|
code: s,
|
|
@@ -962,17 +977,17 @@ function qt(e, t, s, r) {
|
|
|
962
977
|
if (t === 204 || !r)
|
|
963
978
|
return i;
|
|
964
979
|
if (n.resolve === "body")
|
|
965
|
-
return i.data =
|
|
980
|
+
return i.data = it(r) ? L(M(r), e.converter) : r, i;
|
|
966
981
|
const a = L(M(r), e.converter);
|
|
967
982
|
if (!a || !b(a))
|
|
968
983
|
return i.ok = !1, i.code = "ResponseFormatError", i.message = "响应内容无法格式化为 Object", i;
|
|
969
|
-
const h = n.statusField, c = n.statusOKValue || "", m = n.dataField || ut, f = n.messageField ||
|
|
984
|
+
const h = n.statusField, c = n.statusOKValue || "", m = n.dataField || ut, f = n.messageField || N, d = n.ignoreMessage || "";
|
|
970
985
|
if (h && !(h in a))
|
|
971
986
|
return i.ok = !1, i.code = "ResponseFieldMissing", i.message = "响应内容找不到状态字段 " + h, i;
|
|
972
987
|
const p = h ? a[h] + "" : "";
|
|
973
988
|
return i.ok = h ? p === c : !0, i.code = p || s, i.data = m === !0 ? a : m in a ? a[m] : null, i.message = P(a, f), d && i.message && (Array.isArray(d) && d.includes(i.message) || typeof d == "string" && i.message === d) && (i.message = ""), i;
|
|
974
989
|
}
|
|
975
|
-
function
|
|
990
|
+
function V(e) {
|
|
976
991
|
return e >= 200 && e < 400;
|
|
977
992
|
}
|
|
978
993
|
function L(e, t) {
|
|
@@ -1018,7 +1033,7 @@ function te(e, t, s, r) {
|
|
|
1018
1033
|
s.get("responseRule"),
|
|
1019
1034
|
r == null ? void 0 : r.responseRule
|
|
1020
1035
|
);
|
|
1021
|
-
if (!
|
|
1036
|
+
if (!V(n)) {
|
|
1022
1037
|
const k = zt({
|
|
1023
1038
|
url: e.url,
|
|
1024
1039
|
method: e.method,
|
|
@@ -1042,7 +1057,7 @@ function te(e, t, s, r) {
|
|
|
1042
1057
|
});
|
|
1043
1058
|
}
|
|
1044
1059
|
if (n < 0)
|
|
1045
|
-
return
|
|
1060
|
+
return et(
|
|
1046
1061
|
{ ok: !1, status: n, code: e.statusText, headers: {}, message: "", data: null },
|
|
1047
1062
|
`${i} ${t} ${e.statusText}`,
|
|
1048
1063
|
i,
|
|
@@ -1053,9 +1068,9 @@ function te(e, t, s, r) {
|
|
|
1053
1068
|
const d = { ok: h, data: m, code: c, message: f, status: n, headers: a };
|
|
1054
1069
|
(y = s.get("responseHandler")) == null || y({ ...d }, i, t);
|
|
1055
1070
|
const p = h ? f : f || e.statusText;
|
|
1056
|
-
return
|
|
1071
|
+
return et(d, p, i, t, s, r);
|
|
1057
1072
|
}
|
|
1058
|
-
function
|
|
1073
|
+
function et(e, t, s, r, n, i) {
|
|
1059
1074
|
const a = n.get("message"), h = a === !1 || (i == null ? void 0 : i.message) === !1 ? !1 : (i == null ? void 0 : i.message) || a;
|
|
1060
1075
|
if (h !== !1) {
|
|
1061
1076
|
const c = typeof h == "function" ? h(e, s, r, t) : t;
|
|
@@ -1093,7 +1108,7 @@ async function Wt(e, t, s, r, n) {
|
|
|
1093
1108
|
headers: f.headers,
|
|
1094
1109
|
cost: p
|
|
1095
1110
|
});
|
|
1096
|
-
const k =
|
|
1111
|
+
const k = V(d);
|
|
1097
1112
|
if (!a || i >= a || h === "network" && d > 0 || h === "status" && k || Array.isArray(h) && !h.includes(d) || typeof h == "function" && h(f, i) !== !0)
|
|
1098
1113
|
return f;
|
|
1099
1114
|
const w = (r == null ? void 0 : r.retryInterval) ?? s.get("retryInterval") ?? 100;
|
|
@@ -1105,10 +1120,10 @@ async function Wt(e, t, s, r, n) {
|
|
|
1105
1120
|
), await Wt(e, t, s, r, i + 1);
|
|
1106
1121
|
}
|
|
1107
1122
|
export {
|
|
1108
|
-
|
|
1123
|
+
I as A,
|
|
1109
1124
|
Xt as N,
|
|
1110
1125
|
St as R,
|
|
1111
|
-
|
|
1126
|
+
lt as a,
|
|
1112
1127
|
Vt as b,
|
|
1113
1128
|
Yt as c,
|
|
1114
1129
|
Qt as d,
|
package/dist/wx.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const t=require("./retry-8pExkGke.cjs"),l=async function(e,o,u){return t.handleResponse(await t.retryRequest(p,e,o,u),e,o,u)},p=async function(e,o,u){const s=await t.convertOptions(e,o,u),a=s.method==="PATCH"?"POST":s.method,c=t.ge(s.url,s.params);return t.jt.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:t.RequestInternalError.Unknown,body:""})},success(n){d({url:c,method:a,status:n.statusCode,statusText:n.statusCode+"",headers:{...n.header},body:s.method==="HEAD"||n.statusCode===204?"":f(n.data)})}})}):{url:c,method:a,status:-1,statusText:t.RequestInternalError.NotSupport,body:""}};function f(e){return typeof e=="string"?e:e instanceof ArrayBuffer&&t.jt.TextDecoder?new TextDecoder().decode(e):JSON.stringify(e)}function i(e){return new t.NetRequestHandler(l,e)}const r=i(),R=r.setConfig,g=r.head,y=r.get,q=r.post,h=r.del,x=r.put;exports.RequestInternalError=t.RequestInternalError;exports.getResponseRulesDescription=t.getResponseRulesDescription;exports.NetRequest=i;exports.del=h;exports.get=y;exports.head=g;exports.post=q;exports.put=x;exports.setGlobalConfig=R;
|
package/dist/wx.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { h as
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import { h as p, r as f, c as l, g as h, j as i, a as d, N as x } from "./retry-_AqXIa5P.js";
|
|
2
|
+
import { d as H } from "./retry-_AqXIa5P.js";
|
|
3
|
+
const y = async function(t, n, r) {
|
|
4
|
+
return p(await f(g, t, n, r), t, n, r);
|
|
4
5
|
}, g = async function(t, n, r) {
|
|
5
6
|
const e = await l(t, n, r), a = e.method === "PATCH" ? "POST" : e.method, u = h(e.url, e.params);
|
|
6
7
|
return i.wx ? new Promise((c) => {
|
|
@@ -42,16 +43,18 @@ const x = async function(t, n, r) {
|
|
|
42
43
|
function m(t) {
|
|
43
44
|
return typeof t == "string" ? t : t instanceof ArrayBuffer && i.TextDecoder ? new TextDecoder().decode(t) : JSON.stringify(t);
|
|
44
45
|
}
|
|
45
|
-
function
|
|
46
|
-
return new y
|
|
46
|
+
function R(t) {
|
|
47
|
+
return new x(y, t);
|
|
47
48
|
}
|
|
48
|
-
const s =
|
|
49
|
+
const s = R(), T = s.setConfig, q = s.head, b = s.get, C = s.post, N = s.del, D = s.put;
|
|
49
50
|
export {
|
|
50
|
-
|
|
51
|
+
R as NetRequest,
|
|
52
|
+
d as RequestInternalError,
|
|
51
53
|
N as del,
|
|
52
54
|
b as get,
|
|
53
|
-
|
|
55
|
+
H as getResponseRulesDescription,
|
|
56
|
+
q as head,
|
|
54
57
|
C as post,
|
|
55
|
-
|
|
56
|
-
|
|
58
|
+
D as put,
|
|
59
|
+
T as setGlobalConfig
|
|
57
60
|
};
|
package/package.json
CHANGED
package/types/inc/type.d.ts
CHANGED
|
@@ -1,22 +1,22 @@
|
|
|
1
1
|
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
|
-
export declare const
|
|
4
|
+
export declare const RequestInternalError: {
|
|
5
5
|
/** 响应数据校验失败 */
|
|
6
|
-
UnexpectResponse
|
|
6
|
+
readonly UnexpectResponse: "UnexpectResponse";
|
|
7
7
|
/** 请求被取消 */
|
|
8
|
-
Aborted
|
|
8
|
+
readonly Aborted: "Aborted";
|
|
9
9
|
/** 未知错误 */
|
|
10
|
-
Unknown
|
|
10
|
+
readonly Unknown: "Unknown";
|
|
11
11
|
/** 网络错误 */
|
|
12
|
-
NetworkError
|
|
12
|
+
readonly NetworkError: "NetworkError";
|
|
13
13
|
/** 超时 */
|
|
14
|
-
Timeout
|
|
14
|
+
readonly Timeout: "Timeout";
|
|
15
15
|
/** 请求方法不被支持,在微信小程序环境下有效 */
|
|
16
|
-
NotSupport
|
|
16
|
+
readonly NotSupport: "NotSupport";
|
|
17
17
|
/** url 格式错误 */
|
|
18
|
-
URLFormatError
|
|
19
|
-
}
|
|
18
|
+
readonly URLFormatError: "URLFormatError";
|
|
19
|
+
};
|
|
20
20
|
/** 通用网络请求工具的基本配置 */
|
|
21
21
|
export interface IBaseRequestOptions {
|
|
22
22
|
/** 网络请求方法,默认 GET */
|
package/types/node.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { NetRequestHandler } from "./inc/main";
|
|
2
2
|
import type { IRequestGlobalConfig } from "./inc/type";
|
|
3
|
-
export
|
|
3
|
+
export { getResponseRulesDescription } from "./inc/rule";
|
|
4
|
+
export { RequestInternalError } from "./inc/type";
|
|
5
|
+
export type { NetRequestHandler } from "./inc/main";
|
|
6
|
+
export type { IRequestOptions, IRequestGlobalConfig, IResponseRule, IResponseResult, IRequestLog } from "./inc/type";
|
|
4
7
|
/**
|
|
5
8
|
* 创建新的实例空间,配置和缓存跟全局默认实例是隔离的
|
|
6
9
|
*/
|
package/types/wx.d.ts
CHANGED
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import { NetRequestHandler } from "./inc/main";
|
|
2
2
|
import type { IRequestGlobalConfig } from "./inc/type";
|
|
3
|
-
export
|
|
3
|
+
export { getResponseRulesDescription } from "./inc/rule";
|
|
4
|
+
export { RequestInternalError } from "./inc/type";
|
|
5
|
+
export type { NetRequestHandler } from "./inc/main";
|
|
6
|
+
export type { IRequestOptions, IRequestGlobalConfig, IResponseRule, IResponseResult, IRequestLog } from "./inc/type";
|
|
4
7
|
/**
|
|
5
8
|
* 创建新的实例空间,配置和缓存跟全局默认实例是隔离的
|
|
6
9
|
*/
|