1.7.3 • Published 1 year ago

axios-controller v1.7.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

axios-controller

Easily create a controller proxy that matches your Web API Controller using axios.

Installation

npm i axios-controller

Usage

CommonJS

const axios = require('axios');
const axiosController = require('axios-controller');

ES Module

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 => {
  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:

const Controller = buildController(axiosInstance, { unwrap: false });
1.7.3

1 year ago

1.7.2

1 year ago

0.6.3

3 years ago

0.6.2

3 years ago

0.6.1

3 years ago

0.6.0

3 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.3.0

4 years ago