# cowl

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

Latest version **1.1.0** (published 2022-01-30) · MIT license · 0 weekly downloads

## Install

```sh
npm install cowl
pnpm add cowl
yarn add cowl
bun add cowl
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 1.1.0 |
| Published | 2022-01-30 |
| First published | 2019-02-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 6 |
| Unpacked size | 56.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 3 |
| Author | Perry Mitchell |
| Maintainers | perrymitchell |
| Keywords | request, xhr, xmlhttprequest, fetch, post, react-native, browser, node |

## Links

- npm: https://www.npmjs.com/package/cowl
- Repository: https://github.com/perry-mitchell/cowl
- Homepage: https://github.com/perry-mitchell/cowl#readme
- Issues: https://github.com/perry-mitchell/cowl/issues
- npm.io page: https://npm.io/package/cowl

## Dependencies (6)

- [xhr2](https://npm.io/package/xhr2.md) ^0.2.1
- [layerr](https://npm.io/package/layerr.md) ^0.1.2
- [caseless](https://npm.io/package/caseless.md) ^0.12.0
- [get-headers](https://npm.io/package/get-headers.md) ^1.0.5
- [query-string](https://npm.io/package/query-string.md) ^7.1.0
- [is-in-browser](https://npm.io/package/is-in-browser.md) ^1.1.3

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 1.1.0 (latest) — 2022-01-30
- 1.0.3 — 2022-01-30
- 1.0.2 — 2022-01-29
- 1.0.1 — 2020-12-06
- 1.0.0 — 2020-08-29
- 0.9.0 — 2020-08-01
- 0.8.0 — 2019-08-06
- 0.7.0 — 2019-08-06
- 0.6.0 — 2019-08-02
- 0.5.0 — 2019-07-22
- 0.4.0 — 2019-07-18
- 0.3.2 — 2019-07-17
- 0.3.1 — 2019-07-16
- 0.3.0 — 2019-07-16
- 0.2.0 — 2019-03-17
- … 1 more at https://npm.io/package/cowl/versions

## README

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

[![npm version](https://badge.fury.io/js/cowl.svg)](https://www.npmjs.com/package/cowl) [![Build Status](https://travis-ci.org/perry-mitchell/cowl.svg?branch=master)](https://travis-ci.org/perry-mitchell/cowl)

## 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.

Cowl can return `ArrayBuffer`s in the browser by specifying `arraybuffer` as the `responseType`. Specifying a `responseType` of `buffer` in the browser will still result in an `ArrayBuffer` being returned. Cowl can return both `ArrayBuffer`s and `Buffer`s when running on NodeJS.

## Usage

Install it by running `npm install cowl`.

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

```javascript
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 (`ArrayBuffer`s are converted to `Buffer` instances).

You can set `responseType` to be any of the following:

 * `auto` - Automatically detect the response type (default) (ideal for text/JSON)
 * `text` - Treat the response as text
 * `json` - Treat the response as JSON
 * `arraybuffer` - Treat the response as an Array Buffer. Supported on NodeJS and in the browser.
 * `buffer` - Treat the response as a Buffer. Supported on NodeJS only. Specifying `buffer` in the browser will automatically default to requesting as `arraybuffer`.

 **NB:** The response type is provided to the XHR mechanism, and the output is largely dependent on the server's response. The type you specify as `responseType` should match what you _expect_ to receive, not what you wish to.

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

request({
    url: "https://server.com/res/item",
    method: "GET",
    responseType: "buffer"
}).then(resp => {
    // resp.data will be a Buffer under NodeJS, and an ArrayBuffer in the browser
});

request({
    url: "https://server.com/res/item",
    method: "GET",
    responseType: "arraybuffer"
}).then(resp => {
    // resp.data will be an ArrayBuffer
});
```

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: auto)     |
| `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|String | The response body          |
| `status`      | Number    | The status code                       |
| `statusText`  | String    | The status code text                  |

### Response headers

Headers sent by the server are parsed and all keys converted to lower-case for easier handling.

### 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, accessible by using the [`Layerr`](https://github.com/perry-mitchell/layerr) Node library. The properties are as follows:

| 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)       |

_Note that not all the properties will be available for all errors._

_Early versions of this project set these properties directly to the `Error` instance itself, but this method is **deprecated**. Use `Layerr` or `VError` to fetch the info (via `Layerr.info(err)` or `VError.info(err)`) instead._

You can specify a new validation method for status codes by providing a `validateStatus` method in the request options.

## Packaging

If you're using webpack to bundle this library, make sure to check out the [example in this repo](webpack.config.js). Specifically, make sure to stub `fs` and `net`:

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

## Compatibility

Cowl works on NodeJS version 10 and above. Compiling it via Babel or Webpack may allow it to function on earlier versions, but this is not officially supported.

---
_Source: https://npm.io/package/cowl · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
