ff-request-dist/dist/ff-request.umd.cjs
2025-01-03 17:37:03 +08:00

3 lines
12 KiB
JavaScript

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