npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

axios-data-unpacker

v1.0.0

Published

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

Downloads

331

Readme

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

    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.