@seayoo-web/request 1.7.0 → 1.7.2

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
 
@@ -321,7 +321,7 @@ status 表示网络错误或者http 状态码错误时重试;
321
321
 
322
322
  - 当请求结束时无效;
323
323
  - 仅浏览器环境有效,
324
- - 不支持 AbortSignal 的浏览器可以引入 https://www.npmjs.com/package/abortcontroller-polyfill
324
+ - 不支持 AbortSignal 的浏览器可以引入 https://www.npmjs.com/package/abortcontroller-polyfill 或 https://polyfill.io/v3/polyfill.min.js?features=AbortController
325
325
 
326
326
  ### message
327
327
 
@@ -0,0 +1,7 @@
1
+ import type { NetRequestAgent } from "./type";
2
+ /**
3
+ * 基于 fetch 的网络请求包装函数,文件上传不支持进度,需要进度请使用 upload 方法
4
+ *
5
+ * 在浏览器或支持 fetch 的 Node 环境均可以使用
6
+ */
7
+ export declare const fetchRequest: NetRequestAgent;
@@ -1,11 +1,11 @@
1
- import { type XHRequestOptions } from "./core.xhr";
2
- import type { NetRequestAgent, IRequestGlobalConfig, IRequestOptions, IResponseResult, IRetryRequestOptions } from "./type";
3
- /**
4
- * 基于 fetch 的网络请求包装函数,文件上传不支持进度,需要进度请使用 upload 方法
5
- *
6
- * 该方法必定 resolve,限制在浏览器环境中使用
7
- */
8
- export declare const fetchRequest: NetRequestAgent;
1
+ import type { NetRequestAgent, IRequestGlobalConfig, IRequestOptions, IResponseResult, IRetryRequestOptions, IBaseRequestOptions } from "./type";
2
+ /** 进度回调函数配置 */
3
+ interface XHRequestOptions extends IBaseRequestOptions {
4
+ onUploadProgress?: (progress: {
5
+ total: number;
6
+ loaded: number;
7
+ }) => void;
8
+ }
9
9
  /**
10
10
  * 基于 xmlHttpRequest 的网络请求包装函数
11
11
  *
@@ -21,3 +21,4 @@ export declare function xhrUpload(url: string, files: Record<string, Blob> | Blo
21
21
  },
22
22
  /** 全局配置中独有的配置,以受全局配置控制 */
23
23
  config?: Omit<IRequestGlobalConfig, keyof IRequestOptions>): Promise<IResponseResult>;
24
+ export {};
@@ -59,6 +59,8 @@ export interface IOtherRequestOptions {
59
59
  }
60
60
  /** 对外工具接口的请求配置 */
61
61
  export type IRequestOptions = IBaseRequestOptions & IRetryRequestOptions & IOtherRequestOptions;
62
+ /** 请求的原始错误信息 */
63
+ type ERequestRawError = Error | ProgressEvent;
62
64
  /** 全局默认配置 */
63
65
  export type IRequestGlobalConfig = {
64
66
  /**
@@ -85,7 +87,7 @@ export type IRequestGlobalConfig = {
85
87
  body?: IBaseRequestBody;
86
88
  }) => MaybePromise<void | string>);
87
89
  /** 全局错误处理函数,仅仅在 statusCode 错误时触发 */
88
- errorHandler?: null | ((status: number, method: string, url: string) => void);
90
+ errorHandler?: null | ((status: number, method: string, url: string, rawError?: ERequestRawError) => void);
89
91
  /** 全局响应处理函数,任意状态都会触发 */
90
92
  responseHandler?: null | ((result: IResponseResult, method: string, url: string) => void);
91
93
  /** 全局消息提示函数 */
@@ -156,6 +158,8 @@ export interface IRequestBaseResponse {
156
158
  headers?: Record<string, string | undefined>;
157
159
  /** 响应体,如果网络错误或 204/202,则返回空或错误信息 */
158
160
  body: string;
161
+ /** 原始的请求错误信息,仅当请求失败时有值,用以进行错误上报用 */
162
+ rawError?: ERequestRawError;
159
163
  }
160
164
  /** 对外工具接口的返回内容 */
161
165
  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 c=require("./version-CCXW7_VI.cjs"),y=async function(n,s,t){const e=await c.convertOptions(n,s,t),r=new URL(e.url),i=e.params;i instanceof Object&&Object.keys(i).forEach(a=>r.searchParams.set(a,i[a]));const o=c.Support.AbortController?new AbortController:null;function l(){o&&!o.signal.aborted&&o.abort()}e.abort&&e.abort.addEventListener("abort",l);const b=e.timeout>0?setTimeout(l,e.timeout):null,g=new Request(r,{method:e.method,headers:Object.keys(e.headers).length>0?new Headers(e.headers):void 0,body:e.body,credentials:e.credentials,signal:o==null?void 0:o.signal,redirect:"follow"});return await fetch(g).then(async a=>({url:r.toString(),method:e.method,status:a.status,statusText:a.statusText,headers:c.fromEntries([...a.headers.entries()]),body:e.method==="HEAD"?"":await a.text()})).catch(a=>({url:r.toString(),method:e.method,status:-1,statusText:o!=null&&o.signal.aborted?"Aborted":"NetworkError",body:String(a)})).finally(()=>{b!==null&&clearTimeout(b),e.abort&&e.abort.removeEventListener("abort",l)})},m=async function(n,s,t){const e=await c.convertOptions(n,s,t),r=e.method,i=t==null?void 0:t.onUploadProgress,o=c.Ut(e.url,e.params);return await new Promise(l=>{let b=null,g=!1;const a=function(){g||(d.abort(),g=!0)};function f(){b!==null&&clearTimeout(b),e.abort&&e.abort.removeEventListener("abort",a)}const d=new XMLHttpRequest;if(d.open(r,o,!0),i){let h=1;d.upload.addEventListener("progress",w=>{h=w.total,i({total:w.total,loaded:w.loaded})}),d.addEventListener("load",()=>{f(),i({loaded:h,total:h}),l({url:o,method:r,status:d.status,statusText:d.statusText,headers:R(d),body:r==="HEAD"?"":d.responseText})})}d.addEventListener("error",()=>{f(),l({url:o,method:r,status:-1,statusText:"Failed",body:""})}),d.addEventListener("abort",()=>{f(),l({url:o,method:r,status:-1,statusText:"Aborted",body:""})}),Object.entries(e.headers).forEach(([h,w])=>{d.setRequestHeader(h,w)}),e.credentials==="include"&&(d.withCredentials=!0),d.send(e.body||void 0),e.abort&&e.abort.addEventListener("abort",a),e.timeout>0&&(b=setTimeout(a,e.timeout))})};function R(n){const s={};if(!n)return s;const t=n.getAllResponseHeaders();return t&&t!=="null"&&t.replace(/\r/g,"").split(`
2
- `).forEach(e=>{const r=e.trim();if(!r)return;const i=r.split(":"),o=i[0].trim();o&&(s[o]=(i[1]||"").trim())}),s}const T=async function(n,s,t){return c.handleResponse(await c.retryRequest(y,n,s,t),n,s,t)},q=async function(n,s,t){return c.handleResponse(await c.retryRequest(m,n,s,t),n,s,t)};async function H(n,s,t,e){const r=t==null?void 0:t.body,i=(t==null?void 0:t.method)==="PUT"?"PUT":"POST";if(s instanceof Blob){const a=new c.RequestGlobalConfig(e),f=await m(n,a,{...t,method:i,body:s});return c.handleResponse(f,n,a,t)}const o=new FormData,l={...s};r instanceof Object&&Object.entries(r).forEach(([a,f])=>{f instanceof Blob?l[a]=f:Array.isArray(f)?f.forEach((d,h)=>{o.append(`${a}[${h}]`,String(d))}):o.append(a,String(f))});for(const a in l)o.append(a,l[a]);const b=new c.RequestGlobalConfig(e),g=await m(n,b,{...t,method:i,body:o});return c.handleResponse(g,n,b,t)}async function E(n,s,t={}){const e=window;"callback"in t||(t.callback="jsonxData"+Math.random().toString(16).slice(2));const r=t.callback+"";if(!n)return null;const i=c.Ut(n,t,!0);return new Promise(o=>{e[r]=function(l){if(r in window&&delete e[r],s(l))return l;console.warn("response type check faild",n,l),o(null)},c.Ae(i).catch(function(){o(null),delete e[r]})})}async function S(n,s,t={}){const e=window;return"var"in t||(t.var="jsonxData"+Math.random().toString(16).slice(2)),n?await c.Ae(c.Ut(n,t,!0)).then(()=>{const r=e[t.var+""];return s(r)?r:(console.warn("response type check faild",n,r),null)}).catch(()=>null):null}const v=async function(n,s,t){return await H(n,s,t,{baseURL:u.getConfig("baseURL"),logHandler:u.getConfig("logHandler"),errorHandler:u.getConfig("errorHandler"),requestTransformer:u.getConfig("requestTransformer"),messageHandler:u.getConfig("messageHandler"),responseHandler:u.getConfig("responseHandler")})};function p(n){if(!c.Support.window)throw new Error("Default Module Only Support In Browser");return c.Support.fetch?new c.NetRequestHandler(T,n):new c.NetRequestHandler(q,n)}const u=p(),x=u.setConfig,C=u.head,j=u.get,L=u.post,U=u.del,A=u.put,O=u.patch;exports.getResponseRulesDescription=c.getResponseRulesDescription;exports.version=c.version;exports.NetRequest=p;exports.del=U;exports.get=j;exports.head=C;exports.jsonp=E;exports.jsonx=S;exports.patch=O;exports.post=L;exports.put=A;exports.setGlobalConfig=x;exports.upload=v;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./version-BZ8fy-0W.cjs"),y=require("./request.fetch-BtrGinni.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.d.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { xhrUpload } from "./inc/request.browser";
2
1
  import { NetRequestHandler } from "./inc/main";
2
+ import { xhrUpload } from "./inc/request.xhr";
3
3
  import type { IRequestGlobalConfig } from "./inc/type";
4
4
  export * from "./version";
5
5
  export { jsonp, jsonx } from "./inc/jsonp";
@@ -10,7 +10,7 @@ export type { IRequestOptions, IRequestGlobalConfig, IResponseRule, IResponseRes
10
10
  */
11
11
  export declare const upload: (url: string, files: Parameters<typeof xhrUpload>[1], options?: Parameters<typeof xhrUpload>[2]) => ReturnType<typeof xhrUpload>;
12
12
  /**
13
- * 创建新的实例空间,配置和缓存跟全局默认实例是隔离的
13
+ * 创建新的实例空间,配置和缓存跟全局默认实例是隔离的,仅支持在浏览器环境使用
14
14
  */
15
15
  export declare function NetRequest(config?: IRequestGlobalConfig): NetRequestHandler;
16
16
  /**
package/dist/index.js CHANGED
@@ -1,154 +1,126 @@
1
- import { c as E, S as w, f as q, U as p, h as m, r as H, R, A as x, N as T } from "./version-DOOsgzgL.js";
2
- import { g as z, v as J } from "./version-DOOsgzgL.js";
3
- const L = async function(n, o, e) {
4
- const t = await E(n, o, e), r = new URL(t.url), c = t.params;
5
- c instanceof Object && Object.keys(c).forEach((s) => r.searchParams.set(s, c[s]));
6
- const a = w.AbortController ? new AbortController() : null;
7
- function i() {
8
- a && !a.signal.aborted && a.abort();
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-BLZ49ejb.js";
2
+ import { g as z, v as I } from "./version-BLZ49ejb.js";
3
+ import { f as x } from "./request.fetch-DlebtNMD.js";
4
+ const C = async function(t, a, e) {
5
+ return w(await E(m, t, a, e), t, a, e);
6
+ };
7
+ async function S(t, a, e, n) {
8
+ const r = e == null ? void 0 : e.body, i = (e == null ? void 0 : e.method) === "PUT" ? "PUT" : "POST";
9
+ if (a instanceof Blob) {
10
+ const c = new y(n), u = await m(t, c, {
11
+ ...e,
12
+ method: i,
13
+ body: a
14
+ });
15
+ return w(u, t, c, e);
9
16
  }
10
- t.abort && t.abort.addEventListener("abort", i);
11
- const f = t.timeout > 0 ? setTimeout(i, t.timeout) : null, h = new Request(r, {
12
- method: t.method,
13
- headers: Object.keys(t.headers).length > 0 ? new Headers(t.headers) : void 0,
14
- body: t.body,
15
- credentials: t.credentials,
16
- signal: a == null ? void 0 : a.signal,
17
- redirect: "follow"
17
+ const o = new FormData(), d = { ...a };
18
+ r instanceof Object && Object.entries(r).forEach(([c, u]) => {
19
+ u instanceof Blob ? d[c] = u : Array.isArray(u) ? u.forEach((s, f) => {
20
+ o.append(`${c}[${f}]`, String(s));
21
+ }) : o.append(c, String(u));
18
22
  });
19
- return await fetch(h).then(async (s) => ({
20
- url: r.toString(),
21
- method: t.method,
22
- status: s.status,
23
- statusText: s.statusText,
24
- headers: q([...s.headers.entries()]),
25
- body: t.method === "HEAD" ? "" : await s.text()
26
- })).catch((s) => ({
27
- url: r.toString(),
28
- method: t.method,
29
- status: -1,
30
- statusText: a != null && a.signal.aborted ? "Aborted" : "NetworkError",
31
- body: String(s)
32
- })).finally(() => {
33
- f !== null && clearTimeout(f), t.abort && t.abort.removeEventListener("abort", i);
23
+ for (const c in d)
24
+ o.append(c, d[c]);
25
+ const b = new y(n), h = await m(t, b, {
26
+ ...e,
27
+ method: i,
28
+ body: o
34
29
  });
35
- }, y = async function(n, o, e) {
36
- const t = await E(n, o, e), r = t.method, c = e == null ? void 0 : e.onUploadProgress, a = p(t.url, t.params);
37
- return await new Promise((i) => {
38
- let f = null, h = !1;
39
- const s = function() {
40
- h || (d.abort(), h = !0);
30
+ return w(h, t, b, e);
31
+ }
32
+ const m = async function(t, a, e) {
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
+ return await new Promise((d) => {
35
+ let b = null, h = !1;
36
+ const c = function() {
37
+ h || (s.abort(), h = !0);
41
38
  };
42
39
  function u() {
43
- f !== null && clearTimeout(f), t.abort && t.abort.removeEventListener("abort", s);
40
+ b !== null && clearTimeout(b), n.abort && n.abort.removeEventListener("abort", c);
44
41
  }
45
- const d = new XMLHttpRequest();
46
- if (d.open(r, a, !0), c) {
47
- let b = 1;
48
- d.upload.addEventListener("progress", (g) => {
49
- b = g.total, c({ total: g.total, loaded: g.loaded });
50
- }), d.addEventListener("load", () => {
51
- u(), c({ loaded: b, total: b }), i({
52
- url: a,
42
+ const s = new XMLHttpRequest();
43
+ if (s.open(r, o, !0), i) {
44
+ let f = 1;
45
+ s.upload.addEventListener("progress", (g) => {
46
+ f = g.total, i({ total: g.total, loaded: g.loaded });
47
+ }), s.addEventListener("load", () => {
48
+ u(), i({ loaded: f, total: f }), d({
49
+ url: o,
53
50
  method: r,
54
- status: d.status,
55
- statusText: d.statusText,
56
- headers: S(d),
57
- body: r === "HEAD" ? "" : d.responseText
51
+ status: s.status,
52
+ statusText: s.statusText,
53
+ headers: U(s),
54
+ body: r === "HEAD" ? "" : s.responseText
58
55
  });
59
56
  });
60
57
  }
61
- d.addEventListener("error", () => {
62
- u(), i({
63
- url: a,
64
- method: r,
65
- status: -1,
66
- statusText: "Failed",
67
- body: ""
68
- });
69
- }), d.addEventListener("abort", () => {
70
- u(), i({
71
- url: a,
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", () => {
72
+ u(), d({
73
+ url: o,
72
74
  method: r,
73
75
  status: -1,
74
76
  statusText: "Aborted",
75
77
  body: ""
76
78
  });
77
- }), Object.entries(t.headers).forEach(([b, g]) => {
78
- d.setRequestHeader(b, g);
79
- }), t.credentials === "include" && (d.withCredentials = !0), d.send(t.body || void 0), t.abort && t.abort.addEventListener("abort", s), t.timeout > 0 && (f = setTimeout(s, t.timeout));
79
+ }), Object.entries(n.headers).forEach(([f, g]) => {
80
+ s.setRequestHeader(f, g);
81
+ }), n.credentials === "include" && (s.withCredentials = !0), s.send(n.body || void 0), n.abort && n.abort.addEventListener("abort", c), n.timeout > 0 && (b = setTimeout(c, n.timeout));
80
82
  });
81
83
  };
82
- function S(n) {
83
- const o = {};
84
- if (!n)
85
- return o;
86
- const e = n.getAllResponseHeaders();
84
+ function U(t) {
85
+ const a = {};
86
+ if (!t)
87
+ return a;
88
+ const e = t.getAllResponseHeaders();
87
89
  return e && e !== "null" && e.replace(/\r/g, "").split(`
88
- `).forEach((t) => {
89
- const r = t.trim();
90
+ `).forEach((n) => {
91
+ const r = n.trim();
90
92
  if (!r)
91
93
  return;
92
- const c = r.split(":"), a = c[0].trim();
93
- a && (o[a] = (c[1] || "").trim());
94
- }), o;
95
- }
96
- const v = async function(n, o, e) {
97
- return m(await H(L, n, o, e), n, o, e);
98
- }, C = async function(n, o, e) {
99
- return m(await H(y, n, o, e), n, o, e);
100
- };
101
- async function U(n, o, e, t) {
102
- const r = e == null ? void 0 : e.body, c = (e == null ? void 0 : e.method) === "PUT" ? "PUT" : "POST";
103
- if (o instanceof Blob) {
104
- const s = new R(t), u = await y(n, s, {
105
- ...e,
106
- method: c,
107
- body: o
108
- });
109
- return m(u, n, s, e);
110
- }
111
- const a = new FormData(), i = { ...o };
112
- r instanceof Object && Object.entries(r).forEach(([s, u]) => {
113
- u instanceof Blob ? i[s] = u : Array.isArray(u) ? u.forEach((d, b) => {
114
- a.append(`${s}[${b}]`, String(d));
115
- }) : a.append(s, String(u));
116
- });
117
- for (const s in i)
118
- a.append(s, i[s]);
119
- const f = new R(t), h = await y(n, f, {
120
- ...e,
121
- method: c,
122
- body: a
123
- });
124
- return m(h, n, f, e);
94
+ const i = r.split(":"), o = i[0].trim();
95
+ o && (a[o] = (i[1] || "").trim());
96
+ }), a;
125
97
  }
126
- async function k(n, o, e = {}) {
127
- const t = window;
98
+ async function k(t, a, e = {}) {
99
+ const n = window;
128
100
  "callback" in e || (e.callback = "jsonxData" + Math.random().toString(16).slice(2));
129
101
  const r = e.callback + "";
130
- if (!n)
102
+ if (!t)
131
103
  return null;
132
- const c = p(n, e, !0);
133
- return new Promise((a) => {
134
- t[r] = function(i) {
135
- if (r in window && delete t[r], o(i))
136
- return i;
137
- console.warn("response type check faild", n, i), a(null);
138
- }, x(c).catch(function() {
139
- a(null), delete t[r];
104
+ const i = p(t, e, !0);
105
+ return new Promise((o) => {
106
+ n[r] = function(d) {
107
+ if (r in window && delete n[r], a(d))
108
+ return d;
109
+ console.warn("response type check faild", t, d), o(null);
110
+ }, T(i).catch(function() {
111
+ o(null), delete n[r];
140
112
  });
141
113
  });
142
114
  }
143
- async function O(n, o, e = {}) {
144
- const t = window;
145
- return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), n ? await x(p(n, e, !0)).then(() => {
146
- const r = t[e.var + ""];
147
- return o(r) ? r : (console.warn("response type check faild", n, r), null);
115
+ async function A(t, a, e = {}) {
116
+ const n = window;
117
+ return "var" in e || (e.var = "jsonxData" + Math.random().toString(16).slice(2)), t ? await T(p(t, e, !0)).then(() => {
118
+ const r = n[e.var + ""];
119
+ return a(r) ? r : (console.warn("response type check faild", t, r), null);
148
120
  }).catch(() => null) : null;
149
121
  }
150
- const D = async function(n, o, e) {
151
- return await U(n, o, e, {
122
+ const D = async function(t, a, e) {
123
+ return await S(t, a, e, {
152
124
  baseURL: l.getConfig("baseURL"),
153
125
  logHandler: l.getConfig("logHandler"),
154
126
  errorHandler: l.getConfig("errorHandler"),
@@ -157,24 +129,24 @@ const D = async function(n, o, e) {
157
129
  responseHandler: l.getConfig("responseHandler")
158
130
  });
159
131
  };
160
- function A(n) {
161
- if (!w.window)
132
+ function L(t) {
133
+ if (!R.window)
162
134
  throw new Error("Default Module Only Support In Browser");
163
- return w.fetch ? new T(v, n) : new T(C, n);
135
+ return R.fetch ? new H(x, t) : new H(C, t);
164
136
  }
165
- const l = A(), P = l.setConfig, M = l.head, N = l.get, F = l.post, B = l.del, X = l.put, G = l.patch;
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;
166
138
  export {
167
- A as NetRequest,
139
+ L as NetRequest,
168
140
  B as del,
169
- N as get,
141
+ M as get,
170
142
  z as getResponseRulesDescription,
171
- M as head,
143
+ P as head,
172
144
  k as jsonp,
173
- O as jsonx,
174
- G as patch,
175
- F as post,
176
- X as put,
177
- P as setGlobalConfig,
145
+ A as jsonx,
146
+ X as patch,
147
+ N as post,
148
+ F as put,
149
+ O as setGlobalConfig,
178
150
  D as upload,
179
- J as version
151
+ I as version
180
152
  };
package/dist/node.cjs CHANGED
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("node:http"),q=require("node:https"),o=require("./version-CCXW7_VI.cjs"),R=async function(n,u,c){return o.handleResponse(await o.retryRequest(w,n,u,c),n,u,c)},w=async function(n,u,c){const t=await o.convertOptions(n,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)?q:y,a=new URL(t.url),i=t.params;i instanceof Object&&Object.keys(i).forEach(r=>a.searchParams.set(r,i[r]));const b=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(([g,m])=>[g.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:b?"":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})}),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(n){return new o.NetRequestHandler(R,n)}const s=l(),S=s.setConfig,T=s.head,j=s.get,C=s.post,O=s.del,U=s.put,x=s.patch;exports.version=o.version;exports.NetRequest=l;exports.del=O;exports.get=j;exports.head=T;exports.patch=x;exports.post=C;exports.put=U;exports.setGlobalConfig=S;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const y=require("node:http"),R=require("node:https"),o=require("./version-BZ8fy-0W.cjs"),g=require("./request.fetch-BtrGinni.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,12 +1,13 @@
1
- import l from "node:http";
2
- import y from "node:https";
3
- import { h as R, r as g, c as q, D as w, f as x, N as T } from "./version-DOOsgzgL.js";
4
- import { v as G } from "./version-DOOsgzgL.js";
5
- const U = async function(s, a, u) {
6
- return R(await g(j, s, a, u), s, a, u);
7
- }, j = async function(s, a, u) {
8
- const t = await q(s, a, u);
9
- if (!w(t.url))
1
+ import y from "node:http";
2
+ import R from "node:https";
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-BLZ49ejb.js";
4
+ import { v as z } from "./version-BLZ49ejb.js";
5
+ import { f as T } from "./request.fetch-DlebtNMD.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
+ const t = await g(o, a, u);
10
+ if (!x(t.url))
10
11
  return {
11
12
  url: t.url,
12
13
  method: t.method,
@@ -15,12 +16,12 @@ const U = async function(s, a, u) {
15
16
  headers: {},
16
17
  body: ""
17
18
  };
18
- const p = /^https:\/\//i.test(t.url) ? y : l, r = new URL(t.url), d = t.params;
19
- d instanceof Object && Object.keys(d).forEach((n) => r.searchParams.set(n, d[n]));
20
- const f = t.method === "HEAD";
21
- return new Promise((n) => {
22
- const c = p.request(
23
- r,
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
+ const b = t.method === "HEAD";
22
+ return new Promise((r) => {
23
+ const c = f.request(
24
+ n,
24
25
  {
25
26
  headers: t.headers,
26
27
  method: t.method,
@@ -29,31 +30,32 @@ const U = async function(s, a, u) {
29
30
  function(e) {
30
31
  const h = [];
31
32
  e.on("data", (i) => h.push(i)), e.on("end", () => {
32
- const i = x(
33
- Object.entries(e.headers).map(([b, m]) => [b.toLowerCase(), Array.isArray(m) ? m.join(",") : m])
33
+ const i = S(
34
+ Object.entries(e.headers).map(([l, m]) => [l.toLowerCase(), Array.isArray(m) ? m.join(",") : m])
34
35
  );
35
- n({
36
- url: r.toString(),
36
+ r({
37
+ url: n.toString(),
37
38
  method: t.method,
38
39
  status: e.statusCode || -1,
39
40
  statusText: e.statusMessage || "Unknown",
40
41
  headers: i,
41
- body: f ? "" : Buffer.concat(h).toString("utf-8")
42
+ body: b ? "" : Buffer.concat(h).toString("utf-8")
42
43
  });
43
44
  });
44
45
  }
45
46
  );
46
47
  c.on("error", (e) => {
47
- n({
48
- url: r.toString(),
48
+ r({
49
+ url: n.toString(),
49
50
  method: t.method,
50
51
  status: -1,
51
52
  statusText: e.name || "Unknown",
52
- body: e.message
53
+ body: e.message,
54
+ rawError: e
53
55
  });
54
56
  }), c.on("timeout", () => {
55
- n({
56
- url: r.toString(),
57
+ r({
58
+ url: n.toString(),
57
59
  method: t.method,
58
60
  status: -1,
59
61
  statusText: "Timeout",
@@ -62,18 +64,18 @@ const U = async function(s, a, u) {
62
64
  }), t.body && c.write(t.body), c.end();
63
65
  });
64
66
  };
65
- function C(s) {
66
- return new T(U, s);
67
+ function C(o) {
68
+ return E.fetch ? new p(T, o) : new p(U, o);
67
69
  }
68
- const o = C(), O = o.setConfig, S = o.head, A = o.get, H = o.post, k = o.del, D = o.put, P = o.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;
69
71
  export {
70
72
  C as NetRequest,
71
- k as del,
72
- A as get,
73
- S as head,
74
- P as patch,
75
- H as post,
76
- D as put,
77
- O as setGlobalConfig,
78
- G as version
73
+ B as del,
74
+ D as get,
75
+ k as head,
76
+ G as patch,
77
+ P as post,
78
+ F as put,
79
+ H as setGlobalConfig,
80
+ z as version
79
81
  };
@@ -0,0 +1 @@
1
+ "use strict";const s=require("./version-BZ8fy-0W.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;
@@ -0,0 +1,40 @@
1
+ import { h, r as b, c as m, S as l, f } from "./version-BLZ49ejb.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
+ i instanceof Object && Object.keys(i).forEach((a) => n.searchParams.set(a, i[a]));
7
+ const e = l.AbortController ? new AbortController() : null;
8
+ function d() {
9
+ e && !e.signal.aborted && e.abort();
10
+ }
11
+ t.abort && t.abort.addEventListener("abort", d);
12
+ const u = t.timeout > 0 ? setTimeout(d, t.timeout) : null, c = new Request(n, {
13
+ method: t.method,
14
+ headers: Object.keys(t.headers).length > 0 ? new Headers(t.headers) : void 0,
15
+ body: t.body,
16
+ credentials: t.credentials,
17
+ signal: e == null ? void 0 : e.signal,
18
+ redirect: "follow"
19
+ });
20
+ return await fetch(c).then(async (a) => ({
21
+ url: n.toString(),
22
+ method: t.method,
23
+ status: a.status,
24
+ statusText: a.statusText,
25
+ headers: f([...a.headers.entries()]),
26
+ body: t.method === "HEAD" ? "" : await a.text()
27
+ })).catch((a) => ({
28
+ url: n.toString(),
29
+ method: t.method,
30
+ status: -1,
31
+ statusText: e != null && e.signal.aborted ? "Aborted" : "NetworkError",
32
+ body: String(a),
33
+ rawError: e != null && e.signal.aborted ? void 0 : a
34
+ })).finally(() => {
35
+ u !== null && clearTimeout(u), t.abort && t.abort.removeEventListener("abort", d);
36
+ });
37
+ };
38
+ export {
39
+ g as f
40
+ };