/** * @glam/api-client - Base API client */ import axios, { AxiosInstance, AxiosRequestConfig } from 'axios'; import { getApiConfig, ApiConfig } from '../config'; export class BaseApiClient { protected client: AxiosInstance; protected config: ApiConfig; constructor(baseURL: string, options: AxiosRequestConfig = {}) { this.config = getApiConfig(); this.client = axios.create({ baseURL, timeout: 30000, headers: { 'Content-Type': 'application/json', }, ...options, }); // Request interceptor for logging this.client.interceptors.request.use( (config) => { console.debug(`[API] ${config.method?.toUpperCase()} ${config.url}`); return config; }, (error) => Promise.reject(error) ); // Response interceptor for error handling this.client.interceptors.response.use( (response) => response, (error) => { console.error('[API Error]', error.response?.data || error.message); return Promise.reject(error); } ); } protected async get(url: string, config?: AxiosRequestConfig): Promise { const response = await this.client.get(url, config); return response.data; } protected async post(url: string, data?: unknown, config?: AxiosRequestConfig): Promise { const response = await this.client.post(url, data, config); return response.data; } protected async put(url: string, data?: unknown, config?: AxiosRequestConfig): Promise { const response = await this.client.put(url, data, config); return response.data; } protected async delete(url: string, config?: AxiosRequestConfig): Promise { const response = await this.client.delete(url, config); return response.data; } }