0.4.0 • Published 5 years ago

@capriza/http-utils v0.4.0

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

@capriza/http-utils

Table Of Contents

Overview

Http requests client based on axios. 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 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 and retryNetworkErrors, respectively. 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. 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. default: [502, 503, 504]

limit

Maximum number of http requests per interval. default: Infinity

interval

Amount of time (milliseconds) during which the http requests limit can to be executed. default: 1000

maxConcurrent

Number of requests allowed to execute simultaneously 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);
}
0.4.0

5 years ago

0.4.0-test.0

5 years ago

0.3.0-test.1

5 years ago

0.4.0-dev.0

5 years ago

0.3.0

5 years ago

0.3.0-prod.0

5 years ago

0.3.0-test.0

5 years ago

0.3.0-dev.0

5 years ago

0.2.16-test.0

5 years ago

0.2.16-dev.1

5 years ago

0.2.16-dev.0

5 years ago

0.2.14-dev.8

5 years ago

0.2.14-dev.7

5 years ago

0.2.14-dev.6

5 years ago

0.2.14-dev.5

5 years ago

0.2.14-dev.4

5 years ago

0.2.14-dev.3

5 years ago

0.2.14-dev.2

5 years ago

0.2.16

5 years ago

0.2.15

5 years ago

0.2.14

5 years ago

0.2.14-dev.1

5 years ago

0.2.14-dev.0

5 years ago

0.2.13-dev.0

5 years ago

0.2.13

5 years ago

0.2.12

5 years ago

0.2.11

5 years ago

0.2.10

5 years ago

0.2.9

5 years ago

0.2.8

5 years ago

0.2.7

5 years ago

0.2.6

5 years ago

0.2.5

5 years ago

0.2.5-beta.7

5 years ago

0.2.5-beta.6

5 years ago

0.2.5-beta.5

5 years ago

0.2.5-beta.4

5 years ago

0.2.5-beta.3

5 years ago

0.2.5-beta.2

5 years ago

0.2.5-beta.1

5 years ago

0.2.4

5 years ago

0.2.3

5 years ago

0.2.2

5 years ago

0.2.1

5 years ago

0.2.0

5 years ago

0.2.0-beta.4

5 years ago

0.2.0-beta.3

5 years ago

0.2.0-beta.2

5 years ago

0.2.0-beta.1

5 years ago

0.1.10

5 years ago

0.1.9

5 years ago

0.1.8

5 years ago

0.1.7

5 years ago

0.1.6

6 years ago

0.1.5

6 years ago

0.1.4

6 years ago

0.1.3

6 years ago

0.1.2

6 years ago

0.1.1

6 years ago

0.1.0

6 years ago