adding data-services
This commit is contained in:
parent
e3bfd05b90
commit
405b818c86
139 changed files with 55943 additions and 416 deletions
39
libs/api-client/src/BaseApiClient.ts
Normal file
39
libs/api-client/src/BaseApiClient.ts
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
import axios, { AxiosInstance, AxiosRequestConfig } from 'axios';
|
||||
import { ApiResponse } from '@stock-bot/shared-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');
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue