@yr/agent
v5.0.4
Published
HTTP request agent
Readme
Extension to superagent to support collapsing request queues, response duration, and a status code of 504 for timedout requests.
Usage
const agent = require('@yr/agent');
agent
.get('http://some/api')
.timeout(1000)
.retry(3)
.then((res) => {
console.log(res.body);
console.log(res.duration);
})
.catch((err) => {
// handle error...
});API
get(url, options): GET request for url. Returns superagent Request instance. Behaviour is configurable with the following options properties:
abort: abort existing (outstanding) request to same url (defaultfalse)id: optional string for tagging requestignoreQuery: ignore query parameters ofurlwhen matching existing, oustanding requests for the same url (defaultfalse)
Default behaviour is to return a cached Request instance for the same url (request collapsing). Setting both abort and ignoreQuery to true will automatically abort an outstanding request with a different query parameter:
agent
.get('http://some/api?search=foo')
.then((res) => {
// Aborted, won't fire
});
agent
.get('http://some/api?search=bar')
.then((res) => {
console.log(res.body);
});Although the full Superagent API is supported, it's recommended to use
then()(Promises) instead ofend()to allow for request collapsing
abortAll([filter]): abort all outstanding requests. Takes optional string or function filter. If filter is defined as a string, only aborts requests that are tagged with options.id. If filter is defined as function, only aborts requests that return true for filter(req).
agent
.get('http://some/api', { id: 'foo' })
.then((res) => {
// Aborted, won't fire
});
agent.abortAll('foo');