@httc/promise-request-retry-callback

Simple wrapper on request-promise for retry mechanism with callback

Usage no npm install needed!

<script type="module">
  import httcPromiseRequestRetryCallback from 'https://cdn.skypack.dev/@httc/promise-request-retry-callback';
</script>

README

request-promise-retry-callback

npm : promise-request-retry-callback

npm version coverage status build status npm downloads Simple wrapper on top of request-promise to replicate retry mechanism, i.e, it will try to reprocess the request till a valid response is obtained, or the number of retrys is exhausted. Supports all options from request-promise.

Usage

  • additionals parameter retry and callback function needed in request-promise options.
  • retry supports boolean (defaults to 1 retry) and positive integer.
  • callback is a function who take one parameter: retryValue.
  • in order to ignore retry or use genericrequest-promise,just don't specify the retry parameter.

GET Request sample with retry

var rp = require('@httc/promise-request-retry-callback');
var options = {
    uri: 'https://api.github.com/user/repos',
    qs: {
        access_token: 'xxxxx xxxxx' // -> uri + '?access_token=xxxxx%20xxxxx'
    },
    headers: {
        'User-Agent': 'Request-Promise'
    },
    callback: retry => { console.log(`Trying: ${retry}`) },
    json: true, // Automatically parses the JSON string in the response, 
    retry : 2, // will retry the call twice, in case of error.
    verbose_logging : false, // will log errors only, if set to be true, will log all actions
    accepted: [ 200, 404 ], // Accepted HTTP Status codes (will not retry if request response has any of these HTTP Status Code)
    delay: 2000, // will delay retries by 2000 ms.  The default is 100. 
    factor: 2, // will multiple the delay by the factor each time a retry is attempted. 
};

rp(options)
    .then(function (repos) {
        console.log('User has %d repos', repos.length);
    })
    .catch(function (err) {
        // API call failed...
    });
logging sample
2018-03-13T22:20:21.308Z - info: [request-promise-retry] calling http://adadadadad.com/ with retry 3
2018-03-13T22:20:21.899Z - info: [request-promise-retry] Encountered error Error: getaddrinfo ENOTFOUND adadadadad.com adadadadad.com:80 for GET request to http://adadadadad.com/, retry count 3
2018-03-13T22:20:21.904Z - info: [request-promise-retry] Encountered error Error: getaddrinfo ENOTFOUND adadadadad.com adadadadad.com:80 for GET request to http://adadadadad.com/, retry count 2
2018-03-13T22:20:21.907Z - info: [request-promise-retry] Encountered error Error: getaddrinfo ENOTFOUND adadadadad.com adadadadad.com:80 for GET request to http://adadadadad.com/, retry count 1

For rest of samples, please refer request-promise documentation.

Installation

npm install @httc/promise-request-retry-callback

Test

npm test