A lightweight library to intercept and modify fetch requests and responses, inspired by Angular HTTP interceptors. Version 2.4.0 is the latest stable release, with infrequent updates. It monkey-patches the global fetch function to allow addition of request/response interceptors in browser, Node, and WebWorker environments. Unlike other interceptors, it uses a simple register/unregister pattern and supports TypeScript definitions, making it easy to add logging, authentication headers, or error handling without modifying fetch calls directly. It requires fetch to be available (polyfill suggested for older environments).
npm install fetch-interceptVerified import paths — ran on the pinned version, not inferred.
Demonstrates registering interceptors to modify fetch requests and responses, then unregistering.
Keep track of unregister functions and call them in reverse order to fully clean up.
Always return [url, config] from request interceptor, even if unchanged.
Upgrade to version 2.x for better AbortController support, or manually handle signal in config.
Consider alternative interceptors like 'undici' or custom wrappers if active maintenance is required.
Ensure request interceptor returns a tuple: return [url, config];
Use default import: import fetchIntercept from 'fetch-intercept';
Use optional chaining: config?.headers, or provide default empty object: const newConfig = config || {};No dependency data recorded yet.