Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: export the shouldRetryRequest method #74

Merged
merged 11 commits into from
Oct 23, 2020
21 changes: 12 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,21 @@ function onFulfilled(res: AxiosResponse) {
return res;
}

// tslint:disable-next-line no-any
const values = (objOrArray: { [key: string]: any }) => {
return Object.keys(objOrArray).map((key: string) => objOrArray[key]);
};

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.instance = config.instance || axios;
config.httpMethodsToRetry = config.httpMethodsToRetry || [
'GET',
'HEAD',
'PUT',
'OPTIONS',
'DELETE',
];
config.httpMethodsToRetry = values(
config.httpMethodsToRetry || ['GET', 'HEAD', 'PUT', 'OPTIONS', 'DELETE']
);
config.noResponseRetries =
config.noResponseRetries === undefined || config.noResponseRetries === null
? 2
Expand All @@ -119,7 +120,9 @@ function onError(err: AxiosError) {
[429, 429],
[500, 599],
];
config.statusCodesToRetry = config.statusCodesToRetry || retryRanges;
config.statusCodesToRetry = values(
config.statusCodesToRetry || retryRanges
).map(values);

// Put the config back into the err
(err.config as RaxConfig).raxConfig = config;
Expand Down Expand Up @@ -157,7 +160,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.
Expand Down