From fa17de46bd6c6c33db99aca5668a3d58a81c761e Mon Sep 17 00:00:00 2001 From: Justin Beckwith Date: Tue, 30 Jan 2018 12:36:24 -0800 Subject: [PATCH] feat: add support for onRetryAttempt handler --- README.md | 10 ++++++++++ src/index.ts | 14 +++++++++++++- test/index.ts | 20 ++++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 5238c1e..58bd0da 100644 --- a/README.md +++ b/README.md @@ -51,15 +51,25 @@ const res = await axios({ raxConfig: { // Retry 3 times before giving up. Defaults to 3. retry: 3, + // Milliseconds to delay at first. Defaults to 100. retryDelay: 100, + // # HTTP methods to automatically retry. Defaults to: // ['GET', 'HEAD', 'OPTIONS', 'DELETE', 'PUT'] httpMethodsToRetry: ['GET', 'HEAD', 'OPTIONS', 'DELETE', 'PUT'], + // The response status codes to retry. Supports a double // array with a list of ranges. Defaults to: // [[100, 199], [429, 429], [500, 599]] httpStatusCodesToRetry: [[100, 199], [429, 429], [500, 599]], + + // You can detect when a retry is happening, and figure out how many + // retry attempts have been made + onRetryAttempt: (err) => { + const cfg = rax.getConfig(err); + console.log(`Retry attempt #${cfg.currentRetryAttempt}`); + } } }); ``` diff --git a/src/index.ts b/src/index.ts index ab9cf2f..1e5134e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,6 +35,11 @@ export interface RetryConfig { * Defaults to: [[100, 199], [429, 429], [500, 599]] */ statusCodesToRetry?: number[][]; + + /** + * Function to invoke when a retry attempt is made. + */ + onRetryAttempt?: (err: AxiosError) => void; } export type RaxConfig = { @@ -135,8 +140,15 @@ function onError(err: AxiosError) { setTimeout(resolve, delay); }); - // Return the promise in which recalls axios to retry the request + // Put the config back into the err (err.config as RaxConfig).raxConfig = config; + + // Notify the user if they added an `onRetryAttempt` handler + if (config.onRetryAttempt) { + config.onRetryAttempt(err); + } + + // Return the promise in which recalls axios to retry the request return backoff.then(() => { return config.instance!.request(err.config); }); diff --git a/test/index.ts b/test/index.ts index 97c20f8..6390220 100644 --- a/test/index.ts +++ b/test/index.ts @@ -2,6 +2,7 @@ import * as assert from 'assert'; import axios, {AxiosError} from 'axios'; import * as nock from 'nock'; import * as rax from '../src'; +import {RaxConfig, RetryConfig} from '../src'; const url = 'http://test.local'; @@ -129,4 +130,23 @@ describe('retry-axios', () => { assert.equal(0, cfg!.currentRetryAttempt); } }); + + it('should notify on retry attempts', async () => { + nock(url).get('/').reply(500); + nock(url).get('/').reply(200, 'toast'); + interceptorId = rax.attach(); + let flipped = false; + const config: RaxConfig = { + url, + raxConfig: { + onRetryAttempt: (err) => { + const cfg = rax.getConfig(err); + assert.equal(cfg!.currentRetryAttempt, 1); + flipped = true; + } + } + }; + const res = await axios(config); + assert.equal(flipped, true); + }); }); \ No newline at end of file