JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 206
  • Score
    100M100P100Q113296F
  • License MIT

Request cowl for making requests from NodeJS/Browser/React-Native

Package Exports

  • cowl

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 (cowl) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Cowl

Request cowl for making requests from NodeJS/Browser/React-Native

npm version Build Status

About

Cowl is a wrapper for HTTP/S requests for use in NodeJS and the browser. React-Native is a work-in-progress. It's designed to be useable from 1 script, support bundling (via Webpack) and support sending and receiving data. It provides a simple API that uses a configuration object to make requests.

Usage

Install it by running npm install cowl.

GET requests can be made by using the configuration object or by simply passing a URL:

const { request } = require("cowl");

request("https://server.com/api").then(/* ... */);

request({
    url: "https://server.com/api"
}).then(/* ... */);

request({
    url: "https://server.com/api",
    method: "GET",
    headers: {
        "Authorization": "Bearer ..."
    }
}).then(/* ... */);

Cowl will automatically assume that JSON is being sent if the body is an Object and no Content-Type header is overridden. Cowl will read the response headers to automatically discern the type if responseType is set to "auto".

Cowl will return a Buffer instance for application/octet-stream binary responses, even if in a browser (ArrayBuffers are converted to Buffer instances).

You can set responseType to be any of the following:

  • auto - Automatically detect the response type (default)
  • text - Treat the response as text
  • json - Treat the response as JSON
  • buffer - Treat the response as a buffer (arraybuffer works in the browser, but will still return a Buffer instance)
const { request } = require("cowl");

request({
    url: "https://server.com/res/item",
    method: "GET",
    responseType: "buffer"
}).then(resp => {
    // resp.data will be a Buffer
});

Request objects form the following structure:

Property Required Type Description
url Yes String The request URL
method No String The HTTP request method (default: GET)
headers No Object Headers for the request
query No String / Object Query object/string
responseType No String The response type (default: json)
body No Object / String / Buffer / ArrayBuffer Data to upload

Response objects have the following structure:

Property Type Description
url String The resulting URL that the request was made against
method String The request method used
headers Object The response headers received
data Object Buffer
status Number The status code
statusText String The status code text

Request failures

If a request fails or returns a status code outside the allowed range (200-399), an error is thrown. This particular error will contain some properties to help deal with the failure:

Property Type Description
status Number The status code
statusText String The status text
code Number Usually ERR_REQUEST_FAILED
responseHeaders Object Response headers
responseBody String / * Response body data (unprocessed)

Packaging

If you're using webpack to bundle this library, make sure to check out the example in this repo. Specifically, make sure to stub fs and net:

{
    node: {
        fs: "empty",
        net: "empty"
    }
}