UNPKG

@dbs-portal/core-api

Version:

HTTP client and API utilities for DBS Portal

180 lines 4.85 kB
/** * Interceptor manager for handling request and response interceptors */ /** * Manages interceptors for requests and responses */ export class InterceptorManager { interceptors = []; nextId = 0; /** * Adds an interceptor */ use(onFulfilled, onRejected) { const id = this.nextId++; this.interceptors[id] = { onFulfilled, onRejected, }; return id; } /** * Removes an interceptor by ID */ eject(id) { if (this.interceptors[id]) { this.interceptors[id] = null; } } /** * Clears all interceptors */ clear() { this.interceptors = []; this.nextId = 0; } /** * Gets all active interceptors */ getInterceptors() { return this.interceptors.filter((interceptor) => interceptor !== null); } /** * Gets the number of active interceptors */ size() { return this.getInterceptors().length; } /** * Checks if there are any interceptors */ isEmpty() { return this.size() === 0; } /** * Executes all interceptors in sequence */ async executeAll(input, executor) { let result = input; for (const interceptor of this.getInterceptors()) { try { result = await Promise.resolve(executor(interceptor, result)); } catch (error) { // If an interceptor throws, we still want to continue with the next one // The specific error handling should be done in the executor throw error; } } return result; } /** * Executes interceptors with error handling */ async executeWithErrorHandling(input, onFulfilledExecutor, onRejectedExecutor) { let result = input; let lastError = null; for (const interceptor of this.getInterceptors()) { try { if (lastError && onRejectedExecutor) { // If there was an error and we have an error handler, use it lastError = await Promise.resolve(onRejectedExecutor(interceptor, lastError)); if (lastError === null || lastError === undefined) { // Error was handled, continue with normal flow lastError = null; continue; } } else if (!lastError) { // Normal execution result = await Promise.resolve(onFulfilledExecutor(interceptor, result)); } } catch (error) { lastError = error; } } if (lastError) { throw lastError; } return result; } /** * Creates a new interceptor manager with copied interceptors */ clone() { const cloned = new InterceptorManager(); cloned.interceptors = [...this.interceptors]; cloned.nextId = this.nextId; return cloned; } /** * Merges interceptors from another manager */ merge(other) { for (const interceptor of other.getInterceptors()) { this.interceptors.push(interceptor); } } /** * Filters interceptors based on a predicate */ filter(predicate) { const filtered = new InterceptorManager(); for (const interceptor of this.getInterceptors()) { if (predicate(interceptor)) { filtered.interceptors.push(interceptor); } } return filtered; } /** * Maps interceptors to a new type */ map(mapper) { return this.getInterceptors().map(mapper); } /** * Finds an interceptor by predicate */ find(predicate) { return this.getInterceptors().find(predicate); } /** * Checks if any interceptor matches the predicate */ some(predicate) { return this.getInterceptors().some(predicate); } /** * Checks if all interceptors match the predicate */ every(predicate) { return this.getInterceptors().every(predicate); } /** * Iterates over all interceptors */ forEach(callback) { this.getInterceptors().forEach(callback); } /** * Converts to array */ toArray() { return this.getInterceptors(); } /** * Gets interceptor statistics */ getStats() { const active = this.getInterceptors().length; const total = this.interceptors.length; return { total, active, removed: total - active, }; } } //# sourceMappingURL=interceptor-manager.js.map