{"library":"middleware-axios","title":"Axios Middleware Layer","description":"middleware-axios is a JavaScript and TypeScript library that extends the popular Axios HTTP client, providing an Express or Koa-like middleware interface for intercepting and modifying HTTP requests and responses. The current stable version is 3.0.0, which introduced dual CommonJS and ESM support and updated its peer dependency to Axios `^1.7.4`. The project demonstrates an active release cadence, with major versions addressing significant architectural changes like module system compatibility and dependency updates, while patch releases focus on type fixes and minor improvements. Its key differentiator lies in abstracting the native Axios interceptor pattern into a more structured, sequential middleware pipeline, allowing developers to easily add global or instance-specific pre-request and post-response logic, access `axios.defaults`, and control the flow with a `next()` function call, making request handling more modular and maintainable.","language":"javascript","status":"active","last_verified":"Thu Apr 23","install":{"commands":["npm install middleware-axios"],"cli":null},"imports":["import { create } from 'middleware-axios';","import type { MiddlewareAxiosInstance } from 'middleware-axios';","import type { AxiosMiddleware } from 'middleware-axios';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { create } from 'middleware-axios';\nimport type { AxiosResponse } from 'axios';\n\n// Create a wrapped Axios instance, similar to a normal axios.create()\nconst api = create({\n  baseURL: 'https://jsonplaceholder.typicode.com',\n  timeout: 5000,\n});\n\n// Add a middleware to log requests and responses\napi.use(async (config, next, defaults) => {\n  console.log(`[REQUEST] ${config.method?.toUpperCase()} ${config.url}`);\n  console.log('  Base URL from defaults:', defaults.baseURL);\n\n  // Crucially, await next(config) passes control to the next middleware or Axios itself\n  await next(config);\n\n  // This code runs after the response is received\n  const response: AxiosResponse = await config.axiosData; // Access the response data from the config\n  console.log(`[RESPONSE] ${config.method?.toUpperCase()} ${config.url} - Status: ${response.status}`);\n  // console.log('  Response Data:', response.data);\n});\n\n// Use the wrapped instance like a normal Axios instance\napi.get('/todos/1')\n  .then(response => {\n    console.log('\\n--- API Call Successful ---');\n    console.log('Todo Title:', response.data.title);\n    console.log('Response Status:', response.status);\n  })\n  .catch(error => {\n    console.error('\\n--- API Call Failed ---');\n    if (error.response) {\n      console.error('Error Status:', error.response.status);\n      console.error('Error Data:', error.response.data);\n    } else if (error.request) {\n      console.error('No response received:', error.request);\n    } else {\n      console.error('Error message:', error.message);\n    }\n  });\n\n// You can also access the pure Axios instance if needed\n// console.log(api.axiosInstance);","lang":"typescript","description":"This quickstart demonstrates how to create a `middleware-axios` instance, add a basic logging middleware that intercepts requests and responses, and then make a GET request using the wrapped instance.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}