ff-request-dist/dist/ff-request.umd.cjs

3 lines
12 KiB
JavaScript
Raw Normal View History

2024-11-27 16:42:25 +08:00
(function(u,p){typeof exports=="object"&&typeof module<"u"?p(exports):typeof define=="function"&&define.amd?define(["exports"],p):(u=typeof globalThis<"u"?globalThis:u||self,p(u["ff-request"]={}))})(this,function(u){"use strict";var Se=Object.defineProperty;var xe=(u,p,b)=>p in u?Se(u,p,{enumerable:!0,configurable:!0,writable:!0,value:b}):u[p]=b;var w=(u,p,b)=>(xe(u,typeof p!="symbol"?p+"":p,b),b),he=(u,p,b)=>{if(!p.has(u))throw TypeError("Cannot "+b)};var i=(u,p,b)=>(he(u,p,"read from private field"),b?b.call(u):p.get(u)),P=(u,p,b)=>{if(p.has(u))throw TypeError("Cannot add the same private member more than once");p instanceof WeakSet?p.add(u):p.set(u,b)},M=(u,p,b,G)=>(he(u,p,"write to private field"),G?G.call(u,b):p.set(u,b),b);var q,J,j,I,X,Z,H,k,ee,L,z,V,K,te,U,m;function p(r){return r&&r.__esModule&&Object.prototype.hasOwnProperty.call(r,"default")?r.default:r}var b={};(function(r){(function(e){e([],function(){var t=-1,n=-2,s=-3,d=-4,f=-5,F=function(a,c){c=c||{};var l=c.verbose||!1;l&&console.log("Normalize the JSON Object"),a=typeof a=="string"?this.JSON.parse(a):a,l&&console.log("Creating a empty dictionary");var g={strings:[],integers:[],floats:[]};l&&console.log("Creating the AST");var v=function x(o){l&&console.log("Calling recursiveAstBuilder with "+this.JSON.stringify(o));var E=typeof o;if(o===null)return{type:"null",index:s};if(typeof o>"u")return{type:"undefined",index:f};if(o instanceof Array){var _=["@"];for(var O in o)o.hasOwnProperty(O)&&_.push(x(o[O]));return _}if(E==="object"){var _=["$"];for(var R in o)o.hasOwnProperty(R)&&(_.push(x(R)),_.push(x(o[R])));return _}if(o==="")return{type:"empty",index:d};if(E==="string"){var S=h.call(g.strings,o);return S==-1&&(g.strings.push(Y(o)),S=g.strings.length-1),{type:"strings",index:S}}if(E==="number"&&o%1===0){var S=h.call(g.integers,o);return S==-1&&(g.integers.push(D(o)),S=g.integers.length-1),{type:"integers",index:S}}if(E==="number"){var S=h.call(g.floats,o);return S==-1&&(g.floats.push(o),S=g.floats.length-1),{type:"floats",index:S}}if(E==="boolean")return{type:"boolean",index:o?t:n};throw new Error("Unexpected argument of type "+typeof o)}(a),T=g.strings.length,B=g.integers.length;g.floats.length,l&&console.log("Parsing the dictionary");var $=g.strings.join("|");return $+="^"+g.integers.join("|"),$+="^"+g.floats.join("|"),l&&console.log("Parsing the structure"),$+="^"+function x(o){if(l&&console.log("Calling a recursiveParser with "+this.JSON.stringify(o)),o instanceof Array){var E=o.shift();for(var _ in o)o.hasOwnProperty(_)&&(E+=x(o[_])+"|");return(E[E.length-1]==="|"?E.slice(0,-1):E)+"]"}var O=o.type,R=o.index;if(O==="strings")return D(R);if(O==="integers")return D(T+R);if(O==="floats")return D(T+B+R);if(O==="boolean")return o.index;if(O==="null")return s;if(O==="undefined")return f;if(O==="empty")return d;throw new TypeError("The item is alien!")}(v),l&&console.log("Ending parser"),c.debug?{dictionary:g,ast:v,packed:$}:$},N=function(a,c){c=c||{};var l=a.split("^");c.verbose&&console.log("Building dictionary");var g=[],v=l[0];if(v!==""){v=v.split("|"),c.verbose&&console.log("Parse the strings dictionary");for(var T=0,B=v.length;T<B;T++)g.push(re(v[T]))}if(v=l[1],v!==""){v=v.split("|"),c.verbose&&console.log("Parse the integers dictionary");for(var T=0,B=v.length;T<B;T++)g.push(Q(v[T]))}if(v=l[2],v!==""){v=v.split("|"),c.verbose&&console.log("Parse the floats dictionary");for(var T=0,B=v.length;T<B;T++)g.push(parseFloat(v[T]))}v=null,c.verbose&&console.log("Tokenizing the structure");for(var $="",x=[],o=l[3].length,T=0;T<o;T++){var E=l[3].charAt(T);E==="|"||E==="$"||E==="@"||E==="]"?($&&(x.push(Q($)),$=""),E!=="|"&&x.push(E)):$+=E}var _=x.length,O=0;return c.verbose&&console.log("Starting recursive parser"),function R(){var S=x[O++];if(c.verbose&&console.log("Reading collection type "+(S==="$"?"object":"Array")),S==="@"){for(var y=[];O<_;O++){var A=x[O];if(c.verbose&&console.log("Read "+A+" symbol"),A==="]")return y;if(A==="@"||A==="$")y.push(R());else switch(A){case t:y.push(!0);break;case n:y.push(!1);break;case s:y.push(null);break;case f:y.push(void
`)||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<e.length;t++)this.decodeMap[e.charCodeAt(t)]=t}encode(e){if(!e||e.length===0)return[];const t=new Te(this).encodeV2(e);return String.fromCharCode(...t)}decode(e){if(!e||e.length===0)return new Uint8Array;const t=new Pe(this).decode(e);return new TextDecoder().decode(t)}}class Te{constructor(e){P(this,L,null);M(this,L,e)}get6bits(){let e=this.pos&7,t=this.pos>>3;e===0&&(t-=1,e=8);let n=this.src[t]>>8-e;return e<6&&t>0&&(n|=this.src[t-1]<<e),n&Ee}encodeV2(e){this.src=le(e),this.pos=this.src.length*8;const t=[];for(;this.pos>0;){let n=6,s=this.get6bits();(s&W)===W&&((this.pos>6||s>ue)&&(n=5),s&=ue),t.push(i(this,L).encodeTable[s].charCodeAt(0)),this.pos-=n}return t}}L=new WeakMap;class Pe{constructor(e){P(this,z,null);M(this,z,e)}decode(e){const t=new Uint8Array(Math.ceil(e.length*6/8)+1);let n=t.length,s=0,d=0;for(let f=0;f<e.length;f++){const F=e[f].charCodeAt(0),N=i(this,z).decodeMap[F];if(N===255)throw new Error(`CorruptInputError at index ${f}`);f===e.length-1?(d|=N<<s,s+=Math.ceil(Math.log2(N+1))):(N&W)===W?(d|=N<<s,s+=5):(d|=N<<s,s+=6),s>=8&&(n--,t[n]=d&255,s%=8,d>>=8)}return s>0&&(n--,t[n]=d&255),t.slice(n)}}z=new WeakMap,Promise.withResolvers||(Promise.withResolvers=function(){let r,e;return{promise:new Promise((n,s)=>{r=n,e=s}),resolve:r,reject:e}});class ie{constructor(e,t,n=({})=>Promise.reject({code:-1,msg:"请初始化HTTP请求方法主体"})){P(this,V,void 0);P(this,K,void 0);P(this,te,void 0);P(this,U,{"#":new Map,$:new Map});P(this,m,new Map);w(this,"request",({url:e="/",method:t="GET",...n},s=!1)=>{var D,Q;const d=t.toUpperCase(),f=ce(JSON.stringify([e,d,n])),F=(D=e==null?void 0:e.includes)!=null&&D.call(e,"/_/")?"#":"$",{promise:N,resolve:Y,reject:re}=Promise.withResolvers();return i(this,U)[F].has(f)?(console.log("缓存命中",e),Y(i(this,U)[F].get(f))):(i(this,m).has(f)?i(this,m).get(f).push([Y,re]):i(this,m).set(f,[[Y,re]]),(Q=i(this,m).get(f))!=null&&Q.some(h=>h)&&i(this,V).call(this,{url:e,method:d,...n}).then(h=>{if(![0,1].includes(h.code))throw se(h.code,h.msg,h.data,e,h==null?void 0:h.res);return[h.code,h.msg,h.data,e,h==null?void 0:h.res]}).then(h=>((F=="#"||s&&d==="GET")&&i(this,U)[F].set(f,h),h)).then(h=>{var a;return(a=i(this,m).get(f))==null?void 0:a.forEach((c,l,g)=>{c&&c[0](h),delete g[l]})}).catch(h=>{var a;i(this,U)[F].delete(f),(a=i(this,m).get(f))==null||a.forEach((c,l,g)=>{c&&c[1](h instanceof ne?h:se(-1,h,null,e)),delete g[l]})}).finally(()=>i(this,m).delete(f))),new Proxy(N,{get:(h,a)=>(...c)=>h.then(l=>se(...l)[a](...c)).catch(l=>{if(!l instanceof ne)throw l;return typeof l[a]=="function"?l[a](...c):l[a]})})});w(this,"get",(e,t={})=>this.request({url:e,method:"GET",params:t}));w(this,"post",(e,t={},n={})=>this.request({url:e,method:"POST",data:t,params:n}));w(this,"put",(e,t={},n={})=>this.request({url:e,method:"PUT",data:t,params:n}));w(this,"del",(e,t={},n={})=>this.request({url:e,method:"DELETE",data:t,params:n}));w(this,"download",(e,t)=>{});w(this,"cache",(e,t={})=>this.request({url:e,method:"GET",params:t},!0));w(this,"list",(e,t={})=>this.get(`/api/${e}/${this.encode(t)}`));w(this,"getAppInfo",()=>{});w(this,"refreshCache",(e=!1)=>{e&&i(this,U)["#"].clear(),i(this,U).$.clear()});w(this,"decode",(e="",t={})=>{if(e===void 0||e===""||e===null)return t;try{const n=i(this,K).decode(e);return G.unpack(n)}catch{return console.warn("core.decode",e),t}});w(this,"encode",(e={})=>{const t=JSON.stringify(e,(n,s)=>s===void 0?null:s);return i(this,K).encode(G.pack(t))});M(this,te,e),M(this,V,n),M(this,K,new Oe(t))}}V=new WeakMap,K=new WeakMap,te=new WeakMap,U=new WeakMap,m=new WeakMap;const fe=ie;ie.onUnhandledRejection=r=>{var e,t,n,s,d,f;r!=null&&r.IS_ECHO_MSG?((e=r==null?void 0:r.$echoMsg)==null||e.call(r),(t=r==null?void 0:r.preventDefault)==null||t.call(r)):(n=r.reason)!=null&&n.IS_ECHO_MSG&&((d=(s=r.reason)==null?void 0:s.$echoMsg)==null||d.call(s),(f=r==null?void 0:r.preventDefault)==null||f.call(r))},ie.onMsg