Skip to content

Commit 75b280f

Browse files
committed
Refactor config name statuses => errorCodes
1 parent d54abd8 commit 75b280f

File tree

4 files changed

+13
-13
lines changed

4 files changed

+13
-13
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,17 @@ _Default_: 0
5959

6060
_Example_: `waitTime: 3000`
6161

62-
#### statuses
62+
#### errorCodes
6363

64-
Response statuses for which the interceptor should retry.
64+
Response errorCodes for which the interceptor should retry.
6565

6666
Ideally any implementation should retry only 5xx status(server errors) and should not retry 4xx status(client errors). The reason is, if a http call fails with a client error, then the retry call will have the same headers/params and will obviously fail. So **by default all 5xx errors will be retried**. If you want to customize the status for which the retries should be made, use this config.
6767

6868
_Type_: Array
6969

7070
_Default_: []
7171

72-
_Example_: `[500, 501, 401]`
72+
_Example_: `errorCodes: [500, 501, 401]`
7373

7474
## Author's note
7575

examples/example.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const http = axios.create({
88
axiosRetryInterceptor(http, {
99
maxAttempts: 3,
1010
waitTime: 1000, // in milliseconds
11-
statuses: [408, 522] // [] for all errors
11+
errorCodes: [408, 522] // [] for all errors
1212
});
1313

1414
// all failed http calls will now be retried 3 times

src/axios-retry-interceptor.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import t from 'typy';
44
const DEFAULT_OPTIONS = {
55
maxAttempts: 3,
66
waitTime: 0,
7-
statuses: []
7+
errorCodes: []
88
};
99

1010
const ALLOWED_RETRY_METHODS = ['get', 'put', 'delete', 'head', 'options'];
1111

1212
const shouldRetry = (error) => {
13-
const { method: httpMethod, statuses } = error.config;
13+
const { method: httpMethod, errorCodes } = error.config;
1414
const { maxAttempts, __retryCount: retryCount = 0 } = error.config;
1515
const { response: { status: statusCode } = {} } = error;
1616

@@ -20,8 +20,8 @@ const shouldRetry = (error) => {
2020
if (ALLOWED_RETRY_METHODS.includes(httpMethod)) shouldRetryForMethod = true;
2121

2222
if (
23-
(statuses.length === 0 && statusCode >= 500 && statusCode < 600) ||
24-
statuses.includes(statusCode)
23+
(errorCodes.length === 0 && statusCode >= 500 && statusCode < 600) ||
24+
errorCodes.includes(statusCode)
2525
) {
2626
shouldRetryForStatus = true;
2727
}
@@ -39,7 +39,7 @@ const axiosRetryInterceptor = (axios, options = {}) => {
3939
? options.maxAttempts
4040
: DEFAULT_OPTIONS.maxAttempts,
4141
waitTime: t(options.waitTime).isNumber ? options.waitTime : DEFAULT_OPTIONS.waitTime,
42-
statuses: t(options.statuses).isArray ? options.statuses : DEFAULT_OPTIONS.statuses
42+
errorCodes: t(options.errorCodes).isArray ? options.errorCodes : DEFAULT_OPTIONS.errorCodes
4343
};
4444

4545
axios.interceptors.request.use(

test/axios-retry-interceptor.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ describe('Axios Retry Interceptor', () => {
166166
});
167167
});
168168

169-
it('should retry only for {statuses} in config', () => {
169+
it('should retry only for {errorCodes} in config', () => {
170170
nock.cleanAll();
171171
nock(BASE_URL)
172172
.persist()
@@ -175,7 +175,7 @@ describe('Axios Retry Interceptor', () => {
175175

176176
options = Object.assign(options, {
177177
maxAttempts: 3,
178-
statuses: [408]
178+
errorCodes: [408]
179179
});
180180

181181
axiosRetryInterceptor(http, options);
@@ -191,10 +191,10 @@ describe('Axios Retry Interceptor', () => {
191191
});
192192
});
193193

194-
it('should not retry for status not in {statuses} config', () => {
194+
it('should not retry for status not in {errorCodes} config', () => {
195195
options = Object.assign(options, {
196196
maxAttempts: 1,
197-
statuses: [505]
197+
errorCodes: [505]
198198
});
199199

200200
axiosRetryInterceptor(http, options);

0 commit comments

Comments
 (0)