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