@seayoo-web/request 1.5.3 → 1.5.4
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/dist/index.cjs +2 -2
- package/dist/index.d.ts +1 -0
- package/dist/index.js +16 -15
- package/dist/node.cjs +1 -1
- package/dist/node.d.ts +1 -0
- package/dist/node.js +12 -10
- package/dist/{main-BOUibI7F.cjs → version-B09aAqk-.cjs} +2 -2
- package/dist/{main-DuhwX_wU.js → version-BXzKbsH3.js} +19 -17
- package/dist/version.d.ts +1 -0
- package/dist/wx.cjs +1 -1
- package/dist/wx.d.ts +1 -0
- package/dist/wx.js +16 -14
- package/package.json +3 -3
package/dist/index.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("./
|
|
2
|
-
`).forEach(e=>{const r=e.trim();if(!r)return;const i=r.split(":"),a=i[0].trim();a&&(o[a]=(i[1]||"").trim())}),o}const q=async function(n,o,t){return c.handleResponse(await c.retryRequest(H,n,o,t),n,o,t)},E=async function(n,o,t){return c.handleResponse(await c.retryRequest(m,n,o,t),n,o,t)};async function x(n,o,t,e){const r=new FormData,i=t==null?void 0:t.body,a={...o};i instanceof Object&&Object.entries(i).forEach(([f,s])=>{s instanceof Blob?a[f]=s:Array.isArray(s)?s.forEach((w,h)=>{r.append(`${f}[${h}]`,String(w))}):r.append(f,String(s))});for(const f in a)r.append(f,a[f]);const l=new c.RequestGlobalConfig(e),b=await m(n,l,{...t,method:"POST",body:r});return c.handleResponse(b,n,l,t)}async function
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const c=require("./version-B09aAqk-.cjs"),H=async function(n,o,t){const e=await c.convertOptions(n,o,t),r=new URL(e.url),i=e.params;i instanceof Object&&Object.keys(i).forEach(s=>r.searchParams.set(s,i[s]));const a=new AbortController;function l(){a.signal.aborted||a.abort()}e.abort&&e.abort.addEventListener("abort",l);const b=e.timeout>0?setTimeout(l,e.timeout):null,f=new Request(r,{method:e.method,headers:new Headers(e.headers),body:e.body,credentials:e.credentials,signal:a.signal,redirect:"follow"});return await fetch(f).then(async s=>({url:r.toString(),method:e.method,status:s.status,statusText:s.statusText,headers:Object.fromEntries(s.headers.entries()),body:e.method==="HEAD"?"":await s.text()})).catch(s=>({url:r.toString(),method:e.method,status:-1,statusText:"NetworkError",body:String(s)})).finally(()=>{b!==null&&clearTimeout(b),e.abort&&e.abort.removeEventListener("abort",l)})},m=async function(n,o,t){const e={...await c.convertOptions(n,o,t),onUploadProgress:t==null?void 0:t.onUploadProgress},r=e.method,i=e.onUploadProgress||c.Ot,a=c.wt(e.url,e.params);return await new Promise(l=>{let b=1;function f(g){b=g.total,i({total:g.total,loaded:g.loaded})}let s=null,w=!1;const h=function(){w||(d.abort(),w=!0)};function p(){s!==null&&clearTimeout(s),e.abort&&e.abort.removeEventListener("abort",h)}const d=new XMLHttpRequest;d.open(r,a,!0),d.upload.addEventListener("progress",f),d.addEventListener("load",()=>{p(),i({loaded:b,total:b}),l({url:a,method:r,status:d.status,statusText:d.statusText,headers:T(d),body:r==="HEAD"?"":d.responseText})}),d.addEventListener("error",()=>{p(),l({url:a,method:r,status:-1,statusText:"Failed",body:""})}),d.addEventListener("abort",()=>{p(),l({url:a,method:r,status:-1,statusText:"Aborted",body:""})}),Object.entries(e.headers).forEach(([g,R])=>{d.setRequestHeader(g,R)}),e.credentials==="include"&&(d.withCredentials=!0),d.send(e.body||void 0),e.abort&&e.abort.addEventListener("abort",h),e.timeout>0&&(s=setTimeout(h,e.timeout))})};function T(n){const o={};if(!n)return o;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(":"),a=i[0].trim();a&&(o[a]=(i[1]||"").trim())}),o}const q=async function(n,o,t){return c.handleResponse(await c.retryRequest(H,n,o,t),n,o,t)},E=async function(n,o,t){return c.handleResponse(await c.retryRequest(m,n,o,t),n,o,t)};async function x(n,o,t,e){const r=new FormData,i=t==null?void 0:t.body,a={...o};i instanceof Object&&Object.entries(i).forEach(([f,s])=>{s instanceof Blob?a[f]=s:Array.isArray(s)?s.forEach((w,h)=>{r.append(`${f}[${h}]`,String(w))}):r.append(f,String(s))});for(const f in a)r.append(f,a[f]);const l=new c.RequestGlobalConfig(e),b=await m(n,l,{...t,method:"POST",body:r});return c.handleResponse(b,n,l,t)}async function v(n,o,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.wt(n,t,!0);return new Promise(a=>{e[r]=function(l){if(r in window&&delete e[r],o(l))return l;console.warn("response type check faild",n,l),a(null)},c.sn(i).catch(function(){a(null),delete e[r]})})}async function j(n,o,t={}){const e=window;return"var"in t||(t.var="jsonxData"+Math.random().toString(16).slice(2)),n?await c.sn(c.wt(n,t,!0)).then(()=>{const r=e[t.var+""];return o(r)?r:(console.warn("response type check faild",n,r),null)}).catch(()=>null):null}const L=async function(n,o,t){return await x(n,o,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 y(n){if("window"in globalThis)return"fetch"in window?new c.NetRequestHandler(q,n):new c.NetRequestHandler(E,n);throw new Error("Default Module Only Support In Browser")}const u=y(),S=u.setConfig,C=u.head,O=u.get,U=u.post,D=u.del,k=u.put,P=u.patch;exports.getResponseRulesDescription=c.getResponseRulesDescription;exports.version=c.version;exports.NetRequest=y;exports.del=D;exports.get=O;exports.head=C;exports.jsonp=v;exports.jsonx=j;exports.patch=P;exports.post=U;exports.put=k;exports.setGlobalConfig=S;exports.upload=L;
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { xhrUpload } from "./inc/request.browser";
|
|
2
2
|
import { NetRequestHandler } from "./inc/main";
|
|
3
3
|
import type { IRequestGlobalConfig } from "./inc/type";
|
|
4
|
+
export * from "./version";
|
|
4
5
|
export { jsonp, jsonx } from "./inc/jsonp";
|
|
5
6
|
export { getResponseRulesDescription } from "./inc/rule";
|
|
6
7
|
export type { IRequestOptions, IRequestGlobalConfig, IResponseRule, IResponseResult, IRequestLog } from "./inc/type";
|
package/dist/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { c as R, w as m, O as q, h as p, r as E, R as L, s as H, N as y } from "./
|
|
2
|
-
import { g as J } from "./
|
|
3
|
-
const
|
|
1
|
+
import { c as R, w as m, O as q, h as p, r as E, R as L, s as H, N as y } from "./version-BXzKbsH3.js";
|
|
2
|
+
import { g as J, v as K } from "./version-BXzKbsH3.js";
|
|
3
|
+
const v = async function(n, o, e) {
|
|
4
4
|
const t = await R(n, o, e), r = new URL(t.url), c = t.params;
|
|
5
5
|
c instanceof Object && Object.keys(c).forEach((a) => r.searchParams.set(a, c[a]));
|
|
6
6
|
const s = new AbortController();
|
|
@@ -56,7 +56,7 @@ const C = async function(n, o, e) {
|
|
|
56
56
|
method: r,
|
|
57
57
|
status: l.status,
|
|
58
58
|
statusText: l.statusText,
|
|
59
|
-
headers:
|
|
59
|
+
headers: C(l),
|
|
60
60
|
body: r === "HEAD" ? "" : l.responseText
|
|
61
61
|
});
|
|
62
62
|
}), l.addEventListener("error", () => {
|
|
@@ -80,7 +80,7 @@ const C = async function(n, o, e) {
|
|
|
80
80
|
}), t.credentials === "include" && (l.withCredentials = !0), l.send(t.body || void 0), t.abort && t.abort.addEventListener("abort", b), t.timeout > 0 && (a = setTimeout(b, t.timeout));
|
|
81
81
|
});
|
|
82
82
|
};
|
|
83
|
-
function
|
|
83
|
+
function C(n) {
|
|
84
84
|
const o = {};
|
|
85
85
|
if (!n)
|
|
86
86
|
return o;
|
|
@@ -94,12 +94,12 @@ function O(n) {
|
|
|
94
94
|
s && (o[s] = (c[1] || "").trim());
|
|
95
95
|
}), o;
|
|
96
96
|
}
|
|
97
|
-
const
|
|
98
|
-
return p(await E(
|
|
99
|
-
},
|
|
97
|
+
const O = async function(n, o, e) {
|
|
98
|
+
return p(await E(v, n, o, e), n, o, e);
|
|
99
|
+
}, S = async function(n, o, e) {
|
|
100
100
|
return p(await E(T, n, o, e), n, o, e);
|
|
101
101
|
};
|
|
102
|
-
async function
|
|
102
|
+
async function j(n, o, e, t) {
|
|
103
103
|
const r = new FormData(), c = e == null ? void 0 : e.body, s = { ...o };
|
|
104
104
|
c instanceof Object && Object.entries(c).forEach(([u, a]) => {
|
|
105
105
|
a instanceof Blob ? s[u] = a : Array.isArray(a) ? a.forEach((g, b) => {
|
|
@@ -140,7 +140,7 @@ async function A(n, o, e = {}) {
|
|
|
140
140
|
}).catch(() => null) : null;
|
|
141
141
|
}
|
|
142
142
|
const P = async function(n, o, e) {
|
|
143
|
-
return await
|
|
143
|
+
return await j(n, o, e, {
|
|
144
144
|
baseURL: d.getConfig("baseURL"),
|
|
145
145
|
logHandler: d.getConfig("logHandler"),
|
|
146
146
|
errorHandler: d.getConfig("errorHandler"),
|
|
@@ -149,14 +149,14 @@ const P = async function(n, o, e) {
|
|
|
149
149
|
responseHandler: d.getConfig("responseHandler")
|
|
150
150
|
});
|
|
151
151
|
};
|
|
152
|
-
function
|
|
152
|
+
function U(n) {
|
|
153
153
|
if ("window" in globalThis)
|
|
154
|
-
return "fetch" in window ? new y(
|
|
154
|
+
return "fetch" in window ? new y(O, n) : new y(S, n);
|
|
155
155
|
throw new Error("Default Module Only Support In Browser");
|
|
156
156
|
}
|
|
157
|
-
const d =
|
|
157
|
+
const d = U(), M = d.setConfig, N = d.head, F = d.get, X = d.post, B = d.del, G = d.put, I = d.patch;
|
|
158
158
|
export {
|
|
159
|
-
|
|
159
|
+
U as NetRequest,
|
|
160
160
|
B as del,
|
|
161
161
|
F as get,
|
|
162
162
|
J as getResponseRulesDescription,
|
|
@@ -167,5 +167,6 @@ export {
|
|
|
167
167
|
X as post,
|
|
168
168
|
G as put,
|
|
169
169
|
M as setGlobalConfig,
|
|
170
|
-
P as upload
|
|
170
|
+
P as upload,
|
|
171
|
+
K as version
|
|
171
172
|
};
|
package/dist/node.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const r=require("./version-B09aAqk-.cjs"),p={},R=async function(s,u,c){return r.handleResponse(await r.retryRequest(q,s,u,c),s,u,c)},q=async function(s,u,c){const t=await r.convertOptions(s,u,c);if(!r.R(t.url))return{url:t.url,method:t.method,status:-1,statusText:"URLFormatError",headers:{},body:""};const f=(/^https:\/\//i.test(t.url),p),a=new URL(t.url),i=t.params;i instanceof Object&&Object.keys(i).forEach(n=>a.searchParams.set(n,i[n]));const g=t.method==="HEAD";return new Promise(n=>{const d=f.request(a,{headers:t.headers,method:t.method,timeout:t.timeout>0?t.timeout:void 0},function(e){const l=[];e.on("data",h=>l.push(h)),e.on("end",()=>{const h=Object.fromEntries(Object.entries(e.headers).map(([y,m])=>[y.toLowerCase(),Array.isArray(m)?m.join(","):m]));n({url:a.toString(),method:t.method,status:e.statusCode||-1,statusText:e.statusMessage||"Unknown",headers:h,body:g?"":Buffer.concat(l).toString("utf-8")})})});d.on("error",e=>{n({url:a.toString(),method:t.method,status:-1,statusText:e.name||"Unknown",body:e.message})}),d.on("timeout",()=>{n({url:a.toString(),method:t.method,status:-1,statusText:"Timeout",body:""})}),t.body&&d.write(t.body),d.end()})};function b(s){return new r.NetRequestHandler(R,s)}const o=b(),w=o.setConfig,j=o.head,O=o.get,S=o.post,T=o.del,C=o.put,U=o.patch;exports.version=r.version;exports.NetRequest=b;exports.del=T;exports.get=O;exports.head=j;exports.patch=U;exports.post=S;exports.put=C;exports.setGlobalConfig=w;
|
package/dist/node.d.ts
CHANGED
package/dist/node.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
import { h as y, r as R, c as g,
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { h as y, r as R, c as g, a as q, N as w } from "./version-BXzKbsH3.js";
|
|
2
|
+
import { v as P } from "./version-BXzKbsH3.js";
|
|
3
|
+
const p = {}, x = async function(s, a, u) {
|
|
4
|
+
return y(await R(j, s, a, u), s, a, u);
|
|
5
|
+
}, j = async function(s, a, u) {
|
|
5
6
|
const t = await g(s, a, u);
|
|
6
7
|
if (!q(t.url))
|
|
7
8
|
return {
|
|
@@ -24,10 +25,10 @@ const p = {}, j = async function(s, a, u) {
|
|
|
24
25
|
timeout: t.timeout > 0 ? t.timeout : void 0
|
|
25
26
|
},
|
|
26
27
|
function(e) {
|
|
27
|
-
const
|
|
28
|
-
e.on("data", (i) =>
|
|
28
|
+
const h = [];
|
|
29
|
+
e.on("data", (i) => h.push(i)), e.on("end", () => {
|
|
29
30
|
const i = Object.fromEntries(
|
|
30
|
-
Object.entries(e.headers).map(([l,
|
|
31
|
+
Object.entries(e.headers).map(([l, m]) => [l.toLowerCase(), Array.isArray(m) ? m.join(",") : m])
|
|
31
32
|
);
|
|
32
33
|
n({
|
|
33
34
|
url: r.toString(),
|
|
@@ -35,7 +36,7 @@ const p = {}, j = async function(s, a, u) {
|
|
|
35
36
|
status: e.statusCode || -1,
|
|
36
37
|
statusText: e.statusMessage || "Unknown",
|
|
37
38
|
headers: i,
|
|
38
|
-
body: b ? "" : Buffer.concat(
|
|
39
|
+
body: b ? "" : Buffer.concat(h).toString("utf-8")
|
|
39
40
|
});
|
|
40
41
|
});
|
|
41
42
|
}
|
|
@@ -60,7 +61,7 @@ const p = {}, j = async function(s, a, u) {
|
|
|
60
61
|
});
|
|
61
62
|
};
|
|
62
63
|
function O(s) {
|
|
63
|
-
return new w(
|
|
64
|
+
return new w(x, s);
|
|
64
65
|
}
|
|
65
66
|
const o = O(), U = o.setConfig, C = o.head, E = o.get, L = o.post, N = o.del, S = o.put, A = o.patch;
|
|
66
67
|
export {
|
|
@@ -71,5 +72,6 @@ export {
|
|
|
71
72
|
A as patch,
|
|
72
73
|
L as post,
|
|
73
74
|
S as put,
|
|
74
|
-
U as setGlobalConfig
|
|
75
|
+
U as setGlobalConfig,
|
|
76
|
+
P as version
|
|
75
77
|
};
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
"use strict";var
|
|
2
|
-
`)};function
|
|
1
|
+
"use strict";var L=Object.defineProperty;var U=(t,e,n)=>e in t?L(t,e,{enumerable:!0,configurable:!0,writable:!0,value:n}):t[e]=n;var f=(t,e,n)=>(U(t,typeof e!="symbol"?e+"":e,n),n);async function x(t,e,n){var b;const s=Object.assign({method:"GET"},n),r="FormData"in globalThis?s.body instanceof FormData:!1,a=r&&s.method!=="POST"&&s.method!=="PUT"?"POST":s.method;(a==="GET"||a==="HEAD"||a==="DELETE")&&s.body!==void 0&&(console.warn("request body is invalid with method get, head, delete"),delete s.body);const o=Object.assign(r?{}:{"Content-Type":"Blob"in globalThis&&s.body instanceof Blob?s.body.type||"application/octet-stream":"application/json;charset=utf-8"},s.headers),i=s.params||{},c={};Object.keys(i).forEach(h=>{i[h]!==void 0&&(c[h]=H(i[h]))});const d=e.getFullUrl(t),l=P(s.body),u=s.timeout||e.get("timeout"),g=await async function(){var O;const h=e.get("requestTransformer");return h?await h({headers:o,params:c,method:a,url:d,body:l}):await((O=e.get("requestHandler"))==null?void 0:O(o,c,a,t))}(),m=typeof g=="string"&&g?g:d;return(b=e.get("logHandler"))==null||b({type:"ready",url:m,method:a,headers:o,timeout:u,body:l}),{url:m,method:a,body:l,params:c,headers:o,timeout:u,abort:s.abort,credentials:s.credentials||e.get("credentials")}}function H(t){return typeof t=="string"?t:Array.isArray(t)?t.join(","):t+""}function P(t){if(t)return typeof t=="string"||t instanceof URLSearchParams||t instanceof ArrayBuffer||"Blob"in globalThis&&t instanceof Blob||"FormData"in globalThis&&t instanceof FormData?t:JSON.stringify(t)}function B(t){return new Promise(e=>setTimeout(e,Math.max(0,t)))}function k(){}function R(t,e){try{const n=JSON.parse(t);return e?e(n)?n:null:n}catch{return null}}""+Math.random().toString(32).slice(2);const q=/^(?:https?:)?\/\/.+$/i,K=/^https?:\/\/.+$/i,S=/^\{[\d\D]*\}$/,I=/^\[[\d\D]*\]$/;function p(t,e=!1){return e?q.test(t):K.test(t)}function N(t){return t==null}function M(t,...e){if(!t||typeof t!="object")return!1;const n=Object.getPrototypeOf(t);return n!==Object.prototype&&n!==null?!1:e.every(s=>s in t)}function E(t){return S.test(t)||I.test(t)}async function G(t){return await new Promise(function(e){const n=document.getElementsByTagName("head")[0],s=document.createElement("script");s.setAttribute("type","text/javascript"),s.setAttribute("charset","utf-8"),s.onload=function(){n.removeChild(s),e(!0)},s.onerror=function(){n.removeChild(s),e(!1)},s.setAttribute("src",t),n.appendChild(s)})}function J(t,e="数据未能正确识别"){return typeof t=="function"?{guard:t,message:e}:{guard:t.guard,message:t.message||e}}function $(t,e=""){return!e||p(t,!0)?v(t):(v(e)+"/"+t).replace(/\/{2,}/g,"/").replace(/:\//,"://")}function v(t){return p(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 _(t,e){if(e){if(e===!0)return t.replace(/\?[^#]*/,"")}else return t;const n=t.split("#"),s=n[0].split("?"),r=s[0],a=s.length>1?s[1]:"",o=n.length>1?"#"+n[1]:"",i=typeof e=="string"?[e]:Array.isArray(e)?e:[];return!i.length||!a?n[0]+o:(i.map(c=>c.replace(/([\\(){}[\]^$+\-*?|])/g,"\\$1")),(r+"?"+a.replace(new RegExp("(?:^|&)(?:"+i.join("|")+")=[^&$]+","g"),"").replace(/^&/,"")).replace(/\?$/,"")+o)}function V(t,e,n=!1){const s=typeof e=="string"?e:Object.keys(e).map(o=>`${o}=${encodeURIComponent(e[o])}`).join("&");if(!s)return t;const r=t.split("#");n&&(r[0]=_(r[0],(s.match(/([^=&#?]+)=[^&#]+/g)||[]).map(o=>o.replace(/=.+$/,""))));const a=r[0].indexOf("?")+1?"&":"?";return(r[0]+a+s+(r.length>1?"#"+r[1]:"")).replace(/\?&/,"?")}const C="data",y="message";function W(t,e,n,s,r){const a=r||s;return j(t)?te(a.ok||s.ok,t,e,n):Y(a.failed||s.failed,e,n)}const X=function(t){const e=[],n=t.failed||{resolve:"json"};switch(e.push("- 当http状态码 <200 或者 >=400 时"),n.resolve){case"body":e.push(" 将响应内容格式化为字符串并作为错误消息");break;case"json":e.push(" 将响应解析为json,并读取 "+(n.messageField||y)+" 作为错误消息");break}const s=t.ok||{resolve:"body"};switch(e.push("- 当http状态码 >=200 并且 <400 时"),s.resolve){case"body":e.push(" 将响应尝试解析为 json,并作为数据内容返回");break;case"json":e.push(" 将响应解析为 json,读取 "+(s.dataField||C)+" 作为响应数据,读取 "+(s.messageField||y)+" 作为提示消息"),s.statusField&&e.push(" 当 "+s.statusField+" 为 "+(s.statusOKValue||"空值")+" 时是成功提示,否则是错误消息"),s.ignoreMessage&&e.push(" 并忽略以下消息:"+s.ignoreMessage);break}return e.join(`
|
|
2
|
+
`)};function Y(t,e,n){const s=t||{resolve:"json",messageField:y},r={ok:!1,code:e,message:n,data:null};switch(s.resolve){case"body":r.message=F(n)||n;break;case"json":const{code:a,message:o}=z(n,s.statusField,s.messageField);r.code=a||e,r.message=F(n)||o;break}return r}function z(t,e,n=y){if(!E(t))return{message:""};const s=R(t);return!s||!M(s)?{message:t}:{code:e?w(s,e):"",message:w(s,n)||t}}function w(t,e){const n=Array.isArray(e)?e:[e];for(const s of n)if(s in t)return Q(t[s]);return""}function Q(t){return t?typeof t=="string"?t:JSON.stringify(t):""}const Z=/<title>([^<]+)<\/title>/i,ee=/<message>([^<]+)<\/message>/i;function F(t){const e=t.match(Z);if(e)return e[1];const n=t.match(ee);return n?n[1]:""}function te(t,e,n,s){const r=t||{resolve:"body"},a={ok:!0,code:n,message:"",data:null};if(e===202||e===204||!s)return a;if(r.resolve==="body")return a.data=E(s)?R(s):s,a;const o=R(s);if(!o||!M(o))return a.ok=!1,a.code="ResponseFormatError",a.message="响应内容无法格式化为 Object",a;const i=r.statusField,c=r.statusOKValue||"",d=r.dataField||C,l=r.messageField||y,u=r.ignoreMessage||"";if(i&&!(i in o))return a.ok=!1,a.code="ResponseFieldMissing",a.message="响应内容找不到状态字段 "+i,a;const g=i?o[i]+"":"";return a.ok=i?g===c:!0,a.code=g||n,a.data=d===!0?o:d in o?o[d]:null,a.message=w(o,l),u&&a.message&&(Array.isArray(u)&&u.includes(a.message)||typeof u=="string"&&a.message===u)&&(a.message=""),a}function j(t){return t>=200&&t<400}async function D(t,e,n,s,r){const a=r||0,o=Math.max(0,Math.min(10,(s==null?void 0:s.maxRetry)??n.get("maxRetry")??0)),i=(s==null?void 0:s.retryResolve)??n.get("retryResolve"),c=n.get("logHandler")||k;c({type:"prepear",url:e,method:(s==null?void 0:s.method)||"GET",retry:a,maxRetry:o,message:a===0?"start":`retry ${a}/${o} start`,headers:s==null?void 0:s.headers,options:s});const d=Date.now(),l=await t(e,n,s),u=l.status,g=Date.now()-d,m=`[cost ${g}][${u}] ${u<0?l.body:""}`;c({type:"finished",url:e,method:l.method,retry:a,maxRetry:o,message:a===0?`finish ${m}`:`retry ${a}/${o} finish ${m}`,response:l,headers:l.headers,cost:g});const b=j(u);if(!o||i==="network"&&u>0||i==="status"&&b||Array.isArray(i)&&!i.includes(u)||typeof i=="function"&&i(l,a)!==!0||a>=o)return l;const h=(s==null?void 0:s.retryInterval)??n.get("retryInterval")??1e3;return await B(Math.max(100,h==="2EB"?Math.pow(2,a)*1e3:typeof h=="function"?h(a+1)||0:h)),await D(t,e,n,s,a+1)}function se(t,e,n,s){var o,i;if(t.status<0)return T({ok:!1,status:t.status,code:t.statusText,headers:{},message:"",data:null},`${t.method} ${e} ${t.statusText}`,t.method,e,n,s);j(t.status)||(o=n.get("errorHandler"))==null||o(t.status,t.method,e);const r={...W(t.status,t.statusText,t.body,n.get("responseRule"),s==null?void 0:s.responseRule),status:t.status,headers:Object.fromEntries(Object.entries(t.headers||{}).map(([c,d])=>[c.toLowerCase(),d]))};(i=n.get("responseHandler"))==null||i({...r},t.method,e);const a=r.ok?r.message:`${t.method} ${e} [${r.code||t.statusText}] ${r.message||t.statusText}`;return T(r,a,t.method,e,n,s)}function T(t,e,n,s,r,a){const o=r.get("message"),i=o===!1||(a==null?void 0:a.message)===!1?!1:(a==null?void 0:a.message)||o;if(i!==!1){const c=typeof i=="function"?i(t,n,s,e):e;c instanceof Error?r.showMessage(!0,c.message):c&&typeof c=="object"&&"message"in c?r.showMessage(!1,c.message):r.showMessage(!t.ok,c)}return t}class A{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)&&!p(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("/")?$(e):$(e,this.config.baseURL)}showMessage(e,n){this.config.messageHandler&&n&&this.config.messageHandler(e,n)}}class ne{constructor(e=500){f(this,"ttl");f(this,"cache");this.cache={},this.ttl=Math.max(e,0)}getKey(e,n){const s=Object.keys(n||{}).sort().map(r=>`${r}#${n==null?void 0:n[r]}`);return e+s.join(",")}updateTTL(e){this.ttl=Math.max(e,0)}get(e){if(this.ttl===0)return null;const n=this.cache[e];return n?n.ttl<Date.now()?(delete this.cache[e],null):n.res:null}set(e,n){this.ttl!==0&&(this.cache[e]={ttl:Date.now()+this.ttl,res:n})}}function ae(t,e,n,s){if(e.ok&&!N(e.data)&&s){const r=J(s,"响应数据未能正确识别");return r.guard(e.data)||(console.error("ResponseCheckFaild",t,e.data),n.showMessage(!0,`${t} ${r.message}`),e.data=null),e}return e}class re{constructor(e,n){f(this,"agent");f(this,"config");f(this,"cache");this.config=new A(n),this.agent=e,this.cache=new ne(this.config.get("cacheTTL")),this.setConfig=this.setConfig.bind(this),this.getConfig=this.getConfig.bind(this),this.get=this.get.bind(this),this.post=this.post.bind(this),this.del=this.del.bind(this),this.patch=this.patch.bind(this),this.put=this.put.bind(this),this.head=this.head.bind(this)}async exec(e,n){return await this.agent(e,this.config,n)}async guard(e,n,s){return ae(e,n,this.config,s)}setConfig(e){this.config.set(e),this.cache.updateTTL(this.config.get("cacheTTL"))}getConfig(e){return this.config.get(e)}async head(e,n){const s=Object.assign({},n||null);return s.method="HEAD",this.guard(e,await this.exec(e,s),null)}async get(e,n,s){const r=Object.assign({},s||null);r.method="GET";const a=this.cache.getKey(e,r.params),o=this.cache.get(a);if(o)return this.guard(e,await o,n||null);const i=this.exec(e,r);return this.cache.set(a,i),this.guard(e,await i,n||null)}async post(e,n,s,r){const a=Object.assign({},r||null);return a.method="POST",a.body=n,this.guard(e,await this.exec(e,a),s||null)}async del(e,n,s){const r=Object.assign({},s||null);return r.method="DELETE",this.guard(e,await this.exec(e,r),n||null)}async put(e,n,s,r){const a=Object.assign({},r||null);return a.method="PUT",a.body=n,this.guard(e,await this.exec(e,a),s||null)}async patch(e,n,s,r){const a=Object.assign({},r||null);return a.method="PATCH",a.body=n,this.guard(e,await this.exec(e,a),s||null)}}const oe="1.5.4";exports.NetRequestHandler=re;exports.Ot=k;exports.R=p;exports.RequestGlobalConfig=A;exports.convertOptions=x;exports.getResponseRulesDescription=X;exports.handleResponse=se;exports.retryRequest=D;exports.sn=G;exports.version=oe;exports.wt=V;
|
|
@@ -12,9 +12,9 @@ async function te(t, e, a) {
|
|
|
12
12
|
s.headers
|
|
13
13
|
), i = s.params || {}, c = {};
|
|
14
14
|
Object.keys(i).forEach((h) => {
|
|
15
|
-
i[h] !== void 0 && (c[h] =
|
|
15
|
+
i[h] !== void 0 && (c[h] = A(i[h]));
|
|
16
16
|
});
|
|
17
|
-
const d = e.getFullUrl(t), l =
|
|
17
|
+
const d = e.getFullUrl(t), l = L(s.body), u = s.timeout || e.get("timeout"), g = await async function() {
|
|
18
18
|
var O;
|
|
19
19
|
const h = e.get("requestTransformer");
|
|
20
20
|
return h ? await h({ headers: o, params: c, method: n, url: d, body: l }) : await ((O = e.get("requestHandler")) == null ? void 0 : O(o, c, n, t));
|
|
@@ -30,10 +30,10 @@ async function te(t, e, a) {
|
|
|
30
30
|
credentials: s.credentials || e.get("credentials")
|
|
31
31
|
};
|
|
32
32
|
}
|
|
33
|
-
function
|
|
33
|
+
function A(t) {
|
|
34
34
|
return typeof t == "string" ? t : Array.isArray(t) ? t.join(",") : t + "";
|
|
35
35
|
}
|
|
36
|
-
function
|
|
36
|
+
function L(t) {
|
|
37
37
|
if (t)
|
|
38
38
|
return typeof t == "string" || t instanceof URLSearchParams || t instanceof ArrayBuffer || "Blob" in globalThis && t instanceof Blob || "FormData" in globalThis && t instanceof FormData ? t : JSON.stringify(t);
|
|
39
39
|
}
|
|
@@ -87,9 +87,9 @@ function I(t, e = "数据未能正确识别") {
|
|
|
87
87
|
};
|
|
88
88
|
}
|
|
89
89
|
function $(t, e = "") {
|
|
90
|
-
return !e || R(t, !0) ?
|
|
90
|
+
return !e || R(t, !0) ? v(t) : (v(e) + "/" + t).replace(/\/{2,}/g, "/").replace(/:\//, "://");
|
|
91
91
|
}
|
|
92
|
-
function
|
|
92
|
+
function v(t) {
|
|
93
93
|
return R(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(/^\/+/, "");
|
|
94
94
|
}
|
|
95
95
|
function N(t, e) {
|
|
@@ -151,11 +151,11 @@ function G(t, e, a) {
|
|
|
151
151
|
};
|
|
152
152
|
switch (s.resolve) {
|
|
153
153
|
case "body":
|
|
154
|
-
r.message =
|
|
154
|
+
r.message = F(a) || a;
|
|
155
155
|
break;
|
|
156
156
|
case "json":
|
|
157
157
|
const { code: n, message: o } = J(a, s.statusField, s.messageField);
|
|
158
|
-
r.code = n || e, r.message =
|
|
158
|
+
r.code = n || e, r.message = F(a) || o;
|
|
159
159
|
break;
|
|
160
160
|
}
|
|
161
161
|
return r;
|
|
@@ -180,7 +180,7 @@ function _(t) {
|
|
|
180
180
|
return t ? typeof t == "string" ? t : JSON.stringify(t) : "";
|
|
181
181
|
}
|
|
182
182
|
const V = /<title>([^<]+)<\/title>/i, W = /<message>([^<]+)<\/message>/i;
|
|
183
|
-
function
|
|
183
|
+
function F(t) {
|
|
184
184
|
const e = t.match(V);
|
|
185
185
|
if (e)
|
|
186
186
|
return e[1];
|
|
@@ -210,7 +210,7 @@ function X(t, e, a, s) {
|
|
|
210
210
|
function j(t) {
|
|
211
211
|
return t >= 200 && t < 400;
|
|
212
212
|
}
|
|
213
|
-
async function
|
|
213
|
+
async function Y(t, e, a, s, r) {
|
|
214
214
|
const n = r || 0, o = Math.max(0, Math.min(10, (s == null ? void 0 : s.maxRetry) ?? a.get("maxRetry") ?? 0)), i = (s == null ? void 0 : s.retryResolve) ?? a.get("retryResolve"), c = a.get("logHandler") || x;
|
|
215
215
|
c({
|
|
216
216
|
type: "prepear",
|
|
@@ -243,7 +243,7 @@ async function Q(t, e, a, s, r) {
|
|
|
243
243
|
100,
|
|
244
244
|
h === "2EB" ? Math.pow(2, n) * 1e3 : typeof h == "function" ? h(n + 1) || 0 : h
|
|
245
245
|
)
|
|
246
|
-
), await
|
|
246
|
+
), await Y(t, e, a, s, n + 1);
|
|
247
247
|
}
|
|
248
248
|
function re(t, e, a, s) {
|
|
249
249
|
var o, i;
|
|
@@ -274,7 +274,7 @@ function T(t, e, a, s, r, n) {
|
|
|
274
274
|
}
|
|
275
275
|
return t;
|
|
276
276
|
}
|
|
277
|
-
class
|
|
277
|
+
class z {
|
|
278
278
|
constructor(e) {
|
|
279
279
|
// 保存的配置需要部分字段强制设置默认值
|
|
280
280
|
f(this, "config", {
|
|
@@ -314,7 +314,7 @@ class Y {
|
|
|
314
314
|
this.config.messageHandler && a && this.config.messageHandler(e, a);
|
|
315
315
|
}
|
|
316
316
|
}
|
|
317
|
-
class
|
|
317
|
+
class Q {
|
|
318
318
|
constructor(e = 500) {
|
|
319
319
|
f(this, "ttl");
|
|
320
320
|
f(this, "cache");
|
|
@@ -352,7 +352,7 @@ class oe {
|
|
|
352
352
|
f(this, "agent");
|
|
353
353
|
f(this, "config");
|
|
354
354
|
f(this, "cache");
|
|
355
|
-
this.config = new
|
|
355
|
+
this.config = new z(a), this.agent = e, this.cache = new Q(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);
|
|
356
356
|
}
|
|
357
357
|
/**
|
|
358
358
|
* 执行网络请求
|
|
@@ -411,15 +411,17 @@ class oe {
|
|
|
411
411
|
return n.method = "PATCH", n.body = a, this.guard(e, await this.exec(e, n), s || null);
|
|
412
412
|
}
|
|
413
413
|
}
|
|
414
|
+
const ie = "1.5.4";
|
|
414
415
|
export {
|
|
415
|
-
R as M,
|
|
416
416
|
oe as N,
|
|
417
417
|
x as O,
|
|
418
|
-
|
|
418
|
+
z as R,
|
|
419
|
+
R as a,
|
|
419
420
|
te as c,
|
|
420
421
|
ne as g,
|
|
421
422
|
re as h,
|
|
422
|
-
|
|
423
|
+
Y as r,
|
|
423
424
|
se as s,
|
|
425
|
+
ie as v,
|
|
424
426
|
ae as w
|
|
425
427
|
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const version = "1.5.4";
|
package/dist/wx.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const s=require("./version-B09aAqk-.cjs"),l=async function(t,n,r){return s.handleResponse(await s.retryRequest(f,t,n,r),t,n,r)},f=async function(t,n,r){const e=await s.convertOptions(t,n,r),u=e.method==="PATCH"?"POST":e.method,a=s.wt(e.url,e.params);return globalThis.wx?new Promise(i=>{wx.request({url:a,data:e.body,header:e.headers,method:u,dataType:"string",responseType:"text",fail(){i({url:a,method:u,status:-1,statusText:"NetworkError",body:""})},success(c){i({url:a,method:u,status:c.statusCode,statusText:c.statusCode+"",headers:{...c.header},body:e.method==="HEAD"?"":p(c.data)})}})}):{url:a,method:u,status:-1,statusText:"NotSupport",body:"NotFound namespace of wx"}};function p(t){return typeof t=="string"?t:t instanceof ArrayBuffer&&"TextDecoder"in globalThis?new TextDecoder().decode(t):JSON.stringify(t)}function d(t){return new s.NetRequestHandler(l,t)}const o=d(),g=o.setConfig,h=o.head,y=o.get,T=o.post,b=o.del,w=o.put;exports.version=s.version;exports.NetRequest=d;exports.del=b;exports.get=y;exports.head=h;exports.post=T;exports.put=w;exports.setGlobalConfig=g;
|
package/dist/wx.d.ts
CHANGED
package/dist/wx.js
CHANGED
|
@@ -1,20 +1,21 @@
|
|
|
1
|
-
import { h as d, r as i, c as
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
import { h as d, r as i, c as f, w as l, N as p } from "./version-BXzKbsH3.js";
|
|
2
|
+
import { v as A } from "./version-BXzKbsH3.js";
|
|
3
|
+
const h = async function(t, o, r) {
|
|
4
|
+
return d(await i(x, t, o, r), t, o, r);
|
|
5
|
+
}, x = async function(t, o, r) {
|
|
6
|
+
const e = await f(t, o, r), n = e.method === "PATCH" ? "POST" : e.method, a = l(e.url, e.params);
|
|
6
7
|
return globalThis.wx ? new Promise((c) => {
|
|
7
8
|
wx.request({
|
|
8
9
|
url: a,
|
|
9
10
|
data: e.body,
|
|
10
11
|
header: e.headers,
|
|
11
|
-
method:
|
|
12
|
+
method: n,
|
|
12
13
|
dataType: "string",
|
|
13
14
|
responseType: "text",
|
|
14
15
|
fail() {
|
|
15
16
|
c({
|
|
16
17
|
url: a,
|
|
17
|
-
method:
|
|
18
|
+
method: n,
|
|
18
19
|
status: -1,
|
|
19
20
|
statusText: "NetworkError",
|
|
20
21
|
body: ""
|
|
@@ -23,35 +24,36 @@ const h = async function(t, o, n) {
|
|
|
23
24
|
success(u) {
|
|
24
25
|
c({
|
|
25
26
|
url: a,
|
|
26
|
-
method:
|
|
27
|
+
method: n,
|
|
27
28
|
status: u.statusCode,
|
|
28
29
|
statusText: u.statusCode + "",
|
|
29
30
|
headers: { ...u.header },
|
|
30
|
-
body: e.method === "HEAD" ? "" :
|
|
31
|
+
body: e.method === "HEAD" ? "" : w(u.data)
|
|
31
32
|
});
|
|
32
33
|
}
|
|
33
34
|
});
|
|
34
35
|
}) : {
|
|
35
36
|
url: a,
|
|
36
|
-
method:
|
|
37
|
+
method: n,
|
|
37
38
|
status: -1,
|
|
38
39
|
statusText: "NotSupport",
|
|
39
40
|
body: "NotFound namespace of wx"
|
|
40
41
|
};
|
|
41
42
|
};
|
|
42
|
-
function
|
|
43
|
+
function w(t) {
|
|
43
44
|
return typeof t == "string" ? t : t instanceof ArrayBuffer && "TextDecoder" in globalThis ? new TextDecoder().decode(t) : JSON.stringify(t);
|
|
44
45
|
}
|
|
45
46
|
function y(t) {
|
|
46
47
|
return new p(h, t);
|
|
47
48
|
}
|
|
48
|
-
const s = y(),
|
|
49
|
+
const s = y(), T = s.setConfig, g = s.head, b = s.get, q = s.post, N = s.del, R = s.put;
|
|
49
50
|
export {
|
|
50
51
|
y as NetRequest,
|
|
51
52
|
N as del,
|
|
52
53
|
b as get,
|
|
53
|
-
|
|
54
|
+
g as head,
|
|
54
55
|
q as post,
|
|
55
56
|
R as put,
|
|
56
|
-
|
|
57
|
+
T as setGlobalConfig,
|
|
58
|
+
A as version
|
|
57
59
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@seayoo-web/request",
|
|
3
|
-
"version": "1.5.
|
|
3
|
+
"version": "1.5.4",
|
|
4
4
|
"description": "requst tools for seayoo web",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -42,7 +42,7 @@
|
|
|
42
42
|
"node": ">=18"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
|
-
"build": "vite build && tsc --emitDeclarationOnly",
|
|
45
|
+
"build": "node scripts/emitVersion.mjs && vite build && tsc --emitDeclarationOnly",
|
|
46
46
|
"emit-types": "tsc --emitDeclarationOnly",
|
|
47
47
|
"clear": "rimraf dist && rimraf node_modules",
|
|
48
48
|
"type-check": "tsc --noEmit",
|
|
@@ -82,6 +82,6 @@
|
|
|
82
82
|
"vitest": "^1.3.1"
|
|
83
83
|
},
|
|
84
84
|
"dependencies": {
|
|
85
|
-
"@seayoo-web/utils": "^1.15.
|
|
85
|
+
"@seayoo-web/utils": "^1.15.2"
|
|
86
86
|
}
|
|
87
87
|
}
|