JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 982
  • Score
    100M100P100Q100705F
  • License ISC

Small wrapper around {http,https}.request()

Package Exports

  • http-request-plus
  • http-request-plus/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (http-request-plus) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

http-request-plus

Package Version Build Status PackagePhobia Latest Commit

Small wrapper around {http,https}.request()

Features:

  • HTTP & HTTPs
  • promise oriented
  • stream oriented
  • cancelable via cancel token or response.cancel()
  • request body can be either a buffer/string or a stream
  • content length header automatically set if available
  • support pathname & query (string or object) if no path provided
  • handle redirects
  • response emits error on abort and timeout
  • URL objects can be used as params

Install

Installation of the npm package:

> npm install --save http-request-plus

Usage

Example

Easiest use case: just downloads and prints a page with error handling.

ES2015 - ES2016:

import httpRequestPlus from "http-request-plus";

(async () => {
  try {
    console.log(
      await httpRequestPlus("http://example.org", {
        // A request body can provided, either as a buffer/string or a stream
        body: "foo bar",

        // By default, http-request-plus throws if the reponse's status Code is not 2xx
        //
        // This option can be used to bypass this
        bypassStatusCheck: true,

        // Maximum number of redirects that should be handled by http-request-plus
        //
        // Defaults to 5
        maxRedirects: 0,

        onRequest(request) {
          // this function will be called multiple times in case of redirections

          request.setTimeout(10 * 1e3);
          request.on("timeout", request.abort);
        },

        // all other options are forwarded to native {http,https}.request()
      }).readAll("utf8")
    );
  } catch (error) {
    console.error("An error as occured", error);
  }
})();

ES5:

var httpRequestPlus = require("http-request-plus");

httpRequestPlus("http://example.org")
  .readAll("utf8")
  .then((body) => {
    console.log(body);
  })
  .catch((error) => {
    console.error("An error as occured", error);
  });

HTTP method helpers

httpRequestPlus.delete();
httpRequestPlus.head();
httpRequestPlus.patch();
httpRequestPlus.post();
httpRequestPlus.put();

httpRequestPlus.extend(opts)

const githubRequest = httpRequestPlus.extend("https://github.com");

githubRequest.post("/api");

httpRequestPlus(options...)Promise<response>

Promise<response>.cancel()

Promise<response>.readAll()Promise<buffer>

response.cancel()

response.readAll()Promise<buffer>

response.length

error.code

error.response

Development

# Install dependencies
> npm install

# Run the tests
> npm test

# Continuously compile
> npm run dev

# Continuously run the tests
> npm run dev-test

# Build for production (automatically called by npm install)
> npm run build

Contributions

Contributions are very welcomed, either on the documentation or on the code.

You may:

  • report any issue you've encountered;
  • fork and create a pull request.

License

ISC © Julien Fontanet