# @capriza/http-utils

> HTTP Request utils that handles, request-response, errors, concurrency, priority and authentication

Latest version **0.4.0** (published 2019-07-28) · ISC license · 0 weekly downloads

## Install

```sh
npm install @capriza/http-utils
pnpm add @capriza/http-utils
yarn add @capriza/http-utils
bun add @capriza/http-utils
```

## Health

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

Positive: no vulnerabilities.

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

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.4.0 |
| Published | 2019-07-28 |
| First published | 2018-11-05 |
| Weekly downloads | 0 |
| License | ISC |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 1 |
| Unpacked size | 42.8 KB |
| Known vulnerabilities | 0 (+24 in 1 direct dependencies) |
| Install scripts | no |
| Author | Capriza |
| Maintainers | connector-dev, fujifish, galsinay128, sagimonza |
| Keywords | http, https, xhr, ajax, promise, node |

## Links

- npm: https://www.npmjs.com/package/@capriza/http-utils
- Repository: https://github.com/capriza/http-utils
- Homepage: https://github.com/capriza/http-utils#readme
- Issues: https://github.com/capriza/http-utils/issues
- npm.io page: https://npm.io/package/@capriza/http-utils

## Dependencies (1)

- [axios](https://npm.io/package/axios.md) ^0.18.1

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

- 0.4.0 (latest) — 2019-07-28
- 0.4.0-test.0 (test) — 2019-07-23
- 0.4.0-dev.0 (dev) — 2019-07-22
- 0.3.0 (prod) — 2019-07-21
- 0.2.5-beta.7 (next) — 2019-03-05
- 0.3.0-test.1 — 2019-07-22
- 0.3.0-prod.0 — 2019-07-21
- 0.3.0-test.0 — 2019-07-15
- 0.3.0-dev.0 — 2019-07-15
- 0.2.16-test.0 — 2019-07-15
- 0.2.16-dev.1 — 2019-07-10
- 0.2.16-dev.0 — 2019-07-02
- 0.2.14-dev.8 — 2019-06-27
- 0.2.14-dev.7 — 2019-06-19
- 0.2.14-dev.6 — 2019-06-18
- … 45 more at https://npm.io/package/@capriza/http-utils/versions

## README

# @capriza/http-utils
## Table Of Contents
  - [Overview](#overview)
  - [Installing](#installing)
  - [Instance Configuring](#instance-configuring)
    - [headers](#headers)
    - [timeout](#timeout)
    - [baseUrl](#baseUrl)
    - [auth](#auth)
    - [maxRetries](#maxRetries)
    - [retryInterval](#retryInterval)
    - [retryNetworkErrors](#retryNetworkErrors)
    - [retryStatusCodes](#retryStatusCodes)
    - [limit](#limit)
    - [interval](#interval)
    - [maxConcurrent](#maxConcurrent)
  - [Instance Methods](#instance-methods)
  - [Examples](#examples)

## Overview
Http requests client based on [axios](https://www.npmjs.com/package/axios).<br />
The Http-Utils library supports rate limits, concurrent requests, retry requests, request prioritization etc.
 
## Installing

```
$ npm install @capriza/http-utils
```
 
## Instance Configuring
An http-utils instance can be configured with the following properties:

### headers
default: ```{ Accept: "application/json", Content-Type: "application/json" }```

### timeout
Maximum duration (milliseconds) allowed for request execution
<br />
default: `30000`
 
### baseUrl
Base URL for requests executed by this instance.

### auth
Request authentication by username and password, formatted as follows:
```
{ username : 'aaa', password : '123456' }
```

### maxRetries
Maximum retries permitted in case of server or network errors, depending on settings of [retryStatusCodes](#retryStatusCodes) and [retryNetworkErrors](#retryNetworkErrors), respectively.
<br />
default: `0`

### retryInterval
Amount of time (milliseconds) between retries.

### retryNetworkErrors
If one of these values is received as the error code of the response, a retry of the request will be triggered.
<br />
default: `["ECONNABORTED", "ECONNRESET"]`

### retryStatusCodes
If one of these values is received as the status code of the response, a retry of the request will be triggered.
<br />
default: `[502, 503, 504]`

### limit
Maximum number of http requests per [interval](#interval).
<br />
default: `Infinity`

### interval
Amount of time (milliseconds) during which the http requests [limit](#limit) can to be executed.
<br />
default: `1000`

### maxConcurrent
Number of requests allowed to execute simultaneously
<br />
default: `Infinity`

## Instance Methods
The available instance methods are listed below:

```
httpClient.get(url, data, props)
```
```
httpClient.post(url, data, props)
```
```
httpClient.put(url, data, props)
```
```
httpClient.patch(url, data, props)
```
```
httpClient.head(url, data, props)
```

 
## Examples
Instance with base URL, 5 http requests allowed every 10 seconds, allowing 3 retries in case of network or server (502, 503) errors.
```
const HttpUtils = require('@capriza/http-utils');
const httpClient = new HttpUtils({
    baseUrl: 'https://api.xxxxxxx.xx',
    limit: 5,
    interval: 10 * 1000,
    maxRetries: 3,
    retryStatusCodes: [502, 503]
});
```

Performing a `GET` request with priority

```
const HttpUtils = require('@capriza/http-utils');
const httpClient = new HttpUtils( { baseUrl: 'https://api.xxxxxxx.xx' } );
try {
    let result = await httpClient.get('/route', {}, { priority: true })
    console.log(result);
} catch(ex) {
    console.error(ex);
}
```

Performing a `POST` request

```
const HttpUtils = require('@capriza/http-utils');
const headers = {
   Content-Type: "application/x-www-form-urlencoded"
};

const httpClient = new HttpUtils( {
    baseUrl: 'https://api.xxxxxxx.xx',
    headers,
    retryStatusCodes: [502, 503]
    maxRetries: 3
});
try {
    let result = await httpClient.post('/route', { data: 'sessionId=aaa-bbb-ccc&token=xxx-yyy-zzz' });
    console.log(result);
} catch(ex) {
    console.error(ex);
}
```

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