1.0.2 • Published 6 years ago

fly-retry v1.0.2

Weekly downloads
3
License
ISC
Repository
github
Last release
6 years ago

fly-retry

fly plugin that intercepts failed requests and retries them whenever possible.

Installation

npm install fly-retry
or
yarn add fly-retry

Usage

// CommonJS
// const flyRetry = require('fly-retry');

// ES6
import flyRetry from 'fly-retry';

flyRetry(fly, { retries: 3 });

fly.get('http://example.com/test') // The first request fails and the second returns 'ok'
  .then(result => {
    result.data; // 'ok'
  });

// Exponential back-off retry delay between requests
flyRetry(fly, { retryDelay: flyRetry.exponentialDelay});

// Custom retry delay
flyRetry(fly, { retryDelay: (retryCount) => {
  return retryCount * 1000;
}});

// Works with custom fly instances
const client = fly.create({ baseURL: 'http://example.com' });
flyRetry(client, { retries: 3 });

client.get('/test') // The first request fails and the second returns 'ok'
  .then(result => {
    result.data; // 'ok'
  });

// Allows request-specific configuration
client
  .get('/test', {
    'fly-retry': {
      retries: 0
    }
  })
  .catch(error => { // The first request fails
    error !== undefined
  });

Note: Unless shouldResetTimeout is set, the plugin interprets the request timeout as a global value, so it is not used for each retry but for the whole request lifecycle.

Options

NameTypeDefaultDescription
retriesNumber3The number of times to retry before failing.
retryConditionFunctionisNetworkOrIdempotentRequestErrorA callback to further control if a request should be retried. By default, it retries if it is a network error or a 5xx error on an idempotent request (GET, HEAD, OPTIONS, PUT or DELETE).
shouldResetTimeoutBooleanfalseDefines if the timeout should be reset between retries
retryDelayFunctionfunction noDelay() { return 0; }A callback to further control the delay between retried requests. By default there is no delay between retries. Another option is exponentialDelay (Exponential Backoff). The function is passed retryCount and error.

Contribute

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D
1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago