Skip to content

Commit

Permalink
feat: add support for onRetryAttempt handler
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Jan 30, 2018
1 parent 42a8a1a commit fa17de4
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
}
});
```
Expand Down
14 changes: 13 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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);
});
Expand Down
20 changes: 20 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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);
});
});

0 comments on commit fa17de4

Please sign in to comment.