Overview
TheAxiosHttpClient class provides a type-safe HTTP client implementation using Axios. It implements the HttpClient interface and handles all HTTP communication with the backend API, including error handling, request configuration, and response transformation.
HttpClient Interface
The core interface that defines the contract for HTTP operations:AxiosHttpClient Class
Location
infraestructure/http/axios-http.client.ts
Constructor
The base URL for all API requests. Defaults to
NEXT_PUBLIC_API_URL environment variable or http://localhost:3004Configuration
The client is configured with the following default settings:Base URL for all requests (configurable via constructor or environment variable)
Request timeout set to 8000ms (8 seconds)
Default headers including
Content-Type: application/jsonInterceptors
The client includes a response interceptor that:- Returns successful responses unchanged
- Extracts error data from
error.response.datafor failed requests - Provides a fallback “Unexpected HTTP error” message if no error data is available
Methods
get()
Performs an HTTP GET request.The endpoint URL (relative to baseURL)
Additional Axios configuration options (params, headers, etc.)
Promise<T> - The response data typed as T
post()
Performs an HTTP POST request.The endpoint URL (relative to baseURL)
The request body to send
Additional Axios configuration options
Promise<T> - The response data typed as T
put()
Performs an HTTP PUT request.The endpoint URL (relative to baseURL)
The request body to send
Additional Axios configuration options
Promise<T> - The response data typed as T
patch()
Performs an HTTP PATCH request.The endpoint URL (relative to baseURL)
The request body to send
Additional Axios configuration options
Promise<T> - The response data typed as T
delete()
Performs an HTTP DELETE request.The endpoint URL (relative to baseURL)
Additional Axios configuration options
Promise<T> - The response data typed as T
Usage Example
Here’s how the HTTP client is used in theHttpOrderRepository:
Implementation Details
Error Handling
The response interceptor automatically extracts error data from failed requests, making it easier to handle API errors consistently throughout the application.
Type Safety
All methods are generic and accept a type parameter<T> for the expected response data type. This provides compile-time type checking and IntelliSense support.
Configuration Priority
The base URL is resolved in the following order:- Constructor parameter
baseURL NEXT_PUBLIC_API_URLenvironment variable- Default fallback:
http://localhost:3004
Related
- Order Repository - Uses this HTTP client
- WebSocket Client - For real-time communication