1.0.0-beta.2 • Published 8 months ago

@podium/http-client v1.0.0-beta.2

Weekly downloads
-
License
MIT
Repository
github
Last release
8 months ago

@podium/http-client

⚠️ This project is still work in progress, should not be used for anything just yet.

Generic http client built on undici with a circuit breaker using opossum, error handling and metrics out of the box.

GitHub Actions status Known Vulnerabilities

Installation

Note! Requires Node.js v20 or later.

npm install @podium/http-client

Usage

import client from '@podium/http-client';
const client = new HttpClient(options);

const response = await client.request({ path: '/', origin: 'https://host.domain' })
if (response.ok) {
    //
}

API

Constructor

import client from '@podium/http-client';

const client = new HttpClient(options);

options

optiondefaulttyperequireddetails
abortControllerundefinedobjectnoSee abortController
connections50numbernoSee connections
fallbackundefinedfunctionnoFunction to call when requests fail, see fallback
followRedirectsfalsebooleannoFlag for whether to follow redirects or not, see followRedirects.
keepAliveMaxTimeoutundefinednumbernoSee keepAliveMaxTimeout
keepAliveTimeoutundefinednumbernoSee keepAliveTimeout
loggerundefinedobjectnoA logger which conform to a log4j interface
pipelining10numbernoSee pipelining
reset2000numbernoCircuit breaker: How long, in milliseconds, to wait before a tripped circuit should be reset.
threshold25numbernoCircuit breaker: How many, in %, requests should error before the circuit should trip. Ex; when 25% of requests fail, trip the circuit.
throwOn400falsebooleannoIf the client should throw on HTTP 400 errors.If true, HTTP 400 errors will counts against tripping the circuit.
throwOn500truebooleannoIf the client should throw on HTTP 500 errors.If true, HTTP 500 errors will counts against tripping the circuit.
timeout500numbernoCircuit breaker: How long, in milliseconds, a request can maximum take. Requests exceeding this limit counts against tripping the circuit.
abortController

Passing in an AbortController enables aborting requests.

connections

Property is sent to the underlying http library. See library docs on connections

fallback

Optional function to run when a request fails.

// TBA
followRedirects

TODO!!! decide what to do with the redirects stuff...

By default, the library does not follow redirect. If set to true it will follow redirects according to maxRedirections. It will by default throw on reaching throwOnMaxRedirects

keepAliveMaxTimeout

Property is sent to the underlying http library. See library docs on keepAliveTimeout

keepAliveMaxTimeout

Property is sent to the underlying http library. See library docs on keepAliveMaxTimeout

logger

Any log4j compatible logger can be passed in and will be used for logging. Console is also supported for easy test / development.

Example:

const layout = new Layout({
    name: 'myLayout',
    pathname: '/foo',
    logger: console,
});

Under the hood abslog is used to abstract out logging. Please see abslog for further details.

pipelining

Property is sent to the underlying http library. See library docs on pipelining

reset

Circuit breaker: How long, in milliseconds, to wait before a tripped circuit should be reset.

threshold

Circuit breaker: How many, in %, requests should error before the circuit should trip. Ex; when 25% of requests fail, trip the circuit.

timeout

Circuit breaker: How long, in milliseconds, a request can maximum take. Requests exceeding this limit counts against tripping the circuit.

throwOn400

If the client should throw on http 400 errors. If true, http 400 errors will count against tripping the circuit.

throwOn500

If the client should throw on http 500 errors. If true, http 500 errors will count against tripping the circuit.

Methods

async request(options = {})

Sends a request using the passed in options object.

nametypedescription
originstring \| URLRequest origin, ex https://server.domain:9090
pathstringURL path, ex /foo
methodstringHTTP method name
headersobjectObject with key / value which are strings
queryobjectObject with key / value which are strings
signalAbortSignalAbort signal for canceling requests.

For a complete list of options, consult the undici documentation.

async close()

Closes the client and it's connections.