retry-axios
Advanced tools
Comparing version 0.4.1 to 0.5.0
@@ -61,17 +61,20 @@ "use strict"; | ||
} | ||
// Calculate time to wait with exponential backoff. | ||
// Formula: (2^c - 1 / 2) * 1000 | ||
const 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 | ||
const backoff = new Promise(resolve => { | ||
const onBackoffPromise = new Promise((resolve) => { | ||
// Calculate time to wait with exponential backoff. | ||
// Formula: (2^c - 1 / 2) * 1000 | ||
const delay = (Math.pow(2, config.currentRetryAttempt) - 1) / 2 * 1000; | ||
// We're going to retry! Incremenent the counter. | ||
err.config.raxConfig.currentRetryAttempt += 1; | ||
setTimeout(resolve, delay); | ||
}); | ||
// Notify the user if they added an `onRetryAttempt` handler | ||
if (config.onRetryAttempt) { | ||
config.onRetryAttempt(err); | ||
} | ||
const onRetryAttemptPromise = (config.onRetryAttempt) ? | ||
Promise.resolve(config.onRetryAttempt(err)) : | ||
Promise.resolve(); | ||
// Return the promise in which recalls axios to retry the request | ||
return backoff.then(() => config.instance.request(err.config)); | ||
return Promise.resolve() | ||
.then(() => onBackoffPromise) | ||
.then(() => onRetryAttemptPromise) | ||
.then(() => config.instance.request(err.config)); | ||
} | ||
@@ -78,0 +81,0 @@ /** |
{ | ||
"name": "retry-axios", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "Retry HTTP requests with Axios.", | ||
@@ -52,3 +52,3 @@ "main": "./build/src/index.js", | ||
"js-green-licenses": "^0.5.0", | ||
"mocha": "^5.2.0", | ||
"mocha": "^6.0.0", | ||
"nock": "^10.0.0", | ||
@@ -58,3 +58,3 @@ "nyc": "^13.0.0", | ||
"source-map-support": "^0.5.6", | ||
"typescript": "~3.2.0" | ||
"typescript": "~3.3.0" | ||
}, | ||
@@ -61,0 +61,0 @@ "files": [ |
@@ -11,3 +11,2 @@ # retry-axios | ||
## Installation | ||
@@ -40,7 +39,5 @@ | ||
const myAxiosInstance = axios.create(); | ||
myAxiosInstance.defaults = { | ||
raxConfig: { | ||
instance: myAxiosInstance | ||
} | ||
} | ||
myAxiosInstance.defaults.raxConfig = { | ||
instance: myAxiosInstance | ||
}; | ||
const interceptorId = rax.attach(myAxiosInstance); | ||
@@ -89,2 +86,25 @@ const res = await myAxiosInstance.get('https://test.local'); | ||
If the logic in onRetryAttempt requires to be asynchronous, you can return a promise, then retry will be executed only after the promise is resolved: | ||
```js | ||
const res = await axios({ | ||
url: 'https://test.local', | ||
raxConfig: { | ||
onRetryAttempt: (err) => { | ||
return new Promise((resolve, reject) => { | ||
// call a custom asynchronous function | ||
refreshToken(err, function(token, error) { | ||
if (!error) { | ||
window.localStorage.setItem('token',token); | ||
resolve(); | ||
} else { | ||
reject(); | ||
} | ||
}) | ||
}); | ||
} | ||
} | ||
}); | ||
``` | ||
Or if you want, you can just decide if it should retry or not: | ||
@@ -91,0 +111,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
28408
197
141