ff-request-dist/dist/ff-request.umd.cjs
2024-04-04 11:47:06 +08:00

2 lines
12 KiB
JavaScript

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