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-request-handler

v1.0.4

Published

An Axios handler for making requests with polling, lock and cancel support.

Downloads

2,154

Readme

axios-request

An axios handler for making requests with polling, lock and cancel support.


Installation

Examples

API


Installation

Install with yarn or npm

yarn add axios-request-handler
        #or
npm install axios-request-handler

API

import Request from 'axios-request-handler';

const requestInstance = new Request(url = '', options = {
    lockable: false, // if true if you try to make a request when there is a pending one, the second will not be executed
    cancelable: true, // if true if you try to make a request when there is a pending one, the first will be canceled and the new will executed
    errorHandler:(error,method) => {} // function for handling the errors
    ...axionOptions, // all the supported options from axios
})

// send request

requestInstance.get() // sends a get request with the above axios options
requestInstance.post()// sends a post request with the above axios options

requestInstance.get(options = {}) // sends a get request overriding the above options
requestInstance.post(options = {})// sends a post request overriding the above options

// cancel request

requestInstance.cancel() // cancel all requests
requestInstance.cancel(method = 'get') // cancel get request
requestInstance.cancel(method = 'post') // cancel post request

// polling requests
requestInstance.poll(intervalTime = 2000).get(callback = (response) => {
    //callback function that executes in every response
    //if return false the interval will discontinue
}, options = {})


//request status

requestInstance.isPending(method = '') // returns true when a request is on the fly
requestInstance.isUpdating(method = 'post') // returns true when a request is on the fly after interval request
requestInstance.isPolling(method = 'get') // returns true when polling is enabled

//setters

requestInstance.setOptions(options = {}) // changes the instance's options
requestInstance.setUrl(options = {}) // changes the instance's url

Examples

Basic

import Request from 'axios-request-handler';

const products = new Request('http://example.com/api/products', {
    params: {
        category: 'keyboards'
    }
});

products.get().then(res => (console.log(res.data)));

products.get({
    params: {
        category: 'mouses'
    }
}).then(res => (console.log(res.data)));

Polling

import Request from 'axios-request-handler';

const reviews = new Request('http://example.com/api/reviews');

reviews.poll(5000).get((response) => {
    console.log(response.data);
    // you can cancel polling by returning false
});

Cancel pending requests

import Request from 'axios-request-handler';

const reviews = new Request('http://example.com/api/reviews');

// cancel all
reviews.get();
reviews.cancel();

// cancel get request
reviews.get();
reviews.post();

reviews.cancel('get');

// cancel post request
reviews.get();
reviews.post();

reviews.cancel('post');

Check request status

import Request from 'axios-request-handler';

const reviews = new Request('http://example.com/api/reviews');

// pending status
reviews.get().then(res => {
    reviews.isPending(); // false
})
reviews.isPending(); // true

// updating status
reviews.poll(1000).get().then(res => {
   setTimeout(()=> {
        reviews.isPending('get'); // true
        reviews.isUpdating('get'); // true
    }, 1000);
})

reviews.isPending('get'); //true
reviews.isUpdating('get'); //false (this will be true when the interval request start and the request is pending)

reviews.cancel('get');

// polling status
reviews.poll(1000).get();
reviews.isPending(); //true
reviews.isUpdating(); //false
reviews.isPolling(); //true

Lockable, Cancelable

import Request from 'axios-request-handler';

 // cancelable
const reviews = new Request('http://example.com/api/reviews', {
    cancelable:true, //default is true
});
reviews.get(); // this request will be canceled by the next one
reviews.get();

// ----------------------

const reviews = new Request('http://example.com/api/reviews', {
    cancelable:false, //default is true
});
reviews.get(); // both requests will be executed
reviews.get();

// ----------------------

// lockable
const reviews = new Request('http://example.com/api/reviews', {
    lockable:true, //default is false
});
reviews.get();
reviews.get(); // this request will not be executed and will throw Promise error;

To run tests:

yarn test
   #or
npm test