From 694d638ccc0d91727cbcd9990690a9b29815353c Mon Sep 17 00:00:00 2001 From: Bryce Carr Date: Sat, 24 Oct 2020 06:20:41 +1100 Subject: [PATCH] feat: export the shouldRetryRequest method (#74) --- README.md | 20 ++++++++++++++++++++ src/index.ts | 2 +- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3aa0a91..67d8145 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/src/index.ts b/src/index.ts index 3cd5bc3..65269f3 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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.