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

minimal-request-promise

v1.5.0

Published

A+ Promise interface to Node.js HTTPS request, with no dependencies

Downloads

64,997

Readme

Minimal Promise version of HTTPS request

Build Status

This is a wrapper for the standard HTTPS Node Request object, that provides an A+ Promise interface to request execution and automates the process of assembling the response body as a string. It can handle posting body contents, and automatically rejects the promise if the response code is not between 200 and 399.

The intent of this library is to wrap requests into a promise interface with minimal overhead, with no dependencies, and just expose the standard Node.js arguments. It's not trying to be a fully-featured replacement for complex workflows, streaming etc. For more complex libraries that can provide all kind of workflows like that, see request-promise and got.

Installation

Install using NPM:

npm install minimal-request-promise

NPM

Usage

You can use the standard Node HTTPS Request Options, with the following additional options:

  • body: string, the content to include in the request body when posting
  • resolveErrors: boolean, if true, HTTP error response codes will result in a resolved promise (instead of rejected). Only network errors will result in a rejected promise. If false (default), network errors and successful HTTP requests with an error response code will cause the promise to be rejected.
  • timeout: number, Integer containing the number of milliseconds to wait for a server to send response headers (and start the response body) before aborting the request. Note that if the underlying TCP connection cannot be established, the OS-wide TCP connection timeout will overrule the timeout option ([the default in Linux can be anywhere from 20-120 seconds][linux-timeout]).

If you want to execute a FORM POST, remember to add the Content-Length header as well. This library intentionally does not automatically add that, to keep the interface in line with standard Node.JS requests.

Example


var requestPromise = require('minimal-request-promise'),
  options = {
    method: 'POST',
    hostname: 'graph.facebook.com',
    path: '/v2.6/me/messages?access_token=' + fbAccessToken,
    port: 443,
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      recipient: {
        id: recipient
      },
      message: message
    })
  };

requestPromise(options).then(
  function (response) {
    console.log('got response', response.body, response.headers);
  },
  function (response) {
    console.log('got error', response.body, response.headers, response.statusCode, response.statusMessage);
  }
);

GET, POST, PUT and DELETE method shortcuts

In addition to using the standard Node.js request parameters, you can also generate basic parameters from URLS for GET and POST using the helper methods. The helper methods are .get, .post, .put and .delete and they expect the following arguments:

  • url: string, a URL to GET, POST, PUT or DELETE to
  • options: (optional) object, key-value map of additional options, described in the Usage section
  • Promise: (optional) Function, an alternate Promise implementation. See Using with a different Promise library.

Example:

var requestPromise = require('minimal-request-promise'),
  options = {
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      recipient: {
        id: recipient
      },
      message: message
    })
  };

requestPromise.post('https://graph.facebook.com/v2.6/me/messages?access_token=' + fbAccessToken, options).then(
  function (response) {
    console.log('got response', response.body, response.headers);
  },
  function (response) {
    console.log('got error', response.body, response.headers, response.statusCode, response.statusMessage);
  }
);

Using with a different Promise library

By default, this library uses the built-in Promise from Node.js. If you'd like to use a different A+ Promise library, just pass it in as the second argument. For example:

var bluebird = require('bluebird'),
  requestPromise = require('minimal-request-promise'),
  options = {
   // some options here ...
  };
requestPromise(options, bluebird).then(report);

License

MIT