Registry / testing / fetch-interceptor

fetch-interceptor

JSON →
library1.1.0jsnpmunverified

A lightweight library to intercept and modify fetch requests and responses in the browser. Current stable version is 1.1.0. It provides hooks for before request, on response success, and on response failure. Key differentiator is its simplicity and zero dependencies, unlike more complex alternatives like Mock Service Worker or Polly.js. The library is actively maintained with a small API surface, making it easy to integrate into any project that uses the Fetch API. It is released on npm and supports CommonJS and ES modules via module field.

npm install fetch-interceptor
INSTALL
IMPORT
SIG · FETCH-INTERCEPTOR
F
fetch-interceptor
testingjavascriptv1.1.0
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

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

default
✓ import fetchInterceptor from 'fetch-interceptor';
✗ const { register } = require('fetch-interceptor');
The library exports a default object. Use default import for ESM or require for CJS.
FetchInterceptor
✓ const FetchInterceptor = require('fetch-interceptor');
✗ import { FetchInterceptor } from 'fetch-interceptor';
In CJS, require gives the default export. Using named import will not work.
register
✓ const interceptor = FetchInterceptor.register({ onBeforeRequest, onRequestSuccess, onRequestFailure });
✗ const interceptor = FetchInterceptor({ onBeforeRequest });
register must be called as a method of the default export, not as a direct function. Omitting arguments is allowed but hooks will be undefined.
unregister
✓ interceptor.unregister();
✗ FetchInterceptor.unregister();
unregister is a method on the object returned by register, not on the imported default.

This shows how to import fetch-interceptor, register interceptor hooks, make a fetch request, and unregister the interceptor.

const FetchInterceptor = require('fetch-interceptor'); const interceptor = FetchInterceptor.register({ onBeforeRequest(request, controller) { console.log('Request:', request); // Optionally modify request headers or delay }, onRequestSuccess(response, request, controller) { console.log('Response success:', response); }, onRequestFailure(response, request, controller) { console.log('Response failure:', response); } }); fetch('https://jsonplaceholder.typicode.com/posts/1') .then(res => res.json()) .then(data => console.log('Data:', data)); interceptor.unregister();
Debug
Known issues
gotchaThe interceptor only works for fetch calls made after register() is called. Existing fetch calls are not intercepted.
fix
Call register() before any fetch you want to intercept.
affects: >=1.0.0
gotchaThe interceptor does not intercept fetch calls made in other execution contexts (e.g., iframes, workers). It only works in the same window where it is registered.
fix
Ensure the interceptor is registered in the same context as the fetch calls.
affects: >=1.0.0
gotchaCalling register() multiple times overwrites previous interceptor hooks. There is no stacking of interceptors.
fix
Use a single interceptor or chain hooks manually within the callbacks.
affects: >=1.0.0
breakingThe hooks receive a 'controller' parameter that allows modifying the request/response. The API may change in future versions.
fix
Check the documentation for the current controller API and avoid relying on undocumented behaviors.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: FetchInterceptor.register is not a function
Using named import instead of default import in ESM or wrong require.
fix
Use `import fetchInterceptor from 'fetch-interceptor'` or `const FetchInterceptor = require('fetch-interceptor');`
SyntaxError: Unexpected identifier at require('fetch-interceptor')
Using require in an ESM module that doesn't support CommonJS.
fix
Use `import fetchInterceptor from 'fetch-interceptor';` instead of require.
Error: Interceptor not registered
Calling interceptor.unregister() before register() or on a different interceptor instance.
fix
Ensure you call unregister only on the object returned by register().
fetch is not defined
The library depends on global fetch (e.g., browser or Node 18+). In Node <18, fetch may not be available.
fix
Use a polyfill like node-fetch or upgrade Node to 18+.
Upgrade
Version history
1.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
4 hits · last 30 days
node
4
Resources
fetch-interceptor — npm install fetch-interceptor · libregistry