39 lines
1.1 KiB
TypeScript
39 lines
1.1 KiB
TypeScript
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
|
|
import { ApiResponse } from '@stock-bot/types';
|
|
|
|
/**
|
|
* Base API client that all service clients extend
|
|
*/
|
|
export abstract class BaseApiClient {
|
|
protected client: AxiosInstance;
|
|
|
|
constructor(baseURL: string, config?: AxiosRequestConfig) {
|
|
this.client = axios.create({
|
|
baseURL,
|
|
timeout: 10000, // 10 seconds timeout
|
|
...config
|
|
});
|
|
|
|
// Add response interceptor for consistent error handling
|
|
this.client.interceptors.response.use(
|
|
(response) => response.data,
|
|
(error) => {
|
|
// Format error for consistent error handling
|
|
const formattedError = {
|
|
status: error.response?.status || 500,
|
|
message: error.response?.data?.error || error.message || 'Unknown error',
|
|
originalError: error
|
|
};
|
|
|
|
return Promise.reject(formattedError);
|
|
}
|
|
);
|
|
}
|
|
|
|
/**
|
|
* Get the health status of a service
|
|
*/
|
|
async getHealth(): Promise<ApiResponse<{ status: string }>> {
|
|
return this.client.get('/api/health');
|
|
}
|
|
}
|