https://github.com/compwright/axios-retry-after
A tiny HTTP 429 Retry-After interceptor for axios
https://github.com/compwright/axios-retry-after
Last synced: 9 months ago
JSON representation
A tiny HTTP 429 Retry-After interceptor for axios
- Host: GitHub
- URL: https://github.com/compwright/axios-retry-after
- Owner: compwright
- License: mit
- Created: 2020-07-20T19:09:25.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-19T13:17:53.000Z (over 1 year ago)
- Last Synced: 2025-04-09T02:38:02.243Z (9 months ago)
- Language: JavaScript
- Homepage:
- Size: 445 KB
- Stars: 8
- Watchers: 1
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# axios-retry-after
[](https://travis-ci.org/compwright/axios-retry-after)
[](https://www.npmjs.com/package/axios-retry-after)
[](https://github.com/sponsors/compwright)
A tiny HTTP retry interceptor for [axios](https://www.npmjs.com/package/axios).
This interceptor catches HTTP 429 errors, reads the `Retry-After` header, and retries the request at the proper type.
## Installation
With NPM:
```bash
npm install --save axios-retry-after
```
With Yarn:
```bash
yarn add axios-retry-after
```
## Example usage
```javascript
import axios from 'axios'
import retry from '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:
```javascript
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)
}
}))
```