@yr/agent

5.0.4 • Public • Published

NPM Version Build Status

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 (default false)
  • id: optional string for tagging request
  • ignoreQuery: ignore query parameters of url when matching existing, oustanding requests for the same url (default false)

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 of end() 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');

Package Sidebar

Install

npm i @yr/agent

Weekly Downloads

3

Version

5.0.4

License

MIT

Last publish

Collaborators

  • yr
  • saegrov