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