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

@tactics/axios-tools

v0.0.6

Published

Utilities to help ease use of Axios in javascript

Downloads

1,866

Readme

AxiosTools

Commonly use tooling around the axios library.

Mock


const successJson = {
    id: 'cde4fb59-8699-4fcf-be4e-37dae895dd73',
    name: 'My Product'
}

const failureJson = {
    message: 'failed to load the product with id : "cde4fb59-8699-4fcf-be4e-37dae895dd73"',
    error: 'BAD REQUEST'
};


const axiosMock = new AxiosMock();
axiosMock.get({
    success: successJson,
    failure: failureJson,
    failure_code: 400,
    time: 2700, // 2.7 sec
    successRate: 0.9 // success rate of 9 out of 10 calls
});

Interceptors

Authorization

Get an access token from storage and pass it along as param. When param is set, the access token is attached via the interceptor.


const AxiosInstance = axios.create();

AxiosInstance.defaults.params = { access_token : 'MY ACCESS TOKEN'};
AxiosInstance.interceptors.request.use(addAccessToken, interceptorFailure);

// or 

AxiosInstance
    .get(
        '/api',
        {
            params: {
                access_token: "MY ACCESS TOKEN"
            }
        }
)

Domain

Add a custom HTTP header with the domain the request came from. This will be derived from the javascript function "window.location.host.split(":")[0];"

Since this is a custom header, the header will be prefixed with X-. You can also provide a namespace to further specify the header.


const AxiosInstance = axios.create();
AxiosInstance.defaults.params = { namespace : 'My-Project'};

// X-My-Project-Domain 
AxiosInstance.interceptors.request.use(addDomainHeader, interceptorFailure);

Json

Most modern api's communicate request and response in JSON. To make clear, the api will send JSON and expects JSON back we add it to the headers of the request.


const AxiosInstance = axios.create();

AxiosInstance.interceptors.request.use(addJsonHeaders, interceptorFailure);

Language

When the api sends back textual fields that are shown to the end user. It needs to provide these in the correct language.


const AxiosInstance = axios.create();

AxiosInstance.defaults.params = { language : 'nl'};
AxiosInstance.interceptors.request.use(addLanguageHeader, interceptorFailure);

// or 

AxiosInstance
    .get(
        '/api',
        {
            params: {
                language: "nl"
            }
        }
    )


Device

Add a custom HTTP header with the device the request came from.

Since this is a custom header, the header will be prefixed with X-. You can also provide a namespace to further specify the header.


const AxiosInstance = axios.create();
AxiosInstance.defaults.params = {
    namespace : 'My-Project',
    device_id : 'MY DEVICE ID',
    device_name : 'MY DEVICE NAME'
};

// X-My-Project-Device-Id 
// X-My-Project-Device-Name
AxiosInstance.interceptors.request.use(addDeviceHeader, interceptorFailure);

// Or

AxiosInstance
    .get(
        '/api',
        {
            params: {
                device_id: "MY DEVICE ID",
                device_name: "MY DEVICE NAME"
            }
        }
    )