retry-axios
Advanced tools
Comparing version 0.0.1 to 0.1.0
import { AxiosError, AxiosInstance, AxiosRequestConfig } from 'axios'; | ||
/** | ||
* Configuration for the Axios `request` method. | ||
*/ | ||
export interface RetryConfig { | ||
/** | ||
* The number of times to retry the request. Defaults to 3. | ||
*/ | ||
retry?: number; | ||
/** | ||
* The number of retries already attempted. | ||
*/ | ||
currentRetryAttempt?: number; | ||
/** | ||
* The amount of time to initially delay the retry. Defaults to 100. | ||
*/ | ||
retryDelay?: number; | ||
/** | ||
* The instance of the axios object to which the interceptor is attached. | ||
*/ | ||
instance?: AxiosInstance; | ||
/** | ||
* The HTTP Methods that will be automatically retried. | ||
* Defaults to ['GET','PUT','HEAD','OPTIONS','DELETE'] | ||
*/ | ||
httpMethodsToRetry?: string[]; | ||
/** | ||
* The HTTP response status codes that will automatically be retried. | ||
* Defaults to: [[100, 199], [429, 429], [500, 599]] | ||
*/ | ||
statusCodesToRetry?: number[][]; | ||
} | ||
@@ -24,2 +49,6 @@ export declare type RaxConfig = { | ||
export declare function detach(interceptorId: number, instance?: AxiosInstance): void; | ||
/** | ||
* Acquire the raxConfig object from an AxiosError if available. | ||
* @param err The Axios error with a config object. | ||
*/ | ||
export declare function getConfig(err: AxiosError): RetryConfig | undefined; |
@@ -35,10 +35,4 @@ "use strict"; | ||
config.instance = config.instance || axios_1.default; | ||
// If there's no config, or retries are disabled, return. | ||
if (!config || config.retry === 0) { | ||
return Promise.reject(err); | ||
} | ||
// If this was anything other than a GET, return. | ||
if (!err.config.method || err.config.method.toLowerCase() !== 'get') { | ||
return Promise.reject(err); | ||
} | ||
config.httpMethodsToRetry = | ||
config.httpMethodsToRetry || ['GET', 'HEAD', 'PUT', 'OPTIONS', 'DELETE']; | ||
// If this wasn't in the list of status codes where we want | ||
@@ -56,2 +50,14 @@ // to automatically retry, return. | ||
]; | ||
config.statusCodesToRetry = config.statusCodesToRetry || retryRanges; | ||
// If there's no config, or retries are disabled, return. | ||
if (!config || config.retry === 0) { | ||
return Promise.reject(err); | ||
} | ||
// Only retry with configured HttpMethods. | ||
if (!err.config.method || | ||
config.httpMethodsToRetry.indexOf(err.config.method.toUpperCase()) < 0) { | ||
return Promise.reject(err); | ||
} | ||
// If this wasn't in the list of status codes where we want | ||
// to automatically retry, return. | ||
if (err.response && err.response.status) { | ||
@@ -91,2 +97,6 @@ var isInRange = false; | ||
} | ||
/** | ||
* Acquire the raxConfig object from an AxiosError if available. | ||
* @param err The Axios error with a config object. | ||
*/ | ||
function getConfig(err) { | ||
@@ -93,0 +103,0 @@ if (err && err.config) { |
{ | ||
"name": "retry-axios", | ||
"version": "0.0.1", | ||
"version": "0.1.0", | ||
"description": "Retry HTTP requests with Axios.", | ||
@@ -5,0 +5,0 @@ "main": "./build/src/index.js", |
@@ -12,3 +12,3 @@ # retry-axios | ||
Use Axios interceptors to automatically retry failed requests. | ||
Use Axios interceptors to automatically retry failed requests. Super flexible. Built in exponential backoff. | ||
@@ -46,3 +46,3 @@ ## Installation | ||
You can control the number of retries and backoff delay: | ||
You have a lot of options... | ||
@@ -53,4 +53,15 @@ ```js | ||
url: 'https://test.local', | ||
retry: 14, // Retry 14 times before giving up | ||
retryDelay: 200, // # milliseconds to delay at first | ||
raxConfig: { | ||
// Retry 3 times before giving up. Defaults to 3. | ||
retry: 3, | ||
// Milliseconds to delay at first. Defaults to 100. | ||
retryDelay: 100, | ||
// # 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 | ||
// array with a list of ranges. Defaults to: | ||
// [[100, 199], [429, 429], [500, 599]] | ||
httpStatusCodesToRetry: [[100, 199], [429, 429], [500, 599]], | ||
} | ||
}); | ||
@@ -57,0 +68,0 @@ ``` |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24952
158
90