axios-request
An axios handler for making requests with polling, lock and cancel support.
Installation
yarn add axios-request-handler #or npm install axios-request-handler
API
; const requestInstance = 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 {} // function for handling the errors ...axionOptions // all the supported options from axios // send request requestInstance // sends a get request with the above axios optionsrequestInstance// sends a post request with the above axios options requestInstance // sends a get request overriding the above optionsrequestInstance// sends a post request overriding the above options // cancel request requestInstance // cancel all requestsrequestInstance // cancel get requestrequestInstance // cancel post request // polling requestsrequestInstance //request status requestInstance // returns true when a request is on the flyrequestInstance // returns true when a request is on the fly after interval requestrequestInstance // returns true when polling is enabled //setters requestInstance // changes the instance's optionsrequestInstance // changes the instance's url
Examples
Basic
; const products = 'http://example.com/api/products' params: category: 'keyboards' ; products; products;
Polling
; const reviews = 'http://example.com/api/reviews'; reviews;
Cancel pending requests
; const reviews = 'http://example.com/api/reviews'; // cancel allreviews;reviews; // cancel get requestreviews;reviews; reviews; // cancel post requestreviews;reviews; reviews;
Check request status
; const reviews = 'http://example.com/api/reviews'; // pending statusreviewsreviews; // true // updating statusreviews reviews; //truereviews; //false (this will be true when the interval request start and the request is pending) reviews; // polling statusreviews;reviews; //truereviews; //falsereviews; //true
Lockable, Cancelable
; // cancelableconst reviews = 'http://example.com/api/reviews' cancelable:true //default is true;reviews; // this request will be canceled by the next onereviews; // ---------------------- const reviews = 'http://example.com/api/reviews' cancelable:false //default is true;reviews; // both requests will be executedreviews; // ---------------------- // lockableconst reviews = 'http://example.com/api/reviews' lockable:true //default is false;reviews;reviews; // this request will not be executed and will throw Promise error;
To run tests:
yarn test #or npm test