small-request

A thin Promise wrapper around Node.js http/https request functions

Usage no npm install needed!

<script type="module">
  import smallRequest from 'https://cdn.skypack.dev/small-request';
</script>

README

small-request

npm version Build Status codecov

This Node.js module is a thin wrapper around the Node.js http/https request functions that provides:

  • A Promise interface
  • Handling of response chunking
  • Auto-selection of the http/https request function

The promise is resolved when:

  • A response is received from the server regardless of status code

The promise is rejected when:

  • Any other exception is encountered

Example usage:

const request = require('small-request');

request.send({
  url: 'https://httpbin.org/get'
}).then(response => {
  console.log(response.body);
});

RequestModel

The request.send(requestModel) function takes in a requestModel object. This object is an extended version of the Node.js http.request options object. Its extended fields are:

  • url (string)
  • body (string)
  • socketTimeout (number in milliseconds)

If the original http.request option field values conflict with the extended field values, then the original field values take precedence.

ResponseModel

The promise returned by request.send() resolves to an object with the following fields:

  • body (string)
  • headers (object)
  • statusCode (number)