requestxn
v3.1.6
Published
request-promise wrapper with retries
Downloads
10
Readme
requestXn
Wraps request-promise with a retry handler, in order to provide an easy way to send requests with retries and promises.
Options
requestXn supports all request-promise-native functionality, so you can pass all options as you would pass them to the original package
In addition to the original request-promise options, the following extra options are available
max
Maximum number of attempts. Default: 1
If you would like requestXn to retry on a network error, set this options to a value above 1.
This handles all non-HTTP errors including ENOTFOUND, ECONNRESET, ECONNREFUSED, and ETIMEOUT.
max: 1
retryOn5xx
Retry on 5xx status codes. Default: false
Make requestXn also retry in case of 5xx status codes.
retryOn5xx: true
rejectOn5xx
Reject when getting 5xx status code and simple=false. Default: false
By default, requestXn would resolve with the response when simple=false.
By setting this option to true, requestXn behavior is changed to reject on such cases.
rejectOn5xx: true
retryStrategy
Custom retry logic function. Receives a response object and returns a boolean.
// Retry is HTTP status is "Too many requests" or a server error.
retryStrategy: res => res.statusCode === 429 || res.statusCode >= 500;
backoffBase
Initial backoff duration in ms. Default: 100
backoffBase: 100
backoffExponent
Exponent to increase backoff on each attempt. Default: 1.1
backoffExponent: 1.1
onSuccess
Function to be executed on success New in v3.0.0: Support async functions
onSuccess: function (options, response, attempts) {
// do something on success
}
onError
Function to be executed on error New in v3.0.0: Support async functions
onError: function (options, error, attempts) {
// do something on error
}
Example
const request = require('requestxn');
const options = {
url: 'http://www.site-with-issues.com',
body: {/* body */},
json: true,
max: 3,
backoffBase: 500,
backoffExponent: 1.3,
retryOn5xx: true,
retryStrategy: function(response) {
return response.statusCode === 500 && response.body.match(/Temporary error/);
},
onError: function(options, error, attempts) {
console.error(`- Request to ${options.uri} failed on the ${attempts} attempt with error ${error.message}`);
},
onSuccess: function(options, response, attempts) {
console.info(`- Got status-code ${response.statusCode} on request to ${request.uri} after ${attempts}`);
}
}
Result
> request.post(options).then()...
- "Request to http://www.site-with-issues.com failed on the 1 attempt with RequestError: Error: getaddrinfo ENOTFOUND www.site-with-issues.com www.site-with-issues.com:80"
- "Request to http://www.site-with-issues.com failed on the 2 attempt with RequestError: Error: getaddrinfo ENOTFOUND www.site-with-issues.com www.site-with-issues.com:80"
- "Got status-code 200 on request to http://www.site-with-issues.com"
Usage with defaults
const request = require('requestxn');
const requestWithDefaults = request.defaults({
json: true,
max: 3,
backoffBase: 500,
retryOn5xx: true,
retryStrategy: function(response) {
return response.statusCode === 500 && response.body.match(/Temporary error/);
},
onError: function(request, error, errorCount) {
console.error(`- Request to ${request.url} failed on the ${retries} attempt with error ${error.message}`);
},
onSuccess: function(request, response) {
console.info(`- Got status-code ${response.statusCode} on request to ${request.url}`);
}
});
requestWithDefaults.get('http://www.site-with-issues.com').then...