Skip to content

Commit

Permalink
feat: allow retryDelay to be 0 (#132)
Browse files Browse the repository at this point in the history
In previous releases, a `retryDelay` set to 0 would trigger the default retry delay.  It now respects 0 as a number :)
  • Loading branch information
wuzi committed Nov 23, 2020
1 parent 74e1873 commit 57ba46f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ function onError(err: AxiosError) {

const config = getConfig(err) || {};
config.currentRetryAttempt = config.currentRetryAttempt || 0;
config.retry =
config.retry === undefined || config.retry === null ? 3 : config.retry;
config.retryDelay = config.retryDelay || 100;
config.retry = typeof config.retry === 'number' ? config.retry : 3;
config.retryDelay =
typeof config.retryDelay === 'number' ? config.retryDelay : 100;
config.instance = config.instance || axios;
config.backoffType = config.backoffType || 'exponential';
config.httpMethodsToRetry = normalizeArray(config.httpMethodsToRetry) || [
Expand All @@ -144,9 +144,7 @@ function onError(err: AxiosError) {
'DELETE',
];
config.noResponseRetries =
config.noResponseRetries === undefined || config.noResponseRetries === null
? 2
: config.noResponseRetries;
typeof config.noResponseRetries === 'number' ? config.noResponseRetries : 2;

// If this wasn't in the list of status codes where we want
// to automatically retry, return.
Expand Down
54 changes: 54 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -342,4 +342,58 @@ describe('retry-axios', () => {
}
assert.strictEqual(scopes[1].isDone(), false);
});

it('should accept 0 for config.retryDelay', async () => {
const scope = nock(url).get('/').replyWithError({code: 'ETIMEDOUT'});
interceptorId = rax.attach();
const config: AxiosRequestConfig = {
url,
raxConfig: {retryDelay: 0},
};
try {
await axios(config);
} catch (e) {
const cfg = rax.getConfig(e);
assert.strictEqual(cfg!.retryDelay, 0);
scope.isDone();
return;
}
assert.fail('Expected to throw');
});

it('should accept 0 for config.retry', async () => {
const scope = nock(url).get('/').replyWithError({code: 'ETIMEDOUT'});
interceptorId = rax.attach();
const config: AxiosRequestConfig = {
url,
raxConfig: {retry: 0},
};
try {
await axios(config);
} catch (e) {
const cfg = rax.getConfig(e);
assert.strictEqual(cfg!.retry, 0);
scope.isDone();
return;
}
assert.fail('Expected to throw');
});

it('should accept 0 for config.noResponseRetries', async () => {
const scope = nock(url).get('/').replyWithError({code: 'ETIMEDOUT'});
interceptorId = rax.attach();
const config: AxiosRequestConfig = {
url,
raxConfig: {noResponseRetries: 0},
};
try {
await axios(config);
} catch (e) {
const cfg = rax.getConfig(e);
assert.strictEqual(cfg!.noResponseRetries, 0);
scope.isDone();
return;
}
assert.fail('Expected to throw');
});
});

0 comments on commit 57ba46f

Please sign in to comment.