UNPKG

@vegajs/http-client-adapter

Version:

A flexible, type-safe HTTP client adapter for TypeScript and JavaScript. Supports custom adapters and includes an out-of-the-box Axios adapter for easy HTTP request handling.

1 lines 1.07 kB
class e{constructor(){this.requestInterceptors=[],this.responseInterceptors=[],this.nextId=0}useRequest(e){const t=this.nextId++;return this.requestInterceptors.push({fulfilled:e,id:t}),t}useResponse(e){const t=this.nextId++;return this.responseInterceptors.push({fulfilled:e,id:t}),t}ejectRequest(e){const t=this.requestInterceptors.findIndex((t=>t?.id===e));-1!==t&&(this.requestInterceptors[t]=null)}ejectResponse(e){const t=this.responseInterceptors.findIndex((t=>t?.id===e));-1!==t&&(this.responseInterceptors[t]=null)}clearRequestInterceptors(){this.requestInterceptors=[]}clearResponseInterceptors(){this.responseInterceptors=[]}async executeRequestInterceptors(e){let t=e;for(const s of this.requestInterceptors)if(null!==s)try{t=await s.fulfilled(t)}catch(r){throw console.error("Error in request interceptor:",r),r}return t}async executeResponseInterceptors(e){let t=e;for(const s of this.responseInterceptors)if(null!==s)try{t=await s.fulfilled(t)}catch(r){throw console.error("Error in response interceptor:",r),r}return t}}export{e as InterceptorManager};