retry-axios
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -32,2 +32,10 @@ import { AxiosError, AxiosInstance, AxiosRequestConfig } from 'axios'; | ||
statusCodesToRetry?: number[][]; | ||
/** | ||
* Function to invoke when a retry attempt is made. | ||
*/ | ||
onRetryAttempt?: (err: AxiosError) => void; | ||
/** | ||
* Function to invoke which determines if you should retry | ||
*/ | ||
shouldRetry?: (err: AxiosError) => boolean; | ||
} | ||
@@ -34,0 +42,0 @@ export declare type RaxConfig = { |
@@ -50,5 +50,36 @@ "use strict"; | ||
config.statusCodesToRetry = config.statusCodesToRetry || retryRanges; | ||
// Put the config back into the err | ||
err.config.raxConfig = config; | ||
// Determine if we should retry the request | ||
var shouldRetryFn = config.shouldRetry || shouldRetryRequest; | ||
if (!shouldRetryFn(err)) { | ||
return Promise.reject(err); | ||
} | ||
// Calculate time to wait with exponential backoff. | ||
// Formula: (2^c - 1 / 2) * 1000 | ||
var delay = (Math.pow(2, config.currentRetryAttempt) - 1) / 2 * 1000; | ||
// We're going to retry! Incremenent the counter. | ||
err.config.raxConfig.currentRetryAttempt += 1; | ||
// Create a promise that invokes the retry after the backOffDelay | ||
var backoff = new Promise(function (resolve) { | ||
setTimeout(resolve, delay); | ||
}); | ||
// 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(function () { | ||
return config.instance.request(err.config); | ||
}); | ||
} | ||
/** | ||
* Determine based on config if we should retry the request. | ||
* @param err The AxiosError passed to the interceptor. | ||
*/ | ||
function shouldRetryRequest(err) { | ||
var config = err.config.raxConfig; | ||
// If there's no config, or retries are disabled, return. | ||
if (!config || config.retry === 0) { | ||
return Promise.reject(err); | ||
return false; | ||
} | ||
@@ -58,3 +89,3 @@ // Only retry with configured HttpMethods. | ||
config.httpMethodsToRetry.indexOf(err.config.method.toUpperCase()) < 0) { | ||
return Promise.reject(err); | ||
return false; | ||
} | ||
@@ -65,4 +96,4 @@ // If this wasn't in the list of status codes where we want | ||
var isInRange = false; | ||
for (var _i = 0, retryRanges_1 = retryRanges; _i < retryRanges_1.length; _i++) { | ||
var _a = retryRanges_1[_i], min = _a[0], max = _a[1]; | ||
for (var _i = 0, _a = config.statusCodesToRetry; _i < _a.length; _i++) { | ||
var _b = _a[_i], min = _b[0], max = _b[1]; | ||
var status = err.response.status; | ||
@@ -75,3 +106,3 @@ if (status >= min && status <= max) { | ||
if (!isInRange) { | ||
return Promise.reject(err); | ||
return false; | ||
} | ||
@@ -82,18 +113,5 @@ } | ||
if (config.currentRetryAttempt >= config.retry) { | ||
return Promise.reject(err); | ||
return false; | ||
} | ||
// Calculate time to wait with exponential backoff. | ||
// Formula: (2^c - 1 / 2) * 1000 | ||
var delay = (Math.pow(2, config.currentRetryAttempt) - 1) / 2 * 1000; | ||
// We're going to retry! Incremenent the counter. | ||
config.currentRetryAttempt += 1; | ||
// Create a promise that invokes the retry after the backOffDelay | ||
var backoff = new Promise(function (resolve) { | ||
setTimeout(resolve, delay); | ||
}); | ||
// Return the promise in which recalls axios to retry the request | ||
err.config.raxConfig = config; | ||
return backoff.then(function () { | ||
return config.instance.request(err.config); | ||
}); | ||
return true; | ||
} | ||
@@ -100,0 +118,0 @@ /** |
{ | ||
"name": "retry-axios", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "Retry HTTP requests with Axios.", | ||
@@ -5,0 +5,0 @@ "main": "./build/src/index.js", |
@@ -54,7 +54,10 @@ # retry-axios | ||
retry: 3, | ||
// Milliseconds to delay at first. Defaults to 100. | ||
retryDelay: 100, | ||
// # HTTP methods to automatically retry. Defaults to: | ||
// 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 | ||
@@ -64,2 +67,9 @@ // array with a list of ranges. Defaults to: | ||
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}`); | ||
} | ||
} | ||
@@ -69,2 +79,17 @@ }); | ||
Or if you want, you can just decide if it should retry or not: | ||
```js | ||
const res = await axios({ | ||
url: 'https://test.local', | ||
raxConfig: { | ||
// Override the decision making process on if you should retry | ||
shouldRetry: (err) => { | ||
const cfg = rax.getConfig(err); | ||
return true; | ||
} | ||
} | ||
}); | ||
``` | ||
## How it works | ||
@@ -71,0 +96,0 @@ |
Sorry, the diff of this file is not supported yet
26655
184
115