@podium/http-client v1.0.0-beta.2
@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.
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
option | default | type | required | details |
---|---|---|---|---|
abortController | undefined | object | no | See abortController |
connections | 50 | number | no | See connections |
fallback | undefined | function | no | Function to call when requests fail, see fallback |
followRedirects | false | boolean | no | Flag for whether to follow redirects or not, see followRedirects. |
keepAliveMaxTimeout | undefined | number | no | See keepAliveMaxTimeout |
keepAliveTimeout | undefined | number | no | See keepAliveTimeout |
logger | undefined | object | no | A logger which conform to a log4j interface |
pipelining | 10 | number | no | See pipelining |
reset | 2000 | number | no | Circuit breaker: How long, in milliseconds, to wait before a tripped circuit should be reset. |
threshold | 25 | number | no | Circuit breaker: How many, in %, requests should error before the circuit should trip. Ex; when 25% of requests fail, trip the circuit. |
throwOn400 | false | boolean | no | If the client should throw on HTTP 400 errors.If true, HTTP 400 errors will counts against tripping the circuit. |
throwOn500 | true | boolean | no | If the client should throw on HTTP 500 errors.If true, HTTP 500 errors will counts against tripping the circuit. |
timeout | 500 | number | no | Circuit 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.
name | type | description |
---|---|---|
origin | string \| URL | Request origin, ex https://server.domain:9090 |
path | string | URL path, ex /foo |
method | string | HTTP method name |
headers | object | Object with key / value which are strings |
query | object | Object with key / value which are strings |
signal | AbortSignal | Abort signal for canceling requests. |
For a complete list of options, consult the undici documentation.
async close()
Closes the client and it's connections.
8 months ago