Skip to content

Commit

Permalink
fix: handle arrays that are converted to objects (#83)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith authored Jan 11, 2020
1 parent d26d297 commit 554fd4c
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,47 @@ function onFulfilled(res: AxiosResponse) {
return res;
}

/**
* Some versions of axios are converting arrays into objects during retries.
* This will attempt to convert an object with the following structure into
* an array, where the keys correspond to the indices:
* {
* 0: {
* // some property
* },
* 1: {
* // another
* }
* }
* @param obj The object that (may) have integers that correspond to an index
* @returns An array with the pucked values
*/
function normalizeArray<T>(obj?: T[]): T[] | undefined {
const arr: T[] = [];
if (!obj) {
return undefined;
}
if (Array.isArray(obj)) {
return obj;
}
if (typeof obj === 'object') {
Object.keys(obj).forEach(key => {
if (typeof key === 'number') {

This comment has been minimized.

Copy link
@mortiy

mortiy Mar 6, 2020

@JustinBeckwith
But... Object.keys() always returns array of strings.
This method will convert any object to empty array.

arr[key] = obj[key];
}
});
}
return arr;
}

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 || [
config.httpMethodsToRetry = normalizeArray(config.httpMethodsToRetry) || [
'GET',
'HEAD',
'PUT',
Expand All @@ -119,7 +152,8 @@ function onError(err: AxiosError) {
[429, 429],
[500, 599],
];
config.statusCodesToRetry = config.statusCodesToRetry || retryRanges;
config.statusCodesToRetry =
normalizeArray(config.statusCodesToRetry) || retryRanges;

// Put the config back into the err
(err.config as RaxConfig).raxConfig = config;
Expand Down

0 comments on commit 554fd4c

Please sign in to comment.