ff-request-dist/dist/ff-request.umd.cjs
2024-05-25 22:03:00 +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,_)=>w in p?ye(p,w,{enumerable:!0,configurable:!0,writable:!0,value:_}):p[w]=_;var P=(p,w,_)=>(be(p,typeof w!="symbol"?w+"":w,_),_),le=(p,w,_)=>{if(!w.has(p))throw TypeError("Cannot "+_)};var h=(p,w,_)=>(le(p,w,"read from private field"),_?_.call(p):w.get(p)),N=(p,w,_)=>{if(w.has(p))throw TypeError("Cannot add the same private member more than once");w instanceof WeakSet?w.add(p):w.set(p,_)},D=(p,w,_,Y)=>(le(p,w,"write to private field"),Y?Y.call(p,_):w.set(p,_),_);var G,B,J,q,Q,W,L,X,Z,H,K,k,I,m;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 i=0;i<t.length;i++){var l=t.charAt(i),y=l.charCodeAt(0);if(e[y]!==255)throw new TypeError(l+" is ambiguous");e[y]=i}var c=t.length,$=t.charAt(0),V=Math.log(c)/Math.log(256),z=Math.log(256)/Math.log(c);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 s=0,o=0,g=0,a=f.length;g!==a&&f[g]===0;)g++,s++;for(var u=(a-g)*z+1>>>0,d=new Uint8Array(u);g!==a;){for(var A=f[g],T=0,b=u-1;(A!==0||T<o)&&b!==-1;b--,T++)A+=256*d[b]>>>0,d[b]=A%c>>>0,A=A/c>>>0;if(A!==0)throw new Error("Non-zero carry");o=T,g++}for(var n=u-o;n!==u&&d[n]===0;)n++;for(var E=$.repeat(s);n<u;++n)E+=t.charAt(d[n]);return E}function M(f){if(typeof f!="string")throw new TypeError("Expected String");if(f.length===0)return new Uint8Array;for(var s=0,o=0,g=0;f[s]===$;)o++,s++;for(var a=(f.length-s)*V+1>>>0,u=new Uint8Array(a);f[s];){var d=e[f.charCodeAt(s)];if(d===255)return;for(var A=0,T=a-1;(d!==0||A<g)&&T!==-1;T--,A++)d+=c*u[T]>>>0,u[T]=d%256>>>0,d=d/256>>>0;if(d!==0)throw new Error("Non-zero carry");g=A,s++}for(var b=a-g;b!==a&&u[b]===0;)b++;for(var n=new Uint8Array(o+(a-b)),E=o;b!==a;)n[E++]=u[b++];return n}function v(f){var s=M(f);if(s)return s;throw new Error("Non-base"+c+" character")}return{encode:F,decodeUnsafe:M,decode:v}}var Y=_;const ce=w(Y);var ne={};(function(t){(function(e){e([],function(){var r=-1,i=-2,l=-3,y=-4,c=-5,$=function(s,o){o=o||{};var g=o.verbose||!1;g&&console.log("Normalize the JSON Object"),s=typeof s=="string"?this.JSON.parse(s):s,g&&console.log("Creating a empty dictionary");var a={strings:[],integers:[],floats:[]};g&&console.log("Creating the AST");var u=function b(n){g&&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:c};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 j in n)n.hasOwnProperty(j)&&(U.push(b(j)),U.push(b(n[j])));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(M(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:i};throw new Error("Unexpected argument of type "+typeof n)}(s),d=a.strings.length,A=a.integers.length;a.floats.length,g&&console.log("Parsing the dictionary");var T=a.strings.join("|");return T+="^"+a.integers.join("|"),T+="^"+a.floats.join("|"),g&&console.log("Parsing the structure"),T+="^"+function b(n){if(g&&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,j=n.index;if(S==="strings")return M(j);if(S==="integers")return M(d+j);if(S==="floats")return M(d+A+j);if(S==="boolean")return n.index;if(S==="null")return l;if(S==="undefined")return c;if(S==="empty")return y;throw new TypeError("The item is alien!")}(u),g&&console.log("Ending parser"),o.debug?{dictionary:a,ast:u,packed:T}:T},V=function(s,o){o=o||{};var g=s.split("^");o.verbose&&console.log("Building dictionary");var a=[],u=g[0];if(u!==""){u=u.split("|"),o.verbose&&console.log("Parse the strings dictionary");for(var d=0,A=u.length;d<A;d++)a.push(F(u[d]))}if(u=g[1],u!==""){u=u.split("|"),o.verbose&&console.log("Parse the integers dictionary");for(var d=0,A=u.length;d<A;d++)a.push(v(u[d]))}if(u=g[2],u!==""){u=u.split("|"),o.verbose&&console.log("Parse the floats dictionary");for(var d=0,A=u.length;d<A;d++)a.push(parseFloat(u[d]))}u=null,o.verbose&&console.log("Tokenizing the structure");for(var T="",b=[],n=g[3].length,d=0;d<n;d++){var E=g[3].charAt(d);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 j(){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(j());else switch(R){case r:O.push(!0);break;case i:O.push(!1);break;case l:O.push(null);break;case c: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]=j();else switch(R){case r:O[x]=!0;break;case i:O[x]=!1;break;case l:O[x]=null;break;case c: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(s){return typeof s!="string"?s:s.replace(/[\+ \|\^\%]/g,function(o){return{" ":"+","+":"%2B","|":"%7C","^":"%5E","%":"%25"}[o]})},F=function(s){return typeof s!="string"?s:s.replace(/\+|%2B|%7C|%5E|%25/g,function(o){return{"+":" ","%2B":"+","%7C":"|","%5E":"^","%25":"%"}[o]})},M=function(s){return Number.prototype.toString.call(s,36).toUpperCase()},v=function(s){return parseInt(s,36)},f=Array.prototype.indexOf||function(s,o){for(var g=o||0,a=this.length;g<a;g++)if(this[g]===s)return g;return-1};return{JSON,pack:$,unpack:V}})})(function(e,r){var i=r();for(var l in i)t[l]=i[l]})})(ne);const ie=w(ne);class ee{constructor(e,r,i,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)=>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),ue.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=l,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="/",l="")=>new ee(t,e,r,i,l);var se=["utf8","utf-8","unicode-1-1-utf-8"];function ge(t){if(se.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 l=r.split("");for(let y=0;y<l.length;y++)i[y]=l[y].charCodeAt(0);return i}}function de(t){if(se.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 l=new Uint8Array(e.buffer,e.byteOffset,e.byteLength),y=new Array(l.length);for(let c=0;c<l.length;c++)y[c]=String.fromCharCode(l[c]);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,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,m,new Map);P(this,"request",({url:e="/",method:r="GET",...i},l=!1)=>{var M;const y=r.toUpperCase(),c=ae(JSON.stringify([e,y,i])),$=(M=e==null?void 0:e.includes)!=null&&M.call(e,"/_/")?"#":"$",{promise:V,resolve:z,reject:F}=Promise.withResolvers();return h(this,I)[$].has(c)?(console.log("缓存命中",e),z(h(this,I)[$].get(c))):h(this,m).has(c)?h(this,m).get(c).push([z,F]):h(this,m).set(c,[[z,F]]),h(this,m).get(c).length===1&&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=>(($=="#"||l&&y==="GET")&&h(this,I)[$].set(c,v),v)).then(v=>h(this,m).get(c).forEach(([f,s])=>f(v))).catch(v=>(h(this,I)[$].delete(c),h(this,m).get(c).forEach(([f,s])=>s(v instanceof ee?v:te(-1,v,null,e))))).finally(()=>h(this,m).delete(c)),new Proxy(V,{get:(v,f)=>(...s)=>v.then(o=>te(...o)[f](...s)).catch(o=>{if(!o instanceof ee)throw o;return typeof o[f]=="function"?o[f](...s):o[f]})})});P(this,"get",(e,r={})=>this.request({url:e,method:"GET",params:r}));P(this,"post",(e,r={},i={})=>this.request({url:e,method:"POST",data:r,params:i}));P(this,"put",(e,r={},i={})=>this.request({url:e,method:"PUT",data:r,params:i}));P(this,"del",(e,r={},i={})=>this.request({url:e,method:"DELETE",data:r,params:i}));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&&h(this,I)["#"].clear(),h(this,I).$.clear()});P(this,"decode",(e="",r={})=>{if(e===void 0||e===""||e===null)return r;try{const i=h(this,K).decode(e).buffer;return ie.unpack(he(i))}catch{return console.warn("core.decode",e),r}});P(this,"encode",(e={})=>{const r=JSON.stringify(e,(i,l)=>l===void 0?null:l);return h(this,K).encode(fe(ie.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,m=new WeakMap;const ue=re;re.onUnhandledRejection=t=>{var e,r,i,l,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=(l=t.reason)==null?void 0:l.$toast)==null||y.call(l),(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=ue,p.signature=ae,p.str2uint8array=fe,p.trim=we,p.uint8array2str=he,Object.defineProperties(p,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});