@hopeio/utils
Version:
javascript utils
2 lines (1 loc) • 1.75 kB
JavaScript
;var l=Object.defineProperty;var y=(u,e,t)=>e in u?l(u,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):u[e]=t;var a=(u,e,t)=>y(u,typeof e!="symbol"?e+"":e,t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const I=require("qs");class m{constructor(e){a(this,"defaults",{baseUrl:"",header:{},dataType:"json",responseType:"json",timeout:3e4});a(this,"requestInterceptors",[]);a(this,"responseInterceptors",[]);a(this,"responseErrorInterceptors",[]);a(this,"interceptors",{request:{use:e=>{this.requestInterceptors.push(e)}},response:{use:(e,t)=>{this.responseInterceptors.push(e),this.responseErrorInterceptors.push(t)}}});e&&(this.defaults=Object.assign(this.defaults,e))}request(e,t,s){return new Promise((i,o)=>{if(s!=null&&s.query){const r=I.stringify(s.query);t.includes("?")?t+=`&${r}`:t+=`?${r}`}t=((s==null?void 0:s.baseUrl)||this.defaults.baseUrl)+t;const p=s!=null&&s.header?Object.assign(this.defaults.header,s.header):this.defaults.header,h=s!=null&&s.timeout?s.timeout:this.defaults.timeout;this.requestInterceptors.length>0&&!s&&(s={});for(const r of this.requestInterceptors)s=r(s);let d={...s,method:e,url:t,header:p,timeout:h,success:r=>{let n={...r,config:s};for(const q of this.responseInterceptors)if(!q(n)){o(n);return}i(n.data)},fail:r=>{for(const n of this.responseErrorInterceptors)if(!n(r)){o(r);return}o(r)}};uni.request(d)})}get(e,t){return this.request("GET",e,t)}post(e,t){return this.request("POST",e,t)}put(e,t){return this.request("PUT",e,t)}delete(e,t){return this.request("DELETE",e,t)}}const b=new m({header:{"content-type":"application/json"},timeout:1e4});async function E(u,e){uni.showLoading({title:u}),await e(),uni.hideLoading()}exports.loading=E;exports.unirequest=b;