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