npm: possibly marvellous

    axios-controller
    TypeScript icon, indicating that this package has built-in type declarations

    0.6.2 • Public • Published

    axios-controller

    Easily create a proxy that matches your Web API Controller.

    Installation

    npm i axios-controller
    

    Usage

    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 });

    Install

    npm i axios-controller

    DownloadsWeekly Downloads

    37

    Version

    0.6.2

    License

    MIT

    Unpacked Size

    13.5 kB

    Total Files

    9

    Last publish

    Collaborators

    • wiiseguy