Create a new Client instance
Client configuration options
Configuration for the API client instance
OptionalbaseURL?: stringBase URL for the request - will be combined with url
Optionaltimeout?: numberRequest timeout in milliseconds before aborting
Optionalretry?: number | RetryConfigRetry configuration for failed requests
Optionalheaders?: Record<string, string>Default headers for all requests made by this client
OptionalresponseType?: ResponseTypeDefault response type for all requests
Initializes the HTTP client with the provided configuration. Sets up interceptors and validates the configuration for common issues.
// Minimal configuration
const api = new Client();
// Full configuration
const api = new Client({
baseURL: 'https://api.example.com',
timeout: 15000,
headers: {
'User-Agent': 'MyApp/1.0',
'Accept': 'application/json',
'Authorization': 'Bearer your-token'
},
retryConfig: {
retries: 3,
retryDelay: 1000,
retryCondition: (error) => error.status >= 500
},
responseType: 'json'
});
ReadonlyinterceptorsRequest and response interceptors for middleware processing
Provides hooks to transform requests before they're sent and responses before they're returned. Useful for authentication, logging, error handling, and data transformation.
// Add authentication to all requests
api.interceptors.request.use((request) => {
request.headers.set('Authorization', `Bearer ${getToken()}`);
return request;
});
// Handle errors globally
api.interceptors.response.use((response) => {
if (response.status === 401) {
redirectToLogin();
}
return response;
});
Sends an HTTP GET request
The expected response data type
The endpoint path (relative to baseURL or absolute URL)
Optional request configuration
Promise resolving to parsed response data of type T
Sends an HTTP POST request
The expected response data type
The endpoint path (relative to baseURL or absolute URL)
Optionaldata: BodyRequest body payload (JSON object, FormData, string, etc.)
Optional request configuration
Promise resolving to parsed response data of type T
// POST JSON data
const newUser = await api.post<User>('/users', {
name: 'John Doe',
email: 'john@example.com'
});
// POST FormData
const formData = new FormData();
formData.append('file', file);
const result = await api.post('/upload', formData);
// POST with custom headers
const response = await api.post('/webhook', payload, {
headers: { 'X-Webhook-Secret': secret }
});
Sends an HTTP PUT request
The expected response data type
The endpoint path (relative to baseURL or absolute URL)
Optionaldata: BodyRequest body payload for updating the entire resource
Optional request configuration
Promise resolving to parsed response data of type T
Sends an HTTP PATCH request
The expected response data type
The endpoint path (relative to baseURL or absolute URL)
Optionaldata: BodyPartial payload to update specific fields of the resource
Optional request configuration
Promise resolving to parsed response data of type T
Sends an HTTP DELETE request
The expected response data type (often void or deletion confirmation)
The endpoint path (relative to baseURL or absolute URL)
Optional request configuration
Promise resolving to parsed response data of type T
Sends an HTTP HEAD request.
The endpoint path.
Additional request options.
Parsed response headers as T.
Sets a default header applied to all outgoing requests.
The header name.
The header value.
Removes a default header.
The header name to remove.
Returns the current client configuration.
A read-only copy of the configuration.
Executes the raw request flow:
The user-supplied request configuration.
The wrapped ApiResponse instance.
High-performance API client with automatic retries, interceptors, and type safety
Description
Modern HTTP client built on the native fetch API with enterprise-grade features including request/response interceptors, automatic retry logic with exponential backoff, comprehensive error handling, and full TypeScript support.
Example