Files
ff-request-dist/ff-request.umd.cjs
2026-04-15 15:19:04 +08:00

3 lines
13 KiB
JavaScript
Raw Permalink Blame History

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