UNPKG

@metis-w/api-client

Version:

Modern TypeScript HTTP API client with dynamic routes, parameterized endpoints, interceptors, and advanced features

99 lines 3.22 kB
export class InterceptorManager { constructor() { this.requestInterceptors = new Set(); this.responseInterceptors = new Set(); } /** * Adds a request interceptor to the client. * @param interceptor Adds a request interceptor to the client. */ addRequestInterceptor(interceptor) { const id = `auto-${Date.now()}-${Math.random()}`; this.requestInterceptors.add({ id, interceptor }); } /** * Adds a response interceptor to the client. * Interceptors can modify the response before it is returned. * @param interceptor Adds a response interceptor to the client. */ addResponseInterceptor(interceptor) { const id = `auto-${Date.now()}-${Math.random()}`; this.responseInterceptors.add({ id, interceptor }); } /** * Adds a request interceptor with a specific ID. * @param id - Unique identifier for the interceptor * @param interceptor - The interceptor function to be added */ addRequestInterceptorWithId(id, interceptor) { this.requestInterceptors.add({ id, interceptor }); } /** * Adds a response interceptor with a specific ID. * @param id - Unique identifier for the interceptor * @param interceptor - The interceptor function to be added */ addResponseInterceptorWithId(id, interceptor) { this.responseInterceptors.add({ id, interceptor }); } /** * Removes a request interceptor by its unique ID. * @param id - Unique identifier for the interceptor * @return True if the interceptor was removed, false if not found */ removeRequestInterceptor(id) { for (const config of this.requestInterceptors) { if (config.id === id) { return this.requestInterceptors.delete(config); } } return false; } /** * Removes a response interceptor by its unique ID. * @param id - Unique identifier for the interceptor * @return True if the interceptor was removed, false if not found */ removeResponseInterceptor(id) { for (const config of this.responseInterceptors) { if (config.id === id) { return this.responseInterceptors.delete(config); } } return false; } /** * Clears all request interceptors. */ clearRequestInterceptors() { this.requestInterceptors.clear(); } /** * Clears all response interceptors. */ clearResponseInterceptors() { this.responseInterceptors.clear(); } /** * Clears all request and response interceptors. */ clearAllInterceptors() { this.clearRequestInterceptors(); this.clearResponseInterceptors(); } /** * Gets all request interceptors for execution * @return Set of request interceptors */ getRequestInterceptors() { return this.requestInterceptors; } /** * Gets all response interceptors for execution * @return Set of response interceptors */ getResponseInterceptors() { return this.responseInterceptors; } } //# sourceMappingURL=interceptor-manager.js.map