Skip to content

Commit

Permalink
feat: export the shouldRetryRequest method (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdcarr authored Oct 23, 2020
1 parent fca9674 commit 694d638
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,26 @@ const res = await axios({
});
```

If you want to add custom retry logic without duplicating too much of the built-in logic, `rax.shouldRetryRequest` will tell you if a request would normally be retried:
```js
const res = await axios({
url: 'https://test.local',
raxConfig: {
// Override the decision making process on if you should retry
shouldRetry: err => {
const cfg = rax.getConfig(err);
if (cfg.currentRetryAttempt >= cfg.retry) return false // ensure max retries is always respected

// Always retry this status text, regardless of code or request type
if (err.response.statusText.includes('Try again')) return true

// Handle the request based on your other config options, e.g. `statusCodesToRetry`
return rax.shouldRetryRequest(err)
}
}
});
```

## How it works

This library attaches an `interceptor` to an axios instance you pass to the API. This way you get to choose which version of `axios` you want to run, and you can compose many interceptors on the same request pipeline.
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ function onError(err: AxiosError) {
* Determine based on config if we should retry the request.
* @param err The AxiosError passed to the interceptor.
*/
function shouldRetryRequest(err: AxiosError) {
export function shouldRetryRequest(err: AxiosError) {
const config = (err.config as RaxConfig).raxConfig;

// If there's no config, or retries are disabled, return.
Expand Down

0 comments on commit 694d638

Please sign in to comment.