(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;On(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"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>>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>3;e===0&&(t-=1,e=8);let r=this.src[t]>>8-e;return e<6&&t>0&&(r|=this.src[t-1]<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=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"}})});