Giter Site home page Giter Site logo

anubhavsrivastava / axios-data-unpacker Goto Github PK

View Code? Open in Web Editor NEW
12.0 3.0 1.0 1.61 MB

Axios interceptor that unpacks HTTP responses so that you can focus on actual server data

License: MIT License

JavaScript 100.00%
axios axios-ecosystem axios-restful axios-instance axios-plugin middleware axios-middleware

axios-data-unpacker's Introduction

Axios Data Unpacker

Axios middleware/interceptor that unpacks HTTP responses so that you can focus on actual response

Build Status Coverage Status PRs Welcome GitHub issues HitCount

NPM


Introduction

Axios data unpacker is interceptor for axios that unpacks data from axios standard response and makes API response content to be called so that one can focus on actual response.

The Problem

Any HTTP request using axios will return into following object that is available to callee function,

{
    // `data` is the response that was provided by the server
    data: {},

    // `status` is the HTTP status code from the server response
    status: 200,

    // `statusText` is the HTTP status message from the server response
    statusText: 'OK',

    // `headers` the headers that the server responded with
    headers: {},

    // `config` is the config that was provided to `axios` for the request
    config: {}
}

This would imply that application has to unpack data object at all places of consumption something like,

getUsers() {
    return axios.get('/users').then( function (result) {
        return result.data;
    });
}

or something like,

getUsers() {
    return axios.get('/users').then(result => result.data);
}

Solution

The above consumption code changes with this module. The above code or all places of consumption would change as below.

getUsers() {
    return axios.get('/users');
}

Install

$ npm install axios-data-unpacker --save

or

yarn add axios-data-unpacker

Usage

Important : This should be last interceptor to be added as response interceptor for your axios instance. This is important because any other response interceptor in chain may use values from complete axios response, like status or headers.

  • Simple usage

    import axios from 'axios';
    import {axiosResponseDataUnpacker} from 'axios-data-unpacker';
    
    // after adding other response interceptors
    axiosResponseDataUnpacker(axios)
    

    axiosResponseDataUnpacker function also accepts instance of axios as its parameter.

  • Default Instance

    import axios from 'axios';
    import axiosDataUnpacker from 'axios-data-unpacker';
    ... //other chain of interceptors and config
    axios.interceptors.response.use(axiosDataUnpacker);
    
  • At instance level

    import axiosDataUnpacker from 'axios-data-unpacker';
    const instance = axios.create();
    ... //other chain of interceptors and config
    instance.interceptors.response.use(axiosDataUnpacker);
    

Configuration

One can disable this interceptor by passing packResponseData as configuration in axios instance or per axios api call. This configuration can also be set on axios.default object.

Setting Name type description default value
packResponseData Boolean Flag to disable data unpacking false
  1. To disable unpacking for specific call (in case API layer needs to work with header, status, config from standard axios response)

     axios.get('/users', { packResponseData;:true } ).then(response=>{
         //response is standard axios response with config, header, status, data, statusText
         response.header('csrf') // sample usage of non-data fields
     })
    

    This config as parameter is available for all calls(get, post, put, etc) in axios, refer here.

  2. To disable interceptor for all calls of a instance (if already configured as interceptor in axios default configuration)

    const instance = axios.create({packResponseData: true});
    ... //other chain of interceptors and config
    instance.interceptors.response.use(axiosDataUnpacker);
    

Contribution

Suggestions and PRs are welcome!

Please read the contribution guidelines to get started.


License

Open Source Love

refer LICENSE file in this repository.


axios-data-unpacker's People

Contributors

anubhavsrivastava avatar dependabot[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

ntiyison

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.