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

2 lines
12 KiB
JavaScript

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