ff-request-dist/dist/ff-request.umd.cjs
2024-05-25 22:47:20 +08:00

2 lines
12 KiB
JavaScript

(function(p,w){typeof exports=="object"&&typeof module<"u"?w(exports):typeof define=="function"&&define.amd?define(["exports"],w):(p=typeof globalThis<"u"?globalThis:p||self,w(p["ff-request"]={}))})(this,function(p){"use strict";var ye=Object.defineProperty;var be=(p,w,A)=>w in p?ye(p,w,{enumerable:!0,configurable:!0,writable:!0,value:A}):p[w]=A;var _=(p,w,A)=>(be(p,typeof w!="symbol"?w+"":w,A),A),ue=(p,w,A)=>{if(!w.has(p))throw TypeError("Cannot "+A)};var h=(p,w,A)=>(ue(p,w,"read from private field"),A?A.call(p):w.get(p)),N=(p,w,A)=>{if(w.has(p))throw TypeError("Cannot add the same private member more than once");w instanceof WeakSet?w.add(p):w.set(p,A)},D=(p,w,A,Y)=>(ue(p,w,"write to private field"),Y?Y.call(p,A):w.set(p,A),A);var G,B,J,q,Q,W,L,X,Z,H,K,k,I,$;function w(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}function A(t){if(t.length>=255)throw new TypeError("Alphabet too long");for(var e=new Uint8Array(256),r=0;r<e.length;r++)e[r]=255;for(var i=0;i<t.length;i++){var u=t.charAt(i),y=u.charCodeAt(0);if(e[y]!==255)throw new TypeError(u+" is ambiguous");e[y]=i}var c=t.length,m=t.charAt(0),V=Math.log(c)/Math.log(256),z=Math.log(256)/Math.log(c);function F(a){if(a instanceof Uint8Array||(ArrayBuffer.isView(a)?a=new Uint8Array(a.buffer,a.byteOffset,a.byteLength):Array.isArray(a)&&(a=Uint8Array.from(a))),!(a instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(a.length===0)return"";for(var o=0,n=0,d=0,f=a.length;d!==f&&a[d]===0;)d++,o++;for(var l=(f-d)*z+1>>>0,g=new Uint8Array(l);d!==f;){for(var P=a[d],T=0,b=l-1;(P!==0||T<n)&&b!==-1;b--,T++)P+=256*g[b]>>>0,g[b]=P%c>>>0,P=P/c>>>0;if(P!==0)throw new Error("Non-zero carry");n=T,d++}for(var s=l-n;s!==l&&g[s]===0;)s++;for(var E=m.repeat(o);s<l;++s)E+=t.charAt(g[s]);return E}function M(a){if(typeof a!="string")throw new TypeError("Expected String");if(a.length===0)return new Uint8Array;for(var o=0,n=0,d=0;a[o]===m;)n++,o++;for(var f=(a.length-o)*V+1>>>0,l=new Uint8Array(f);a[o];){var g=e[a.charCodeAt(o)];if(g===255)return;for(var P=0,T=f-1;(g!==0||P<d)&&T!==-1;T--,P++)g+=c*l[T]>>>0,l[T]=g%256>>>0,g=g/256>>>0;if(g!==0)throw new Error("Non-zero carry");d=P,o++}for(var b=f-d;b!==f&&l[b]===0;)b++;for(var s=new Uint8Array(n+(f-b)),E=n;b!==f;)s[E++]=l[b++];return s}function v(a){var o=M(a);if(o)return o;throw new Error("Non-base"+c+" character")}return{encode:F,decodeUnsafe:M,decode:v}}var Y=A;const ce=w(Y);var ne={};(function(t){(function(e){e([],function(){var r=-1,i=-2,u=-3,y=-4,c=-5,m=function(o,n){n=n||{};var d=n.verbose||!1;d&&console.log("Normalize the JSON Object"),o=typeof o=="string"?this.JSON.parse(o):o,d&&console.log("Creating a empty dictionary");var f={strings:[],integers:[],floats:[]};d&&console.log("Creating the AST");var l=function b(s){d&&console.log("Calling recursiveAstBuilder with "+this.JSON.stringify(s));var E=typeof s;if(s===null)return{type:"null",index:u};if(typeof s>"u")return{type:"undefined",index:c};if(s instanceof Array){var U=["@"];for(var S in s)s.hasOwnProperty(S)&&U.push(b(s[S]));return U}if(E==="object"){var U=["$"];for(var j in s)s.hasOwnProperty(j)&&(U.push(b(j)),U.push(b(s[j])));return U}if(s==="")return{type:"empty",index:y};if(E==="string"){var C=a.call(f.strings,s);return C==-1&&(f.strings.push(z(s)),C=f.strings.length-1),{type:"strings",index:C}}if(E==="number"&&s%1===0){var C=a.call(f.integers,s);return C==-1&&(f.integers.push(M(s)),C=f.integers.length-1),{type:"integers",index:C}}if(E==="number"){var C=a.call(f.floats,s);return C==-1&&(f.floats.push(s),C=f.floats.length-1),{type:"floats",index:C}}if(E==="boolean")return{type:"boolean",index:s?r:i};throw new Error("Unexpected argument of type "+typeof s)}(o),g=f.strings.length,P=f.integers.length;f.floats.length,d&&console.log("Parsing the dictionary");var T=f.strings.join("|");return T+="^"+f.integers.join("|"),T+="^"+f.floats.join("|"),d&&console.log("Parsing the structure"),T+="^"+function b(s){if(d&&console.log("Calling a recursiveParser with "+this.JSON.stringify(s)),s instanceof Array){var E=s.shift();for(var U in s)s.hasOwnProperty(U)&&(E+=b(s[U])+"|");return(E[E.length-1]==="|"?E.slice(0,-1):E)+"]"}var S=s.type,j=s.index;if(S==="strings")return M(j);if(S==="integers")return M(g+j);if(S==="floats")return M(g+P+j);if(S==="boolean")return s.index;if(S==="null")return u;if(S==="undefined")return c;if(S==="empty")return y;throw new TypeError("The item is alien!")}(l),d&&console.log("Ending parser"),n.debug?{dictionary:f,ast:l,packed:T}:T},V=function(o,n){n=n||{};var d=o.split("^");n.verbose&&console.log("Building dictionary");var f=[],l=d[0];if(l!==""){l=l.split("|"),n.verbose&&console.log("Parse the strings dictionary");for(var g=0,P=l.length;g<P;g++)f.push(F(l[g]))}if(l=d[1],l!==""){l=l.split("|"),n.verbose&&console.log("Parse the integers dictionary");for(var g=0,P=l.length;g<P;g++)f.push(v(l[g]))}if(l=d[2],l!==""){l=l.split("|"),n.verbose&&console.log("Parse the floats dictionary");for(var g=0,P=l.length;g<P;g++)f.push(parseFloat(l[g]))}l=null,n.verbose&&console.log("Tokenizing the structure");for(var T="",b=[],s=d[3].length,g=0;g<s;g++){var E=d[3].charAt(g);E==="|"||E==="$"||E==="@"||E==="]"?(T&&(b.push(v(T)),T=""),E!=="|"&&b.push(E)):T+=E}var U=b.length,S=0;return n.verbose&&console.log("Starting recursive parser"),function j(){var C=b[S++];if(n.verbose&&console.log("Reading collection type "+(C==="$"?"object":"Array")),C==="@"){for(var O=[];S<U;S++){var R=b[S];if(n.verbose&&console.log("Read "+R+" symbol"),R==="]")return O;if(R==="@"||R==="$")O.push(j());else switch(R){case r:O.push(!0);break;case i:O.push(!1);break;case u:O.push(null);break;case c:O.push(void 0);break;case y:O.push("");break;default:O.push(f[R])}}return n.verbose&&console.log("Parsed "+this.JSON.stringify(O)),O}if(C==="$"){for(var O={};S<U;S++){var x=b[S];if(x==="]")return O;x===y?x="":x=f[x];var R=b[++S];if(R==="@"||R==="$")O[x]=j();else switch(R){case r:O[x]=!0;break;case i:O[x]=!1;break;case u:O[x]=null;break;case c:O[x]=void 0;break;case y:O[x]="";break;default:O[x]=f[R]}}return n.verbose&&console.log("Parsed "+this.JSON.stringify(O)),O}throw new TypeError("Bad token "+C+" isn't a type")}()},z=function(o){return typeof o!="string"?o:o.replace(/[\+ \|\^\%]/g,function(n){return{" ":"+","+":"%2B","|":"%7C","^":"%5E","%":"%25"}[n]})},F=function(o){return typeof o!="string"?o:o.replace(/\+|%2B|%7C|%5E|%25/g,function(n){return{"+":" ","%2B":"+","%7C":"|","%5E":"^","%25":"%"}[n]})},M=function(o){return Number.prototype.toString.call(o,36).toUpperCase()},v=function(o){return parseInt(o,36)},a=Array.prototype.indexOf||function(o,n){for(var d=n||0,f=this.length;d<f;d++)if(this[d]===o)return d;return-1};return{JSON,pack:m,unpack:V}})})(function(e,r){var i=r();for(var u in i)t[u]=i[u]})})(ne);const se=w(ne);class ee{constructor(e,r,i,u,y){_(this,"code",0);_(this,"message","操作成功");_(this,"data",null);_(this,"url","");_(this,"res","");N(this,G,null);N(this,B,!0);N(this,J,!0);N(this,q,{get:(e,r)=>h(this,Q).call(this,r),ownKeys:()=>Object.keys(h(this,q).getPrototypeOf()||{}),getPrototypeOf:()=>h(this,B)?this:this.data,getOwnPropertyDescriptor:()=>({configurable:!0,enumerable:!0,writable:!0,value:h(this,q).getPrototypeOf()})});N(this,Q,e=>e==="$toast"?h(this,L):e==="msg"?h(this,W):e==="then"||e==="resp"?(D(this,J,!0),D(this,B,e==="resp"),h(this,X)):e==="catch"?h(this,Z):e==="IS_ECHO_MSG"?h(this,J):Reflect.get(this,e));N(this,W,(e=i=>i,r=!1)=>(h(this,L).call(this),D(this,B,r),Promise.resolve(e==null?void 0:e(h(this,q).getPrototypeOf()))));N(this,L,()=>{h(this,J)&&(D(this,J,!1),le.onToast(this.code,this.message))});N(this,X,e=>[0,1].includes(this.code)?Promise.resolve(e==null?void 0:e(h(this,q).getPrototypeOf())):Promise.reject(h(this,G)));N(this,Z,e=>Promise.resolve(e(h(this,G))));return this.code=e,this.message=r,this.data=i,this.url=u,this.res=y,D(this,G,new Proxy(this,h(this,q)))}}G=new WeakMap,B=new WeakMap,J=new WeakMap,q=new WeakMap,Q=new WeakMap,W=new WeakMap,L=new WeakMap,X=new WeakMap,Z=new WeakMap;const te=(t,e,r,i="/",u="")=>new ee(t,e,r,i,u);var ie=["utf8","utf-8","unicode-1-1-utf-8"];function de(t){if(ie.indexOf(t)<0&&typeof t<"u"&&t!=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 r=unescape(encodeURIComponent(e)),i=new Uint8Array(r.length);const u=r.split("");for(let y=0;y<u.length;y++)i[y]=u[y].charCodeAt(0);return i}}function ge(t){if(ie.indexOf(t)<0&&typeof t<"u"&&t!=null)throw new RangeError("Invalid encoding type. Only utf-8 is supported");this.encoding="utf-8",this.decode=function(e,r){if(typeof e>"u")return"";var i=typeof r<"u"&&i in r?r.stream:!1;if(typeof i!="boolean")throw new TypeError("stream option must be boolean");if(ArrayBuffer.isView(e)){var u=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),y=new Array(u.length);for(let c=0;c<u.length;c++)y[c]=String.fromCharCode(u[c]);return decodeURIComponent(escape(y.join("")))}else throw new TypeError("passed argument must be an array buffer view")}}var oe={TextEncoder:de,TextDecoder:ge};const pe=new oe.TextEncoder("utf8"),ve=new oe.TextDecoder("utf8"),ae=t=>{let e=5381;for(let r=0;r<t.length;r++)e=e*33^t.charCodeAt(r);return(e>>>0).toString(16)},we=(t,e)=>t.replace(new RegExp(`^${e}+|${e}+$`,"g"),""),fe=t=>pe.encode(t||""),he=t=>ve.decode(new DataView(t),{});class re{constructor(e,r,i=({})=>Promise.reject({code:-1,msg:"请初始化HTTP请求方法主体"})){N(this,H,void 0);N(this,K,void 0);N(this,k,void 0);N(this,I,{"#":new Map,$:new Map});N(this,$,new Map);_(this,"request",({url:e="/",method:r="GET",...i},u=!1)=>{var M;const y=r.toUpperCase(),c=ae(JSON.stringify([e,y,i])),m=(M=e==null?void 0:e.includes)!=null&&M.call(e,"/_/")?"#":"$",{promise:V,resolve:z,reject:F}=Promise.withResolvers();return h(this,I)[m].has(c)?(console.log("缓存命中",e),z(h(this,I)[m].get(c))):h(this,$).has(c)?h(this,$).get(c).push([z,F]):(h(this,$).set(c,[[z,F]]),h(this,H).call(this,{url:e,method:y,...i}).then(v=>{if(![0,1].includes(v.code))throw te(v.code,v.msg,v.data,e,v==null?void 0:v.res);return[v.code,v.msg,v.data,e,v==null?void 0:v.res]}).then(v=>((m=="#"||u&&y==="GET")&&h(this,I)[m].set(c,v),v)).then(v=>h(this,$).get(c).forEach((a,o,n)=>{a&&a[0](v),delete n[o]})).catch(v=>{h(this,I)[m].delete(c),h(this,$).get(c).forEach((a,o,n)=>{a&&a[1](v instanceof ee?v:te(-1,v,null,e)),delete n[o]})}).finally(()=>{console.log(1111,h(this,$).get(c)),h(this,$).delete(c)})),new Proxy(V,{get:(v,a)=>(...o)=>v.then(n=>te(...n)[a](...o)).catch(n=>{if(!n instanceof ee)throw n;return typeof n[a]=="function"?n[a](...o):n[a]})})});_(this,"get",(e,r={})=>this.request({url:e,method:"GET",params:r}));_(this,"post",(e,r={},i={})=>this.request({url:e,method:"POST",data:r,params:i}));_(this,"put",(e,r={},i={})=>this.request({url:e,method:"PUT",data:r,params:i}));_(this,"del",(e,r={},i={})=>this.request({url:e,method:"DELETE",data:r,params:i}));_(this,"download",(e,r)=>{});_(this,"cache",(e,r={})=>this.request({url:e,method:"GET",params:r},!0));_(this,"list",(e,r={})=>this.get(`/api/${e}/${this.encode(r)}`));_(this,"getAppInfo",()=>{});_(this,"refreshCache",(e=!1)=>{e&&h(this,I)["#"].clear(),h(this,I).$.clear()});_(this,"decode",(e="",r={})=>{if(e===void 0||e===""||e===null)return r;try{const i=h(this,K).decode(e).buffer;return se.unpack(he(i))}catch{return console.warn("core.decode",e),r}});_(this,"encode",(e={})=>{const r=JSON.stringify(e,(i,u)=>u===void 0?null:u);return h(this,K).encode(fe(se.pack(r)))});D(this,k,e),D(this,H,i),D(this,K,ce(r))}}H=new WeakMap,K=new WeakMap,k=new WeakMap,I=new WeakMap,$=new WeakMap;const le=re;re.onUnhandledRejection=t=>{var e,r,i,u,y,c;t!=null&&t.IS_ECHO_MSG?((e=t==null?void 0:t.$toast)==null||e.call(t),(r=t==null?void 0:t.preventDefault)==null||r.call(t)):(i=t.reason)!=null&&i.IS_ECHO_MSG&&((y=(u=t.reason)==null?void 0:u.$toast)==null||y.call(u),(c=t==null?void 0:t.preventDefault)==null||c.call(t))},re.onToast=(t,e)=>[0,1].includes(t)?console.log(e):console.warn(e),p.default=le,p.signature=ae,p.str2uint8array=fe,p.trim=we,p.uint8array2str=he,Object.defineProperties(p,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});