{"id":17166,"library":"axios-middleware","title":"Axios Middleware Service","description":"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`.","status":"abandoned","version":"0.4.0","language":"javascript","source_language":"en","source_url":"https://github.com/emileber/axios-middleware","tags":["javascript","axios","request","adapter","middleware","response","http"],"install":[{"cmd":"npm install axios-middleware","lang":"bash","label":"npm"},{"cmd":"yarn add axios-middleware","lang":"bash","label":"yarn"},{"cmd":"pnpm add axios-middleware","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Peer dependency for HTTP client integration; note the strict version range.","package":"axios","optional":false}],"imports":[{"note":"The library primarily exports a default class/function to initialize the middleware service.","wrong":"import { axiosMiddleware } from 'axios-middleware';","symbol":"default","correct":"import axiosMiddleware from 'axios-middleware';"},{"note":"While often used as a class, it's a default export, so the instantiation pattern is `new axiosMiddleware(...)` after a default import.","wrong":"import { AxiosMiddleware } from 'axios-middleware';","symbol":"AxiosMiddleware","correct":"import axiosMiddleware from 'axios-middleware';\nconst middleware = new axiosMiddleware(client);"},{"note":"Middleware logic is defined via an object passed to the `use` method, containing methods like `onRequest`, `onResponse`, `onRequestError`, `onResponseError`, and `onSync`.","symbol":"Middleware Handler Object","correct":"middleware.use({\n  onRequest(config) { /* ... */ },\n  onResponse(response) { /* ... */ }\n});"}],"quickstart":{"code":"import axios from 'axios';\nimport axiosMiddleware from 'axios-middleware';\n\nconst client = axios.create({\n  baseURL: 'https://jsonplaceholder.typicode.com',\n  timeout: 5000\n});\n\nconst middleware = new axiosMiddleware(client);\n\nmiddleware.use({\n  onRequest(config) {\n    console.log('[Middleware] Request sent:', config.method?.toUpperCase(), config.url);\n    // Example: Add an authorization header\n    config.headers['X-Custom-Auth'] = 'my-secret-token';\n    return config;\n  },\n  onResponse(response) {\n    console.log('[Middleware] Response received for', response.config.url, 'Status:', response.status);\n    return response;\n  },\n  onRequestError(error) {\n    console.error('[Middleware] Request error:', error.message);\n    return Promise.reject(error);\n  },\n  onResponseError(error) {\n    console.error('[Middleware] Response error for', error.config.url, 'Status:', error.response?.status, error.message);\n    return Promise.reject(error);\n  }\n});\n\n// Make a request using the configured Axios instance\nclient.get('/posts/1')\n  .then(response => {\n    console.log('API Response Data:', response.data);\n  })\n  .catch(error => {\n    console.error('API Call Failed:', error.message);\n  });\n\n// Example of a request that might trigger an error (e.g., non-existent endpoint)\nclient.get('/nonexistent-path')\n  .catch(error => {\n    console.error('Non-existent path call failed, caught by application:', error.message);\n  });","lang":"javascript","description":"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."},"warnings":[{"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()`.","message":"The `HttpMiddleware` base class was removed in v0.3.0. Projects inheriting from this class will break.","severity":"breaking","affected_versions":">=0.3.0"},{"fix":"Rename middleware handler methods (e.g., `handleRequest` to `onRequest`, `handleResponseError` to `onResponseError`).","message":"Method prefixes for middleware handlers changed from `handle` to `on` in v0.1.3. For example, `handleRequestError` became `onRequestError`.","severity":"breaking","affected_versions":">=0.1.3"},{"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.","message":"This 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.","severity":"gotcha","affected_versions":">=0.4.0"},{"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.","message":"The 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.","severity":"gotcha","affected_versions":">=0.4.0"}],"env_vars":null,"last_verified":"2026-04-22T00:00:00.000Z","next_check":"2026-07-21T00:00:00.000Z","problems":[{"fix":"Remove the `extends HttpMiddleware` clause from your middleware class definition. Implement handler methods directly or use plain objects for middleware.","cause":"Attempting to extend the `HttpMiddleware` base class after upgrading to `axios-middleware` v0.3.0 or later, where this class was removed.","error":"TypeError: Class extends value undefined is not a constructor or null"},{"fix":"Update method names from `handleX` to `onX` (e.g., `handleRequestError` becomes `onRequestError`, `handleResponse` becomes `onResponse`).","cause":"Using old `handle` prefixed method names (e.g., `handleRequestError`) after upgrading to `axios-middleware` v0.1.3 or later.","error":"Property 'handleRequestError' does not exist on type '...' or 'middleware.use' expects a different type of handler."},{"fix":"Ensure `axiosMiddleware` is instantiated with a valid `axios` instance created using `axios.create()` or the global `axios` object.","cause":"Incorrectly initializing `axios-middleware` with a non-Axios instance or a misconfigured Axios instance.","error":"TypeError: Cannot read properties of undefined (reading 'interceptors') or TypeError: axiosInstance.interceptors is undefined"}],"ecosystem":"npm","meta_description":null}