Registry / http-networking / axios-middleware

axios-middleware

JSON →
library0.4.0jsnpmunverified

axios-middleware is a utility library designed to simplify the creation and management of HTTP middleware for Axios, aiming to offer more flexibility and testability compared to native Axios interceptors. It allows developers to register middleware objects with methods like `onRequest`, `onResponse`, and `onResponseError` to hook into different stages of a request lifecycle. The current stable version is 0.4.0, which was released as an 'End of life' version. The package is now abandoned, with the maintainer recommending alternatives due to breaking changes in newer Axios versions and a shift in recommended HTTP client practices. It supports Node.js environments and had a peer dependency on Axios versions `>=0.17.1 <1.2.0`.

npm install axios-middleware
INSTALL
IMPORT
SIG · AXIOS-MIDDLEWARE
A
axios-middleware
http-networkingjavascriptv0.4.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
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 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

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

default
import axiosMiddleware from 'axios-middleware';
import { axiosMiddleware } from 'axios-middleware';
The library primarily exports a default class/function to initialize the middleware service.
AxiosMiddleware
import axiosMiddleware from 'axios-middleware'; const middleware = new axiosMiddleware(client);
import { AxiosMiddleware } from 'axios-middleware';
While often used as a class, it's a default export, so the instantiation pattern is `new axiosMiddleware(...)` after a default import.
Middleware Handler Object
middleware.use({ onRequest(config) { /* ... */ }, onResponse(response) { /* ... */ } });
Middleware logic is defined via an object passed to the `use` method, containing methods like `onRequest`, `onResponse`, `onRequestError`, `onResponseError`, and `onSync`.

This quickstart demonstrates how to set up `axios-middleware` with an Axios instance, register request and response interceptors, and then make API calls, showcasing both success and error handling.

import axios from 'axios'; import axiosMiddleware from 'axios-middleware'; const client = axios.create({ baseURL: 'https://jsonplaceholder.typicode.com', timeout: 5000 }); const middleware = new axiosMiddleware(client); middleware.use({ onRequest(config) { console.log('[Middleware] Request sent:', config.method?.toUpperCase(), config.url); // Example: Add an authorization header config.headers['X-Custom-Auth'] = 'my-secret-token'; return config; }, onResponse(response) { console.log('[Middleware] Response received for', response.config.url, 'Status:', response.status); return response; }, onRequestError(error) { console.error('[Middleware] Request error:', error.message); return Promise.reject(error); }, onResponseError(error) { console.error('[Middleware] Response error for', error.config.url, 'Status:', error.response?.status, error.message); return Promise.reject(error); } }); // Make a request using the configured Axios instance client.get('/posts/1') .then(response => { console.log('API Response Data:', response.data); }) .catch(error => { console.error('API Call Failed:', error.message); }); // Example of a request that might trigger an error (e.g., non-existent endpoint) client.get('/nonexistent-path') .catch(error => { console.error('Non-existent path call failed, caught by application:', error.message); });
Debug
Known issues
breakingThe `HttpMiddleware` base class was removed in v0.3.0. Projects inheriting from this class will break.
fix
Refactor middleware classes to no longer extend `HttpMiddleware`. Instead, use plain objects or classes implementing the handler methods (`onRequest`, `onResponse`, etc.) directly with `middleware.use()`.
affects: >=0.3.0
breakingMethod prefixes for middleware handlers changed from `handle` to `on` in v0.1.3. For example, `handleRequestError` became `onRequestError`.
fix
Rename middleware handler methods (e.g., `handleRequest` to `onRequest`, `handleResponseError` to `onResponseError`).
affects: >=0.1.3
gotchaThis package has reached its End of Life (EOL) with version 0.4.0. The maintainer explicitly states it will no longer be maintained due to breaking changes in Axios and general shifts in recommended HTTP client practices.
fix
It is strongly recommended to migrate to an alternative solution or directly use Axios's built-in interceptors. Continuing to use this package may lead to compatibility issues with newer Axios versions and lack of security updates.
affects: >=0.4.0
gotchaThe package's peer dependency for `axios` is `>=0.17.1 <1.2.0`. Using `axios-middleware` with Axios v1.2.0 or newer versions (e.g., Axios v2.x) will likely lead to incompatibility issues and unexpected behavior.
fix
Either downgrade your `axios` version to match the peer dependency range (`<1.2.0`) or, preferably, migrate away from `axios-middleware` to a maintained solution that supports modern Axios versions or use Axios's native interceptors.
affects: >=0.4.0
Errors
Common errors & fixes
TypeError: Class extends value undefined is not a constructor or null
Attempting to extend the `HttpMiddleware` base class after upgrading to `axios-middleware` v0.3.0 or later, where this class was removed.
fix
Remove the `extends HttpMiddleware` clause from your middleware class definition. Implement handler methods directly or use plain objects for middleware.
Property 'handleRequestError' does not exist on type '...' or 'middleware.use' expects a different type of handler.
Using old `handle` prefixed method names (e.g., `handleRequestError`) after upgrading to `axios-middleware` v0.1.3 or later.
fix
Update method names from `handleX` to `onX` (e.g., `handleRequestError` becomes `onRequestError`, `handleResponse` becomes `onResponse`).
TypeError: Cannot read properties of undefined (reading 'interceptors') or TypeError: axiosInstance.interceptors is undefined
Incorrectly initializing `axios-middleware` with a non-Axios instance or a misconfigured Axios instance.
fix
Ensure `axiosMiddleware` is instantiated with a valid `axios` instance created using `axios.create()` or the global `axios` object.
Upgrade
Version history
0.4.0latest on npm
Audit
Dependencies
axiosrequiredPeer dependency for HTTP client integration; note the strict version range.
Agent activity
19 hits · last 30 days
node
18
OpenAI (training)
1
Resources
axios-middleware — npm install axios-middleware · libregistry