https://github.com/wiiseguy/axios-controller
Easily create a proxy that matches your Web API Controller. Supports axios.
https://github.com/wiiseguy/axios-controller
Last synced: 6 months ago
JSON representation
Easily create a proxy that matches your Web API Controller. Supports axios.
- Host: GitHub
- URL: https://github.com/wiiseguy/axios-controller
- Owner: Wiiseguy
- License: mit
- Created: 2021-03-19T08:57:37.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2024-07-12T11:12:56.000Z (about 2 years ago)
- Last Synced: 2025-06-07T04:05:46.646Z (about 1 year ago)
- Language: JavaScript
- Size: 117 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# axios-controller
Easily create a controller proxy that matches your Web API Controller using axios.
### Installation
```
npm i axios-controller
```
### Usage
CommonJS
```js
const axios = require('axios');
const axiosController = require('axios-controller');
```
ES Module
```js
import axios from 'axios';
import axiosController from 'axios-controller';
```
```js
const axiosInstance = axios.create({
baseURL: 'https://api.example.com/'
});
const Controller = axiosController.build(axiosInstance);
const bookController = Controller('book', http => {
return {
all: _ => http.get(),
get: id => http.get(id),
create: book => http.post(book),
update: book => http.put(book),
delete: id => http.delete(id),
}
});
// You can use Controller to create multiple controller proxies
const authorController = Controller('author', http => {
return {
get: id => http.get(id),
getBooksByAuthor: id => http.get(`books/${id}`)
}
});
await bookController.get(1); // { id: 1, author: 8, title: 'ABC' }
await authorController.getBooksByAuthor(8); // [{ id: 1, author: 8, title: 'ABC' }, ...]
```
#### No response unwrapping
Note that axios-controller will 'unwrap' the response object. This means that by default the `data` property of the Axios response will be returned.
You can disable this behavior by setting the `unwrap` option to false:
```js
const Controller = buildController(axiosInstance, { unwrap: false });
```