HttpClient is a lightweight HTTP client library based on the Fetch API, offering a simple and flexible API for making HTTP requests. This library allows you to easily perform basic HTTP operations and customize requests and responses using interceptors.
- Supports GET, POST, PUT, PATCH, DELETE request methods
- Reduce code redundancy by setting a base URL
- Customize requests and responses with interceptor support
- Provides type safety for catching errors at compile time
npm install @hanse-kim/http-clientyarn add @hanse-kim/http-clientYou can create a basic HttpClient instance as follows:
import { HttpClient } from 'your-http-client';
const client = new HttpClient({
baseUrl: 'https://api.example.com',
headers: {
Authorization: 'Bearer your-token',
},
});client
.get<User[]>('/users')
.then((response) => console.log(response.data))
.catch((error) => console.error(error));client
.post<User>('/users', { name: 'John Doe' })
.then((response) => console.log(response.data))
.catch((error) => console.error(error));client
.put<User>('/users/1', { name: 'Jane Doe' })
.then((response) => console.log(response.data))
.catch((error) => console.error(error));client
.delete<void>('/users/1')
.then(() => console.log('User deleted'))
.catch((error) => console.error(error));You can intercept and modify requests and responses:
const client = new HttpClient({
interceptors: {
requestInterceptor: (request) => {
console.log('Request:', request);
return request;
},
responseInterceptor: (response) => {
console.log('Response:', response);
return response;
},
},
});If the response has an error status, an HttpClientError exception will be thrown.
client
.get<User>('/invalid-url')
.then((response) => console.log(response.data))
.catch((error: HttpClientError<any>) => {
console.error(`Error ${error.status}:`, error.body);
});constructor(options?: HttpClientOptions): Creates a newHttpClientinstance.get<T>(url: string, options?: RequestOptions): Performs a GET request.post<T>(url: string, body: RequestBody, options?: RequestOptions): Performs a POST request.put<T>(url: string, body: RequestBody, options?: RequestOptions): Performs a PUT request.patch<T>(url: string, body: RequestBody, options?: RequestOptions): Performs a PATCH request.delete<T>(url: string, options?: RequestOptions): Performs a DELETE request.
baseUrl: Sets the base URL for all requests.headers: Sets default headers for all requests.interceptors: Allows you to define request and response interceptors.
url: The URL to request.method: The HTTP method (GET, POST, PUT, PATCH, DELETE).body: The data to send with the request.query: Query parameters to append to the URL.
data: Contains the response data.status: The HTTP status code of the response.
MIT Licensed.