@aindu/http-client
v1.0.6
Published
Idiomatic HttpClient like axios
Downloads
14
Readme
@aindu/http-client
An idiomatic and isomorphic HttpClient based on fetch with native support for TypeScript.
Only supports modern browsers and NodeJS >= 18
Installation
npm install @aindu/http-client
Usage
Instancing the client:
This module was thinking like an instantiable HTTP client for that you need to import and create an instance of the client:
import { HttpClient } from "@aindu/http-client";
const restClient = new HttpClient();
This client by default doesn't need any config, but you can set some things in the config to have a better experience.
HttpClient Config
| field | description | type | default value |
|-------|-------------|------|---------------|
| timeout | timeout of the request in ms | number | infinite |
| retries | the number of retries after the first request | number | 0 |
| basepath | A URL used as a base of all the request | string | null |
| serialization | an object with two fields (input & output Functions) to map the input (body) or output (response) to another naming convention | { input: Function, output: Function }
| { input: null, output: null }
|
For instantiating the HttpClient with any config like be:
import { HttpClient } from "@aindu/http-client";
const restClient = new HttpClient({ ... })
Getting some info
restClient.get<{ status: "UP" | "DOWN" }>("http://www.api.com/health-check")
Posting some info
restClient.post<{ userId: string }>("http://www.api.com/users", { name: "Pepe", surname: "Argento" })
HttpClient API
NOTE: All the methods return Promises.
| method | url | body | options | |-------------|-----|------|---------| | get | ✅ | ❌ | ✅ | | post | ✅ | ✅ | ✅ | | put | ✅ | ✅ | ✅ | | delete | ✅ | ❌ | ✅ | | patch | ✅ | ✅ | ✅ | | makeRequest | ❌ | ❌ | ✅ |