@seayoo-web/request 2.1.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +120 -121
- package/dist/index.cjs +2 -2
- package/dist/index.js +12 -11
- package/dist/node.cjs +1 -1
- package/dist/node.js +24 -24
- package/dist/request.fetch-C7PHZPCL.cjs +1 -0
- package/dist/request.fetch-zIatpucl.js +46 -0
- package/dist/retry-DsJb9GfI.cjs +3 -0
- package/dist/retry-zhJVeNp_.js +595 -0
- package/dist/wx.cjs +1 -1
- package/dist/wx.js +8 -8
- package/package.json +3 -3
- package/types/inc/main.d.ts +2 -2
- package/types/inc/rule.d.ts +1 -1
- package/types/inc/type.d.ts +9 -9
- package/types/index.d.ts +2 -1
- package/dist/request.fetch-3tu4c4YP.js +0 -42
- package/dist/request.fetch-WuNmpi3J.cjs +0 -1
- package/dist/retry-C2OSRA9r.cjs +0 -2
- package/dist/retry-DUbMIN1-.js +0 -572
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { h as m, r as w, c as y, d as E, f as R, b } from "./retry-zhJVeNp_.js";
|
|
2
|
+
const x = async function(s, o, n) {
|
|
3
|
+
return m(await w(g, s, o, n), s, o, n);
|
|
4
|
+
}, g = async function(s, o, n) {
|
|
5
|
+
const t = await y(s, o, n), i = new URL(t.url), c = t.params;
|
|
6
|
+
c instanceof Object && Object.keys(c).forEach((e) => i.searchParams.set(e, c[e]));
|
|
7
|
+
const r = E.AbortController ? new AbortController() : null;
|
|
8
|
+
function d() {
|
|
9
|
+
r && !r.signal.aborted && r.abort();
|
|
10
|
+
}
|
|
11
|
+
t.abort && t.abort.addEventListener("abort", d);
|
|
12
|
+
const h = t.timeout > 0 ? setTimeout(d, t.timeout) : null, l = t.method === "HEAD";
|
|
13
|
+
return await fetch(i, {
|
|
14
|
+
method: t.method,
|
|
15
|
+
headers: Object.keys(t.headers).length > 0 ? new Headers(t.headers) : void 0,
|
|
16
|
+
body: t.body,
|
|
17
|
+
credentials: t.credentials,
|
|
18
|
+
signal: r == null ? void 0 : r.signal,
|
|
19
|
+
redirect: "follow"
|
|
20
|
+
}).then(async (e) => {
|
|
21
|
+
const a = {
|
|
22
|
+
url: i.toString(),
|
|
23
|
+
method: t.method,
|
|
24
|
+
status: e.status,
|
|
25
|
+
statusText: e.statusText,
|
|
26
|
+
headers: R([...e.headers.entries()])
|
|
27
|
+
}, u = l || e.status === 204 ? "" : await e.text().catch((f) => f);
|
|
28
|
+
return u instanceof Error ? { ...a, body: "", statusText: b.Unknown, rawError: u } : { ...a, body: u };
|
|
29
|
+
}).catch((e) => {
|
|
30
|
+
const a = r == null ? void 0 : r.signal.aborted;
|
|
31
|
+
return {
|
|
32
|
+
url: i.toString(),
|
|
33
|
+
method: t.method,
|
|
34
|
+
status: a ? 0 : -1,
|
|
35
|
+
// 设置为 0 可以防止自动重试的默认策略
|
|
36
|
+
statusText: a ? b.Aborted : b.NetworkError,
|
|
37
|
+
body: "",
|
|
38
|
+
rawError: e
|
|
39
|
+
};
|
|
40
|
+
}).finally(() => {
|
|
41
|
+
h !== null && clearTimeout(h), t.abort && t.abort.removeEventListener("abort", d);
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
export {
|
|
45
|
+
x as f
|
|
46
|
+
};
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
"use strict";var J=Object.defineProperty;var z=(e,t,s)=>t in e?J(e,t,{enumerable:!0,configurable:!0,writable:!0,value:s}):e[t]=s;var b=(e,t,s)=>z(e,typeof t!="symbol"?t+"":t,s);const c=typeof globalThis<"u"?globalThis:typeof self<"u"?self:typeof window<"u"?window:Function("return this")(),R={wx:"wx"in c&&j(c.wx)&&"getSystemInfo"in c.wx&&d(c.wx.getSystemInfo),fetch:"fetch"in c&&d(c.fetch),window:"window"in c&&j(c.window),requestAnimationFrame:"requestAnimationFrame"in c&&d(c.requestAnimationFrame),localStorage:"localStorage"in c&&j(c.localStorage)&&"getItem"in c.localStorage&&d(c.localStorage.getItem),sessionStorage:"sessionStorage"in c&&j(c.sessionStorage)&&"getItem"in c.sessionStorage&&d(c.sessionStorage.getItem),File:"File"in c&&d(c.File),Blob:"Blob"in c&&d(c.Blob),Proxy:"Proxy"in c&&d(c.Proxy),Request:"Request"in c&&d(c.Request),FormData:"FormData"in c&&d(c.FormData),TextDecoder:"TextDecoder"in c&&d(c.TextDecoder),ResizeObserver:"ResizeObserver"in c&&d(c.ResizeObserver),AbortController:"AbortController"in c&&d(c.AbortController)};function d(e){return typeof e=="function"}function j(e){return typeof e=="object"&&e!==null}function V(e){return new Promise(t=>setTimeout(t,Math.max(0,e)))}function W(){}function q(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(o=>{const n=console[o];s[o]=(...a)=>{n.apply(console,[r,...a])}}),s}function Z(e){const t=e||"CustomError";return class extends Error{constructor(s){super(s),Object.defineProperty(this,"name",{value:t,enumerable:!1,configurable:!0}),"setPrototypeOf"in Object&&Object.setPrototypeOf(this,new.target.prototype),"captureStackTrace"in Error&&typeof Error.captureStackTrace=="function"&&Error.captureStackTrace(this,this.constructor)}}}const X=/^(?:https?:)?\/\/.+$/i,Q=/^https?:\/\/.+$/i,Y=/^\{[\d\D]*\}$/,ee=/^\[[\d\D]*\]$/;function F(e,t=!1){return t?X.test(e):Q.test(e)}function te(e){return e==null}function v(e,...t){if(!e||typeof e!="object")return!1;const s=Object.getPrototypeOf(e);return s!==Object.prototype&&s!==null?!1:t.every(r=>r in e)}function B(e){return Y.test(e)||ee.test(e)}function O(e){return e?e[0].toLowerCase()+e.slice(1):""}const _=/_\w*/,H=/-\w*/;function x(e){const t=_.test(e)?e.replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(s,r)=>r.toUpperCase()):H.test(e)?e.replace(/(?:^-*|-*$)/g,"").replace(/-+(\w)/g,(s,r)=>r.toUpperCase()):e;return O(t)}function M(e){return _.test(e)?O(e).replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(t,s)=>"_"+s.toLowerCase()):H.test(e)?O(e.replace(/(?:^-*|-*$)/g,"")).replace(/-+(\w)/g,(t,s)=>"_"+s.toLowerCase()):O(e).replace(/[A-Z]/g,t=>`_${t.toLowerCase()}`)}function A(e,t){try{const s=JSON.parse(e);return t?t(s)?s:null:s}catch{return null}}""+Math.random().toString(32).slice(2);function E(e,t=!1){return typeof e=="string"?x(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?E(s):typeof s=="object"&&s?E(s,!0):s):v(e)?Object.keys(e).reduce((s,r)=>{const o=x(String(r)),n=o.charAt(0).toLowerCase()+o.slice(1);return s[n]=t?e[r]:E(e[r]),s},{}):e}function k(e,t=!1){return typeof e=="string"?M(e):Array.isArray(e)?e.map(s=>!t||typeof s=="string"?k(s):typeof s=="object"&&s?k(s,t):s):v(e)?Object.keys(e).reduce((s,r)=>{const o=M(String(r));return s[o]=t?e[r]:k(e[r]),s},{}):e}q("LoadUtils");async function se(e){return await new Promise(function(t){const s=document.getElementsByTagName("head")[0],r=document.createElement("script");r.setAttribute("type","text/javascript"),r.setAttribute("charset","utf-8"),r.onload=function(){s.removeChild(r),t(!0)},r.onerror=function(){s.removeChild(r),t(!1)},r.setAttribute("src",e),s.appendChild(r)})}q("Validator");function re(e,t="数据未能正确识别"){return typeof e=="function"?{guard:e,message:t}:{guard:e.guard,message:e.message||t}}function ne(e,t=""){return!t||F(e,!0)?D(e):(D(t)+"/"+e).replace(/\/{2,}/g,"/").replace(/:\//,"://")}function D(e){return F(e,!0)?e.startsWith("http")?e:("location"in globalThis?location.protocol:"https:")+e:("location"in globalThis?location.origin:"http://127.0.0.1")+"/"+e.replace(/^\/+/,"")}function oe(e,t){const s={};return(e.match(/([^=&#?]+)=[^&#]*/g)||[]).forEach(function(r){const o=r.split("="),n=o[0],a=decodeURIComponent(o[1]||"");s[n]!==void 0?s[n]+=","+a:s[n]=a}),t!==!0?s[t]||"":s}function ae(e,t){if(t){if(t===!0)return e.replace(/\?[^#]*/,"")}else return e;const s=e.split("#"),r=s[0].split("?"),o=r[0],n=r.length>1?r[1]:"",a=s.length>1?"#"+s[1]:"",i=typeof t=="string"?[t]:Array.isArray(t)?t:[];return!i.length||!n?s[0]+a:(i.map(u=>u.replace(/([\\(){}[\]^$+\-*?|])/g,"\\$1")),(o+"?"+n.replace(new RegExp("(?:^|&)(?:"+i.join("|")+")=[^&$]+","g"),"").replace(/^&/,"")).replace(/\?$/,"")+a)}function ce(e,t,s=!1){const r=typeof t=="string"?t:Object.keys(t).map(a=>`${a}=${encodeURIComponent(t[a])}`).join("&");if(!r)return e;const o=e.split("#");s&&(o[0]=ae(o[0],(r.match(/([^=&#?]+)=[^&#]+/g)||[]).map(a=>a.replace(/=.+$/,""))));const n=o[0].indexOf("?")+1?"&":"?";return(o[0]+n+r+(o.length>1?"#"+o[1]:"")).replace(/\?&/,"?")}function ie(e){const t=e.match(/(?:\?|&)([^=]+)(?:&|$)/g);return t?t.join("").replace(/(?:\?|^&+|&+$)/g,"").replace(/&{2}/g,"&").split("&").sort():[]}class ue{constructor(t=500){b(this,"ttl");b(this,"cache");this.cache={},this.ttl=Math.max(t,0)}getKey(t,s){const r=t.replace(/#.+/,""),o=r.replace(/\?.+/g,""),n=Object.assign(oe(r,!0),s),a=ie(r),i=Object.keys(n).sort().map(u=>`${u}#${n[u]}`);return`${o}_${i.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 I{constructor(t){b(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)&&!F(t.baseURL))throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"),new Error("BaseURLError");Object.assign(this.config,t)}get(t){return this.config[t]}getFullUrl(t){return ne(t,this.config.baseURL)}showMessage(t,s,r,o){this.config.messageHandler&&s&&this.config.messageHandler(t,s,r,o)}}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 le(e,t,s,r){if(t.ok&&!te(t.data)&&r){const o=re(r,s.get("defaultTypeGuardMessage"));return o.guard(t.data)||(t.code=T.UnexpectResponse,s.showMessage(!0,`${e} ${o.message}`,t.code,t.status),console.error(t.code,e,t.data),t.data=null,t.message=o.message),t}return t}class he{constructor(t,s){b(this,"agent");b(this,"config");b(this,"cache");this.config=new I(s),this.agent=t,this.cache=new ue(this.config.get("cacheTTL")),this.setConfig=this.setConfig.bind(this),this.getConfig=this.getConfig.bind(this),this.request=this.request.bind(this),this.get=this.get.bind(this),this.post=this.post.bind(this),this.del=this.del.bind(this),this.patch=this.patch.bind(this),this.put=this.put.bind(this),this.head=this.head.bind(this)}async request(t,s){try{return await this.agent(t,this.config,s)}catch(r){return console.error("RequestError",r),{ok:!1,status:-9,code:T.Unknown,message:r instanceof Error?`${r.message}
|
|
2
|
+
${r.stack||""}`:String(r),headers:{},data:null}}}async guard(t,s,r){return le(t,s,this.config,r)}setConfig(t){this.config.set(t),this.cache.updateTTL(this.config.get("cacheTTL"))}getConfig(t){return this.config.get(t)}async head(t,s){const r=Object.assign({},s||null);return r.method="HEAD",this.guard(t,await this.request(t,r),null)}async get(t,s,r){const o=Object.assign({},r||null);o.method="GET";const n=this.cache.getKey(t,o.params),a=this.cache.get(n);if(a)return this.guard(t,await a,s||null);const i=this.request(t,o);return this.cache.set(n,i),this.guard(t,await i,s||null)}async post(t,s,r,o){const n=Object.assign({},o||null);return n.method="POST",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}async del(t,s,r){const o=Object.assign({},r||null);return o.method="DELETE",this.guard(t,await this.request(t,o),s||null)}async put(t,s,r,o){const n=Object.assign({},o||null);return n.method="PUT",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}async patch(t,s,r,o){const n=Object.assign({},o||null);return n.method="PATCH",n.body=s||{},this.guard(t,await this.request(t,n),r||null)}}async function de(e,t,s){var y;const r=Object.assign({method:"GET"},s),o=R.FormData?r.body instanceof FormData:!1,n=o&&r.method!=="POST"&&r.method!=="PUT"?"POST":r.method,a=n==="GET"||n==="HEAD"||n==="DELETE";a&&r.body!==void 0&&(console.warn("request body is invalid with method get, head, delete"),delete r.body);const i=Object.assign(o||a?{}:{"Content-Type":R.Blob&&r.body instanceof Blob?r.body.type||"application/octet-stream":"application/json;charset=utf-8"},r.headers),u=r.params||{},f={};Object.keys(u).forEach(w=>{u[w]!==void 0&&(f[w]=fe(u[w]))});const l=t.getFullUrl(e),h=ge(r.body),g=r.timeout||t.get("timeout"),m=await async function(){const w=t.get("requestTransformer");if(w)return await w({headers:i,params:f,method:n,url:l,body:h})}(),p=typeof m=="string"&&m?m:l;return(y=t.get("logHandler"))==null||y({type:"ready",url:p,method:n,headers:i,timeout:g,body:h}),{url:p,method:n,body:h,params:f,headers:i,timeout:g,abort:r.abort,credentials:r.credentials||t.get("credentials")}}function fe(e){return typeof e=="string"?e:Array.isArray(e)?e.join(","):e+""}function ge(e){if(e)return typeof e=="string"||e instanceof URLSearchParams||e instanceof ArrayBuffer||R.Blob&&e instanceof Blob||R.FormData&&e instanceof FormData?e:JSON.stringify(e)}const N="data",$="message";function me(e,t,s,r,o){const n=o||r;return S(e)?je(n.ok||r.ok,e,t,s):pe(n.failed||r.failed,t,s)}const ye=function(e){const t=[],s=e.failed||{resolve:"json"};switch(t.push("- 当http状态码 <200 或者 >=400 时"),s.resolve){case"body":t.push(" 将响应内容格式化为字符串并作为错误消息");break;case"json":t.push(" 将响应解析为json,并读取 "+(s.messageField||$)+" 作为错误消息");break}const r=e.ok||{resolve:"body"};switch(t.push("- 当http状态码 >=200 并且 <400 时"),r.resolve){case"body":t.push(" 将响应尝试解析为 json,并作为数据内容返回");break;case"json":t.push(" 将响应解析为 json,读取 "+(r.dataField||N)+" 作为响应数据,读取 "+(r.messageField||$)+" 作为提示消息"),r.statusField&&t.push(" 当 "+r.statusField+" 为 "+(r.statusOKValue||"空值")+" 时是成功提示,否则是错误消息"),r.ignoreMessage&&t.push(" 并忽略以下消息:"+r.ignoreMessage);break}return t.join(`
|
|
3
|
+
`)};function pe(e,t,s){const r=e||{resolve:"json",messageField:$},o={ok:!1,code:t,message:s,data:null};switch(r.resolve){case"body":o.message=L(s)||s;break;case"json":const{code:n,message:a}=be(s,r.converter,r.statusField,r.messageField);o.code=n||t,o.message=L(s)||a;break}return o}function be(e,t,s,r=$){if(!B(e))return{message:""};const o=C(A(e),t);return!o||!v(o)?{message:e}:{code:s?U(o,s):"",message:U(o,r)||e}}function U(e,t){const s=Array.isArray(t)?t:[t];for(const r of s)if(r in e)return we(e[r]);return""}function we(e){return e?typeof e=="string"?e:JSON.stringify(e):""}const Re=/<title>([^<]+)<\/title>/i,$e=/<message>([^<]+)<\/message>/i;function L(e){const t=e.match(Re);if(t)return t[1];const s=e.match($e);return s?s[1]:""}function je(e,t,s,r){const o=e||{resolve:"body"},n={ok:!0,code:s,message:"",data:null};if(t===204||!r)return n;if(o.resolve==="body")return n.data=B(r)?C(A(r),e.converter):r,n;const a=C(A(r),e.converter);if(!a||!v(a))return n.ok=!1,n.code="ResponseFormatError",n.message="响应内容无法格式化为 Object",n;const i=o.statusField,u=o.statusOKValue||"",f=o.dataField||N,l=o.messageField||$,h=o.ignoreMessage||"";if(i&&!(i in a))return n.ok=!1,n.code="ResponseFieldMissing",n.message="响应内容找不到状态字段 "+i,n;const g=i?a[i]+"":"";return n.ok=i?g===u:!0,n.code=g||s,n.data=f===!0?a:f in a?a[f]:null,n.message=U(a,l),h&&n.message&&(Array.isArray(h)&&h.includes(n.message)||typeof h=="string"&&n.message===h)&&(n.message=""),n}function S(e){return e>=200&&e<400}function C(e,t){return t==="camelize"?E(e):t==="snakify"?k(e):e}const Oe=Z("APIError");function Ee(e){const t={};for(const o in e.headers)(o.startsWith("x-")||o.includes("trace")||o.includes("server")||/\b(?:id|uuid)\b/.test(o))&&(t[o]=e.headers[o]);const s=e.url.replace(/^(?:https?:)?\/*/i,"").replace(/\?.+/,""),r=e.status<0?"unknown":e.status;return{sentryError:new Oe(`${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 G(e){return e.reduce((t,[s,r])=>(s&&(t[s]=r||""),t),{})}function ke(e,t,s,r){var m;const o=e.status,n=e.method,a=G(Object.entries(e.headers||{}).map(([p,y])=>[p.toLowerCase(),y])),{ok:i,code:u,data:f,message:l}=me(o,e.statusText,e.body,s.get("responseRule"),r==null?void 0:r.responseRule);if(!S(o)){const p=Ee({url:e.url,method:e.method,status:o,code:u,message:l,body:e.body,headers:a,error:e.rawError});(s.get("errorHandler")||Fe)({url:t,method:n,status:o,code:u,message:l,headers:a,rawError:e.rawError,responseBody:e.body,...p})}if(o<0)return P({ok:!1,status:o,code:e.statusText,headers:{},message:"",data:null},`${n} ${t} ${e.statusText}`,n,t,s,r);const h={ok:i,data:f,code:u,message:l,status:o,headers:a};(m=s.get("responseHandler"))==null||m({...h},n,t);const g=i?l:l||e.statusText;return P(h,g,n,t,s,r)}function P(e,t,s,r,o,n){const a=o.get("message"),i=a===!1||(n==null?void 0:n.message)===!1?!1:(n==null?void 0:n.message)||a;if(i!==!1){const u=typeof i=="function"?i(e,s,r,t):t;u instanceof Error?o.showMessage(!0,u.message,e.code,e.status):u&&typeof u=="object"&&"message"in u?o.showMessage(!1,u.message,e.code,e.status):o.showMessage(!e.ok,u,e.code,e.status)}return e}function Fe(e){const t={};for(const s in e)s.startsWith("sentry")||(t[s]=e[s]);console.error("RequestError",t)}async function K(e,t,s,r,o){const n=o||0,a=Math.max(0,Math.min(10,(r==null?void 0:r.maxRetry)??s.get("maxRetry")??0)),i=(r==null?void 0:r.retryResolve)??s.get("retryResolve"),u=s.get("logHandler")||W;u({type:"prepare",url:t,method:(r==null?void 0:r.method)||"GET",retry:n,maxRetry:a,message:n===0?"start":`retry ${n}/${a} start`,headers:r==null?void 0:r.headers,options:r});const f=Date.now(),l=await e(t,s,r),h=l.status,g=Date.now()-f,m=`[cost ${g}][${h}] ${h<0?l.body:""}`;u({type:"finished",url:t,method:l.method,retry:n,maxRetry:a,message:n===0?`finish ${m}`:`retry ${n}/${a} finish ${m}`,response:l,headers:l.headers,cost:g});const p=S(h);if(!a||i==="network"&&h>0||i==="status"&&p||Array.isArray(i)&&!i.includes(h)||typeof i=="function"&&i(l,n)!==!0||n>=a)return l;const y=(r==null?void 0:r.retryInterval)??s.get("retryInterval")??100;return await V(Math.max(100,y==="2EB"?Math.pow(2,n)*100:typeof y=="function"?y(n+1)||0:y)),await K(e,t,s,r,n+1)}exports.$t=F;exports.Ge=se;exports.NetRequestHandler=he;exports.RequestGlobalConfig=I;exports.RequestInternalError=T;exports.Rt=R;exports.ce=ce;exports.convertOptions=de;exports.fromEntries=G;exports.getResponseRulesDescription=ye;exports.handleResponse=ke;exports.retryRequest=K;
|
|
@@ -0,0 +1,595 @@
|
|
|
1
|
+
var N = Object.defineProperty;
|
|
2
|
+
var G = (e, t, s) => t in e ? N(e, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : e[t] = s;
|
|
3
|
+
var b = (e, t, s) => G(e, typeof t != "symbol" ? t + "" : t, s);
|
|
4
|
+
const c = typeof globalThis < "u" ? globalThis : typeof self < "u" ? self : typeof window < "u" ? window : Function("return this")(), E = {
|
|
5
|
+
wx: "wx" in c && R(c.wx) && "getSystemInfo" in c.wx && d(c.wx.getSystemInfo),
|
|
6
|
+
fetch: "fetch" in c && d(c.fetch),
|
|
7
|
+
window: "window" in c && R(c.window),
|
|
8
|
+
requestAnimationFrame: "requestAnimationFrame" in c && d(c.requestAnimationFrame),
|
|
9
|
+
localStorage: "localStorage" in c && R(c.localStorage) && "getItem" in c.localStorage && d(c.localStorage.getItem),
|
|
10
|
+
sessionStorage: "sessionStorage" in c && R(c.sessionStorage) && "getItem" in c.sessionStorage && d(c.sessionStorage.getItem),
|
|
11
|
+
File: "File" in c && d(c.File),
|
|
12
|
+
Blob: "Blob" in c && d(c.Blob),
|
|
13
|
+
Proxy: "Proxy" in c && d(c.Proxy),
|
|
14
|
+
Request: "Request" in c && d(c.Request),
|
|
15
|
+
FormData: "FormData" in c && d(c.FormData),
|
|
16
|
+
TextDecoder: "TextDecoder" in c && d(c.TextDecoder),
|
|
17
|
+
ResizeObserver: "ResizeObserver" in c && d(c.ResizeObserver),
|
|
18
|
+
AbortController: "AbortController" in c && d(c.AbortController)
|
|
19
|
+
};
|
|
20
|
+
function d(e) {
|
|
21
|
+
return typeof e == "function";
|
|
22
|
+
}
|
|
23
|
+
function R(e) {
|
|
24
|
+
return typeof e == "object" && e !== null;
|
|
25
|
+
}
|
|
26
|
+
function K(e) {
|
|
27
|
+
return new Promise((t) => setTimeout(t, Math.max(0, e)));
|
|
28
|
+
}
|
|
29
|
+
function J() {
|
|
30
|
+
}
|
|
31
|
+
function B(e, t) {
|
|
32
|
+
const s = {
|
|
33
|
+
log: console.log.bind(console),
|
|
34
|
+
error: console.error.bind(console),
|
|
35
|
+
warn: console.warn.bind(console)
|
|
36
|
+
}, r = `[${e}]`;
|
|
37
|
+
return ["log", "error", "warn"].forEach((o) => {
|
|
38
|
+
const n = console[o];
|
|
39
|
+
s[o] = (...a) => {
|
|
40
|
+
n.apply(console, [r, ...a]);
|
|
41
|
+
};
|
|
42
|
+
}), s;
|
|
43
|
+
}
|
|
44
|
+
function z(e) {
|
|
45
|
+
const t = e || "CustomError";
|
|
46
|
+
return class extends Error {
|
|
47
|
+
constructor(s) {
|
|
48
|
+
super(s), Object.defineProperty(this, "name", {
|
|
49
|
+
value: t,
|
|
50
|
+
enumerable: !1,
|
|
51
|
+
configurable: !0
|
|
52
|
+
}), "setPrototypeOf" in Object && Object.setPrototypeOf(this, new.target.prototype), "captureStackTrace" in Error && typeof Error.captureStackTrace == "function" && Error.captureStackTrace(this, this.constructor);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
const V = /^(?:https?:)?\/\/.+$/i, W = /^https?:\/\/.+$/i, Z = /^\{[\d\D]*\}$/, X = /^\[[\d\D]*\]$/;
|
|
57
|
+
function U(e, t = !1) {
|
|
58
|
+
return t ? V.test(e) : W.test(e);
|
|
59
|
+
}
|
|
60
|
+
function Q(e) {
|
|
61
|
+
return e == null;
|
|
62
|
+
}
|
|
63
|
+
function F(e, ...t) {
|
|
64
|
+
if (!e || typeof e != "object") return !1;
|
|
65
|
+
const s = Object.getPrototypeOf(e);
|
|
66
|
+
return s !== Object.prototype && s !== null ? !1 : t.every((r) => r in e);
|
|
67
|
+
}
|
|
68
|
+
function _(e) {
|
|
69
|
+
return Z.test(e) || X.test(e);
|
|
70
|
+
}
|
|
71
|
+
function j(e) {
|
|
72
|
+
return e ? e[0].toLowerCase() + e.slice(1) : "";
|
|
73
|
+
}
|
|
74
|
+
const q = /_\w*/, H = /-\w*/;
|
|
75
|
+
function x(e) {
|
|
76
|
+
const t = q.test(e) ? e.replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (s, r) => r.toUpperCase()) : H.test(e) ? e.replace(/(?:^-*|-*$)/g, "").replace(/-+(\w)/g, (s, r) => r.toUpperCase()) : e;
|
|
77
|
+
return j(t);
|
|
78
|
+
}
|
|
79
|
+
function M(e) {
|
|
80
|
+
return q.test(e) ? j(e).replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (t, s) => "_" + s.toLowerCase()) : H.test(e) ? j(e.replace(/(?:^-*|-*$)/g, "")).replace(/-+(\w)/g, (t, s) => "_" + s.toLowerCase()) : j(e).replace(/[A-Z]/g, (t) => `_${t.toLowerCase()}`);
|
|
81
|
+
}
|
|
82
|
+
function v(e, t) {
|
|
83
|
+
try {
|
|
84
|
+
const s = JSON.parse(e);
|
|
85
|
+
return t ? t(s) ? s : null : s;
|
|
86
|
+
} catch {
|
|
87
|
+
return null;
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
"" + Math.random().toString(32).slice(2);
|
|
91
|
+
function O(e, t = !1) {
|
|
92
|
+
return typeof e == "string" ? x(e) : Array.isArray(e) ? e.map((s) => !t || typeof s == "string" ? O(s) : typeof s == "object" && s ? O(s, !0) : s) : F(e) ? Object.keys(e).reduce(
|
|
93
|
+
(s, r) => {
|
|
94
|
+
const o = x(String(r)), n = o.charAt(0).toLowerCase() + o.slice(1);
|
|
95
|
+
return s[n] = t ? e[r] : O(e[r]), s;
|
|
96
|
+
},
|
|
97
|
+
{}
|
|
98
|
+
) : e;
|
|
99
|
+
}
|
|
100
|
+
function k(e, t = !1) {
|
|
101
|
+
return typeof e == "string" ? M(e) : Array.isArray(e) ? e.map((s) => !t || typeof s == "string" ? k(s) : typeof s == "object" && s ? k(s, t) : s) : F(e) ? Object.keys(e).reduce(
|
|
102
|
+
(s, r) => {
|
|
103
|
+
const o = M(String(r));
|
|
104
|
+
return s[o] = t ? e[r] : k(e[r]), s;
|
|
105
|
+
},
|
|
106
|
+
{}
|
|
107
|
+
) : e;
|
|
108
|
+
}
|
|
109
|
+
B("LoadUtils");
|
|
110
|
+
async function je(e) {
|
|
111
|
+
return await new Promise(function(t) {
|
|
112
|
+
const s = document.getElementsByTagName("head")[0], r = document.createElement("script");
|
|
113
|
+
r.setAttribute("type", "text/javascript"), r.setAttribute("charset", "utf-8"), r.onload = function() {
|
|
114
|
+
s.removeChild(r), t(!0);
|
|
115
|
+
}, r.onerror = function() {
|
|
116
|
+
s.removeChild(r), t(!1);
|
|
117
|
+
}, r.setAttribute("src", e), s.appendChild(r);
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
B("Validator");
|
|
121
|
+
function Y(e, t = "数据未能正确识别") {
|
|
122
|
+
return typeof e == "function" ? {
|
|
123
|
+
guard: e,
|
|
124
|
+
message: t
|
|
125
|
+
} : {
|
|
126
|
+
guard: e.guard,
|
|
127
|
+
message: e.message || t
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function ee(e, t = "") {
|
|
131
|
+
return !t || U(e, !0) ? L(e) : (L(t) + "/" + e).replace(/\/{2,}/g, "/").replace(/:\//, "://");
|
|
132
|
+
}
|
|
133
|
+
function L(e) {
|
|
134
|
+
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(/^\/+/, "");
|
|
135
|
+
}
|
|
136
|
+
function te(e, t) {
|
|
137
|
+
const s = {};
|
|
138
|
+
return (e.match(/([^=&#?]+)=[^&#]*/g) || []).forEach(function(r) {
|
|
139
|
+
const o = r.split("="), n = o[0], a = decodeURIComponent(o[1] || "");
|
|
140
|
+
s[n] !== void 0 ? s[n] += "," + a : s[n] = a;
|
|
141
|
+
}), t !== !0 ? s[t] || "" : s;
|
|
142
|
+
}
|
|
143
|
+
function se(e, t) {
|
|
144
|
+
if (t) {
|
|
145
|
+
if (t === !0)
|
|
146
|
+
return e.replace(/\?[^#]*/, "");
|
|
147
|
+
} else return e;
|
|
148
|
+
const s = e.split("#"), r = s[0].split("?"), o = r[0], n = r.length > 1 ? r[1] : "", a = s.length > 1 ? "#" + s[1] : "", i = typeof t == "string" ? [t] : Array.isArray(t) ? t : [];
|
|
149
|
+
return !i.length || !n ? s[0] + a : (i.map((u) => u.replace(/([\\(){}[\]^$+\-*?|])/g, "\\$1")), (o + "?" + n.replace(new RegExp("(?:^|&)(?:" + i.join("|") + ")=[^&$]+", "g"), "").replace(/^&/, "")).replace(/\?$/, "") + a);
|
|
150
|
+
}
|
|
151
|
+
function Oe(e, t, s = !1) {
|
|
152
|
+
const r = typeof t == "string" ? t : Object.keys(t).map((a) => `${a}=${encodeURIComponent(t[a])}`).join("&");
|
|
153
|
+
if (!r)
|
|
154
|
+
return e;
|
|
155
|
+
const o = e.split("#");
|
|
156
|
+
s && (o[0] = se(
|
|
157
|
+
o[0],
|
|
158
|
+
(r.match(/([^=&#?]+)=[^&#]+/g) || []).map((a) => a.replace(/=.+$/, ""))
|
|
159
|
+
));
|
|
160
|
+
const n = o[0].indexOf("?") + 1 ? "&" : "?";
|
|
161
|
+
return (o[0] + n + r + (o.length > 1 ? "#" + o[1] : "")).replace(/\?&/, "?");
|
|
162
|
+
}
|
|
163
|
+
function re(e) {
|
|
164
|
+
const t = e.match(/(?:\?|&)([^=]+)(?:&|$)/g);
|
|
165
|
+
return t ? t.join("").replace(/(?:\?|^&+|&+$)/g, "").replace(/&{2}/g, "&").split("&").sort() : [];
|
|
166
|
+
}
|
|
167
|
+
class ne {
|
|
168
|
+
constructor(t = 500) {
|
|
169
|
+
b(this, "ttl");
|
|
170
|
+
b(this, "cache");
|
|
171
|
+
this.cache = {}, this.ttl = Math.max(t, 0);
|
|
172
|
+
}
|
|
173
|
+
getKey(t, s) {
|
|
174
|
+
const r = t.replace(/#.+/, ""), o = r.replace(/\?.+/g, ""), n = Object.assign(te(r, !0), s), a = re(r), i = Object.keys(n).sort().map((u) => `${u}#${n[u]}`);
|
|
175
|
+
return `${o}_${i.join(",")}_${a.join(",")}`;
|
|
176
|
+
}
|
|
177
|
+
updateTTL(t) {
|
|
178
|
+
this.ttl = Math.max(t, 0);
|
|
179
|
+
}
|
|
180
|
+
get(t) {
|
|
181
|
+
if (this.ttl === 0)
|
|
182
|
+
return null;
|
|
183
|
+
const s = this.cache[t];
|
|
184
|
+
return s ? s.ttl < Date.now() ? (delete this.cache[t], null) : s.res : null;
|
|
185
|
+
}
|
|
186
|
+
set(t, s) {
|
|
187
|
+
this.ttl !== 0 && (this.cache[t] = {
|
|
188
|
+
ttl: Date.now() + this.ttl,
|
|
189
|
+
res: s
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
class oe {
|
|
194
|
+
constructor(t) {
|
|
195
|
+
// 保存的配置需要部分字段强制设置默认值
|
|
196
|
+
b(this, "config", {
|
|
197
|
+
baseURL: "/",
|
|
198
|
+
maxRetry: 0,
|
|
199
|
+
retryInterval: 100,
|
|
200
|
+
retryResolve: "network",
|
|
201
|
+
timeout: 1e4,
|
|
202
|
+
cacheTTL: 500,
|
|
203
|
+
credentials: "same-origin",
|
|
204
|
+
defaultTypeGuardMessage: "响应数据未能正确识别",
|
|
205
|
+
responseRule: {
|
|
206
|
+
ok: {
|
|
207
|
+
resolve: "body"
|
|
208
|
+
},
|
|
209
|
+
failed: {
|
|
210
|
+
resolve: "json",
|
|
211
|
+
messageField: "message"
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
t && this.set(t);
|
|
216
|
+
}
|
|
217
|
+
set(t) {
|
|
218
|
+
if (t.baseURL && !/^\/.+/.test(t.baseURL) && !U(t.baseURL))
|
|
219
|
+
throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"), new Error("BaseURLError");
|
|
220
|
+
Object.assign(this.config, t);
|
|
221
|
+
}
|
|
222
|
+
get(t) {
|
|
223
|
+
return this.config[t];
|
|
224
|
+
}
|
|
225
|
+
/** 基于 baseURL 返回完整的 url 地址 */
|
|
226
|
+
getFullUrl(t) {
|
|
227
|
+
return ee(t, this.config.baseURL);
|
|
228
|
+
}
|
|
229
|
+
/** 提示消息 */
|
|
230
|
+
showMessage(t, s, r, o) {
|
|
231
|
+
this.config.messageHandler && s && this.config.messageHandler(t, s, r, o);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
var S = /* @__PURE__ */ ((e) => (e.UnexpectResponse = "UnexpectResponse", e.Aborted = "Aborted", e.Unknown = "Unknown", e.NetworkError = "NetworkError", e.Timeout = "Timeout", e.NotSupport = "NotSupport", e.URLFormatError = "URLFormatError", e))(S || {});
|
|
235
|
+
function ae(e, t, s, r) {
|
|
236
|
+
if (t.ok && !Q(t.data) && r) {
|
|
237
|
+
const o = Y(r, s.get("defaultTypeGuardMessage"));
|
|
238
|
+
return o.guard(t.data) || (t.code = S.UnexpectResponse, s.showMessage(!0, `${e} ${o.message}`, t.code, t.status), console.error(t.code, e, t.data), t.data = null, t.message = o.message), t;
|
|
239
|
+
}
|
|
240
|
+
return t;
|
|
241
|
+
}
|
|
242
|
+
class ke {
|
|
243
|
+
constructor(t, s) {
|
|
244
|
+
b(this, "agent");
|
|
245
|
+
b(this, "config");
|
|
246
|
+
b(this, "cache");
|
|
247
|
+
this.config = new oe(s), this.agent = t, this.cache = new ne(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);
|
|
248
|
+
}
|
|
249
|
+
/**
|
|
250
|
+
* 执行网络请求
|
|
251
|
+
*/
|
|
252
|
+
async request(t, s) {
|
|
253
|
+
try {
|
|
254
|
+
return await this.agent(t, this.config, s);
|
|
255
|
+
} catch (r) {
|
|
256
|
+
return console.error("RequestError", r), {
|
|
257
|
+
ok: !1,
|
|
258
|
+
status: -9,
|
|
259
|
+
code: S.Unknown,
|
|
260
|
+
message: r instanceof Error ? `${r.message}
|
|
261
|
+
${r.stack || ""}` : String(r),
|
|
262
|
+
headers: {},
|
|
263
|
+
data: null
|
|
264
|
+
};
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* 检查响应的数据类型
|
|
269
|
+
*/
|
|
270
|
+
async guard(t, s, r) {
|
|
271
|
+
return ae(t, s, this.config, r);
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* 修改默认请求配置: baseURL / timeout / credentials / errorHandler / messageHandler / responseHandler / logHandler / responseRule
|
|
275
|
+
*/
|
|
276
|
+
setConfig(t) {
|
|
277
|
+
this.config.set(t), this.cache.updateTTL(this.config.get("cacheTTL"));
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* 读取默认的请求配置
|
|
281
|
+
*/
|
|
282
|
+
getConfig(t) {
|
|
283
|
+
return this.config.get(t);
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* 发送一个 HEAD 请求,并且不处理响应 body
|
|
287
|
+
*/
|
|
288
|
+
async head(t, s) {
|
|
289
|
+
const r = Object.assign({}, s || null);
|
|
290
|
+
return r.method = "HEAD", this.guard(t, await this.request(t, r), null);
|
|
291
|
+
}
|
|
292
|
+
async get(t, s, r) {
|
|
293
|
+
const o = Object.assign({}, r || null);
|
|
294
|
+
o.method = "GET";
|
|
295
|
+
const n = this.cache.getKey(t, o.params), a = this.cache.get(n);
|
|
296
|
+
if (a)
|
|
297
|
+
return this.guard(t, await a, s || null);
|
|
298
|
+
const i = this.request(t, o);
|
|
299
|
+
return this.cache.set(n, i), this.guard(t, await i, s || null);
|
|
300
|
+
}
|
|
301
|
+
async post(t, s, r, o) {
|
|
302
|
+
const n = Object.assign({}, o || null);
|
|
303
|
+
return n.method = "POST", n.body = s || {}, this.guard(t, await this.request(t, n), r || null);
|
|
304
|
+
}
|
|
305
|
+
async del(t, s, r) {
|
|
306
|
+
const o = Object.assign({}, r || null);
|
|
307
|
+
return o.method = "DELETE", this.guard(t, await this.request(t, o), s || null);
|
|
308
|
+
}
|
|
309
|
+
async put(t, s, r, o) {
|
|
310
|
+
const n = Object.assign({}, o || null);
|
|
311
|
+
return n.method = "PUT", n.body = s || {}, this.guard(t, await this.request(t, n), r || null);
|
|
312
|
+
}
|
|
313
|
+
async patch(t, s, r, o) {
|
|
314
|
+
const n = Object.assign({}, o || null);
|
|
315
|
+
return n.method = "PATCH", n.body = s || {}, this.guard(t, await this.request(t, n), r || null);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
async function Ee(e, t, s) {
|
|
319
|
+
var y;
|
|
320
|
+
const r = Object.assign({ method: "GET" }, s), o = E.FormData ? r.body instanceof FormData : !1, n = o && r.method !== "POST" && r.method !== "PUT" ? "POST" : r.method, a = n === "GET" || n === "HEAD" || n === "DELETE";
|
|
321
|
+
a && r.body !== void 0 && (console.warn("request body is invalid with method get, head, delete"), delete r.body);
|
|
322
|
+
const i = Object.assign(
|
|
323
|
+
o || a ? {} : {
|
|
324
|
+
"Content-Type": E.Blob && r.body instanceof Blob ? r.body.type || "application/octet-stream" : "application/json;charset=utf-8"
|
|
325
|
+
},
|
|
326
|
+
r.headers
|
|
327
|
+
), u = r.params || {}, f = {};
|
|
328
|
+
Object.keys(u).forEach((w) => {
|
|
329
|
+
u[w] !== void 0 && (f[w] = ce(u[w]));
|
|
330
|
+
});
|
|
331
|
+
const l = t.getFullUrl(e), h = ie(r.body), g = r.timeout || t.get("timeout"), m = await async function() {
|
|
332
|
+
const w = t.get("requestTransformer");
|
|
333
|
+
if (w)
|
|
334
|
+
return await w({ headers: i, params: f, method: n, url: l, body: h });
|
|
335
|
+
}(), p = typeof m == "string" && m ? m : l;
|
|
336
|
+
return (y = t.get("logHandler")) == null || y({ type: "ready", url: p, method: n, headers: i, timeout: g, body: h }), {
|
|
337
|
+
url: p,
|
|
338
|
+
method: n,
|
|
339
|
+
body: h,
|
|
340
|
+
params: f,
|
|
341
|
+
headers: i,
|
|
342
|
+
timeout: g,
|
|
343
|
+
abort: r.abort,
|
|
344
|
+
credentials: r.credentials || t.get("credentials")
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
function ce(e) {
|
|
348
|
+
return typeof e == "string" ? e : Array.isArray(e) ? e.join(",") : e + "";
|
|
349
|
+
}
|
|
350
|
+
function ie(e) {
|
|
351
|
+
if (e)
|
|
352
|
+
return typeof e == "string" || e instanceof URLSearchParams || e instanceof ArrayBuffer || E.Blob && e instanceof Blob || E.FormData && e instanceof FormData ? e : JSON.stringify(e);
|
|
353
|
+
}
|
|
354
|
+
const I = "data", $ = "message";
|
|
355
|
+
function ue(e, t, s, r, o) {
|
|
356
|
+
const n = o || r;
|
|
357
|
+
return C(e) ? me(n.ok || r.ok, e, t, s) : le(n.failed || r.failed, t, s);
|
|
358
|
+
}
|
|
359
|
+
const Fe = function(e) {
|
|
360
|
+
const t = [], s = e.failed || { resolve: "json" };
|
|
361
|
+
switch (t.push("- 当http状态码 <200 或者 >=400 时"), s.resolve) {
|
|
362
|
+
case "body":
|
|
363
|
+
t.push(" 将响应内容格式化为字符串并作为错误消息");
|
|
364
|
+
break;
|
|
365
|
+
case "json":
|
|
366
|
+
t.push(" 将响应解析为json,并读取 " + (s.messageField || $) + " 作为错误消息");
|
|
367
|
+
break;
|
|
368
|
+
}
|
|
369
|
+
const r = e.ok || { resolve: "body" };
|
|
370
|
+
switch (t.push("- 当http状态码 >=200 并且 <400 时"), r.resolve) {
|
|
371
|
+
case "body":
|
|
372
|
+
t.push(" 将响应尝试解析为 json,并作为数据内容返回");
|
|
373
|
+
break;
|
|
374
|
+
case "json":
|
|
375
|
+
t.push(
|
|
376
|
+
" 将响应解析为 json,读取 " + (r.dataField || I) + " 作为响应数据,读取 " + (r.messageField || $) + " 作为提示消息"
|
|
377
|
+
), r.statusField && t.push(
|
|
378
|
+
" 当 " + r.statusField + " 为 " + (r.statusOKValue || "空值") + " 时是成功提示,否则是错误消息"
|
|
379
|
+
), r.ignoreMessage && t.push(" 并忽略以下消息:" + r.ignoreMessage);
|
|
380
|
+
break;
|
|
381
|
+
}
|
|
382
|
+
return t.join(`
|
|
383
|
+
`);
|
|
384
|
+
};
|
|
385
|
+
function le(e, t, s) {
|
|
386
|
+
const r = e || { resolve: "json", messageField: $ }, o = {
|
|
387
|
+
ok: !1,
|
|
388
|
+
code: t,
|
|
389
|
+
message: s,
|
|
390
|
+
data: null
|
|
391
|
+
};
|
|
392
|
+
switch (r.resolve) {
|
|
393
|
+
case "body":
|
|
394
|
+
o.message = D(s) || s;
|
|
395
|
+
break;
|
|
396
|
+
case "json":
|
|
397
|
+
const { code: n, message: a } = he(s, r.converter, r.statusField, r.messageField);
|
|
398
|
+
o.code = n || t, o.message = D(s) || a;
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
return o;
|
|
402
|
+
}
|
|
403
|
+
function he(e, t, s, r = $) {
|
|
404
|
+
if (!_(e))
|
|
405
|
+
return { message: "" };
|
|
406
|
+
const o = A(v(e), t);
|
|
407
|
+
return !o || !F(o) ? { message: e } : {
|
|
408
|
+
code: s ? T(o, s) : "",
|
|
409
|
+
message: T(o, r) || e
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
function T(e, t) {
|
|
413
|
+
const s = Array.isArray(t) ? t : [t];
|
|
414
|
+
for (const r of s)
|
|
415
|
+
if (r in e)
|
|
416
|
+
return de(e[r]);
|
|
417
|
+
return "";
|
|
418
|
+
}
|
|
419
|
+
function de(e) {
|
|
420
|
+
return e ? typeof e == "string" ? e : JSON.stringify(e) : "";
|
|
421
|
+
}
|
|
422
|
+
const fe = /<title>([^<]+)<\/title>/i, ge = /<message>([^<]+)<\/message>/i;
|
|
423
|
+
function D(e) {
|
|
424
|
+
const t = e.match(fe);
|
|
425
|
+
if (t)
|
|
426
|
+
return t[1];
|
|
427
|
+
const s = e.match(ge);
|
|
428
|
+
return s ? s[1] : "";
|
|
429
|
+
}
|
|
430
|
+
function me(e, t, s, r) {
|
|
431
|
+
const o = e || { resolve: "body" }, n = {
|
|
432
|
+
ok: !0,
|
|
433
|
+
code: s,
|
|
434
|
+
message: "",
|
|
435
|
+
data: null
|
|
436
|
+
};
|
|
437
|
+
if (t === 204 || !r)
|
|
438
|
+
return n;
|
|
439
|
+
if (o.resolve === "body")
|
|
440
|
+
return n.data = _(r) ? A(v(r), e.converter) : r, n;
|
|
441
|
+
const a = A(v(r), e.converter);
|
|
442
|
+
if (!a || !F(a))
|
|
443
|
+
return n.ok = !1, n.code = "ResponseFormatError", n.message = "响应内容无法格式化为 Object", n;
|
|
444
|
+
const i = o.statusField, u = o.statusOKValue || "", f = o.dataField || I, l = o.messageField || $, h = o.ignoreMessage || "";
|
|
445
|
+
if (i && !(i in a))
|
|
446
|
+
return n.ok = !1, n.code = "ResponseFieldMissing", n.message = "响应内容找不到状态字段 " + i, n;
|
|
447
|
+
const g = i ? a[i] + "" : "";
|
|
448
|
+
return n.ok = i ? g === u : !0, n.code = g || s, n.data = f === !0 ? a : f in a ? a[f] : null, n.message = T(a, l), h && n.message && (Array.isArray(h) && h.includes(n.message) || typeof h == "string" && n.message === h) && (n.message = ""), n;
|
|
449
|
+
}
|
|
450
|
+
function C(e) {
|
|
451
|
+
return e >= 200 && e < 400;
|
|
452
|
+
}
|
|
453
|
+
function A(e, t) {
|
|
454
|
+
return t === "camelize" ? O(e) : t === "snakify" ? k(e) : e;
|
|
455
|
+
}
|
|
456
|
+
const ye = z("APIError");
|
|
457
|
+
function pe(e) {
|
|
458
|
+
const t = {};
|
|
459
|
+
for (const o in e.headers)
|
|
460
|
+
(o.startsWith("x-") || o.includes("trace") || o.includes("server") || /\b(?:id|uuid)\b/.test(o)) && (t[o] = e.headers[o]);
|
|
461
|
+
const s = e.url.replace(/^(?:https?:)?\/*/i, "").replace(/\?.+/, ""), r = e.status < 0 ? "unknown" : e.status;
|
|
462
|
+
return {
|
|
463
|
+
sentryError: new ye(`${s} | ${r}${e.code ? ` | ${e.code}` : ""}`),
|
|
464
|
+
sentryTags: {
|
|
465
|
+
...t,
|
|
466
|
+
status: r,
|
|
467
|
+
method: e.method,
|
|
468
|
+
code: e.code || "unknown",
|
|
469
|
+
message: e.message || "empty"
|
|
470
|
+
},
|
|
471
|
+
sentryExtra: {
|
|
472
|
+
url: e.url,
|
|
473
|
+
responseBody: e.body || "empty",
|
|
474
|
+
responseHeaders: e.headers,
|
|
475
|
+
rawError: e.error
|
|
476
|
+
}
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
function be(e) {
|
|
480
|
+
return e.reduce(
|
|
481
|
+
(t, [s, r]) => (s && (t[s] = r || ""), t),
|
|
482
|
+
{}
|
|
483
|
+
);
|
|
484
|
+
}
|
|
485
|
+
function ve(e, t, s, r) {
|
|
486
|
+
var m;
|
|
487
|
+
const o = e.status, n = e.method, a = be(
|
|
488
|
+
Object.entries(e.headers || {}).map(([p, y]) => [p.toLowerCase(), y])
|
|
489
|
+
), { ok: i, code: u, data: f, message: l } = ue(
|
|
490
|
+
o,
|
|
491
|
+
e.statusText,
|
|
492
|
+
e.body,
|
|
493
|
+
s.get("responseRule"),
|
|
494
|
+
r == null ? void 0 : r.responseRule
|
|
495
|
+
);
|
|
496
|
+
if (!C(o)) {
|
|
497
|
+
const p = pe({
|
|
498
|
+
url: e.url,
|
|
499
|
+
method: e.method,
|
|
500
|
+
status: o,
|
|
501
|
+
code: u,
|
|
502
|
+
message: l,
|
|
503
|
+
body: e.body,
|
|
504
|
+
headers: a,
|
|
505
|
+
error: e.rawError
|
|
506
|
+
});
|
|
507
|
+
(s.get("errorHandler") || we)({
|
|
508
|
+
url: t,
|
|
509
|
+
method: n,
|
|
510
|
+
status: o,
|
|
511
|
+
code: u,
|
|
512
|
+
message: l,
|
|
513
|
+
headers: a,
|
|
514
|
+
rawError: e.rawError,
|
|
515
|
+
responseBody: e.body,
|
|
516
|
+
...p
|
|
517
|
+
});
|
|
518
|
+
}
|
|
519
|
+
if (o < 0)
|
|
520
|
+
return P(
|
|
521
|
+
{ ok: !1, status: o, code: e.statusText, headers: {}, message: "", data: null },
|
|
522
|
+
`${n} ${t} ${e.statusText}`,
|
|
523
|
+
n,
|
|
524
|
+
t,
|
|
525
|
+
s,
|
|
526
|
+
r
|
|
527
|
+
);
|
|
528
|
+
const h = { ok: i, data: f, code: u, message: l, status: o, headers: a };
|
|
529
|
+
(m = s.get("responseHandler")) == null || m({ ...h }, n, t);
|
|
530
|
+
const g = i ? l : l || e.statusText;
|
|
531
|
+
return P(h, g, n, t, s, r);
|
|
532
|
+
}
|
|
533
|
+
function P(e, t, s, r, o, n) {
|
|
534
|
+
const a = o.get("message"), i = a === !1 || (n == null ? void 0 : n.message) === !1 ? !1 : (n == null ? void 0 : n.message) || a;
|
|
535
|
+
if (i !== !1) {
|
|
536
|
+
const u = typeof i == "function" ? i(e, s, r, t) : t;
|
|
537
|
+
u instanceof Error ? o.showMessage(!0, u.message, e.code, e.status) : u && typeof u == "object" && "message" in u ? o.showMessage(!1, u.message, e.code, e.status) : o.showMessage(!e.ok, u, e.code, e.status);
|
|
538
|
+
}
|
|
539
|
+
return e;
|
|
540
|
+
}
|
|
541
|
+
function we(e) {
|
|
542
|
+
const t = {};
|
|
543
|
+
for (const s in e)
|
|
544
|
+
s.startsWith("sentry") || (t[s] = e[s]);
|
|
545
|
+
console.error("RequestError", t);
|
|
546
|
+
}
|
|
547
|
+
async function $e(e, t, s, r, o) {
|
|
548
|
+
const n = o || 0, a = Math.max(0, Math.min(10, (r == null ? void 0 : r.maxRetry) ?? s.get("maxRetry") ?? 0)), i = (r == null ? void 0 : r.retryResolve) ?? s.get("retryResolve"), u = s.get("logHandler") || J;
|
|
549
|
+
u({
|
|
550
|
+
type: "prepare",
|
|
551
|
+
url: t,
|
|
552
|
+
method: (r == null ? void 0 : r.method) || "GET",
|
|
553
|
+
retry: n,
|
|
554
|
+
maxRetry: a,
|
|
555
|
+
message: n === 0 ? "start" : `retry ${n}/${a} start`,
|
|
556
|
+
headers: r == null ? void 0 : r.headers,
|
|
557
|
+
options: r
|
|
558
|
+
});
|
|
559
|
+
const f = Date.now(), l = await e(t, s, r), h = l.status, g = Date.now() - f, m = `[cost ${g}][${h}] ${h < 0 ? l.body : ""}`;
|
|
560
|
+
u({
|
|
561
|
+
type: "finished",
|
|
562
|
+
url: t,
|
|
563
|
+
method: l.method,
|
|
564
|
+
retry: n,
|
|
565
|
+
maxRetry: a,
|
|
566
|
+
message: n === 0 ? `finish ${m}` : `retry ${n}/${a} finish ${m}`,
|
|
567
|
+
response: l,
|
|
568
|
+
headers: l.headers,
|
|
569
|
+
cost: g
|
|
570
|
+
});
|
|
571
|
+
const p = C(h);
|
|
572
|
+
if (!a || i === "network" && h > 0 || i === "status" && p || Array.isArray(i) && !i.includes(h) || typeof i == "function" && i(l, n) !== !0 || n >= a)
|
|
573
|
+
return l;
|
|
574
|
+
const y = (r == null ? void 0 : r.retryInterval) ?? s.get("retryInterval") ?? 100;
|
|
575
|
+
return await K(
|
|
576
|
+
Math.max(
|
|
577
|
+
100,
|
|
578
|
+
y === "2EB" ? Math.pow(2, n) * 100 : typeof y == "function" ? y(n + 1) || 0 : y
|
|
579
|
+
)
|
|
580
|
+
), await $e(e, t, s, r, n + 1);
|
|
581
|
+
}
|
|
582
|
+
export {
|
|
583
|
+
U as $,
|
|
584
|
+
je as G,
|
|
585
|
+
ke as N,
|
|
586
|
+
oe as R,
|
|
587
|
+
Oe as a,
|
|
588
|
+
S as b,
|
|
589
|
+
Ee as c,
|
|
590
|
+
E as d,
|
|
591
|
+
be as f,
|
|
592
|
+
Fe as g,
|
|
593
|
+
ve as h,
|
|
594
|
+
$e as r
|
|
595
|
+
};
|