axios-middleware

raw JSON →
0.4.0 verified Sat Apr 25 auth: no javascript deprecated

A simple middleware service for axios HTTP requests. Version 0.4.0 is the final release as the package is deprecated. It allows intercepting axios requests and responses via middleware functions, supporting async handlers and custom adapters. Key differentiators: a clean middleware pattern with onRequest/onResponse/onSync hooks, vs axios's native interceptors. Requires axios >=0.17.1 <1.2.0. The package is unmaintained and alternatives should be considered.

error Cannot find module 'axios-middleware'
cause The package was not installed or is not in node_modules.
fix
Run 'npm install axios-middleware' (ensure axios is also installed as a peer dependency).
error Class extends HttpMiddleware is not a constructor
cause Attempting to use the deprecated HttpMiddleware base class (removed in v0.3.0).
fix
Use a plain object for middleware instead of a class inheriting from HttpMiddleware.
error TypeError: Cannot read property 'register' of undefined
cause The HttpMiddlewareService instance was not created correctly or axios is not passed.
fix
Ensure you instantiate service with: new HttpMiddlewareService(axios).
deprecated axios-middleware is deprecated and no longer maintained. The last version (0.4.0) is the end-of-life release.
fix Migrate to a maintained alternative like axios interceptors or a modern HTTP client library (e.g., ky, got).
breaking From v0.3.0, the HttpMiddleware base class was removed. Middleware must be plain objects, not class instances.
fix Change any middleware defined as class MyMiddleware extends HttpMiddleware to a plain object with onRequest/onResponse/onSync methods.
breaking In v0.1.3, handler method names changed: 'handleRequestError' became 'onRequestError', 'handleResponseError' became 'onResponseError'.
fix Update middleware handlers to use the new 'on' prefix naming convention.
npm install ax-middleware
yarn add ax-middleware
pnpm add ax-middleware

Shows how to create a middleware service, register a middleware with request/response/sync handlers, and use axios normally.

import axios from 'axios';
import { HttpMiddlewareService } from 'axios-middleware';

const service = new HttpMiddlewareService(axios);

// Add a middleware that logs requests
service.register({
  onRequest(config) {
    console.log('Request:', config.url);
    return config;
  },
  onResponse(response) {
    console.log('Response:', response.status);
    return response;
  },
  onSync(config, req) {
    // Sync handlers if using an adapter that blocks
    return config;
  }
});

// Use axios as normal
axios.get('/api/data')
  .then(res => console.log(res.data))
  .catch(err => console.error(err));