Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

axios-retry-after

1.0.0 • Public • Published

axios-retry-after

Build Status Dependency Status Download Status

A tiny HTTP retry interceptor for axios.

This interceptor catches HTTP 429 errors, reads the Retry-After header, and retries the request at the proper type.

Installation

npm install --save axios-retry-after

Example usage

const axios = require('axios')
const retry = require('axios-retry-after')
const client = axios.createClient()
client.interceptors.response.use(null, retry(client))

Customizing retry behavior

You can optionally customize the behavior of this interceptor by passing a second argument including one or more of the methods demonstrated below:

client.interceptors.response.use(null, retry(client, {
  // Determine when we should attempt to retry
  isRetryable (error) {
    return (
      error.response && error.response.status === 429 &&
      // Use X-Retry-After rather than Retry-After, and cap retry delay at 60 seconds
      error.response.headers['x-retry-after'] && error.response.headers['x-retry-after'] <= 60
    )
  }
 
  // Customize the wait behavior
  wait (error) {
    return new Promise(
      // Use X-Retry-After rather than Retry-After 
      resolve => setTimeout(resolve, error.response.headers['x-retry-after'])
    )
  }
 
  // Customize the retry request itself
  retry (axios, error) {
    if (!error.config) {
      throw error
    }
 
    // Apply request customizations before retrying
    // ...
 
    return axios(error.config)
  }
}))

Install

npm i axios-retry-after

DownloadsWeekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

5.23 kB

Total Files

5

Last publish

Collaborators

  • avatar