UNPKG

@bit-ocean/axios

Version:
2 lines 5.29 kB
import w,{HttpStatusCode}from'axios';import {LangUtils,AuthUtils}from'@bit-ocean/utils';var Q=Object.defineProperty,_=Object.defineProperties;var J=Object.getOwnPropertyDescriptors;var U=Object.getOwnPropertySymbols;var K=Object.prototype.hasOwnProperty,V=Object.prototype.propertyIsEnumerable;var z=r=>{throw TypeError(r)};var b=(r,e,t)=>e in r?Q(r,e,{enumerable:true,configurable:true,writable:true,value:t}):r[e]=t,D=(r,e)=>{for(var t in e||(e={}))K.call(e,t)&&b(r,t,e[t]);if(U)for(var t of U(e))V.call(e,t)&&b(r,t,e[t]);return r},k=(r,e)=>_(r,J(e));var E=(r,e,t)=>e.has(r)||z("Cannot "+t);var o=(r,e,t)=>(E(r,e,"read from private field"),t?t.call(r):e.get(r)),R=(r,e,t)=>e.has(r)?z("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(r):e.set(r,t),d=(r,e,t,s)=>(E(r,e,"write to private field"),e.set(r,t),t),h=(r,e,t)=>(E(r,e,"access private method"),t);var q=(r,e,t)=>new Promise((s,i)=>{var c=f=>{try{g(t.next(f));}catch(x){i(x);}},u=f=>{try{g(t.throw(f));}catch(x){i(x);}},g=f=>f.done?s(f.value):Promise.resolve(f.value).then(c,u);g((t=t.apply(r,e)).next());});var S=new Map([[HttpStatusCode.Unauthorized,"401: Unauthorized!"],[HttpStatusCode.Forbidden,"403: Forbidden!"],[HttpStatusCode.NotFound,"404: NotFound!"],[HttpStatusCode.MethodNotAllowed,"405: Method Not Allowed!"],[HttpStatusCode.Conflict,"409: Conflict!"],[HttpStatusCode.UnprocessableEntity,"422: Unprocessable Entity!"],[HttpStatusCode.TooManyRequests,"429: Too Many Requests!"],[HttpStatusCode.InternalServerError,"500: Internal Server Error!"],[HttpStatusCode.BadRequest,"502: Bad Gateway!"],[HttpStatusCode.BadGateway,"504: Gateway Timeout!"]]);w.defaults.timeout=3e4;w.defaults.headers.common["Content-Type"]="application/json;charset=utf-8";var a,y,I,A,T,n,v,G,L,N,m,F,B,H,j,O=class{constructor(e,t){R(this,n);R(this,a);R(this,y,{});R(this,I,false);R(this,A,false);R(this,T,[]);d(this,a,w.create(t)),d(this,y,e!=null?e:{});}initInterceptors(e){return q(this,null,function*(){if(o(this,I))return;d(this,I,true);let{router:t,message:s,i18n:i}=e!=null?e:{};h(this,n,v).call(this),h(this,n,G).call(this,{router:t,message:s});})}request(e){return o(this,a).request(e)}get(e,t,s){return o(this,a).get(e,D({params:t},s))}post(e,t,s){return o(this,a).post(e,t,s)}put(e,t,s){return o(this,a).put(e,t,s)}delete(e,t){return o(this,a).delete(e,t)}patch(e,t,s){return o(this,a).patch(e,t,s)}GET(e,t,s){return o(this,a).get(e,D({params:t},s))}POST(e,t,s){return o(this,a).post(e,t,s)}PUT(e,t,s){return o(this,a).put(e,t,s)}DELETE(e,t){return o(this,a).delete(e,t)}PATCH(e,t,s){return o(this,a).patch(e,t,s)}};a=new WeakMap,y=new WeakMap,I=new WeakMap,A=new WeakMap,T=new WeakMap,n=new WeakSet,v=function(){o(this,a).interceptors.request.use(e=>(h(this,n,L).call(this,e),h(this,n,N).call(this,e),e),e=>Promise.reject(e));},G=function(e){let{message:t}=e;o(this,a).interceptors.response.use(s=>{var u;if((u=s.config)!=null&&u.rawResponse)return s;let{data:i,msg:c}=s.data;return h(this,n,m).call(this,c,t),i},s=>q(this,null,function*(){let{response:i,config:c}=s;if(w.isCancel(s))throw s;if(c!=null&&c.isRefreshRequest)throw d(this,A,false),i==null?void 0:i.data.data;if(o(this,A))return h(this,n,F).call(this,c);let u=yield h(this,n,B).call(this,i,e);if(u)return u;throw i==null?void 0:i.data}));},L=function(e){e.headers["Accept-Language"]=LangUtils.getDefaultLang();},N=function(e){AuthUtils.isAuthenticated()&&(e.headers.Authorization=AuthUtils.getAuthorization());},m=function(e,t,s="success"){!t||!e||(Array.isArray(e)?e.forEach(i=>t[s](i)):t[s](e));},F=function(e){return new Promise(t=>{o(this,T).push({config:e,resolve:t});})},B=function(e,t){return q(this,null,function*(){var x,M;let{status:s=0,data:i,config:c}=e!=null?e:{},{message:u,router:g}=t!=null?t:{},f=(M=(x=i==null?void 0:i.msg)!=null?x:S.get(s))!=null?M:"Unknown error!";switch(s){case HttpStatusCode.Unauthorized:{try{if(o(this,y).enableRefreshToken){let P=yield h(this,n,j).call(this,c);if(P)return P}}catch(P){}h(this,n,m).call(this,f,u,"error"),h(this,n,H).call(this,g);break}case HttpStatusCode.Forbidden:{g==null||g.navigate({to:"/403",replace:true}),h(this,n,m).call(this,f,u,"error");break}case HttpStatusCode.InternalServerError:case HttpStatusCode.BadGateway:case HttpStatusCode.GatewayTimeout:{g==null||g.navigate({to:"/500",replace:true}),h(this,n,m).call(this,f,u,"error");break}default:{h(this,n,m).call(this,f,u,"error");break}}return false})},H=function(e){let{unauthorizedRedirectUrl:t="/login"}=o(this,y);AuthUtils.clearAccessToken(),AuthUtils.clearRefreshToken();let s=e==null?void 0:e.state.location.pathname;s!==t&&(e==null||e.navigate({to:t,replace:true,search:s===t?void 0:{redirect:s}}));},j=function(e){return q(this,null,function*(){d(this,A,true);let t=AuthUtils.getRefreshToken(),{accessToken:s,refreshToken:i}=yield this.POST("/auth/refresh",{token:t},{isRefreshRequest:true});if(AuthUtils.setAccessToken(s),AuthUtils.setRefreshToken(i),d(this,A,false),e){let c=yield this.request(k(D({},e),{headers:k(D({},e.headers),{Authorization:AuthUtils.getAuthorization()})}));return o(this,T).length>0&&(o(this,T).forEach(u=>u.resolve(this.request(u.config))),d(this,T,[])),c}return false})};export{O as HttpClient,S as errorMessageMap};//# sourceMappingURL=index.mjs.map //# sourceMappingURL=index.mjs.map