UNPKG

axios-multi-api

Version:

[npm-url]: https://npmjs.org/package/axios-multi-api [npm-image]: http://img.shields.io/npm/v/axios-multi-api.svg

2 lines 4.31 kB
var m=Object.defineProperty;var R=Object.getOwnPropertyDescriptor;var h=(u,e,t,n)=>{for(var r=n>1?void 0:n?R(e,t):e,s=u.length-1,i;s>=0;s--)(i=u[s])&&(r=(n?i(e,t,r):i(r))||r);return n&&r&&m(e,t,r),r};import{applyMagic as q}from"js-magic";import{applyMagic as b}from"js-magic";var g=class{logger;requestErrorService;constructor(e,t){this.logger=e,this.requestErrorService=t}process(e){var n;(n=this.logger)!=null&&n.warn&&this.logger.warn("API ERROR",e);let t=e;typeof e=="string"&&(t=new Error(e)),this.requestErrorService&&(typeof this.requestErrorService.process<"u"?this.requestErrorService.process(t):typeof this.requestErrorService=="function"&&this.requestErrorService(t))}};var p=class{requestInstance;timeout=3e4;cancellable=!1;strategy="reject";flattenResponse=!0;defaultResponse=null;axios;logger;requestErrorService;requestsQueue;constructor({axios:e,baseURL:t="",timeout:n=null,cancellable:r=!1,strategy:s=null,flattenResponse:i=null,defaultResponse:o={},logger:l=null,onError:a=null,...c}){this.axios=e,this.timeout=n!==null?n:this.timeout,this.strategy=s!==null?s:this.strategy,this.cancellable=r||this.cancellable,this.flattenResponse=i!==null?i:this.flattenResponse,this.defaultResponse=o,this.logger=l||(globalThis?globalThis.console:null)||null,this.requestErrorService=a,this.requestsQueue=new Map,this.requestInstance=e.create({...c,baseURL:t,timeout:this.timeout})}getInstance(){return this.requestInstance}__get(e){return e in this?this[e]:this.prepareRequest.bind(this,e)}prepareRequest(e,t,n=null,r=null){return this.handleRequest({type:e,url:t,data:n,config:r})}buildRequestConfig(e,t,n,r){let s=e.toLowerCase();return{...r,url:t,method:s,[s==="get"||s==="head"?"params":"data"]:n||{}}}processRequestError(e,t){if(this.isRequestCancelled(e,t))return;t.onError&&typeof t.onError=="function"&&t.onError(e),new g(this.logger,this.requestErrorService).process(e)}async outputErrorResponse(e,t){let n=this.isRequestCancelled(e,t),r=t.strategy||this.strategy;return n&&!t.rejectCancelled?this.defaultResponse:r==="silent"?(await new Promise(()=>null),this.defaultResponse):r==="reject"||r==="throwError"?Promise.reject(e):this.defaultResponse}isRequestCancelled(e,t){return this.axios.isCancel(e)}addCancellationToken(e){if(!this.cancellable&&!e.cancellable)return{};if(typeof e.cancellable<"u"&&!e.cancellable)return{};if(typeof AbortController>"u")return console.error("AbortController is unavailable in your ENV."),{};let{method:t,baseURL:n,url:r,params:s,data:i}=e,o=JSON.stringify([t,n,r,s,i]).substring(0,55**5),l=this.requestsQueue.get(o);l&&l.abort();let a=new AbortController;return this.requestsQueue.set(o,a),{signal:a.signal}}async handleRequest({type:e,url:t,data:n=null,config:r=null}){let s=null,i=r||{},o=this.buildRequestConfig(e,t,n,i);o={...this.addCancellationToken(o),...o};try{s=await this.requestInstance.request(o)}catch(l){return this.processRequestError(l,o),this.outputErrorResponse(l,o)}return this.processResponseData(s)}processResponseData(e){return e.data?this.flattenResponse?typeof e.data=="object"&&typeof e.data.data<"u"&&Object.keys(e.data).length===1?e.data.data:e.data:e:this.defaultResponse}};p=h([b],p);var d=class{requestHandler;endpoints;logger;constructor({axios:e,apiUrl:t,endpoints:n,timeout:r=null,cancellable:s=!1,strategy:i=null,flattenResponse:o=null,defaultResponse:l={},logger:a=null,onError:c=null,...f}){this.endpoints=n,this.logger=a,this.requestHandler=new p({...f,baseURL:t,axios:e,timeout:r,cancellable:s,strategy:i,flattenResponse:o,defaultResponse:l,logger:a,onError:c})}getInstance(){return this.requestHandler.getInstance()}__get(e){return e in this?this[e]:this.endpoints[e]?this.handleRequest.bind(this,e):this.handleNonImplemented.bind(this,e)}async handleRequest(...e){let t=e[0],n=this.endpoints[t],r=e[1]||{},s=e[2]||{},i=e[3]||{},o=n.url.replace(/:[a-z]+/gi,c=>s[c.substring(1)]?s[c.substring(1)]:c),l=null,a={...n};return delete a.url,delete a.method,l=await this.requestHandler[(n.method||"get").toLowerCase()](o,r,{...i,...a}),l}handleNonImplemented(e){var t;return(t=this.logger)!=null&&t.log&&this.logger.log(`${e} endpoint not implemented.`),Promise.resolve(null)}};d=h([q],d);var L=u=>new d(u);export{d as ApiHandler,g as RequestErrorHandler,p as RequestHandler,L as createApiFetcher}; //# sourceMappingURL=index.mjs.map