diff --git a/ff-request.js b/ff-request.js index 5bede71..d98430c 100644 --- a/ff-request.js +++ b/ff-request.js @@ -245,7 +245,7 @@ var fe = {}; }); })(fe); const le = /* @__PURE__ */ we(fe); -var $, q, B, D, W, X, z, Z, ee, te, re; +var $, q, j, B, W, X, z, Z, ee, te, re; const ae = class ae { /** * @param {number} code @@ -272,8 +272,8 @@ const ae = class ae { p(this, q, !0); // 判断是否已经给出错误提示 /** @type {boolean} */ - p(this, B, !0); - p(this, D, { + p(this, j, !0); + p(this, B, { /** @param {any} __ @param {string} key */ get: (e, t) => s(this, W).call(this, t), /** @param {any} __ @param {string} key */ @@ -281,7 +281,7 @@ const ae = class ae { throw new TypeError(`HttpResponse 是只读的,不能设置属性 "${t}"`); }, /** @returns {string[]} */ - ownKeys: () => Object.keys(s(this, D).getPrototypeOf() || {}), + ownKeys: () => Object.keys(s(this, B).getPrototypeOf() || {}), /** @returns {HttpResponse | unknown} */ getPrototypeOf: () => s(this, q) ? this : this.data, /** @returns {PropertyDescriptor} */ @@ -289,24 +289,31 @@ const ae = class ae { configurable: !0, enumerable: !0, writable: !0, - value: s(this, D).getPrototypeOf() + value: s(this, B).getPrototypeOf() }) }); /** * @param {string} key * @returns {unknown} */ - p(this, W, (e) => e === "$echoMsg" ? s(this, z) : e === "msg" ? s(this, X) : e === "then" ? (_(this, B, !0), s(this, Z)) : e === "resp" ? (_(this, B, !0), s(this, ee)) : e === "catch" ? s(this, te) : e === "finally" ? s(this, re) : e === "IS_ECHO_MSG" ? s(this, B) : Reflect.get(this, e)); + p(this, W, (e) => e === "$echoMsg" ? s(this, z) : e === "msg" ? s(this, X) : e === "then" ? (_(this, j, !0), s(this, Z)) : e === "resp" ? (_(this, j, !0), s(this, ee)) : e === "catch" ? s(this, te) : e === "finally" ? s(this, re) : e === "IS_ECHO_MSG" ? s(this, j) : Reflect.get(this, e)); /** - * @param {(value: unknown) => unknown} [fn] + * @param {(value: unknown) => unknown} [fn] 回调参数默认是 data(isResp=false), 传 isResp=true 则是完整的 HttpResponse(含 code/message) * @param {boolean} [isResp] - * @returns {Promise} + * @returns {Promise} fn 内部抛出的异常会变成 reject, 而不是像之前那样在参数求值阶段直接同步抛出(违反"永远返回 Promise"的约定, .then() 链式调用捕获不到) */ - p(this, X, (e = (n) => n, t = !1) => (s(this, z).call(this), _(this, q, t), Promise.resolve(e == null ? void 0 : e(s(this, D).getPrototypeOf())))); + p(this, X, (e = (n) => n, t = !1) => { + s(this, z).call(this), _(this, q, t); + try { + return Promise.resolve(e == null ? void 0 : e(s(this, B).getPrototypeOf())); + } catch (n) { + return Promise.reject(n); + } + }); g(this, "toJSON", () => ({ code: this.code, message: this.message, data: this.data, url: this.url, res: this.res })); g(this, "toString", () => this.message); p(this, z, () => { - s(this, B) && (_(this, B, !1), oe.onMsg(this.code, this.message)); + s(this, j) && (_(this, j, !1), oe.onMsg(this.code, this.message)); }); /** * @param {(value: unknown) => unknown} [fn] @@ -329,14 +336,14 @@ const ae = class ae { * @param {(() => void) | undefined} [fn] * @returns {Promise} */ - p(this, re, (e) => (e == null || e(), [0, 1].includes(this.code) ? Promise.resolve(s(this, D).getPrototypeOf()) : Promise.reject(s(this, $)))); - return this.code = e, this.message = t, this.data = n, this.url = i, this.res = c, _(this, $, new Proxy(this, s(this, D))); + p(this, re, (e) => (e == null || e(), [0, 1].includes(this.code) ? Promise.resolve(s(this, B).getPrototypeOf()) : Promise.reject(s(this, $)))); + return this.code = e, this.message = t, this.data = n, this.url = i, this.res = c, _(this, $, new Proxy(this, s(this, B))); } static [Symbol.hasInstance](e) { return (e == null ? void 0 : e.constructor) === ae || (e == null ? void 0 : e[Symbol.for("HttpResponse")]) === !0; } }; -$ = new WeakMap(), q = new WeakMap(), B = new WeakMap(), D = new WeakMap(), W = new WeakMap(), X = new WeakMap(), z = new WeakMap(), Z = new WeakMap(), ee = new WeakMap(), te = new WeakMap(), re = new WeakMap(); +$ = new WeakMap(), q = new WeakMap(), j = new WeakMap(), B = new WeakMap(), W = new WeakMap(), X = new WeakMap(), z = new WeakMap(), Z = new WeakMap(), ee = new WeakMap(), te = new WeakMap(), re = new WeakMap(); let L = ae; const ie = (r, e, t, n = "/", i = "") => new L(r, e, t, n, i); var de = [ @@ -504,7 +511,7 @@ Promise.withResolvers || (Promise.withResolvers = function() { r = n, e = i; }), resolve: r, reject: e }; }); -var G, j, ne, R, F, K, se, Y; +var G, D, ne, R, F, K, se, Y; class oe { /** * @param {string} [appKey] @@ -515,7 +522,7 @@ class oe { /** @type {RequestFn | undefined} */ p(this, G); /** @type {Base62 | undefined} */ - p(this, j); + p(this, D); /** @type {string | undefined} */ p(this, ne); p(this, R, { @@ -535,7 +542,7 @@ class oe { * @param {RequestFn} [reqFn] */ g(this, "init", (e, t, n) => { - e && _(this, ne, e), n && _(this, G, n), t && _(this, j, new Ae(t)), s(this, se).call(this); + e && _(this, ne, e), n && _(this, G, n), t && _(this, D, new Ae(t)), s(this, se).call(this); }); p(this, se, () => { !s(this, K).size || !s(this, G) || (s(this, K).forEach((e) => s(this, Y).call(this, ...e)), s(this, K).clear()); @@ -681,8 +688,8 @@ class oe { if (e === void 0 || e === "" || e === null) return t; try { - if (!s(this, j)) throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret"); - const n = s(this, j).decode(e); + if (!s(this, D)) throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret"); + const n = s(this, D).decode(e); return le.unpack(n); } catch { return console.warn("core.decode", e), t; @@ -694,14 +701,14 @@ class oe { * @returns {string} */ g(this, "encode", (e = {}) => { - if (!s(this, j)) throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret"); + if (!s(this, D)) throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret"); const t = JSON.stringify(e, (n, i) => i === void 0 ? null : i); - return s(this, j).encode(le.pack(t)); + return s(this, D).encode(le.pack(t)); }); this.init(e, t, n); } } -G = new WeakMap(), j = new WeakMap(), ne = new WeakMap(), R = new WeakMap(), F = new WeakMap(), K = new WeakMap(), se = new WeakMap(), Y = new WeakMap(); +G = new WeakMap(), D = new WeakMap(), ne = new WeakMap(), R = new WeakMap(), F = new WeakMap(), K = new WeakMap(), se = new WeakMap(), Y = new WeakMap(); oe.onUnhandledRejection = (r) => { var t; const e = r instanceof L ? r : (r == null ? void 0 : r.reason) instanceof L ? r.reason : null; diff --git a/ff-request.umd.cjs b/ff-request.umd.cjs index 42d1351..3a7ac7e 100644 --- a/ff-request.umd.cjs +++ b/ff-request.umd.cjs @@ -1,2 +1,2 @@ -(function(o,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(o=typeof globalThis<"u"?globalThis:o||self,f(o["ff-request"]={}))})(this,function(o){"use strict";var Me=Object.defineProperty;var we=o=>{throw TypeError(o)};var Ne=(o,f,P)=>f in o?Me(o,f,{enumerable:!0,configurable:!0,writable:!0,value:P}):o[f]=P;var v=(o,f,P)=>Ne(o,typeof f!="symbol"?f+"":f,P),be=(o,f,P)=>f.has(o)||we("Cannot "+P);var n=(o,f,P)=>(be(o,f,"read from private field"),P?P.call(o):f.get(o)),y=(o,f,P)=>f.has(o)?we("Cannot add the same private member more than once"):f instanceof WeakSet?f.add(o):f.set(o,P),N=(o,f,P,V)=>(be(o,f,"write to private field"),V?V.call(o,P):f.set(o,P),P);var R,k,G,H,ne,se,Y,ie,oe,ae,ce,Q,W,L,K,ue,$,j,q,le,X;function f(s){return s&&s.__esModule&&Object.prototype.hasOwnProperty.call(s,"default")?s.default:s}var P={};(function(s){(function(e){e([],function(){var t=-1,r=-2,i=-3,u=-4,d=-5,B=function(l,g){g=g||{};var h=g.verbose||!1;h&&console.log("Normalize the JSON Object"),l=typeof l=="string"?this.JSON.parse(l):l,h&&console.log("Creating a empty dictionary");var p={strings:[],integers:[],floats:[]};h&&console.log("Creating the AST");var w=function x(a){h&&console.log("Calling recursiveAstBuilder with "+this.JSON.stringify(a));var E=typeof a;if(a===null)return{type:"null",index:i};if(typeof a>"u")return{type:"undefined",index:d};if(a instanceof Array){var _=["@"];for(var T in a)a.hasOwnProperty(T)&&_.push(x(a[T]));return _}if(E==="object"){var _=["$"];for(var F in a)a.hasOwnProperty(F)&&(_.push(x(F)),_.push(x(a[F])));return _}if(a==="")return{type:"empty",index:u};if(E==="string"){var S=Z.call(p.strings,a);return S==-1&&(p.strings.push(m(a)),S=p.strings.length-1),{type:"strings",index:S}}if(E==="number"&&a%1===0){var S=Z.call(p.integers,a);return S==-1&&(p.integers.push(C(a)),S=p.integers.length-1),{type:"integers",index:S}}if(E==="number"){var S=Z.call(p.floats,a);return S==-1&&(p.floats.push(a),S=p.floats.length-1),{type:"floats",index:S}}if(E==="boolean")return{type:"boolean",index:a?t:r};throw new Error("Unexpected argument of type "+typeof a)}(l),O=p.strings.length,z=p.integers.length;p.floats.length,h&&console.log("Parsing the dictionary");var I=p.strings.join("|");return I+="^"+p.integers.join("|"),I+="^"+p.floats.join("|"),h&&console.log("Parsing the structure"),I+="^"+function x(a){if(h&&console.log("Calling a recursiveParser with "+this.JSON.stringify(a)),a instanceof Array){var E=a.shift();for(var _ in a)a.hasOwnProperty(_)&&(E+=x(a[_])+"|");return(E[E.length-1]==="|"?E.slice(0,-1):E)+"]"}var T=a.type,F=a.index;if(T==="strings")return C(F);if(T==="integers")return C(O+F);if(T==="floats")return C(O+z+F);if(T==="boolean")return a.index;if(T==="null")return i;if(T==="undefined")return d;if(T==="empty")return u;throw new TypeError("The item is alien!")}(w),h&&console.log("Ending parser"),g.debug?{dictionary:p,ast:w,packed:I}:I},c=function(l,g){g=g||{};var h=l.split("^");g.verbose&&console.log("Building dictionary");var p=[],w=h[0];if(w!==""){w=w.split("|"),g.verbose&&console.log("Parse the strings dictionary");for(var O=0,z=w.length;On(this,ne).call(this,t),set:(e,t)=>{throw new TypeError(`HttpResponse 是只读的,不能设置属性 "${t}"`)},ownKeys:()=>Object.keys(n(this,H).getPrototypeOf()||{}),getPrototypeOf:()=>n(this,k)?this:this.data,getOwnPropertyDescriptor:()=>({configurable:!0,enumerable:!0,writable:!0,value:n(this,H).getPrototypeOf()})});y(this,ne,e=>e==="$echoMsg"?n(this,Y):e==="msg"?n(this,se):e==="then"?(N(this,G,!0),n(this,ie)):e==="resp"?(N(this,G,!0),n(this,oe)):e==="catch"?n(this,ae):e==="finally"?n(this,ce):e==="IS_ECHO_MSG"?n(this,G):Reflect.get(this,e));y(this,se,(e=r=>r,t=!1)=>(n(this,Y).call(this),N(this,k,t),Promise.resolve(e==null?void 0:e(n(this,H).getPrototypeOf()))));v(this,"toJSON",()=>({code:this.code,message:this.message,data:this.data,url:this.url,res:this.res}));v(this,"toString",()=>this.message);y(this,Y,()=>{n(this,G)&&(N(this,G,!1),re.onMsg(this.code,this.message))});y(this,ie,(e,t)=>[0,1].includes(this.code)?Promise.resolve(e==null?void 0:e(this.data)):t?Promise.resolve(t(n(this,R))):Promise.reject(n(this,R)));y(this,oe,(e,t)=>[0,1].includes(this.code)?Promise.resolve(e==null?void 0:e(this)):t?Promise.resolve(t(n(this,R))):Promise.reject(n(this,R)));y(this,ae,e=>Promise.resolve(e(n(this,R))));y(this,ce,e=>(e==null||e(),[0,1].includes(this.code)?Promise.resolve(n(this,H).getPrototypeOf()):Promise.reject(n(this,R))));return this.code=e,this.message=t,this.data=r,this.url=i,this.res=u,N(this,R,new Proxy(this,n(this,H)))}static[Symbol.hasInstance](e){return(e==null?void 0:e.constructor)===he||(e==null?void 0:e[Symbol.for("HttpResponse")])===!0}};R=new WeakMap,k=new WeakMap,G=new WeakMap,H=new WeakMap,ne=new WeakMap,se=new WeakMap,Y=new WeakMap,ie=new WeakMap,oe=new WeakMap,ae=new WeakMap,ce=new WeakMap;let J=he;const ee=(s,e,t,r="/",i="")=>new J(s,e,t,r,i);var fe=["utf8","utf-8","unicode-1-1-utf-8"];function Ee(s){if(fe.indexOf(s)<0&&typeof s<"u"&&s!=null)throw new RangeError("Invalid encoding type. Only utf-8 is supported");this.encoding="utf-8",this.encode=function(e){if(typeof e!="string")throw new TypeError("passed argument must be of tye string");var t=unescape(encodeURIComponent(e)),r=new Uint8Array(t.length);const i=t.split("");for(let u=0;u"u")return"";var r=typeof t<"u"&&r in t?t.stream:!1;if(typeof r!="boolean")throw new TypeError("stream option must be boolean");if(ArrayBuffer.isView(e)){var i=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),u=new Array(i.length);for(let d=0;d>>0).toString(16)}function pe(s,e){return s.replace(new RegExp(`^${e}+|${e}+$`,"g"),"")}function ve(s){return Pe.encode(s||"")}function Se(s){return Oe.decode(new DataView(s),{})}const me=62,te=30,ye=31,xe=63;class _e{constructor(e){v(this,"encodeTable",[]);v(this,"decodeMap",new Uint8Array(256).fill(255));if(e.length!==me)throw new Error("Encoding alphabet must be 62 characters long");if(e.includes(` -`)||e.includes("\r"))throw new Error("Encoding alphabet contains newline character");this.encodeTable=e.split(""),this.decodeMap=new Uint8Array(256).fill(255);for(let t=0;t>3;e===0&&(t-=1,e=8);let r=this.src[t]>>8-e;return e<6&&t>0&&(r|=this.src[t-1]<0;){let r=6,i=this.get6bits();(i&te)===te&&((this.pos>6||i>ye)&&(r=5),i&=ye),t.push(n(this,Q).encodeTable[i].charCodeAt(0)),this.pos-=r}return t}}Q=new WeakMap;class Ce{constructor(e){y(this,W);N(this,W,e)}decode(e){const t=new Uint8Array(Math.ceil(e.length*6/8)+1);let r=t.length,i=0,u=0;for(let d=0;d=8&&(r--,t[r]=u&255,i%=8,u>>=8)}return i>0&&(r--,t[r]=u&255),t.slice(r)}}W=new WeakMap,Promise.withResolvers||(Promise.withResolvers=function(){let s,e;return{promise:new Promise((r,i)=>{s=r,e=i}),resolve:s,reject:e}});class re{constructor(e,t,r){y(this,L);y(this,K);y(this,ue);y(this,$,{"#":new Map,$:new Map});y(this,j,new Map);y(this,q,new Set);v(this,"init",(e,t,r)=>{e&&N(this,ue,e),r&&N(this,L,r),t&&N(this,K,new _e(t)),n(this,le).call(this)});y(this,le,()=>{!n(this,q).size||!n(this,L)||(n(this,q).forEach(e=>n(this,X).call(this,...e)),n(this,q).clear())});y(this,X,(e,t,r,i=!1)=>{if(!n(this,L))n(this,q).add([e,t,r,i]);else{const{url:u,method:d,...B}=r||{};n(this,L).call(this,{url:u,method:d,...B}).then(c=>{if(![0,1].includes(c.code))throw ee(c.code??-1,c.msg??"响应格式错误",c.data,u,c==null?void 0:c.res);return[c.code,c.msg,c.data,u,(c==null?void 0:c.res)??""]}).then(c=>{const m=c;return(t=="#"||d=="GET")&&n(this,$)[t].set(e,m),t!="#"&&d=="GET"&&!i&&setTimeout(()=>{n(this,$)[t].delete(e)},300),m}).then(c=>{var m;return(m=n(this,j).get(e))==null?void 0:m.forEach((U,C,D)=>{U&&U[0](c),delete D[C]})}).catch(c=>{var m;n(this,$)[t].delete(e),(m=n(this,j).get(e))==null||m.forEach((U,C,D)=>{U&&U[1](c instanceof J?c:ee(-1,c,null,u)),delete D[C]})}).finally(()=>n(this,j).delete(e))}});v(this,"request",({url:e="/",method:t="GET",...r},i=!1)=>{var C,D;e=pe(e,"/");const u=t.toUpperCase(),d=ge(JSON.stringify([e,u,r])),B=(C=e==null?void 0:e.includes)!=null&&C.call(e,"/_/")?"#":"$",{promise:c,resolve:m,reject:U}=Promise.withResolvers();return n(this,$)[B].has(d)?(console.log("cache",e),m(n(this,$)[B].get(d))):n(this,j).has(d)?(D=n(this,j).get(d))==null||D.push([m,U]):(n(this,j).set(d,[[m,U]]),n(this,X).call(this,d,B,{url:e,method:u,...r},i)),new Proxy(c,{get:(Z,l)=>l===Symbol.for("HttpResponse")?!0:(...g)=>Z.then(h=>ee(...h)[l](...g)).catch(h=>{if(!(h instanceof J))throw h;return typeof h[l]=="function"?h[l](...g):h[l]})})});v(this,"get",(e,t={})=>this.request({url:e,method:"GET",params:t}));v(this,"post",(e,t={},r={})=>this.request({url:e,method:"POST",data:t,params:r}));v(this,"put",(e,t={},r={})=>this.request({url:e,method:"PUT",data:t,params:r}));v(this,"del",(e,t={},r={})=>this.request({url:e,method:"DELETE",data:t,params:r}));v(this,"download",(e,t)=>{});v(this,"cache",(e,t={})=>this.request({url:e,method:"GET",params:t},!0));v(this,"list",(e,t={})=>this.get(`/api/${e}/${this.encode(t)}`));v(this,"getAppInfo",()=>{});v(this,"refreshCache",(e=!1)=>{e&&n(this,$)["#"].clear(),n(this,$).$.clear()});v(this,"decode",(e="",t={})=>{if(e===void 0||e===""||e===null)return t;try{if(!n(this,K))throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret");const r=n(this,K).decode(e);return V.unpack(r)}catch{return console.warn("core.decode",e),t}});v(this,"encode",(e={})=>{if(!n(this,K))throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret");const t=JSON.stringify(e,(r,i)=>i===void 0?null:i);return n(this,K).encode(V.pack(t))});this.init(e,t,r)}}L=new WeakMap,K=new WeakMap,ue=new WeakMap,$=new WeakMap,j=new WeakMap,q=new WeakMap,le=new WeakMap,X=new WeakMap,re.onUnhandledRejection=s=>{var t;const e=s instanceof J?s:(s==null?void 0:s.reason)instanceof J?s.reason:null;e!=null&&e.IS_ECHO_MSG&&(e.$echoMsg(),(t=s==null?void 0:s.preventDefault)==null||t.call(s))},re.onMsg=(s,e)=>[0,1].includes(s)?console.log(e):console.warn(e),o.HttpResponse=J,o.NewHttpResponse=ee,o.default=re,o.signature=ge,o.str2uint8array=ve,o.trim=pe,o.uint8array2str=Se,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); +(function(o,f){typeof exports=="object"&&typeof module<"u"?f(exports):typeof define=="function"&&define.amd?define(["exports"],f):(o=typeof globalThis<"u"?globalThis:o||self,f(o["ff-request"]={}))})(this,function(o){"use strict";var Me=Object.defineProperty;var we=o=>{throw TypeError(o)};var Ne=(o,f,P)=>f in o?Me(o,f,{enumerable:!0,configurable:!0,writable:!0,value:P}):o[f]=P;var v=(o,f,P)=>Ne(o,typeof f!="symbol"?f+"":f,P),be=(o,f,P)=>f.has(o)||we("Cannot "+P);var n=(o,f,P)=>(be(o,f,"read from private field"),P?P.call(o):f.get(o)),y=(o,f,P)=>f.has(o)?we("Cannot add the same private member more than once"):f instanceof WeakSet?f.add(o):f.set(o,P),N=(o,f,P,V)=>(be(o,f,"write to private field"),V?V.call(o,P):f.set(o,P),P);var R,k,G,H,ne,se,Y,ie,oe,ae,ce,Q,W,L,K,ue,$,F,q,le,X;function f(s){return s&&s.__esModule&&Object.prototype.hasOwnProperty.call(s,"default")?s.default:s}var P={};(function(s){(function(e){e([],function(){var t=-1,r=-2,i=-3,u=-4,d=-5,B=function(l,g){g=g||{};var h=g.verbose||!1;h&&console.log("Normalize the JSON Object"),l=typeof l=="string"?this.JSON.parse(l):l,h&&console.log("Creating a empty dictionary");var p={strings:[],integers:[],floats:[]};h&&console.log("Creating the AST");var w=function x(a){h&&console.log("Calling recursiveAstBuilder with "+this.JSON.stringify(a));var E=typeof a;if(a===null)return{type:"null",index:i};if(typeof a>"u")return{type:"undefined",index:d};if(a instanceof Array){var _=["@"];for(var T in a)a.hasOwnProperty(T)&&_.push(x(a[T]));return _}if(E==="object"){var _=["$"];for(var j in a)a.hasOwnProperty(j)&&(_.push(x(j)),_.push(x(a[j])));return _}if(a==="")return{type:"empty",index:u};if(E==="string"){var S=Z.call(p.strings,a);return S==-1&&(p.strings.push(m(a)),S=p.strings.length-1),{type:"strings",index:S}}if(E==="number"&&a%1===0){var S=Z.call(p.integers,a);return S==-1&&(p.integers.push(C(a)),S=p.integers.length-1),{type:"integers",index:S}}if(E==="number"){var S=Z.call(p.floats,a);return S==-1&&(p.floats.push(a),S=p.floats.length-1),{type:"floats",index:S}}if(E==="boolean")return{type:"boolean",index:a?t:r};throw new Error("Unexpected argument of type "+typeof a)}(l),O=p.strings.length,z=p.integers.length;p.floats.length,h&&console.log("Parsing the dictionary");var I=p.strings.join("|");return I+="^"+p.integers.join("|"),I+="^"+p.floats.join("|"),h&&console.log("Parsing the structure"),I+="^"+function x(a){if(h&&console.log("Calling a recursiveParser with "+this.JSON.stringify(a)),a instanceof Array){var E=a.shift();for(var _ in a)a.hasOwnProperty(_)&&(E+=x(a[_])+"|");return(E[E.length-1]==="|"?E.slice(0,-1):E)+"]"}var T=a.type,j=a.index;if(T==="strings")return C(j);if(T==="integers")return C(O+j);if(T==="floats")return C(O+z+j);if(T==="boolean")return a.index;if(T==="null")return i;if(T==="undefined")return d;if(T==="empty")return u;throw new TypeError("The item is alien!")}(w),h&&console.log("Ending parser"),g.debug?{dictionary:p,ast:w,packed:I}:I},c=function(l,g){g=g||{};var h=l.split("^");g.verbose&&console.log("Building dictionary");var p=[],w=h[0];if(w!==""){w=w.split("|"),g.verbose&&console.log("Parse the strings dictionary");for(var O=0,z=w.length;On(this,ne).call(this,t),set:(e,t)=>{throw new TypeError(`HttpResponse 是只读的,不能设置属性 "${t}"`)},ownKeys:()=>Object.keys(n(this,H).getPrototypeOf()||{}),getPrototypeOf:()=>n(this,k)?this:this.data,getOwnPropertyDescriptor:()=>({configurable:!0,enumerable:!0,writable:!0,value:n(this,H).getPrototypeOf()})});y(this,ne,e=>e==="$echoMsg"?n(this,Y):e==="msg"?n(this,se):e==="then"?(N(this,G,!0),n(this,ie)):e==="resp"?(N(this,G,!0),n(this,oe)):e==="catch"?n(this,ae):e==="finally"?n(this,ce):e==="IS_ECHO_MSG"?n(this,G):Reflect.get(this,e));y(this,se,(e=r=>r,t=!1)=>{n(this,Y).call(this),N(this,k,t);try{return Promise.resolve(e==null?void 0:e(n(this,H).getPrototypeOf()))}catch(r){return Promise.reject(r)}});v(this,"toJSON",()=>({code:this.code,message:this.message,data:this.data,url:this.url,res:this.res}));v(this,"toString",()=>this.message);y(this,Y,()=>{n(this,G)&&(N(this,G,!1),re.onMsg(this.code,this.message))});y(this,ie,(e,t)=>[0,1].includes(this.code)?Promise.resolve(e==null?void 0:e(this.data)):t?Promise.resolve(t(n(this,R))):Promise.reject(n(this,R)));y(this,oe,(e,t)=>[0,1].includes(this.code)?Promise.resolve(e==null?void 0:e(this)):t?Promise.resolve(t(n(this,R))):Promise.reject(n(this,R)));y(this,ae,e=>Promise.resolve(e(n(this,R))));y(this,ce,e=>(e==null||e(),[0,1].includes(this.code)?Promise.resolve(n(this,H).getPrototypeOf()):Promise.reject(n(this,R))));return this.code=e,this.message=t,this.data=r,this.url=i,this.res=u,N(this,R,new Proxy(this,n(this,H)))}static[Symbol.hasInstance](e){return(e==null?void 0:e.constructor)===he||(e==null?void 0:e[Symbol.for("HttpResponse")])===!0}};R=new WeakMap,k=new WeakMap,G=new WeakMap,H=new WeakMap,ne=new WeakMap,se=new WeakMap,Y=new WeakMap,ie=new WeakMap,oe=new WeakMap,ae=new WeakMap,ce=new WeakMap;let J=he;const ee=(s,e,t,r="/",i="")=>new J(s,e,t,r,i);var fe=["utf8","utf-8","unicode-1-1-utf-8"];function Ee(s){if(fe.indexOf(s)<0&&typeof s<"u"&&s!=null)throw new RangeError("Invalid encoding type. Only utf-8 is supported");this.encoding="utf-8",this.encode=function(e){if(typeof e!="string")throw new TypeError("passed argument must be of tye string");var t=unescape(encodeURIComponent(e)),r=new Uint8Array(t.length);const i=t.split("");for(let u=0;u"u")return"";var r=typeof t<"u"&&r in t?t.stream:!1;if(typeof r!="boolean")throw new TypeError("stream option must be boolean");if(ArrayBuffer.isView(e)){var i=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),u=new Array(i.length);for(let d=0;d>>0).toString(16)}function pe(s,e){return s.replace(new RegExp(`^${e}+|${e}+$`,"g"),"")}function ve(s){return Pe.encode(s||"")}function Se(s){return Oe.decode(new DataView(s),{})}const me=62,te=30,ye=31,xe=63;class _e{constructor(e){v(this,"encodeTable",[]);v(this,"decodeMap",new Uint8Array(256).fill(255));if(e.length!==me)throw new Error("Encoding alphabet must be 62 characters long");if(e.includes(` +`)||e.includes("\r"))throw new Error("Encoding alphabet contains newline character");this.encodeTable=e.split(""),this.decodeMap=new Uint8Array(256).fill(255);for(let t=0;t>3;e===0&&(t-=1,e=8);let r=this.src[t]>>8-e;return e<6&&t>0&&(r|=this.src[t-1]<0;){let r=6,i=this.get6bits();(i&te)===te&&((this.pos>6||i>ye)&&(r=5),i&=ye),t.push(n(this,Q).encodeTable[i].charCodeAt(0)),this.pos-=r}return t}}Q=new WeakMap;class Ce{constructor(e){y(this,W);N(this,W,e)}decode(e){const t=new Uint8Array(Math.ceil(e.length*6/8)+1);let r=t.length,i=0,u=0;for(let d=0;d=8&&(r--,t[r]=u&255,i%=8,u>>=8)}return i>0&&(r--,t[r]=u&255),t.slice(r)}}W=new WeakMap,Promise.withResolvers||(Promise.withResolvers=function(){let s,e;return{promise:new Promise((r,i)=>{s=r,e=i}),resolve:s,reject:e}});class re{constructor(e,t,r){y(this,L);y(this,K);y(this,ue);y(this,$,{"#":new Map,$:new Map});y(this,F,new Map);y(this,q,new Set);v(this,"init",(e,t,r)=>{e&&N(this,ue,e),r&&N(this,L,r),t&&N(this,K,new _e(t)),n(this,le).call(this)});y(this,le,()=>{!n(this,q).size||!n(this,L)||(n(this,q).forEach(e=>n(this,X).call(this,...e)),n(this,q).clear())});y(this,X,(e,t,r,i=!1)=>{if(!n(this,L))n(this,q).add([e,t,r,i]);else{const{url:u,method:d,...B}=r||{};n(this,L).call(this,{url:u,method:d,...B}).then(c=>{if(![0,1].includes(c.code))throw ee(c.code??-1,c.msg??"响应格式错误",c.data,u,c==null?void 0:c.res);return[c.code,c.msg,c.data,u,(c==null?void 0:c.res)??""]}).then(c=>{const m=c;return(t=="#"||d=="GET")&&n(this,$)[t].set(e,m),t!="#"&&d=="GET"&&!i&&setTimeout(()=>{n(this,$)[t].delete(e)},300),m}).then(c=>{var m;return(m=n(this,F).get(e))==null?void 0:m.forEach((U,C,D)=>{U&&U[0](c),delete D[C]})}).catch(c=>{var m;n(this,$)[t].delete(e),(m=n(this,F).get(e))==null||m.forEach((U,C,D)=>{U&&U[1](c instanceof J?c:ee(-1,c,null,u)),delete D[C]})}).finally(()=>n(this,F).delete(e))}});v(this,"request",({url:e="/",method:t="GET",...r},i=!1)=>{var C,D;e=pe(e,"/");const u=t.toUpperCase(),d=ge(JSON.stringify([e,u,r])),B=(C=e==null?void 0:e.includes)!=null&&C.call(e,"/_/")?"#":"$",{promise:c,resolve:m,reject:U}=Promise.withResolvers();return n(this,$)[B].has(d)?(console.log("cache",e),m(n(this,$)[B].get(d))):n(this,F).has(d)?(D=n(this,F).get(d))==null||D.push([m,U]):(n(this,F).set(d,[[m,U]]),n(this,X).call(this,d,B,{url:e,method:u,...r},i)),new Proxy(c,{get:(Z,l)=>l===Symbol.for("HttpResponse")?!0:(...g)=>Z.then(h=>ee(...h)[l](...g)).catch(h=>{if(!(h instanceof J))throw h;return typeof h[l]=="function"?h[l](...g):h[l]})})});v(this,"get",(e,t={})=>this.request({url:e,method:"GET",params:t}));v(this,"post",(e,t={},r={})=>this.request({url:e,method:"POST",data:t,params:r}));v(this,"put",(e,t={},r={})=>this.request({url:e,method:"PUT",data:t,params:r}));v(this,"del",(e,t={},r={})=>this.request({url:e,method:"DELETE",data:t,params:r}));v(this,"download",(e,t)=>{});v(this,"cache",(e,t={})=>this.request({url:e,method:"GET",params:t},!0));v(this,"list",(e,t={})=>this.get(`/api/${e}/${this.encode(t)}`));v(this,"getAppInfo",()=>{});v(this,"refreshCache",(e=!1)=>{e&&n(this,$)["#"].clear(),n(this,$).$.clear()});v(this,"decode",(e="",t={})=>{if(e===void 0||e===""||e===null)return t;try{if(!n(this,K))throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret");const r=n(this,K).decode(e);return V.unpack(r)}catch{return console.warn("core.decode",e),t}});v(this,"encode",(e={})=>{if(!n(this,K))throw new Error("未初始化 appSecret,请先通过 init 设置 appSecret");const t=JSON.stringify(e,(r,i)=>i===void 0?null:i);return n(this,K).encode(V.pack(t))});this.init(e,t,r)}}L=new WeakMap,K=new WeakMap,ue=new WeakMap,$=new WeakMap,F=new WeakMap,q=new WeakMap,le=new WeakMap,X=new WeakMap,re.onUnhandledRejection=s=>{var t;const e=s instanceof J?s:(s==null?void 0:s.reason)instanceof J?s.reason:null;e!=null&&e.IS_ECHO_MSG&&(e.$echoMsg(),(t=s==null?void 0:s.preventDefault)==null||t.call(s))},re.onMsg=(s,e)=>[0,1].includes(s)?console.log(e):console.warn(e),o.HttpResponse=J,o.NewHttpResponse=ee,o.default=re,o.signature=ge,o.str2uint8array=ve,o.trim=pe,o.uint8array2str=Se,Object.defineProperties(o,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})}); diff --git a/package.json b/package.json index 94238c2..9af6c8c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "ff-request", - "version": "0.11.3", + "version": "0.11.4", "type": "module", "main": "./ff-request.umd.cjs", "module": "./ff-request.js", diff --git a/types/index.d.ts b/types/index.d.ts index 0c61c0c..3b48da3 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -154,6 +154,12 @@ export declare class HttpResponse { res: string; }; toString: () => string; + msg(fn?: (value: unknown) => unknown, isResp?: false): Promise; + msg(fn?: (value: HttpResponse) => unknown, isResp: true): Promise; + then(fn?: (value: unknown) => unknown, onrejected?: (reason: unknown) => unknown): Promise; + resp(fn?: (value: HttpResponse) => unknown, onrejected?: (reason: unknown) => unknown): Promise; + catch(fn: (reason: HttpResponse) => unknown): Promise; + finally(fn?: () => void): Promise; #private; }