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

nice-request

v1.3.1

Published

A library to help with http requests in nodeland.

Downloads

241

Readme

Nice Request

nice-request is a promise based library to facilitate service to service communication. It makes use of a singleton pattern and exposes two methods as a part of it's API, setup and request. Additionally, data is logged off of failed responses to enable better metrics for API and system performance.

Installation

Run this command in a terminal session to add nice-request to your project:

npm install nice-request --save

API

  • setup()

The setup method should be invoked when the express server is initialized with these arguments:

  • logger: a object with .info() function for logging
  • projectTag: a string used for constructing the metrics tag (ex. dev.monto-user)

const Logger = require('some-logger')
const niceRequest = require('nice-request');

const logger = new Logger();

niceRequest.setup('project tag', logger);
  • request()

The request method is to be used to make HTTP requests. It accepts one argument, a configuration object, to set the details for the outbound request. Successful requests will result in a fulfilled promise with the value of the response.body. Failed requests will result in a rejected promise with a nice error.

const niceRequest = require('nice-request');

niceRequest.request({
    url: `http://some-api/users`, // desired resource
    method: 'GET', // desired http method
    metricTag: 'get_account', // string - tag to identify the request in the log (ex. 'create_user')
  })
  .then(result => {});

Optional object properties:

  • headers: object - additional http headers specific to the request.
  • body: object - req data
  • queryString: object -
  • timeout: number - override timeout(ms). Default timeout is 25000 ms.
  • resolveWithFullResponse: boolean - true resolve with full response (including headers) instead of just body
  • rejectUnauthorized: boolean - ignore SSL certificate errors (i.e. self-signed certs)
  • maxTries: integer - integer specifying the maximum number of retry attempts.
  • errCond: function - function that accepts an error object for evaluation and returns a boolean indicating if the request should be retried or not

Responses

The niceRequest.request(options) function is implemented to return the response body of the request as a JSON object. Please note that headers and http properties are not available in the response body. It is assumed that the http status code is 200 if the requests fulfills.

Error Handling

NiceErrors, and request-promise errors will be returned to the callee with a newly created NiceError.

Timeouts

Nice requests will timeout by default after 25 seconds or at the specified time in the requestOptions object if no response is sent back before that timeframe.