@seayoo-web/request 1.7.1 → 1.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -261,9 +261,9 @@ status 表示网络错误或者http 状态码错误时重试;
261
261
 
262
262
  ### errorHandler
263
263
 
264
- 类型:null | ((status: number, method: string, url: string) => void)
264
+ 类型:null | ((status: number, method: string, url: string, rawError?: Error | ProgressEvent) => void)
265
265
 
266
- 说明:全局错误处理函数,仅仅在 http status 错误时触发,通常用于检查 401 未登录状态进行跳转登录
266
+ 说明:全局错误处理函数,仅仅在 http status 错误时触发,通常用于检查 401 未登录状态进行跳转登录,其中 rawError 为请求工具的原始错误,可用以进行 Sentry 信息上报;
267
267
 
268
268
  ### responseHandler
269
269
 
@@ -50,6 +50,8 @@ export interface IOtherRequestOptions {
50
50
  * 如果返回 Error 则强制以 error 方式提示 Error.message
51
51
  *
52
52
  * 如果返回 { message } 则强制以 success 方式提示 message
53
+ *
54
+ * 💡类型守卫校验不通过则会强制提示,即类型守卫校验提示不受此配置影响
53
55
  */
54
56
  message?: false | ((result: IResponseResult, method: string, url: string, defaultMessage: string) => string | false | Error | {
55
57
  message: string;
@@ -59,6 +61,8 @@ export interface IOtherRequestOptions {
59
61
  }
60
62
  /** 对外工具接口的请求配置 */
61
63
  export type IRequestOptions = IBaseRequestOptions & IRetryRequestOptions & IOtherRequestOptions;
64
+ /** 请求的原始错误信息 */
65
+ type ERequestRawError = Error | ProgressEvent;
62
66
  /** 全局默认配置 */
63
67
  export type IRequestGlobalConfig = {
64
68
  /**
@@ -85,7 +89,7 @@ export type IRequestGlobalConfig = {
85
89
  body?: IBaseRequestBody;
86
90
  }) => MaybePromise<void | string>);
87
91
  /** 全局错误处理函数,仅仅在 statusCode 错误时触发 */
88
- errorHandler?: null | ((status: number, method: string, url: string) => void);
92
+ errorHandler?: null | ((status: number, method: string, url: string, rawError?: ERequestRawError) => void);
89
93
  /** 全局响应处理函数,任意状态都会触发 */
90
94
  responseHandler?: null | ((result: IResponseResult, method: string, url: string) => void);
91
95
  /** 全局消息提示函数 */
@@ -156,6 +160,8 @@ export interface IRequestBaseResponse {
156
160
  headers?: Record<string, string | undefined>;
157
161
  /** 响应体,如果网络错误或 204/202,则返回空或错误信息 */
158
162
  body: string;
163
+ /** 原始的请求错误信息,仅当请求失败时有值,用以进行错误上报用 */
164
+ rawError?: ERequestRawError;
159
165
  }
160
166
  /** 对外工具接口的返回内容 */
161
167
  export interface IResponseResult<T = unknown> {
package/dist/index.cjs CHANGED
@@ -1,2 +1,2 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./version-Cx7ZvtPo.cjs"),y=require("./request.fetch-CaHYPyjf.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,d=(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:d,body:o});return s.handleResponse(f,t,l,e)}const a=new FormData,u={...o};r instanceof Object&&Object.entries(r).forEach(([l,f])=>{f instanceof Blob?u[l]=f:Array.isArray(f)?f.forEach((c,g)=>{a.append(`${l}[${g}]`,String(c))}):a.append(l,String(f))});for(const l in u)a.append(l,u[l]);const b=new s.RequestGlobalConfig(n),p=await w(t,b,{...e,method:d,body:a});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,d=e==null?void 0:e.onUploadProgress,a=s.Ut(n.url,n.params);return await new Promise(u=>{let b=null,p=!1;const l=function(){p||(c.abort(),p=!0)};function f(){b!==null&&clearTimeout(b),n.abort&&n.abort.removeEventListener("abort",l)}const c=new XMLHttpRequest;if(c.open(r,a,!0),d){let g=1;c.upload.addEventListener("progress",h=>{g=h.total,d({total:h.total,loaded:h.loaded})}),c.addEventListener("load",()=>{f(),d({loaded:g,total:g}),u({url:a,method:r,status:c.status,statusText:c.statusText,headers:H(c),body:r==="HEAD"?"":c.responseText})})}c.addEventListener("error",()=>{f(),u({url:a,method:r,status:-1,statusText:"Failed",body:""})}),c.addEventListener("abort",()=>{f(),u({url:a,method:r,status:-1,statusText:"Aborted",body:""})}),Object.entries(n.headers).forEach(([g,h])=>{c.setRequestHeader(g,h)}),n.credentials==="include"&&(c.withCredentials=!0),c.send(n.body||void 0),n.abort&&n.abort.addEventListener("abort",l),n.timeout>0&&(b=setTimeout(l,n.timeout))})};function H(t){const o={};if(!t)return o;const e=t.getAllResponseHeaders();return e&&e!=="null"&&e.replace(/\r/g,"").split(`
2
- `).forEach(n=>{const r=n.trim();if(!r)return;const d=r.split(":"),a=d[0].trim();a&&(o[a]=(d[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 d=s.Ut(t,e,!0);return new Promise(a=>{n[r]=function(u){if(r in window&&delete n[r],o(u))return u;console.warn("response type check faild",t,u),a(null)},s.Ae(d).catch(function(){a(null),delete n[r]})})}async function C(t,o,e={}){const n=window;return"var"in e||(e.var="jsonxData"+Math.random().toString(16).slice(2)),t?await s.Ae(s.Ut(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 E=async function(t,o,e){return await q(t,o,e,{baseURL:i.getConfig("baseURL"),logHandler:i.getConfig("logHandler"),errorHandler:i.getConfig("errorHandler"),requestTransformer:i.getConfig("requestTransformer"),messageHandler:i.getConfig("messageHandler"),responseHandler:i.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 i=R(),S=i.setConfig,x=i.head,U=i.get,j=i.post,v=i.del,L=i.put,D=i.patch;exports.getResponseRulesDescription=s.getResponseRulesDescription;exports.version=s.version;exports.NetRequest=R;exports.del=v;exports.get=U;exports.head=x;exports.jsonp=T;exports.jsonx=C;exports.patch=D;exports.post=j;exports.put=L;exports.setGlobalConfig=S;exports.upload=E;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./version-B52JIKu-.cjs"),y=require("./request.fetch-BSnIDk1j.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,d=(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:d,body:o});return s.handleResponse(f,t,l,e)}const a=new FormData,u={...o};r instanceof Object&&Object.entries(r).forEach(([l,f])=>{f instanceof Blob?u[l]=f:Array.isArray(f)?f.forEach((c,g)=>{a.append(`${l}[${g}]`,String(c))}):a.append(l,String(f))});for(const l in u)a.append(l,u[l]);const b=new s.RequestGlobalConfig(n),p=await w(t,b,{...e,method:d,body:a});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,d=e==null?void 0:e.onUploadProgress,a=s.Ut(n.url,n.params);return await new Promise(u=>{let b=null,p=!1;const l=function(){p||(c.abort(),p=!0)};function f(){b!==null&&clearTimeout(b),n.abort&&n.abort.removeEventListener("abort",l)}const c=new XMLHttpRequest;if(c.open(r,a,!0),d){let g=1;c.upload.addEventListener("progress",h=>{g=h.total,d({total:h.total,loaded:h.loaded})}),c.addEventListener("load",()=>{f(),d({loaded:g,total:g}),u({url:a,method:r,status:c.status,statusText:c.statusText,headers:H(c),body:r==="HEAD"?"":c.responseText})})}c.addEventListener("error",g=>{f(),u({url:a,method:r,status:-1,statusText:"Failed",body:"",rawError:g})},!0),c.addEventListener("abort",()=>{f(),u({url:a,method:r,status:-1,statusText:"Aborted",body:""})}),Object.entries(n.headers).forEach(([g,h])=>{c.setRequestHeader(g,h)}),n.credentials==="include"&&(c.withCredentials=!0),c.send(n.body||void 0),n.abort&&n.abort.addEventListener("abort",l),n.timeout>0&&(b=setTimeout(l,n.timeout))})};function H(t){const o={};if(!t)return o;const e=t.getAllResponseHeaders();return e&&e!=="null"&&e.replace(/\r/g,"").split(`
2
+ `).forEach(n=>{const r=n.trim();if(!r)return;const d=r.split(":"),a=d[0].trim();a&&(o[a]=(d[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 d=s.Ut(t,e,!0);return new Promise(a=>{n[r]=function(u){if(r in window&&delete n[r],o(u))return u;console.warn("response type check faild",t,u),a(null)},s.Ae(d).catch(function(){a(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.Ae(s.Ut(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 C=async function(t,o,e){return await q(t,o,e,{baseURL:i.getConfig("baseURL"),logHandler:i.getConfig("logHandler"),errorHandler:i.getConfig("errorHandler"),requestTransformer:i.getConfig("requestTransformer"),messageHandler:i.getConfig("messageHandler"),responseHandler:i.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 i=R(),S=i.setConfig,x=i.head,U=i.get,j=i.post,v=i.del,L=i.put,D=i.patch;exports.getResponseRulesDescription=s.getResponseRulesDescription;exports.version=s.version;exports.NetRequest=R;exports.del=v;exports.get=U;exports.head=x;exports.jsonp=T;exports.jsonx=E;exports.patch=D;exports.post=j;exports.put=L;exports.setGlobalConfig=S;exports.upload=C;
package/dist/index.js CHANGED
@@ -1,8 +1,8 @@
1
- import { h as w, r as q, R as y, c as x, U as p, A as T, S as R, N as H } from "./version-C-bTPkwR.js";
2
- import { g as z, v as I } from "./version-C-bTPkwR.js";
3
- import { f as E } from "./request.fetch-DnbU5fqv.js";
1
+ import { h as w, r as E, R as y, c as q, U as p, A as T, S as R, N as H } from "./version-DwET8YXx.js";
2
+ import { g as z, v as I } from "./version-DwET8YXx.js";
3
+ import { f as x } from "./request.fetch-jF38fWhN.js";
4
4
  const C = async function(t, a, e) {
5
- return w(await q(m, t, a, e), t, a, e);
5
+ return w(await E(m, t, a, e), t, a, e);
6
6
  };
7
7
  async function S(t, a, e, n) {
8
8
  const r = e == null ? void 0 : e.body, i = (e == null ? void 0 : e.method) === "PUT" ? "PUT" : "POST";
@@ -30,7 +30,7 @@ async function S(t, a, e, n) {
30
30
  return w(h, t, b, e);
31
31
  }
32
32
  const m = async function(t, a, e) {
33
- const n = await x(t, a, e), r = n.method, i = e == null ? void 0 : e.onUploadProgress, o = p(n.url, n.params);
33
+ const n = await q(t, a, e), r = n.method, i = e == null ? void 0 : e.onUploadProgress, o = p(n.url, n.params);
34
34
  return await new Promise((d) => {
35
35
  let b = null, h = !1;
36
36
  const c = function() {
@@ -55,15 +55,20 @@ const m = async function(t, a, e) {
55
55
  });
56
56
  });
57
57
  }
58
- s.addEventListener("error", () => {
59
- u(), d({
60
- url: o,
61
- method: r,
62
- status: -1,
63
- statusText: "Failed",
64
- body: ""
65
- });
66
- }), s.addEventListener("abort", () => {
58
+ s.addEventListener(
59
+ "error",
60
+ (f) => {
61
+ u(), d({
62
+ url: o,
63
+ method: r,
64
+ status: -1,
65
+ statusText: "Failed",
66
+ body: "",
67
+ rawError: f
68
+ });
69
+ },
70
+ !0
71
+ ), s.addEventListener("abort", () => {
67
72
  u(), d({
68
73
  url: o,
69
74
  method: r,
@@ -127,7 +132,7 @@ const D = async function(t, a, e) {
127
132
  function L(t) {
128
133
  if (!R.window)
129
134
  throw new Error("Default Module Only Support In Browser");
130
- return R.fetch ? new H(E, t) : new H(C, t);
135
+ return R.fetch ? new H(x, t) : new H(C, t);
131
136
  }
132
137
  const l = L(), O = l.setConfig, P = l.head, M = l.get, N = l.post, B = l.del, F = l.put, X = l.patch;
133
138
  export {
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"),e=require("./version-Cx7ZvtPo.cjs"),g=require("./request.fetch-CaHYPyjf.cjs"),w=async function(s,u,c){return e.handleResponse(await e.retryRequest(S,s,u,c),s,u,c)},S=async function(s,u,c){const t=await e.convertOptions(s,u,c);if(!e.D(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(o){const p=[];o.on("data",h=>p.push(h)),o.on("end",()=>{const h=e.fromEntries(Object.entries(o.headers).map(([b,m])=>[b.toLowerCase(),Array.isArray(m)?m.join(","):m]));r({url:a.toString(),method:t.method,status:o.statusCode||-1,statusText:o.statusMessage||"Unknown",headers:h,body:q?"":Buffer.concat(p).toString("utf-8")})})});d.on("error",o=>{r({url:a.toString(),method:t.method,status:-1,statusText:o.name||"Unknown",body:o.message})}),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 e.Support.fetch?new e.NetRequestHandler(g.fetchRequest,s):new e.NetRequestHandler(w,s)}const n=l(),T=n.setConfig,j=n.head,C=n.get,N=n.post,O=n.del,U=n.put,x=n.patch;exports.version=e.version;exports.NetRequest=l;exports.del=O;exports.get=C;exports.head=j;exports.patch=x;exports.post=N;exports.put=U;exports.setGlobalConfig=T;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("node:http"),R=require("node:https"),o=require("./version-B52JIKu-.cjs"),g=require("./request.fetch-BSnIDk1j.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.D(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,11 +1,11 @@
1
1
  import y from "node:http";
2
2
  import R from "node:https";
3
- import { h as q, r as w, c as g, D as x, f as S, S as T, N as p } from "./version-C-bTPkwR.js";
4
- import { v as z } from "./version-C-bTPkwR.js";
5
- import { f as U } from "./request.fetch-DnbU5fqv.js";
6
- const j = async function(o, a, u) {
7
- return q(await w(C, o, a, u), o, a, u);
8
- }, C = async function(o, a, u) {
3
+ import { h as w, r as q, c as g, D as x, f as S, S as E, N as p } from "./version-DwET8YXx.js";
4
+ import { v as z } from "./version-DwET8YXx.js";
5
+ import { f as T } from "./request.fetch-jF38fWhN.js";
6
+ const U = async function(o, a, u) {
7
+ return w(await q(j, o, a, u), o, a, u);
8
+ }, j = async function(o, a, u) {
9
9
  const t = await g(o, a, u);
10
10
  if (!x(t.url))
11
11
  return {
@@ -16,12 +16,12 @@ const j = async function(o, a, u) {
16
16
  headers: {},
17
17
  body: ""
18
18
  };
19
- const f = /^https:\/\//i.test(t.url) ? R : y, r = new URL(t.url), d = t.params;
20
- d instanceof Object && Object.keys(d).forEach((n) => r.searchParams.set(n, d[n]));
19
+ const f = /^https:\/\//i.test(t.url) ? R : y, n = new URL(t.url), d = t.params;
20
+ d instanceof Object && Object.keys(d).forEach((r) => n.searchParams.set(r, d[r]));
21
21
  const b = t.method === "HEAD";
22
- return new Promise((n) => {
22
+ return new Promise((r) => {
23
23
  const c = f.request(
24
- r,
24
+ n,
25
25
  {
26
26
  headers: t.headers,
27
27
  method: t.method,
@@ -33,8 +33,8 @@ const j = async function(o, a, u) {
33
33
  const i = S(
34
34
  Object.entries(e.headers).map(([l, m]) => [l.toLowerCase(), Array.isArray(m) ? m.join(",") : m])
35
35
  );
36
- n({
37
- url: r.toString(),
36
+ r({
37
+ url: n.toString(),
38
38
  method: t.method,
39
39
  status: e.statusCode || -1,
40
40
  statusText: e.statusMessage || "Unknown",
@@ -45,16 +45,17 @@ const j = async function(o, a, u) {
45
45
  }
46
46
  );
47
47
  c.on("error", (e) => {
48
- n({
49
- url: r.toString(),
48
+ r({
49
+ url: n.toString(),
50
50
  method: t.method,
51
51
  status: -1,
52
52
  statusText: e.name || "Unknown",
53
- body: e.message
53
+ body: e.message,
54
+ rawError: e
54
55
  });
55
56
  }), c.on("timeout", () => {
56
- n({
57
- url: r.toString(),
57
+ r({
58
+ url: n.toString(),
58
59
  method: t.method,
59
60
  status: -1,
60
61
  statusText: "Timeout",
@@ -63,12 +64,12 @@ const j = async function(o, a, u) {
63
64
  }), t.body && c.write(t.body), c.end();
64
65
  });
65
66
  };
66
- function E(o) {
67
- return T.fetch ? new p(U, o) : new p(j, o);
67
+ function C(o) {
68
+ return E.fetch ? new p(T, o) : new p(U, o);
68
69
  }
69
- const s = E(), H = s.setConfig, k = s.head, D = s.get, P = s.post, B = s.del, F = s.put, G = s.patch;
70
+ const s = C(), H = s.setConfig, k = s.head, D = s.get, P = s.post, B = s.del, F = s.put, G = s.patch;
70
71
  export {
71
- E as NetRequest,
72
+ C as NetRequest,
72
73
  B as del,
73
74
  D as get,
74
75
  k as head,
@@ -0,0 +1 @@
1
+ "use strict";const s=require("./version-B52JIKu-.cjs"),b=async function(r,o,n){return s.handleResponse(await s.retryRequest(m,r,o,n),r,o,n)},m=async function(r,o,n){const t=await s.convertOptions(r,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=s.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,h=new Request(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"});return await fetch(h).then(async a=>({url:i.toString(),method:t.method,status:a.status,statusText:a.statusText,headers:s.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=b;
@@ -1,15 +1,15 @@
1
- import { h, r as b, c as l, S as m, f } from "./version-C-bTPkwR.js";
2
- const p = async function(r, s, o) {
3
- return h(await b(w, r, s, o), r, s, o);
4
- }, w = async function(r, s, o) {
5
- const t = await l(r, s, o), n = new URL(t.url), i = t.params;
1
+ import { h, r as b, c as m, S as l, f } from "./version-DwET8YXx.js";
2
+ const g = async function(s, r, o) {
3
+ return h(await b(w, s, r, o), s, r, o);
4
+ }, w = async function(s, r, o) {
5
+ const t = await m(s, r, o), n = new URL(t.url), i = t.params;
6
6
  i instanceof Object && Object.keys(i).forEach((a) => n.searchParams.set(a, i[a]));
7
- const e = m.AbortController ? new AbortController() : null;
8
- function c() {
7
+ const e = l.AbortController ? new AbortController() : null;
8
+ function d() {
9
9
  e && !e.signal.aborted && e.abort();
10
10
  }
11
- t.abort && t.abort.addEventListener("abort", c);
12
- const d = t.timeout > 0 ? setTimeout(c, t.timeout) : null, u = new Request(n, {
11
+ t.abort && t.abort.addEventListener("abort", d);
12
+ const u = t.timeout > 0 ? setTimeout(d, t.timeout) : null, c = new Request(n, {
13
13
  method: t.method,
14
14
  headers: Object.keys(t.headers).length > 0 ? new Headers(t.headers) : void 0,
15
15
  body: t.body,
@@ -17,7 +17,7 @@ const p = async function(r, s, o) {
17
17
  signal: e == null ? void 0 : e.signal,
18
18
  redirect: "follow"
19
19
  });
20
- return await fetch(u).then(async (a) => ({
20
+ return await fetch(c).then(async (a) => ({
21
21
  url: n.toString(),
22
22
  method: t.method,
23
23
  status: a.status,
@@ -29,11 +29,12 @@ const p = async function(r, s, o) {
29
29
  method: t.method,
30
30
  status: -1,
31
31
  statusText: e != null && e.signal.aborted ? "Aborted" : "NetworkError",
32
- body: String(a)
32
+ body: String(a),
33
+ rawError: e != null && e.signal.aborted ? void 0 : a
33
34
  })).finally(() => {
34
- d !== null && clearTimeout(d), t.abort && t.abort.removeEventListener("abort", c);
35
+ u !== null && clearTimeout(u), t.abort && t.abort.removeEventListener("abort", d);
35
36
  });
36
37
  };
37
38
  export {
38
- p as f
39
+ g as f
39
40
  };
@@ -0,0 +1,2 @@
1
+ "use strict";var G=Object.defineProperty;var J=(t,e,s)=>e in t?G(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var f=(t,e,s)=>(J(t,typeof e!="symbol"?e+"":e,s),s);const m=typeof globalThis<"u"?globalThis:typeof global<"u"?global:typeof window<"u"?window:{},b={fetch:"fetch"in m,window:"window"in m,FormData:"FormData"in m,Blob:"Blob"in m,wx:"wx"in m,TextDecoder:"TextDecoder"in m,AbortController:"AbortController"in m};function P(t){return t.reduce((e,[s,n])=>(s&&(e[s]=n||""),e),{})}function V(t){return new Promise(e=>setTimeout(e,Math.max(0,t)))}function W(){}function j(t){return t?t[0].toLowerCase()+t.slice(1):""}const S=/_\w*/,H=/-\w*/;function U(t){const e=S.test(t)?t.replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(s,n)=>n.toUpperCase()):H.test(t)?t.replace(/(?:^-*|-*$)/g,"").replace(/-+(\w)/g,(s,n)=>n.toUpperCase()):t;return j(e)}function D(t){return S.test(t)?j(t).replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(e,s)=>"_"+s.toLowerCase()):H.test(t)?j(t.replace(/(?:^-*|-*$)/g,"")).replace(/-+(\w)/g,(e,s)=>"_"+s.toLowerCase()):j(t).replace(/[A-Z]/g,e=>`_${e.toLowerCase()}`)}const Z=/^(?:https?:)?\/\/.+$/i,z=/^https?:\/\/.+$/i,Q=/^\{[\d\D]*\}$/,X=/^\[[\d\D]*\]$/;function v(t,e=!1){return e?Z.test(t):z.test(t)}function Y(t){return t==null}function C(t,...e){if(!t||typeof t!="object")return!1;const s=Object.getPrototypeOf(t);return s!==Object.prototype&&s!==null?!1:e.every(n=>n in t)}function q(t){return Q.test(t)||X.test(t)}function F(t,e){try{const s=JSON.parse(t);return e?e(s)?s:null:s}catch{return null}}""+Math.random().toString(32).slice(2);function $(t,e=!1){return typeof t=="string"?U(t):Array.isArray(t)?t.map(s=>!e||typeof s=="string"?$(s):typeof s=="object"&&s?$(s,!0):s):C(t)?Object.keys(t).reduce((s,n)=>{const a=U(String(n)),r=a.charAt(0).toLowerCase()+a.slice(1);return s[r]=e?t[n]:$(t[n]),s},{}):t}function O(t,e=!1){return typeof t=="string"?D(t):Array.isArray(t)?t.map(s=>!e||typeof s=="string"?O(s):typeof s=="object"&&s?O(s,e):s):C(t)?Object.keys(t).reduce((s,n)=>{const a=D(String(n));return s[a]=e?t[n]:O(t[n]),s},{}):t}async function ee(t){return await new Promise(function(e){const s=document.getElementsByTagName("head")[0],n=document.createElement("script");n.setAttribute("type","text/javascript"),n.setAttribute("charset","utf-8"),n.onload=function(){s.removeChild(n),e(!0)},n.onerror=function(){s.removeChild(n),e(!1)},n.setAttribute("src",t),s.appendChild(n)})}function te(t,e="数据未能正确识别"){return typeof t=="function"?{guard:t,message:e}:{guard:t.guard,message:t.message||e}}function M(t,e=""){return!e||v(t,!0)?L(t):(L(e)+"/"+t).replace(/\/{2,}/g,"/").replace(/:\//,"://")}function L(t){return v(t,!0)?t.startsWith("http")?t:("location"in globalThis?location.protocol:"https:")+t:("location"in globalThis?location.origin:"http://127.0.0.1")+"/"+t.replace(/^\/+/,"")}function se(t,e){const s={};return(t.match(/([^=&#?]+)=[^&#]*/g)||[]).forEach(function(n){const a=n.split("="),r=a[0],o=decodeURIComponent(a[1]||"");s[r]!==void 0?s[r]+=","+o:s[r]=o}),e!==!0?s[e]||"":s}function ne(t,e){if(e){if(e===!0)return t.replace(/\?[^#]*/,"")}else return t;const s=t.split("#"),n=s[0].split("?"),a=n[0],r=n.length>1?n[1]:"",o=s.length>1?"#"+s[1]:"",c=typeof e=="string"?[e]:Array.isArray(e)?e:[];return!c.length||!r?s[0]+o:(c.map(i=>i.replace(/([\\(){}[\]^$+\-*?|])/g,"\\$1")),(a+"?"+r.replace(new RegExp("(?:^|&)(?:"+c.join("|")+")=[^&$]+","g"),"").replace(/^&/,"")).replace(/\?$/,"")+o)}function re(t,e,s=!1){const n=typeof e=="string"?e:Object.keys(e).map(o=>`${o}=${encodeURIComponent(e[o])}`).join("&");if(!n)return t;const a=t.split("#");s&&(a[0]=ne(a[0],(n.match(/([^=&#?]+)=[^&#]+/g)||[]).map(o=>o.replace(/=.+$/,""))));const r=a[0].indexOf("?")+1?"&":"?";return(a[0]+r+n+(a.length>1?"#"+a[1]:"")).replace(/\?&/,"?")}function ae(t){const e=t.match(/(?:\?|&)([^=]+)(?:&|$)/g);return e?e.join("").replace(/(?:\?|^&+|&+$)/g,"").replace(/&{2}/g,"&").split("&").sort():[]}class oe{constructor(e=500){f(this,"ttl");f(this,"cache");this.cache={},this.ttl=Math.max(e,0)}getKey(e,s){const n=e.replace(/#.+/,""),a=n.replace(/\?.+/g,""),r=Object.assign(se(n,!0),s),o=ae(n),c=Object.keys(r).sort().map(i=>`${i}#${r[i]}`);return`${a}_${c.join(",")}_${o.join(",")}`}updateTTL(e){this.ttl=Math.max(e,0)}get(e){if(this.ttl===0)return null;const s=this.cache[e];return s?s.ttl<Date.now()?(delete this.cache[e],null):s.res:null}set(e,s){this.ttl!==0&&(this.cache[e]={ttl:Date.now()+this.ttl,res:s})}}const x="UnexpectResponse";function ce(t,e,s,n){if(e.ok&&!Y(e.data)&&n){const a=te(n,"响应数据未能正确识别");return a.guard(e.data)||(s.showMessage(!0,`${t} ${a.message}`),console.error(x,t,e.data),e.data=null,e.code=x,e.message=a.message),e}return e}class I{constructor(e){f(this,"config",{baseURL:"",maxRetry:0,retryInterval:1e3,retryResolve:"network",timeout:5e3,cacheTTL:500,credentials:"same-origin",responseRule:{ok:{resolve:"body"},failed:{resolve:"json",messageField:"message"}}});e&&this.set(e)}set(e){if(e.baseURL&&!/^\/.+/.test(e.baseURL)&&!v(e.baseURL))throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"),new Error("BaseURLError");Object.assign(this.config,e)}get(e){return this.config[e]}getFullUrl(e){return e.startsWith("/")?M(e):M(e,this.config.baseURL)}showMessage(e,s){this.config.messageHandler&&s&&this.config.messageHandler(e,s)}}class ie{constructor(e,s){f(this,"agent");f(this,"config");f(this,"cache");this.config=new I(s),this.agent=e,this.cache=new oe(this.config.get("cacheTTL")),this.setConfig=this.setConfig.bind(this),this.getConfig=this.getConfig.bind(this),this.get=this.get.bind(this),this.post=this.post.bind(this),this.del=this.del.bind(this),this.patch=this.patch.bind(this),this.put=this.put.bind(this),this.head=this.head.bind(this)}async exec(e,s){try{return await this.agent(e,this.config,s)}catch(n){return console.error("RequestError",n),{ok:!1,status:-9,code:"Unkonwn",message:String(n),headers:{},data:null}}}async guard(e,s,n){return ce(e,s,this.config,n)}setConfig(e){this.config.set(e),this.cache.updateTTL(this.config.get("cacheTTL"))}getConfig(e){return this.config.get(e)}async head(e,s){const n=Object.assign({},s||null);return n.method="HEAD",this.guard(e,await this.exec(e,n),null)}async get(e,s,n){const a=Object.assign({},n||null);a.method="GET";const r=this.cache.getKey(e,a.params),o=this.cache.get(r);if(o)return this.guard(e,await o,s||null);const c=this.exec(e,a);return this.cache.set(r,c),this.guard(e,await c,s||null)}async post(e,s,n,a){const r=Object.assign({},a||null);return r.method="POST",r.body=s,this.guard(e,await this.exec(e,r),n||null)}async del(e,s,n){const a=Object.assign({},n||null);return a.method="DELETE",this.guard(e,await this.exec(e,a),s||null)}async put(e,s,n,a){const r=Object.assign({},a||null);return r.method="PUT",r.body=s,this.guard(e,await this.exec(e,r),n||null)}async patch(e,s,n,a){const r=Object.assign({},a||null);return r.method="PATCH",r.body=s,this.guard(e,await this.exec(e,r),n||null)}}const K="data",w="message";function ue(t,e,s,n,a){const r=a||n;return E(t)?pe(r.ok||n.ok,t,e,s):he(r.failed||n.failed,e,s)}const le=function(t){const e=[],s=t.failed||{resolve:"json"};switch(e.push("- 当http状态码 <200 或者 >=400 时"),s.resolve){case"body":e.push(" 将响应内容格式化为字符串并作为错误消息");break;case"json":e.push(" 将响应解析为json,并读取 "+(s.messageField||w)+" 作为错误消息");break}const n=t.ok||{resolve:"body"};switch(e.push("- 当http状态码 >=200 并且 <400 时"),n.resolve){case"body":e.push(" 将响应尝试解析为 json,并作为数据内容返回");break;case"json":e.push(" 将响应解析为 json,读取 "+(n.dataField||K)+" 作为响应数据,读取 "+(n.messageField||w)+" 作为提示消息"),n.statusField&&e.push(" 当 "+n.statusField+" 为 "+(n.statusOKValue||"空值")+" 时是成功提示,否则是错误消息"),n.ignoreMessage&&e.push(" 并忽略以下消息:"+n.ignoreMessage);break}return e.join(`
2
+ `)};function he(t,e,s){const n=t||{resolve:"json",messageField:w},a={ok:!1,code:e,message:s,data:null};switch(n.resolve){case"body":a.message=_(s)||s;break;case"json":const{code:r,message:o}=de(s,n.converter,n.statusField,n.messageField);a.code=r||e,a.message=_(s)||o;break}return a}function de(t,e,s,n=w){if(!q(t))return{message:""};const a=k(F(t),e);return!a||!C(a)?{message:t}:{code:s?A(a,s):"",message:A(a,n)||t}}function A(t,e){const s=Array.isArray(e)?e:[e];for(const n of s)if(n in t)return ge(t[n]);return""}function ge(t){return t?typeof t=="string"?t:JSON.stringify(t):""}const fe=/<title>([^<]+)<\/title>/i,me=/<message>([^<]+)<\/message>/i;function _(t){const e=t.match(fe);if(e)return e[1];const s=t.match(me);return s?s[1]:""}function pe(t,e,s,n){const a=t||{resolve:"body"},r={ok:!0,code:s,message:"",data:null};if(e===204||!n)return r;if(a.resolve==="body")return r.data=q(n)?k(F(n),t.converter):n,r;const o=k(F(n),t.converter);if(!o||!C(o))return r.ok=!1,r.code="ResponseFormatError",r.message="响应内容无法格式化为 Object",r;const c=a.statusField,i=a.statusOKValue||"",l=a.dataField||K,h=a.messageField||w,u=a.ignoreMessage||"";if(c&&!(c in o))return r.ok=!1,r.code="ResponseFieldMissing",r.message="响应内容找不到状态字段 "+c,r;const d=c?o[c]+"":"";return r.ok=c?d===i:!0,r.code=d||s,r.data=l===!0?o:l in o?o[l]:null,r.message=A(o,h),u&&r.message&&(Array.isArray(u)&&u.includes(r.message)||typeof u=="string"&&r.message===u)&&(r.message=""),r}function E(t){return t>=200&&t<400}function k(t,e){return e==="camelize"?$(t):e==="snakify"?O(t):t}async function N(t,e,s,n,a){const r=a||0,o=Math.max(0,Math.min(10,(n==null?void 0:n.maxRetry)??s.get("maxRetry")??0)),c=(n==null?void 0:n.retryResolve)??s.get("retryResolve"),i=s.get("logHandler")||W;i({type:"prepear",url:e,method:(n==null?void 0:n.method)||"GET",retry:r,maxRetry:o,message:r===0?"start":`retry ${r}/${o} start`,headers:n==null?void 0:n.headers,options:n});const l=Date.now(),h=await t(e,s,n),u=h.status,d=Date.now()-l,p=`[cost ${d}][${u}] ${u<0?h.body:""}`;i({type:"finished",url:e,method:h.method,retry:r,maxRetry:o,message:r===0?`finish ${p}`:`retry ${r}/${o} finish ${p}`,response:h,headers:h.headers,cost:d});const R=E(u);if(!o||c==="network"&&u>0||c==="status"&&R||Array.isArray(c)&&!c.includes(u)||typeof c=="function"&&c(h,r)!==!0||r>=o)return h;const g=(n==null?void 0:n.retryInterval)??s.get("retryInterval")??1e3;return await V(Math.max(100,g==="2EB"?Math.pow(2,r)*1e3:typeof g=="function"?g(r+1)||0:g)),await N(t,e,s,n,r+1)}async function ye(t,e,s){var g;const n=Object.assign({method:"GET"},s),a=b.FormData?n.body instanceof FormData:!1,r=a&&n.method!=="POST"&&n.method!=="PUT"?"POST":n.method,o=r==="GET"||r==="HEAD"||r==="DELETE";o&&n.body!==void 0&&(console.warn("request body is invalid with method get, head, delete"),delete n.body);const c=Object.assign(a||o?{}:{"Content-Type":b.Blob&&n.body instanceof Blob?n.body.type||"application/octet-stream":"application/json;charset=utf-8"},n.headers),i=n.params||{},l={};Object.keys(i).forEach(y=>{i[y]!==void 0&&(l[y]=be(i[y]))});const h=e.getFullUrl(t),u=we(n.body),d=n.timeout||e.get("timeout"),p=await async function(){var T;const y=e.get("requestTransformer");return y?await y({headers:c,params:l,method:r,url:h,body:u}):await((T=e.get("requestHandler"))==null?void 0:T(c,l,r,t))}(),R=typeof p=="string"&&p?p:h;return(g=e.get("logHandler"))==null||g({type:"ready",url:R,method:r,headers:c,timeout:d,body:u}),{url:R,method:r,body:u,params:l,headers:c,timeout:d,abort:n.abort,credentials:n.credentials||e.get("credentials")}}function be(t){return typeof t=="string"?t:Array.isArray(t)?t.join(","):t+""}function we(t){if(t)return typeof t=="string"||t instanceof URLSearchParams||t instanceof ArrayBuffer||b.Blob&&t instanceof Blob||b.FormData&&t instanceof FormData?t:JSON.stringify(t)}function Re(t,e,s,n){var o,c;if(t.status<0)return B({ok:!1,status:t.status,code:t.statusText,headers:{},message:"",data:null},`${t.method} ${e} ${t.statusText}`,t.method,e,s,n);E(t.status)||(o=s.get("errorHandler"))==null||o(t.status,t.method,e,t.rawError);const a={...ue(t.status,t.statusText,t.body,s.get("responseRule"),n==null?void 0:n.responseRule),status:t.status,headers:P(Object.entries(t.headers||{}).map(([i,l])=>[i.toLowerCase(),l]))};(c=s.get("responseHandler"))==null||c({...a},t.method,e);const r=a.ok?a.message:`${t.method} ${e} [${a.code||t.statusText}] ${a.message||t.statusText}`;return B(a,r,t.method,e,s,n)}function B(t,e,s,n,a,r){const o=a.get("message"),c=o===!1||(r==null?void 0:r.message)===!1?!1:(r==null?void 0:r.message)||o;if(c!==!1){const i=typeof c=="function"?c(t,s,n,e):e;i instanceof Error?a.showMessage(!0,i.message):i&&typeof i=="object"&&"message"in i?a.showMessage(!1,i.message):a.showMessage(!t.ok,i)}return t}const je="1.7.3";exports.Ae=ee;exports.D=v;exports.NetRequestHandler=ie;exports.RequestGlobalConfig=I;exports.Support=b;exports.Ut=re;exports.convertOptions=ye;exports.fromEntries=P;exports.getResponseRulesDescription=le;exports.handleResponse=Re;exports.retryRequest=N;exports.version=je;
@@ -1,6 +1,6 @@
1
- var q = Object.defineProperty;
2
- var I = (t, e, s) => e in t ? q(t, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : t[e] = s;
3
- var f = (t, e, s) => (I(t, typeof e != "symbol" ? e + "" : e, s), s);
1
+ var I = Object.defineProperty;
2
+ var K = (t, e, s) => e in t ? I(t, e, { enumerable: !0, configurable: !0, writable: !0, value: s }) : t[e] = s;
3
+ var f = (t, e, s) => (K(t, typeof e != "symbol" ? e + "" : e, s), s);
4
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,33 +10,33 @@ 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 K(t) {
13
+ function N(t) {
14
14
  return t.reduce(
15
15
  (e, [s, n]) => (s && (e[s] = n || ""), e),
16
16
  {}
17
17
  );
18
18
  }
19
- function N(t) {
19
+ function J(t) {
20
20
  return new Promise((e) => setTimeout(e, Math.max(0, t)));
21
21
  }
22
- function J() {
22
+ function G() {
23
23
  }
24
24
  function j(t) {
25
25
  return t ? t[0].toLowerCase() + t.slice(1) : "";
26
26
  }
27
- const B = /_\w*/, P = /-\w*/;
27
+ const P = /_\w*/, S = /-\w*/;
28
28
  function U(t) {
29
- const e = B.test(t) ? t.replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (s, n) => n.toUpperCase()) : P.test(t) ? t.replace(/(?:^-*|-*$)/g, "").replace(/-+(\w)/g, (s, n) => n.toUpperCase()) : t;
29
+ const e = P.test(t) ? t.replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (s, n) => n.toUpperCase()) : S.test(t) ? t.replace(/(?:^-*|-*$)/g, "").replace(/-+(\w)/g, (s, n) => n.toUpperCase()) : t;
30
30
  return j(e);
31
31
  }
32
32
  function M(t) {
33
- return B.test(t) ? j(t).replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (e, s) => "_" + s.toLowerCase()) : P.test(t) ? j(t.replace(/(?:^-*|-*$)/g, "")).replace(/-+(\w)/g, (e, s) => "_" + s.toLowerCase()) : j(t).replace(/[A-Z]/g, (e) => `_${e.toLowerCase()}`);
33
+ return P.test(t) ? j(t).replace(/(?:^_*|_*$)/g, "").replace(/_+([^_])/g, (e, s) => "_" + s.toLowerCase()) : S.test(t) ? j(t.replace(/(?:^-*|-*$)/g, "")).replace(/-+(\w)/g, (e, s) => "_" + s.toLowerCase()) : j(t).replace(/[A-Z]/g, (e) => `_${e.toLowerCase()}`);
34
34
  }
35
- const G = /^(?:https?:)?\/\/.+$/i, V = /^https?:\/\/.+$/i, W = /^\{[\d\D]*\}$/, Z = /^\[[\d\D]*\]$/;
36
- function A(t, e = !1) {
37
- return e ? G.test(t) : V.test(t);
35
+ const V = /^(?:https?:)?\/\/.+$/i, W = /^https?:\/\/.+$/i, Z = /^\{[\d\D]*\}$/, z = /^\[[\d\D]*\]$/;
36
+ function k(t, e = !1) {
37
+ return e ? V.test(t) : W.test(t);
38
38
  }
39
- function z(t) {
39
+ function Q(t) {
40
40
  return t == null;
41
41
  }
42
42
  function v(t, ...e) {
@@ -45,10 +45,10 @@ function v(t, ...e) {
45
45
  const s = Object.getPrototypeOf(t);
46
46
  return s !== Object.prototype && s !== null ? !1 : e.every((n) => n in t);
47
47
  }
48
- function S(t) {
49
- return W.test(t) || Z.test(t);
48
+ function H(t) {
49
+ return Z.test(t) || z.test(t);
50
50
  }
51
- function F(t, e) {
51
+ function C(t, e) {
52
52
  try {
53
53
  const s = JSON.parse(t);
54
54
  return e ? e(s) ? s : null : s;
@@ -75,7 +75,7 @@ function R(t, e = !1) {
75
75
  {}
76
76
  ) : t;
77
77
  }
78
- async function me(t) {
78
+ async function ye(t) {
79
79
  return await new Promise(function(e) {
80
80
  const s = document.getElementsByTagName("head")[0], n = document.createElement("script");
81
81
  n.setAttribute("type", "text/javascript"), n.setAttribute("charset", "utf-8"), n.onload = function() {
@@ -85,7 +85,7 @@ async function me(t) {
85
85
  }, n.setAttribute("src", t), s.appendChild(n);
86
86
  });
87
87
  }
88
- function Q(t, e = "数据未能正确识别") {
88
+ function X(t, e = "数据未能正确识别") {
89
89
  return typeof t == "function" ? {
90
90
  guard: t,
91
91
  message: e
@@ -95,19 +95,19 @@ function Q(t, e = "数据未能正确识别") {
95
95
  };
96
96
  }
97
97
  function D(t, e = "") {
98
- return !e || A(t, !0) ? L(t) : (L(e) + "/" + t).replace(/\/{2,}/g, "/").replace(/:\//, "://");
98
+ return !e || k(t, !0) ? L(t) : (L(e) + "/" + t).replace(/\/{2,}/g, "/").replace(/:\//, "://");
99
99
  }
100
100
  function L(t) {
101
- return A(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(/^\/+/, "");
101
+ return k(t, !0) ? t.startsWith("http") ? t : ("location" in globalThis ? location.protocol : "https:") + t : ("location" in globalThis ? location.origin : "http://127.0.0.1") + "/" + t.replace(/^\/+/, "");
102
102
  }
103
- function X(t, e) {
103
+ function Y(t, e) {
104
104
  const s = {};
105
105
  return (t.match(/([^=&#?]+)=[^&#]*/g) || []).forEach(function(n) {
106
106
  const a = n.split("="), r = a[0], o = decodeURIComponent(a[1] || "");
107
107
  s[r] !== void 0 ? s[r] += "," + o : s[r] = o;
108
108
  }), e !== !0 ? s[e] || "" : s;
109
109
  }
110
- function Y(t, e) {
110
+ function ee(t, e) {
111
111
  if (e) {
112
112
  if (e === !0)
113
113
  return t.replace(/\?[^#]*/, "");
@@ -116,30 +116,30 @@ function Y(t, e) {
116
116
  const s = t.split("#"), n = s[0].split("?"), a = n[0], r = n.length > 1 ? n[1] : "", o = s.length > 1 ? "#" + s[1] : "", c = typeof e == "string" ? [e] : Array.isArray(e) ? e : [];
117
117
  return !c.length || !r ? s[0] + o : (c.map((i) => i.replace(/([\\(){}[\]^$+\-*?|])/g, "\\$1")), (a + "?" + r.replace(new RegExp("(?:^|&)(?:" + c.join("|") + ")=[^&$]+", "g"), "").replace(/^&/, "")).replace(/\?$/, "") + o);
118
118
  }
119
- function ye(t, e, s = !1) {
119
+ function pe(t, e, s = !1) {
120
120
  const n = typeof e == "string" ? e : Object.keys(e).map((o) => `${o}=${encodeURIComponent(e[o])}`).join("&");
121
121
  if (!n)
122
122
  return t;
123
123
  const a = t.split("#");
124
- s && (a[0] = Y(
124
+ s && (a[0] = ee(
125
125
  a[0],
126
126
  (n.match(/([^=&#?]+)=[^&#]+/g) || []).map((o) => o.replace(/=.+$/, ""))
127
127
  ));
128
128
  const r = a[0].indexOf("?") + 1 ? "&" : "?";
129
129
  return (a[0] + r + n + (a.length > 1 ? "#" + a[1] : "")).replace(/\?&/, "?");
130
130
  }
131
- function ee(t) {
131
+ function te(t) {
132
132
  const e = t.match(/(?:\?|&)([^=]+)(?:&|$)/g);
133
133
  return e ? e.join("").replace(/(?:\?|^&+|&+$)/g, "").replace(/&{2}/g, "&").split("&").sort() : [];
134
134
  }
135
- class te {
135
+ class se {
136
136
  constructor(e = 500) {
137
137
  f(this, "ttl");
138
138
  f(this, "cache");
139
139
  this.cache = {}, this.ttl = Math.max(e, 0);
140
140
  }
141
141
  getKey(e, s) {
142
- const n = e.replace(/#.+/, ""), a = n.replace(/\?.+/g, ""), r = Object.assign(X(n, !0), s), o = ee(n), c = Object.keys(r).sort().map((i) => `${i}#${r[i]}`);
142
+ const n = e.replace(/#.+/, ""), a = n.replace(/\?.+/g, ""), r = Object.assign(Y(n, !0), s), o = te(n), c = Object.keys(r).sort().map((i) => `${i}#${r[i]}`);
143
143
  return `${a}_${c.join(",")}_${o.join(",")}`;
144
144
  }
145
145
  updateTTL(e) {
@@ -158,14 +158,15 @@ class te {
158
158
  });
159
159
  }
160
160
  }
161
- function se(t, e, s, n) {
162
- if (e.ok && !z(e.data) && n) {
163
- const a = Q(n, "响应数据未能正确识别");
164
- return a.guard(e.data) || (console.error("ResponseCheckFaild", t, e.data), s.showMessage(!0, `${t} ${a.message}`), e.data = null), e;
161
+ const x = "UnexpectResponse";
162
+ function ne(t, e, s, n) {
163
+ if (e.ok && !Q(e.data) && n) {
164
+ const a = X(n, "响应数据未能正确识别");
165
+ return a.guard(e.data) || (s.showMessage(!0, `${t} ${a.message}`), console.error(x, t, e.data), e.data = null, e.code = x, e.message = a.message), e;
165
166
  }
166
167
  return e;
167
168
  }
168
- class ne {
169
+ class re {
169
170
  constructor(e) {
170
171
  // 保存的配置需要部分字段强制设置默认值
171
172
  f(this, "config", {
@@ -189,7 +190,7 @@ class ne {
189
190
  e && this.set(e);
190
191
  }
191
192
  set(e) {
192
- if (e.baseURL && !/^\/.+/.test(e.baseURL) && !A(e.baseURL))
193
+ if (e.baseURL && !/^\/.+/.test(e.baseURL) && !k(e.baseURL))
193
194
  throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"), new Error("BaseURLError");
194
195
  Object.assign(this.config, e);
195
196
  }
@@ -205,12 +206,12 @@ class ne {
205
206
  this.config.messageHandler && s && this.config.messageHandler(e, s);
206
207
  }
207
208
  }
208
- class pe {
209
+ class be {
209
210
  constructor(e, s) {
210
211
  f(this, "agent");
211
212
  f(this, "config");
212
213
  f(this, "cache");
213
- this.config = new ne(s), this.agent = e, this.cache = new te(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);
214
+ this.config = new re(s), this.agent = e, this.cache = new se(this.config.get("cacheTTL")), this.setConfig = this.setConfig.bind(this), this.getConfig = this.getConfig.bind(this), this.get = this.get.bind(this), this.post = this.post.bind(this), this.del = this.del.bind(this), this.patch = this.patch.bind(this), this.put = this.put.bind(this), this.head = this.head.bind(this);
214
215
  }
215
216
  /**
216
217
  * 执行网络请求
@@ -233,7 +234,7 @@ class pe {
233
234
  * 检查响应的数据类型
234
235
  */
235
236
  async guard(e, s, n) {
236
- return se(e, s, this.config, n);
237
+ return ne(e, s, this.config, n);
237
238
  }
238
239
  /**
239
240
  * 修改默认请求配置: baesURL / timeout / credentials / errorHandler / messageHandler / responseHandler / logHandler / responseRule
@@ -280,12 +281,12 @@ class pe {
280
281
  return r.method = "PATCH", r.body = s, this.guard(e, await this.exec(e, r), n || null);
281
282
  }
282
283
  }
283
- const H = "data", b = "message";
284
- function re(t, e, s, n, a) {
284
+ const q = "data", b = "message";
285
+ function ae(t, e, s, n, a) {
285
286
  const r = a || n;
286
- return T(t) ? le(r.ok || n.ok, t, e, s) : ae(r.failed || n.failed, e, s);
287
+ return T(t) ? he(r.ok || n.ok, t, e, s) : oe(r.failed || n.failed, e, s);
287
288
  }
288
- const be = function(t) {
289
+ const we = function(t) {
289
290
  const e = [], s = t.failed || { resolve: "json" };
290
291
  switch (e.push("- 当http状态码 <200 或者 >=400 时"), s.resolve) {
291
292
  case "body":
@@ -302,14 +303,14 @@ const be = function(t) {
302
303
  break;
303
304
  case "json":
304
305
  e.push(
305
- " 将响应解析为 json,读取 " + (n.dataField || H) + " 作为响应数据,读取 " + (n.messageField || b) + " 作为提示消息"
306
+ " 将响应解析为 json,读取 " + (n.dataField || q) + " 作为响应数据,读取 " + (n.messageField || b) + " 作为提示消息"
306
307
  ), n.statusField && e.push(" 当 " + n.statusField + " 为 " + (n.statusOKValue || "空值") + " 时是成功提示,否则是错误消息"), n.ignoreMessage && e.push(" 并忽略以下消息:" + n.ignoreMessage);
307
308
  break;
308
309
  }
309
310
  return e.join(`
310
311
  `);
311
312
  };
312
- function ae(t, e, s) {
313
+ function oe(t, e, s) {
313
314
  const n = t || { resolve: "json", messageField: b }, a = {
314
315
  ok: !1,
315
316
  code: e,
@@ -318,43 +319,43 @@ function ae(t, e, s) {
318
319
  };
319
320
  switch (n.resolve) {
320
321
  case "body":
321
- a.message = x(s) || s;
322
+ a.message = _(s) || s;
322
323
  break;
323
324
  case "json":
324
- const { code: r, message: o } = oe(s, n.converter, n.statusField, n.messageField);
325
- a.code = r || e, a.message = x(s) || o;
325
+ const { code: r, message: o } = ce(s, n.converter, n.statusField, n.messageField);
326
+ a.code = r || e, a.message = _(s) || o;
326
327
  break;
327
328
  }
328
329
  return a;
329
330
  }
330
- function oe(t, e, s, n = b) {
331
- if (!S(t))
331
+ function ce(t, e, s, n = b) {
332
+ if (!H(t))
332
333
  return { message: "" };
333
- const a = k(F(t), e);
334
+ const a = A(C(t), e);
334
335
  return !a || !v(a) ? { message: t } : {
335
- code: s ? C(a, s) : "",
336
- message: C(a, n) || t
336
+ code: s ? F(a, s) : "",
337
+ message: F(a, n) || t
337
338
  };
338
339
  }
339
- function C(t, e) {
340
+ function F(t, e) {
340
341
  const s = Array.isArray(e) ? e : [e];
341
342
  for (const n of s)
342
343
  if (n in t)
343
- return ce(t[n]);
344
+ return ie(t[n]);
344
345
  return "";
345
346
  }
346
- function ce(t) {
347
+ function ie(t) {
347
348
  return t ? typeof t == "string" ? t : JSON.stringify(t) : "";
348
349
  }
349
- const ie = /<title>([^<]+)<\/title>/i, ue = /<message>([^<]+)<\/message>/i;
350
- function x(t) {
351
- const e = t.match(ie);
350
+ const ue = /<title>([^<]+)<\/title>/i, le = /<message>([^<]+)<\/message>/i;
351
+ function _(t) {
352
+ const e = t.match(ue);
352
353
  if (e)
353
354
  return e[1];
354
- const s = t.match(ue);
355
+ const s = t.match(le);
355
356
  return s ? s[1] : "";
356
357
  }
357
- function le(t, e, s, n) {
358
+ function he(t, e, s, n) {
358
359
  const a = t || { resolve: "body" }, r = {
359
360
  ok: !0,
360
361
  code: s,
@@ -364,24 +365,24 @@ function le(t, e, s, n) {
364
365
  if (e === 204 || !n)
365
366
  return r;
366
367
  if (a.resolve === "body")
367
- return r.data = S(n) ? k(F(n), t.converter) : n, r;
368
- const o = k(F(n), t.converter);
368
+ return r.data = H(n) ? A(C(n), t.converter) : n, r;
369
+ const o = A(C(n), t.converter);
369
370
  if (!o || !v(o))
370
371
  return r.ok = !1, r.code = "ResponseFormatError", r.message = "响应内容无法格式化为 Object", r;
371
- const c = a.statusField, i = a.statusOKValue || "", l = a.dataField || H, h = a.messageField || b, u = a.ignoreMessage || "";
372
+ const c = a.statusField, i = a.statusOKValue || "", l = a.dataField || q, h = a.messageField || b, u = a.ignoreMessage || "";
372
373
  if (c && !(c in o))
373
374
  return r.ok = !1, r.code = "ResponseFieldMissing", r.message = "响应内容找不到状态字段 " + c, r;
374
375
  const d = c ? o[c] + "" : "";
375
- return r.ok = c ? d === i : !0, r.code = d || s, r.data = l === !0 ? o : l in o ? o[l] : null, r.message = C(o, h), u && r.message && (Array.isArray(u) && u.includes(r.message) || typeof u == "string" && r.message === u) && (r.message = ""), r;
376
+ return r.ok = c ? d === i : !0, r.code = d || s, r.data = l === !0 ? o : l in o ? o[l] : null, r.message = F(o, h), u && r.message && (Array.isArray(u) && u.includes(r.message) || typeof u == "string" && r.message === u) && (r.message = ""), r;
376
377
  }
377
378
  function T(t) {
378
379
  return t >= 200 && t < 400;
379
380
  }
380
- function k(t, e) {
381
+ function A(t, e) {
381
382
  return e === "camelize" ? $(t) : e === "snakify" ? R(t) : t;
382
383
  }
383
- async function he(t, e, s, n, a) {
384
- const r = a || 0, o = Math.max(0, Math.min(10, (n == null ? void 0 : n.maxRetry) ?? s.get("maxRetry") ?? 0)), c = (n == null ? void 0 : n.retryResolve) ?? s.get("retryResolve"), i = s.get("logHandler") || J;
384
+ async function de(t, e, s, n, a) {
385
+ const r = a || 0, o = Math.max(0, Math.min(10, (n == null ? void 0 : n.maxRetry) ?? s.get("maxRetry") ?? 0)), c = (n == null ? void 0 : n.retryResolve) ?? s.get("retryResolve"), i = s.get("logHandler") || G;
385
386
  i({
386
387
  type: "prepear",
387
388
  url: e,
@@ -408,14 +409,14 @@ async function he(t, e, s, n, a) {
408
409
  if (!o || c === "network" && u > 0 || c === "status" && w || Array.isArray(c) && !c.includes(u) || typeof c == "function" && c(h, r) !== !0 || r >= o)
409
410
  return h;
410
411
  const g = (n == null ? void 0 : n.retryInterval) ?? s.get("retryInterval") ?? 1e3;
411
- return await N(
412
+ return await J(
412
413
  Math.max(
413
414
  100,
414
415
  g === "2EB" ? Math.pow(2, r) * 1e3 : typeof g == "function" ? g(r + 1) || 0 : g
415
416
  )
416
- ), await he(t, e, s, n, r + 1);
417
+ ), await de(t, e, s, n, r + 1);
417
418
  }
418
- async function we(t, e, s) {
419
+ async function je(t, e, s) {
419
420
  var g;
420
421
  const n = Object.assign({ method: "GET" }, s), a = O.FormData ? n.body instanceof FormData : !1, r = a && n.method !== "POST" && n.method !== "PUT" ? "POST" : n.method, o = r === "GET" || r === "HEAD" || r === "DELETE";
421
422
  o && n.body !== void 0 && (console.warn("request body is invalid with method get, head, delete"), delete n.body);
@@ -426,9 +427,9 @@ async function we(t, e, s) {
426
427
  n.headers
427
428
  ), i = n.params || {}, l = {};
428
429
  Object.keys(i).forEach((p) => {
429
- i[p] !== void 0 && (l[p] = de(i[p]));
430
+ i[p] !== void 0 && (l[p] = ge(i[p]));
430
431
  });
431
- const h = e.getFullUrl(t), u = ge(n.body), d = n.timeout || e.get("timeout"), y = await async function() {
432
+ const h = e.getFullUrl(t), u = fe(n.body), d = n.timeout || e.get("timeout"), y = await async function() {
432
433
  var E;
433
434
  const p = e.get("requestTransformer");
434
435
  return p ? await p({ headers: c, params: l, method: r, url: h, body: u }) : await ((E = e.get("requestHandler")) == null ? void 0 : E(c, l, r, t));
@@ -444,17 +445,17 @@ async function we(t, e, s) {
444
445
  credentials: n.credentials || e.get("credentials")
445
446
  };
446
447
  }
447
- function de(t) {
448
+ function ge(t) {
448
449
  return typeof t == "string" ? t : Array.isArray(t) ? t.join(",") : t + "";
449
450
  }
450
- function ge(t) {
451
+ function fe(t) {
451
452
  if (t)
452
453
  return typeof t == "string" || t instanceof URLSearchParams || t instanceof ArrayBuffer || O.Blob && t instanceof Blob || O.FormData && t instanceof FormData ? t : JSON.stringify(t);
453
454
  }
454
- function je(t, e, s, n) {
455
+ function $e(t, e, s, n) {
455
456
  var o, c;
456
457
  if (t.status < 0)
457
- return _(
458
+ return B(
458
459
  { ok: !1, status: t.status, code: t.statusText, headers: {}, message: "", data: null },
459
460
  `${t.method} ${e} ${t.statusText}`,
460
461
  t.method,
@@ -462,17 +463,17 @@ function je(t, e, s, n) {
462
463
  s,
463
464
  n
464
465
  );
465
- T(t.status) || (o = s.get("errorHandler")) == null || o(t.status, t.method, e);
466
+ T(t.status) || (o = s.get("errorHandler")) == null || o(t.status, t.method, e, t.rawError);
466
467
  const a = {
467
- ...re(t.status, t.statusText, t.body, s.get("responseRule"), n == null ? void 0 : n.responseRule),
468
+ ...ae(t.status, t.statusText, t.body, s.get("responseRule"), n == null ? void 0 : n.responseRule),
468
469
  status: t.status,
469
- headers: K(Object.entries(t.headers || {}).map(([i, l]) => [i.toLowerCase(), l]))
470
+ headers: N(Object.entries(t.headers || {}).map(([i, l]) => [i.toLowerCase(), l]))
470
471
  };
471
472
  (c = s.get("responseHandler")) == null || c({ ...a }, t.method, e);
472
473
  const r = a.ok ? a.message : `${t.method} ${e} [${a.code || t.statusText}] ${a.message || t.statusText}`;
473
- return _(a, r, t.method, e, s, n);
474
+ return B(a, r, t.method, e, s, n);
474
475
  }
475
- function _(t, e, s, n, a, r) {
476
+ function B(t, e, s, n, a, r) {
476
477
  const o = a.get("message"), c = o === !1 || (r == null ? void 0 : r.message) === !1 ? !1 : (r == null ? void 0 : r.message) || o;
477
478
  if (c !== !1) {
478
479
  const i = typeof c == "function" ? c(t, s, n, e) : e;
@@ -480,18 +481,18 @@ function _(t, e, s, n, a, r) {
480
481
  }
481
482
  return t;
482
483
  }
483
- const $e = "1.7.1";
484
+ const Re = "1.7.3";
484
485
  export {
485
- me as A,
486
- A as D,
487
- pe as N,
488
- ne as R,
486
+ ye as A,
487
+ k as D,
488
+ be as N,
489
+ re as R,
489
490
  O as S,
490
- ye as U,
491
- we as c,
492
- K as f,
493
- be as g,
494
- je as h,
495
- he as r,
496
- $e as v
491
+ pe as U,
492
+ je as c,
493
+ N as f,
494
+ we as g,
495
+ $e as h,
496
+ de as r,
497
+ Re as v
497
498
  };
package/dist/version.d.ts CHANGED
@@ -1 +1 @@
1
- export declare const version = "1.7.1";
1
+ export declare const version = "1.7.3";
package/dist/wx.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./version-Cx7ZvtPo.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.Ut(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;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const e=require("./version-B52JIKu-.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.Ut(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, U as l, S as d, N as x } from "./version-C-bTPkwR.js";
2
- import { v as D } from "./version-C-bTPkwR.js";
1
+ import { h as i, r as p, c as f, U as l, S as d, N as x } from "./version-DwET8YXx.js";
2
+ import { v as D } from "./version-DwET8YXx.js";
3
3
  const h = async function(t, o, r) {
4
4
  return i(await p(y, t, o, r), t, o, r);
5
5
  }, y = async function(t, o, r) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@seayoo-web/request",
3
- "version": "1.7.1",
3
+ "version": "1.7.3",
4
4
  "description": "requst tools for seayoo web",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",
@@ -1 +0,0 @@
1
- "use strict";const s=require("./version-Cx7ZvtPo.cjs"),b=async function(o,a,n){return s.handleResponse(await s.retryRequest(l,o,a,n),o,a,n)},l=async function(o,a,n){const t=await s.convertOptions(o,a,n),i=new URL(t.url),c=t.params;c instanceof Object&&Object.keys(c).forEach(r=>i.searchParams.set(r,c[r]));const e=s.Support.AbortController?new AbortController:null;function u(){e&&!e.signal.aborted&&e.abort()}t.abort&&t.abort.addEventListener("abort",u);const d=t.timeout>0?setTimeout(u,t.timeout):null,h=new Request(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"});return await fetch(h).then(async r=>({url:i.toString(),method:t.method,status:r.status,statusText:r.statusText,headers:s.fromEntries([...r.headers.entries()]),body:t.method==="HEAD"?"":await r.text()})).catch(r=>({url:i.toString(),method:t.method,status:-1,statusText:e!=null&&e.signal.aborted?"Aborted":"NetworkError",body:String(r)})).finally(()=>{d!==null&&clearTimeout(d),t.abort&&t.abort.removeEventListener("abort",u)})};exports.fetchRequest=b;
@@ -1,2 +0,0 @@
1
- "use strict";var N=Object.defineProperty;var G=(t,e,s)=>e in t?N(t,e,{enumerable:!0,configurable:!0,writable:!0,value:s}):t[e]=s;var f=(t,e,s)=>(G(t,typeof e!="symbol"?e+"":e,s),s);const m=typeof globalThis<"u"?globalThis:typeof global<"u"?global:typeof window<"u"?window:{},b={fetch:"fetch"in m,window:"window"in m,FormData:"FormData"in m,Blob:"Blob"in m,wx:"wx"in m,TextDecoder:"TextDecoder"in m,AbortController:"AbortController"in m};function B(t){return t.reduce((e,[s,n])=>(s&&(e[s]=n||""),e),{})}function J(t){return new Promise(e=>setTimeout(e,Math.max(0,t)))}function V(){}function j(t){return t?t[0].toLowerCase()+t.slice(1):""}const P=/_\w*/,S=/-\w*/;function U(t){const e=P.test(t)?t.replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(s,n)=>n.toUpperCase()):S.test(t)?t.replace(/(?:^-*|-*$)/g,"").replace(/-+(\w)/g,(s,n)=>n.toUpperCase()):t;return j(e)}function D(t){return P.test(t)?j(t).replace(/(?:^_*|_*$)/g,"").replace(/_+([^_])/g,(e,s)=>"_"+s.toLowerCase()):S.test(t)?j(t.replace(/(?:^-*|-*$)/g,"")).replace(/-+(\w)/g,(e,s)=>"_"+s.toLowerCase()):j(t).replace(/[A-Z]/g,e=>`_${e.toLowerCase()}`)}const W=/^(?:https?:)?\/\/.+$/i,Z=/^https?:\/\/.+$/i,z=/^\{[\d\D]*\}$/,Q=/^\[[\d\D]*\]$/;function v(t,e=!1){return e?W.test(t):Z.test(t)}function X(t){return t==null}function C(t,...e){if(!t||typeof t!="object")return!1;const s=Object.getPrototypeOf(t);return s!==Object.prototype&&s!==null?!1:e.every(n=>n in t)}function H(t){return z.test(t)||Q.test(t)}function F(t,e){try{const s=JSON.parse(t);return e?e(s)?s:null:s}catch{return null}}""+Math.random().toString(32).slice(2);function $(t,e=!1){return typeof t=="string"?U(t):Array.isArray(t)?t.map(s=>!e||typeof s=="string"?$(s):typeof s=="object"&&s?$(s,!0):s):C(t)?Object.keys(t).reduce((s,n)=>{const a=U(String(n)),r=a.charAt(0).toLowerCase()+a.slice(1);return s[r]=e?t[n]:$(t[n]),s},{}):t}function O(t,e=!1){return typeof t=="string"?D(t):Array.isArray(t)?t.map(s=>!e||typeof s=="string"?O(s):typeof s=="object"&&s?O(s,e):s):C(t)?Object.keys(t).reduce((s,n)=>{const a=D(String(n));return s[a]=e?t[n]:O(t[n]),s},{}):t}async function Y(t){return await new Promise(function(e){const s=document.getElementsByTagName("head")[0],n=document.createElement("script");n.setAttribute("type","text/javascript"),n.setAttribute("charset","utf-8"),n.onload=function(){s.removeChild(n),e(!0)},n.onerror=function(){s.removeChild(n),e(!1)},n.setAttribute("src",t),s.appendChild(n)})}function ee(t,e="数据未能正确识别"){return typeof t=="function"?{guard:t,message:e}:{guard:t.guard,message:t.message||e}}function M(t,e=""){return!e||v(t,!0)?L(t):(L(e)+"/"+t).replace(/\/{2,}/g,"/").replace(/:\//,"://")}function L(t){return v(t,!0)?t.startsWith("http")?t:("location"in globalThis?location.protocol:"https:")+t:("location"in globalThis?location.origin:"http://127.0.0.1")+"/"+t.replace(/^\/+/,"")}function te(t,e){const s={};return(t.match(/([^=&#?]+)=[^&#]*/g)||[]).forEach(function(n){const a=n.split("="),r=a[0],o=decodeURIComponent(a[1]||"");s[r]!==void 0?s[r]+=","+o:s[r]=o}),e!==!0?s[e]||"":s}function se(t,e){if(e){if(e===!0)return t.replace(/\?[^#]*/,"")}else return t;const s=t.split("#"),n=s[0].split("?"),a=n[0],r=n.length>1?n[1]:"",o=s.length>1?"#"+s[1]:"",c=typeof e=="string"?[e]:Array.isArray(e)?e:[];return!c.length||!r?s[0]+o:(c.map(i=>i.replace(/([\\(){}[\]^$+\-*?|])/g,"\\$1")),(a+"?"+r.replace(new RegExp("(?:^|&)(?:"+c.join("|")+")=[^&$]+","g"),"").replace(/^&/,"")).replace(/\?$/,"")+o)}function ne(t,e,s=!1){const n=typeof e=="string"?e:Object.keys(e).map(o=>`${o}=${encodeURIComponent(e[o])}`).join("&");if(!n)return t;const a=t.split("#");s&&(a[0]=se(a[0],(n.match(/([^=&#?]+)=[^&#]+/g)||[]).map(o=>o.replace(/=.+$/,""))));const r=a[0].indexOf("?")+1?"&":"?";return(a[0]+r+n+(a.length>1?"#"+a[1]:"")).replace(/\?&/,"?")}function re(t){const e=t.match(/(?:\?|&)([^=]+)(?:&|$)/g);return e?e.join("").replace(/(?:\?|^&+|&+$)/g,"").replace(/&{2}/g,"&").split("&").sort():[]}class ae{constructor(e=500){f(this,"ttl");f(this,"cache");this.cache={},this.ttl=Math.max(e,0)}getKey(e,s){const n=e.replace(/#.+/,""),a=n.replace(/\?.+/g,""),r=Object.assign(te(n,!0),s),o=re(n),c=Object.keys(r).sort().map(i=>`${i}#${r[i]}`);return`${a}_${c.join(",")}_${o.join(",")}`}updateTTL(e){this.ttl=Math.max(e,0)}get(e){if(this.ttl===0)return null;const s=this.cache[e];return s?s.ttl<Date.now()?(delete this.cache[e],null):s.res:null}set(e,s){this.ttl!==0&&(this.cache[e]={ttl:Date.now()+this.ttl,res:s})}}function oe(t,e,s,n){if(e.ok&&!X(e.data)&&n){const a=ee(n,"响应数据未能正确识别");return a.guard(e.data)||(console.error("ResponseCheckFaild",t,e.data),s.showMessage(!0,`${t} ${a.message}`),e.data=null),e}return e}class q{constructor(e){f(this,"config",{baseURL:"",maxRetry:0,retryInterval:1e3,retryResolve:"network",timeout:5e3,cacheTTL:500,credentials:"same-origin",responseRule:{ok:{resolve:"body"},failed:{resolve:"json",messageField:"message"}}});e&&this.set(e)}set(e){if(e.baseURL&&!/^\/.+/.test(e.baseURL)&&!v(e.baseURL))throw console.warn("baseURL 需要以 / 开头,或者是完整的 url 地址"),new Error("BaseURLError");Object.assign(this.config,e)}get(e){return this.config[e]}getFullUrl(e){return e.startsWith("/")?M(e):M(e,this.config.baseURL)}showMessage(e,s){this.config.messageHandler&&s&&this.config.messageHandler(e,s)}}class ce{constructor(e,s){f(this,"agent");f(this,"config");f(this,"cache");this.config=new q(s),this.agent=e,this.cache=new ae(this.config.get("cacheTTL")),this.setConfig=this.setConfig.bind(this),this.getConfig=this.getConfig.bind(this),this.get=this.get.bind(this),this.post=this.post.bind(this),this.del=this.del.bind(this),this.patch=this.patch.bind(this),this.put=this.put.bind(this),this.head=this.head.bind(this)}async exec(e,s){try{return await this.agent(e,this.config,s)}catch(n){return console.error("RequestError",n),{ok:!1,status:-9,code:"Unkonwn",message:String(n),headers:{},data:null}}}async guard(e,s,n){return oe(e,s,this.config,n)}setConfig(e){this.config.set(e),this.cache.updateTTL(this.config.get("cacheTTL"))}getConfig(e){return this.config.get(e)}async head(e,s){const n=Object.assign({},s||null);return n.method="HEAD",this.guard(e,await this.exec(e,n),null)}async get(e,s,n){const a=Object.assign({},n||null);a.method="GET";const r=this.cache.getKey(e,a.params),o=this.cache.get(r);if(o)return this.guard(e,await o,s||null);const c=this.exec(e,a);return this.cache.set(r,c),this.guard(e,await c,s||null)}async post(e,s,n,a){const r=Object.assign({},a||null);return r.method="POST",r.body=s,this.guard(e,await this.exec(e,r),n||null)}async del(e,s,n){const a=Object.assign({},n||null);return a.method="DELETE",this.guard(e,await this.exec(e,a),s||null)}async put(e,s,n,a){const r=Object.assign({},a||null);return r.method="PUT",r.body=s,this.guard(e,await this.exec(e,r),n||null)}async patch(e,s,n,a){const r=Object.assign({},a||null);return r.method="PATCH",r.body=s,this.guard(e,await this.exec(e,r),n||null)}}const I="data",w="message";function ie(t,e,s,n,a){const r=a||n;return T(t)?me(r.ok||n.ok,t,e,s):le(r.failed||n.failed,e,s)}const ue=function(t){const e=[],s=t.failed||{resolve:"json"};switch(e.push("- 当http状态码 <200 或者 >=400 时"),s.resolve){case"body":e.push(" 将响应内容格式化为字符串并作为错误消息");break;case"json":e.push(" 将响应解析为json,并读取 "+(s.messageField||w)+" 作为错误消息");break}const n=t.ok||{resolve:"body"};switch(e.push("- 当http状态码 >=200 并且 <400 时"),n.resolve){case"body":e.push(" 将响应尝试解析为 json,并作为数据内容返回");break;case"json":e.push(" 将响应解析为 json,读取 "+(n.dataField||I)+" 作为响应数据,读取 "+(n.messageField||w)+" 作为提示消息"),n.statusField&&e.push(" 当 "+n.statusField+" 为 "+(n.statusOKValue||"空值")+" 时是成功提示,否则是错误消息"),n.ignoreMessage&&e.push(" 并忽略以下消息:"+n.ignoreMessage);break}return e.join(`
2
- `)};function le(t,e,s){const n=t||{resolve:"json",messageField:w},a={ok:!1,code:e,message:s,data:null};switch(n.resolve){case"body":a.message=x(s)||s;break;case"json":const{code:r,message:o}=he(s,n.converter,n.statusField,n.messageField);a.code=r||e,a.message=x(s)||o;break}return a}function he(t,e,s,n=w){if(!H(t))return{message:""};const a=A(F(t),e);return!a||!C(a)?{message:t}:{code:s?k(a,s):"",message:k(a,n)||t}}function k(t,e){const s=Array.isArray(e)?e:[e];for(const n of s)if(n in t)return de(t[n]);return""}function de(t){return t?typeof t=="string"?t:JSON.stringify(t):""}const ge=/<title>([^<]+)<\/title>/i,fe=/<message>([^<]+)<\/message>/i;function x(t){const e=t.match(ge);if(e)return e[1];const s=t.match(fe);return s?s[1]:""}function me(t,e,s,n){const a=t||{resolve:"body"},r={ok:!0,code:s,message:"",data:null};if(e===204||!n)return r;if(a.resolve==="body")return r.data=H(n)?A(F(n),t.converter):n,r;const o=A(F(n),t.converter);if(!o||!C(o))return r.ok=!1,r.code="ResponseFormatError",r.message="响应内容无法格式化为 Object",r;const c=a.statusField,i=a.statusOKValue||"",l=a.dataField||I,h=a.messageField||w,u=a.ignoreMessage||"";if(c&&!(c in o))return r.ok=!1,r.code="ResponseFieldMissing",r.message="响应内容找不到状态字段 "+c,r;const d=c?o[c]+"":"";return r.ok=c?d===i:!0,r.code=d||s,r.data=l===!0?o:l in o?o[l]:null,r.message=k(o,h),u&&r.message&&(Array.isArray(u)&&u.includes(r.message)||typeof u=="string"&&r.message===u)&&(r.message=""),r}function T(t){return t>=200&&t<400}function A(t,e){return e==="camelize"?$(t):e==="snakify"?O(t):t}async function K(t,e,s,n,a){const r=a||0,o=Math.max(0,Math.min(10,(n==null?void 0:n.maxRetry)??s.get("maxRetry")??0)),c=(n==null?void 0:n.retryResolve)??s.get("retryResolve"),i=s.get("logHandler")||V;i({type:"prepear",url:e,method:(n==null?void 0:n.method)||"GET",retry:r,maxRetry:o,message:r===0?"start":`retry ${r}/${o} start`,headers:n==null?void 0:n.headers,options:n});const l=Date.now(),h=await t(e,s,n),u=h.status,d=Date.now()-l,y=`[cost ${d}][${u}] ${u<0?h.body:""}`;i({type:"finished",url:e,method:h.method,retry:r,maxRetry:o,message:r===0?`finish ${y}`:`retry ${r}/${o} finish ${y}`,response:h,headers:h.headers,cost:d});const R=T(u);if(!o||c==="network"&&u>0||c==="status"&&R||Array.isArray(c)&&!c.includes(u)||typeof c=="function"&&c(h,r)!==!0||r>=o)return h;const g=(n==null?void 0:n.retryInterval)??s.get("retryInterval")??1e3;return await J(Math.max(100,g==="2EB"?Math.pow(2,r)*1e3:typeof g=="function"?g(r+1)||0:g)),await K(t,e,s,n,r+1)}async function ye(t,e,s){var g;const n=Object.assign({method:"GET"},s),a=b.FormData?n.body instanceof FormData:!1,r=a&&n.method!=="POST"&&n.method!=="PUT"?"POST":n.method,o=r==="GET"||r==="HEAD"||r==="DELETE";o&&n.body!==void 0&&(console.warn("request body is invalid with method get, head, delete"),delete n.body);const c=Object.assign(a||o?{}:{"Content-Type":b.Blob&&n.body instanceof Blob?n.body.type||"application/octet-stream":"application/json;charset=utf-8"},n.headers),i=n.params||{},l={};Object.keys(i).forEach(p=>{i[p]!==void 0&&(l[p]=pe(i[p]))});const h=e.getFullUrl(t),u=be(n.body),d=n.timeout||e.get("timeout"),y=await async function(){var E;const p=e.get("requestTransformer");return p?await p({headers:c,params:l,method:r,url:h,body:u}):await((E=e.get("requestHandler"))==null?void 0:E(c,l,r,t))}(),R=typeof y=="string"&&y?y:h;return(g=e.get("logHandler"))==null||g({type:"ready",url:R,method:r,headers:c,timeout:d,body:u}),{url:R,method:r,body:u,params:l,headers:c,timeout:d,abort:n.abort,credentials:n.credentials||e.get("credentials")}}function pe(t){return typeof t=="string"?t:Array.isArray(t)?t.join(","):t+""}function be(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 we(t,e,s,n){var o,c;if(t.status<0)return _({ok:!1,status:t.status,code:t.statusText,headers:{},message:"",data:null},`${t.method} ${e} ${t.statusText}`,t.method,e,s,n);T(t.status)||(o=s.get("errorHandler"))==null||o(t.status,t.method,e);const a={...ie(t.status,t.statusText,t.body,s.get("responseRule"),n==null?void 0:n.responseRule),status:t.status,headers:B(Object.entries(t.headers||{}).map(([i,l])=>[i.toLowerCase(),l]))};(c=s.get("responseHandler"))==null||c({...a},t.method,e);const r=a.ok?a.message:`${t.method} ${e} [${a.code||t.statusText}] ${a.message||t.statusText}`;return _(a,r,t.method,e,s,n)}function _(t,e,s,n,a,r){const o=a.get("message"),c=o===!1||(r==null?void 0:r.message)===!1?!1:(r==null?void 0:r.message)||o;if(c!==!1){const i=typeof c=="function"?c(t,s,n,e):e;i instanceof Error?a.showMessage(!0,i.message):i&&typeof i=="object"&&"message"in i?a.showMessage(!1,i.message):a.showMessage(!t.ok,i)}return t}const Re="1.7.1";exports.Ae=Y;exports.D=v;exports.NetRequestHandler=ce;exports.RequestGlobalConfig=q;exports.Support=b;exports.Ut=ne;exports.convertOptions=ye;exports.fromEntries=B;exports.getResponseRulesDescription=ue;exports.handleResponse=we;exports.retryRequest=K;exports.version=Re;