Registry / devops / axios-controller

axios-controller

JSON →
library1.7.3jsnpmunverified

A lightweight library (v1.7.3) for creating controller proxies that mirror your Web API Controllers using Axios. It simplifies making HTTP requests by allowing you to define methods that automatically map to Axios calls, with automatic response unwrapping (returns the `data` property by default). Released on GitHub, it supports both CommonJS and ES Modules, and includes TypeScript type definitions. Compared to raw Axios, it reduces boilerplate for CRUD operations and provides a structured, controller-based design pattern similar to MVC frameworks. Current stable version is 1.7.3, with a consistent API surface.

npm install axios-controller
INSTALL
IMPORT
SIG · AXIOS-CONTROLLER
A
axios-controller
devopsjavascriptv1.7.3
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 (buildController or axiosController)
import axiosController from 'axios-controller'
import { axiosController } from 'axios-controller'
The package exports a single default export named `axiosController` (or `buildController` in older versions). Named imports will not work.
require
const axiosController = require('axios-controller')
CommonJS require works fine; the default export is available directly.
buildController (if using named export from internal module)
import { buildController } from 'axios-controller'
Since v1.0.0, the named export `buildController` is also available alongside the default. Both are the same function.

Demonstrates creating an Axios instance, building a controller proxy, and defining methods that map to HTTP verbs.

import axios from 'axios'; import axiosController from 'axios-controller'; const axiosInstance = axios.create({ baseURL: 'https://api.example.com/' }); const Controller = axiosController.build(axiosInstance); const bookController = Controller('book', http => ({ all: _ => http.get(), get: id => http.get(id), create: book => http.post(book), update: book => http.put(book), delete: id => http.delete(id), })); // use the controller const books = await bookController.all(); console.log(books);
Debug
Known issues
deprecatedThe `build` function may be deprecated in future versions; use default export directly.
fix
Use `import axiosController from 'axios-controller'` and call `axiosController.build(...)` or use the default export as a function directly (if supported).
affects: >=1.0.0
gotchaResponse unwrapping is enabled by default: the `data` property is returned instead of the full Axios response object.
fix
To get the full response, set `unwrap: false` in the options: `axiosController.build(axiosInstance, { unwrap: false })`.
affects: >=1.0.0
gotchaThe controller callback receives an `http` object with methods (`get`, `post`, `put`, `delete`) that map to Axios requests, but the URL path is built by appending the method's path argument to the controller name.
fix
Ensure the path is relative to the controller base. For nested routes, use the full path inside the method call, e.g., `http.get('books/' + id)`.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: axiosController is not a function
Using a named import instead of default import.
fix
Use `import axiosController from 'axios-controller'` (default import) instead of `import { axiosController } from 'axios-controller'`.
TypeError: http.get is not a function
The controller callback expects an http object; if you passed a different argument or forgot to return an object, it may be undefined.
fix
Ensure the callback returns an object with methods that use http methods, e.g., `get: id => http.get(id)`.
Cannot read properties of undefined (reading 'get')
The built controller is expected to be a function; if you called `Controller` without `new`, it still returns a controller proxy, but if you mis-assigned the result, it may be undefined.
fix
Call `const bookController = Controller('book', ...)` and then `await bookController.get(1)`.
Upgrade
Version history
1.7.3latest on npm
Audit
Dependencies
axiosrequiredRequired peer dependency; axios-controller wraps axios instances.
Agent activity
13 hits · last 30 days
node
12
OpenAI (training)
1
Resources
axios-controller — npm install axios-controller · libregistry