Files
ff-request-dist/ff-request.umd.cjs
2026-07-16 11:00:38 +08:00

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