@atlantjs.dev/http-client
v1.0.8
Published
Base HTTP client class.
Downloads
9
Readme
Http Client
Base HTTP client class.
Install
npm install @atlantjs.dev/http-client
yarn add @atlantjs.dev/http-client
Using
getData
parses the data according with the provided parse
callback parameter.
getRawData
gets the response data without safe typing.
hasError
parses the error object according with the provided parse
callback
parameter and determines the error based on the errorValidator
calback parameter.
Example using Zod (https://www.npmjs.com/package/zod)
import * as z from "zod";
import { HttpClient } from "@atlantjs.dev/http-client";
const client = new HttpClient("https://atlantjs.com");
const responseType = z.object({
balance: z.number(),
});
const request = await client.request({
url: "/balance",
method: "get",
});
await request.getData(responseType.parse);
Usage example with custom request listeners:
import * as z from "zod";
import { HttpClient } from "@atlantjs.dev/http-client";
const client = new HttpClient("https://atlantjs.com", {
request: (request: AxiosRequestConfig): AxiosRequestConfig => {
myCustomRequestFunction();
return request;
},
response: (response: AxiosResponse): AxiosResponse => response,
error: (): void => {},
});
await client.request({
url: "/balance",
method: "get",
});