Registry / http-networking / fetch-intercept

fetch-intercept

JSON →
library2.4.0jsnpmunverified

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-intercept
INSTALL
IMPORT
SIG · FETCH-INTERCEPT
F
fetch-intercept
http-networkingjavascriptv2.4.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
✓ import fetchIntercept from 'fetch-intercept';
✗ import { fetchIntercept } from 'fetch-intercept';
The package exports a default object, not named exports. Use default import.
register
✓ import fetchIntercept from 'fetch-intercept'; const unregister = fetchIntercept.register({...});
✗ import { register } from 'fetch-intercept';
register is a method on the default export, not a named export.
unregister
✓ import fetchIntercept from 'fetch-intercept'; const unregister = fetchIntercept.register({...}); unregister();
✗ import { unregister } from 'fetch-intercept';
unregister is a function returned by register(), not a named export.

Demonstrates registering interceptors to modify fetch requests and responses, then unregistering.

import fetchIntercept from 'fetch-intercept'; // Register interceptors const unregister = fetchIntercept.register({ request: function (url, config) { // Modify the request: add headers const newConfig = { ...config, headers: { ...config?.headers, 'X-Interceptor': 'true' } }; return [url, newConfig]; }, response: function (response) { // Modify the response: log status console.log('Response status:', response.status); return response; }, }); // Use fetch normally fetch('https://jsonplaceholder.typicode.com/todos/1') .then(response => response.json()) .then(data => console.log(data)) .finally(() => unregister()); // Clean up export {}; // Ensure module context for TypeScript
Debug
Known issues
gotchafetch-intercept modifies the global fetch. Calling register() multiple times stacks interceptors; unregister() removes the last registered interceptor.
fix
Keep track of unregister functions and call them in reverse order to fully clean up.
affects: >=1.0
gotchaThe request interceptor must return an array of [url, config] or undefined to retain original. Returning null or other types may break fetch.
fix
Always return [url, config] from request interceptor, even if unchanged.
affects: >=1.0
gotchafetch-intercept does not work with AbortController in older versions; abort signals may be lost in interceptors.
fix
Upgrade to version 2.x for better AbortController support, or manually handle signal in config.
affects: <2.0
deprecatedThe library has not been updated for several years; future browser fetch changes may break compatibility.
fix
Consider alternative interceptors like 'undici' or custom wrappers if active maintenance is required.
affects: all
Errors
Common errors & fixes
TypeError: Failed to fetch
Interceptor does not return proper [url, config] array or returns undefined.
fix
Ensure request interceptor returns a tuple: return [url, config];
fetchInterceptor.register is not a function
Incorrect import syntax (e.g., named import instead of default).
fix
Use default import: import fetchIntercept from 'fetch-intercept';
Cannot read properties of undefined (reading 'headers')
Config object is undefined when no options are passed to fetch.
fix
Use optional chaining: config?.headers, or provide default empty object: const newConfig = config || {};
Upgrade
Version history
2.4.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
14
Resources
fetch-intercept — npm install fetch-intercept · libregistry