npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

requestxn

v3.1.6

Published

request-promise wrapper with retries

Downloads

27

Readme

NPM Version Build Status Test Coverage Known Vulnerabilities NPM Downloads

requestXn

Wraps request-promise with a retry handler, in order to provide an easy way to send requests with retries and promises.

NPM

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...