Skip to content

flexdinesh/axios-retry-interceptor

Repository files navigation

Axios Retry Interceptor

Build Status dependencies Status npm version License: MIT

Configurable Axios Interceptor to retry failed http calls.

Install

npm install --save axios-retry-interceptor

Usage

Import

import retryInterceptor from 'axios-retry-interceptor';
// or
const retryInterceptor = require('axios-retry-interceptor');

Set the interceptor for your axios instance. Voila! ✨

retryInterceptor(axios, {
  maxAttempts: 3,
  waitTime: 1000
});

API

retryInterceptor(axiosInstance, options)

  • axiosInstance - your axios instance
  • options - config for retry interceptor

Options

maxAttempts

Max number of times the interceptor should retry the failed http call.

Type: Number

Default: 3

Example: maxAttempts: 5

waitTime

Duration between each retry attempt in milliseconds(1s=1000ms).

Type: Number

Default: 0

Example: waitTime: 3000

errorCodes

Response errorCodes for which the interceptor should retry.

Ideally any implementation should retry only 5xx status(server errors) and should not retry 4xx status(client errors). The reason is, if a http call fails with a client error, then the retry call will have the same headers/params and will obviously fail. So by default all 5xx errors will be retried. If you want to customize the status for which the retries should be made, use this config.

Type: Array

Default: []

Example: errorCodes: [500, 501, 401]

Author's note

Ideally only idempotent http methods (GET, PUT, DELETE, HEAD, OPTIONS) should be retried on failed http calls. Non-idempotent methods like POST should NOT be retried and that's why this library will not permit retry of non-idempotent methods.

License

MIT © Dinesh Pandiyan