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

request-promise-lite

v0.16.0

Published

Lightweight, promiseful http/https request client

Downloads

9,926

Readme

Build Status view on npm npm module downloads per month Dependency Status Coverage Status

request-promise-lite

This is a lightweight HTTP Promise client, somewhat compatible with request-promise for node.js 4 or later. It can be used as a replacement where the original client is too heavy, e.g. as part of AWS Lambda functions, or with WebPack.

Installation

> npm install --save request-promise-lite

Usage

Request in request-promise style:

const request = require('request-promise-lite)'

request.get('https://httpbin.org/get', { json: true })
  .then((response) => {
    console.log(JSON.stringify(response));
  });

Use bundled classes (Request):

const url = 'https://httpbin.org/get';
const req = new request.Request('GET', url, { json: true });

req.run()
  .then((response) => {
    console.log(JSON.stringify(response));
  });

Use bundled classes (StreamReader):

const filePath = path.resolve(__dirname, './fixtures/sample.json');
const stream = fs.createReadStream(filePath);
const reader = new request.StreamReader(stream);

reader.readAll()
  .then((output) => {
    console.log(output.toString());
  });

Use bundled classes (superclass RequestError, or specifics ConnectionError, HTTPError, ParseError):

const error = new request.HTTPError('I\'m a teapot!', 417, 'teapot');
throw new request.ParseError(Invalid JSON', 'some message');

Change logging behaviour (works also on per-request basis):

request.Request.defaults = {
  logger: {
    debug: (...tokens) => {
      console.log('[prefix]', ${util.format(...tokens)});
    }
  }
}

Supported options

Node.js http/https request options are passed forward as-is. In addition the following shorthand options are supported:

// Options & their default values
{
  agent: false, // The HTTP agent for subsequent calls
  compression: ['gzip', 'deflate'], // Support GZIP or deflate compression
  headers: {}, // The headers to pass forward (as-is)
  json: false, // JSON shortcut for req headers & response parsing
  logger: new ConsoleLogger(), // An object that consumes the logging requests
  maxRedirects: 3, // How many redirects to follow
  resolveWithFullResponse: false, // Resolve with the response, not the body
  verbose: false, // Run the requests in verbose mode (produces logs)
  timeout: 0, // Abort the request if it has not completed within a given number of milliseconds
};

The options can be modified per-request by passing the options as a parameter (see above). Defaults are stored as a static variable that you can access and modify through Request.defaults:

// Get the default options and tinker with them.
const options = request.Request.defaults;
options.verbose = true;
request.Request.defaults = options;

// Just add a few overrides
request.Request.defaults = { verbose: false };

You can also set the defauls as an environment variable:

> RPL_DEFAULTS="{ \"verbose\": true}" node myprogram.js

When setting environment variables, please make sure the variable contains a proper stringified JSON. The environment will be parsed when requiring request.Request for the first time, and it will throw a TypeError on failure.

Features

This module already supports a wealth of options. An acceptance test run tells the situation best:

StreamReader
  ✓ Reads a stream fully
  ✓ Reads a that has been chunked by individual writes
  ✓ Fails gracefully on invalid stream
ParseError
  ✓ Supports message, status code and response
  ✓ is an an instance of RequestError
HTTPError
  ✓ Supports message, status code and response
  ✓ Stringifies to a meaningful message
  ✓ is an an instance of RequestError
ConnectionError
  ✓ Supports message and raw message
  ✓ Stringifies to a meaningful message
  ✓ is an an instance of RequestError
Request - test against httpbin.org
  ✓ Supports HTTP (425ms)
  ✓ Supports HTTPS (712ms)
  - Performs TRACE requests
  - Performs HEAD requests
  ✓ Performs OPTIONS requests (381ms)
  ✓ Performs GET requests (332ms)
  ✓ Performs POST requests (306ms)
  ✓ Performs PUT requests (267ms)
  ✓ Performs PATCH requests (346ms)
  ✓ Performs DELETE requests (409ms)
  ✓ Fails with TypeError if no protocol given
  ✓ Fails with TypeError on invalid form data
  ✓ Fails with TypeError on invalid auth data
  ✓ Fails with TypeError on invalid compression scheme
  ✓ Supports query string parameters in URL (815ms)
  ✓ Supports booleans, strings, numbers and undefined in query object (1799ms)
  ✓ Accepts custom headers (556ms)
  ✓ Interprets empty response with JSON request as null (409ms)
  ✓ Honors http agent provided by user (410ms)
  ✓ Supports 301-303 redirects (1431ms)
  ✓ Rejects on 4xx errors (399ms)
  ✓ Limits the maximum number of 301-303 redirects (612ms)
  ✓ Supports TLS with passphrase
  ✓ Supports HTTP Basic Auth (592ms)
  ✓ Supports GZIP compression (676ms)
  ✓ Supports Deflate compression (585ms)
  ✓ Supports null options (687ms)
  ✓ Supports 'json' in options (321ms)
  ✓ Supports 'form' in options (x-www-form-urlencoded) (422ms)
  ✓ Supports 'resolveWithFullResponse' in options (307ms)
  - Supports 'multipart' bodies
  ✓ Supports 'verbose' in options (408ms)
  ✓ Supports 'timeout' in options
  ✓ Supports custom loggers (288ms)
Options handling
  ✓ Overrides built-in defaults by RPL_DEFAULTS env variable
  ✓ Overrides built-in & env defaults by Request.defaults variable
  ✓ Resets the static defaults when set to {} or null
Error handling
  ✓ Throws TypeError if no protocol given
  ✓ Throws TypeError on invalid form data
  ✓ Throws TypeError on invalid auth data
  ✓ Throws TypeError on invalid compression scheme
  ✓ Throws TypeError when constructing with an invalid method
  ✓ Throws TypeError when constructing with an invalid query string
  ✓ Throws TypeError when constructing with an invalid protocol
  ✓ Throws TypeError when constructing with an invalid path
  ✓ Throws connections to non-existing hosts as ConnectionError
  ✓ Throws ConnectionError when client aborted
  ✓ Throws ConnectionError when server aborted
  ✓ Throws ConnectionError on other errors
  ✓ Throws HTTP on HTTP Error code responses 4xx-5xx
  ✓ Throws ParseError when requesting JSON, but getting sth else (280ms)
index.js wrapper
  ✓ Nested methods - request.get (417ms)
  ✓ Nested classes - request.Request (743ms)
  ✓ Nested classes - request.StreamReader


62 passing (15s)
3 pending

Building

The code has been writen in es2015 and transpiled in Babel. The transpilation can be run with gulp:

> gulp build             # If you have gulp in your path
> npm run-script build   # Use gulp devDependency
> gulp watch             # Trigger rebuild & test on file changes
> gulp test              # Run mocha tests & several validators