# resin-request

> Resin.io HTTP client

Latest version **9.3.5** (published 2018-09-17) · Apache-2.0 license · 0 weekly downloads

> **Deprecated.** This package is deprecated.

## Install

```sh
npm install resin-request
pnpm add resin-request
yarn add resin-request
bun add resin-request
```

## Health

**Score 10/100 (F)** — status: deprecated.

Negative: deprecated.

## Facts

| | |
|---|---|
| Version | 9.3.5 |
| Published | 2018-09-17 |
| First published | 2015-03-17 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 9 |
| Unpacked size | 135.6 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Juan Cruz Viotti |
| Maintainers | resin.io |
| Keywords | resin, request, http |

## Links

- npm: https://www.npmjs.com/package/resin-request
- Repository: https://github.com/resin-io-modules/resin-request
- Issues: https://github.com/resin-io-modules/resin-request/issues
- npm.io page: https://npm.io/package/resin-request

## Dependencies (9)

- [qs](https://npm.io/package/qs.md) ^6.3.0
- [lodash](https://npm.io/package/lodash.md) ^4.6.1
- [rindle](https://npm.io/package/rindle.md) ^1.3.1
- [bluebird](https://npm.io/package/bluebird.md) ^3.3.4
- [resin-errors](https://npm.io/package/resin-errors.md) ^2.7.0
- [fetch-ponyfill](https://npm.io/package/fetch-ponyfill.md) ^4.0.0
- [progress-stream](https://npm.io/package/progress-stream.md) ^2.0.0
- [node-web-streams](https://npm.io/package/node-web-streams.md) github:resin-io-modules/node-web-streams#emit-errors
- [fetch-readablestream](https://npm.io/package/fetch-readablestream.md) ^0.1.0

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

- 9.3.5 (latest) — 2018-09-17
- 9.3.4-121-fix-stream-on-edge-c8e5b5ced6100ea4b8d47059edd2eee046e05a72 (121-fix-stream-on-edge-c8e5b5ced6100ea4b8d47059edd2eee046e05a72) — 2018-09-14
- 9.3.5-126-resinci-publishing-ba50fe34f970182279e4ec580c9a2d209d11b9e6 — 2018-09-17
- 9.3.5-126-resinci-publishing-82a73d271b24dd968473934b5600136fcf77da27 — 2018-09-17
- 9.3.3 — 2018-09-14
- 9.3.3-resin-ci-headless-chrome-ea7378ef49177dc48669ee9e8457f322e4b9199f — 2018-09-14
- 9.3.3-resin-ci-headless-chrome-ddb939decbb444e6bb44994e18ec1a6abb285400 — 2018-09-14
- 9.3.1 — 2018-07-09
- 9.3.0 — 2018-07-05
- 9.2.0 — 2018-07-02
- 9.1.0 — 2018-06-12
- 9.0.3 — 2018-04-25
- 9.0.2 — 2017-11-22
- 9.0.1 — 2017-11-01
- 9.0.0 — 2017-10-31
- … 51 more at https://npm.io/package/resin-request/versions

## README

resin-request
=============

> Resin.io HTTP client.

[![npm version](https://badge.fury.io/js/resin-request.svg)](http://badge.fury.io/js/resin-request)
[![dependencies](https://david-dm.org/resin-io-modules/resin-request.svg)](https://david-dm.org/resin-io-modules/resin-request.svg)
[![Build Status](https://travis-ci.org/resin-io-modules/resin-request.svg?branch=master)](https://travis-ci.org/resin-io-modules/resin-request)
[![Build status](https://ci.appveyor.com/api/projects/status/8qmwhh1vhm27otn4/branch/master?svg=true)](https://ci.appveyor.com/project/resin-io/resin-request/branch/master)
[![Gitter](https://badges.gitter.im/Join Chat.svg)](https://gitter.im/resin-io/chat)

Role
----

The intention of this module is to provide an exclusive client to make HTTP requests to the Resin.io servers.

**THIS MODULE IS LOW LEVEL AND IS NOT MEANT TO BE USED BY END USERS DIRECTLY**.

Unless you know what you're doing, use the [Resin SDK](https://github.com/resin-io/resin-sdk) instead.

Installation
------------

Install `resin-request` by running:

```sh
$ npm install --save resin-request
```

Documentation
-------------

The module returns a _factory function_ that you use to get an instance of the auth module.

It accepts the following params:

| Param | Type | Description |
| --- | --- | --- |
| options | <code>Object</code> | options |
| options.auth | <code>Object</code> | An instantiated [resin-auth](https://github.com/resin-io-modules/resin-auth) instance |
| options.debug | <code>boolean</code> | when set to `true` will log the request details in case of error. |
| options.isBrowser | <code>boolean</code> | set to `true` if the runtime is the browser. |
| options.interceptors | <code>Array&lt;Interceptor&gt;</code> | An initial array of interceptors |

**Example**
```js
var request = require('resin-request')({
	auth: auth,
	debug: false,
	isBrowser: false
})
```


* [request](#module_request)
    * _static_
        * [.interceptors](#module_request.interceptors) : <code>Array.&lt;Interceptor&gt;</code>
        * [.send(options)](#module_request.send) ⇒ <code>Promise.&lt;Object&gt;</code>
        * [.stream(options)](#module_request.stream) ⇒ <code>Promise.&lt;Stream&gt;</code>
        * [.refreshToken()](#module_request.refreshToken) ⇒ <code>String</code>
    * _inner_
        * [~Interceptor](#module_request..Interceptor) : <code>object</code>

<a name="module_request.interceptors"></a>

### request.interceptors : <code>Array.&lt;Interceptor&gt;</code>
The current array of interceptors to use. Interceptors intercept requests made
by calls to `.stream()` and `.send()` (some of which are made internally) and
are executed in the order they appear in this array for requests, and in the
reverse order for responses.

**Kind**: static property of [<code>request</code>](#module_request)  
**Summary**: Array of interceptors  
**Access**: public  
**Example**  
```js
request.interceptors.push(
	requestError: (error) ->
		console.log(error)
		throw error
)
```
<a name="module_request.send"></a>

### request.send(options) ⇒ <code>Promise.&lt;Object&gt;</code>
This function automatically handles authorization with Resin.io.

The module scans your environment for a saved session token. Alternatively, you may pass the `apiKey` option. Otherwise, the request is made anonymously.

Requests can be aborted using an AbortController (with a polyfill like https://www.npmjs.com/package/abortcontroller-polyfill
if necessary). This is not well supported everywhere yet, is on a best-efforts basis, and should not be relied upon.

**Kind**: static method of [<code>request</code>](#module_request)  
**Summary**: Perform an HTTP request to Resin.io  
**Returns**: <code>Promise.&lt;Object&gt;</code> - response  
**Access**: public  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| options | <code>Object</code> |  | options |
| [options.method] | <code>String</code> | <code>&#x27;GET&#x27;</code> | method |
| options.url | <code>String</code> |  | relative url |
| [options.apiKey] | <code>String</code> |  | api key |
| [options.responseFormat] | <code>String</code> |  | explicit expected response format, can be one of 'blob', 'json', 'text', 'none'. Defaults to sniffing the content-type |
| [options.signal] | <code>AbortSignal</code> |  | a signal from an AbortController |
| [options.body] | <code>\*</code> |  | body |

**Example**  
```js
request.send
	method: 'GET'
	baseUrl: 'https://api.resin.io'
	url: '/foo'
.get('body')
```
**Example**  
```js
request.send
	method: 'POST'
	baseUrl: 'https://api.resin.io'
	url: '/bar'
	data:
		hello: 'world'
.get('body')
```
<a name="module_request.stream"></a>

### request.stream(options) ⇒ <code>Promise.&lt;Stream&gt;</code>
This function emits a `progress` event, passing an object with the following properties:

- `Number percent`: from 0 to 100.
- `Number total`: total bytes to be transmitted.
- `Number received`: number of bytes transmitted.
- `Number eta`: estimated remaining time, in seconds.

The stream may also contain the following custom properties:

- `String .mime`: Equals the value of the `Content-Type` HTTP header.

See `request.send()` for an explanation on how this function handles authentication, and details
on how to abort requests.

**Kind**: static method of [<code>request</code>](#module_request)  
**Summary**: Stream an HTTP response from Resin.io.  
**Returns**: <code>Promise.&lt;Stream&gt;</code> - response  
**Access**: public  

| Param | Type | Default | Description |
| --- | --- | --- | --- |
| options | <code>Object</code> |  | options |
| [options.method] | <code>String</code> | <code>&#x27;GET&#x27;</code> | method |
| options.url | <code>String</code> |  | relative url |
| [options.body] | <code>\*</code> |  | body |

**Example**  
```js
request.stream
	method: 'GET'
	baseUrl: 'https://img.resin.io'
	url: '/download/foo'
.then (stream) ->
	stream.on 'progress', (state) ->
		console.log(state)

	stream.pipe(fs.createWriteStream('/opt/download'))
```
<a name="module_request.refreshToken"></a>

### request.refreshToken() ⇒ <code>String</code>
This function automatically refreshes the authentication token.

**Kind**: static method of [<code>request</code>](#module_request)  
**Summary**: Refresh token on user request  
**Returns**: <code>String</code> - token - new token  
**Access**: public  

| Param | Type | Description |
| --- | --- | --- |
| options.url | <code>String</code> | relative url |

**Example**  
```js
request.refreshToken
	baseUrl: 'https://api.resin.io'
```
<a name="module_request..Interceptor"></a>

### request~Interceptor : <code>object</code>
An interceptor implements some set of the four interception hook callbacks.
To continue processing, each function should return a value or a promise that
successfully resolves to a value.

To halt processing, each function should throw an error or return a promise that
rejects with an error.

**Kind**: inner typedef of [<code>request</code>](#module_request)  
**Properties**

| Name | Type | Description |
| --- | --- | --- |
| [request] | <code>function</code> | Callback invoked before requests are made. Called with the request options, should return (or resolve to) new request options, or throw/reject. |
| [response] | <code>function</code> | Callback invoked before responses are returned. Called with the response, should return (or resolve to) a new response, or throw/reject. |
| [requestError] | <code>function</code> | Callback invoked if an error happens before a request. Called with the error itself, caused by a preceeding request interceptor rejecting/throwing an error for the request, or a failing in preflight token validation. Should return (or resolve to) new request options, or throw/reject. |
| [responseError] | <code>function</code> | Callback invoked if an error happens in the response. Called with the error itself, caused by a preceeding response interceptor rejecting/throwing an error for the request, a network error, or an error response from the server. Should return (or resolve to) a new response, or throw/reject. |


Support
-------

If you're having any problem, please [raise an issue](https://github.com/resin-io-modules/resin-request/issues/new) on GitHub and the Resin.io team will be happy to help.

Tests
-----

Run the test suite by doing:

```sh
$ npm test
```

Contribute
----------

- Issue Tracker: [github.com/resin-io-modules/resin-request/issues](https://github.com/resin-io-modules/resin-request/issues)
- Source Code: [github.com/resin-io-modules/resin-request](https://github.com/resin-io-modules/resin-request)

Before submitting a PR, please make sure that you include tests, and that [coffeelint](http://www.coffeelint.org/) runs without any warning:

```sh
$ gulp lint
```

License
-------

The project is licensed under the Apache 2.0 license.

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