73 lines
2.1 KiB
TypeScript
73 lines
2.1 KiB
TypeScript
import axios, { AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
|
|
|
|
export interface ApiResponse<T = any> {
|
|
status?: string;
|
|
message?: string;
|
|
data?: T;
|
|
[key: string]: any;
|
|
}
|
|
|
|
class Request {
|
|
private instance: AxiosInstance;
|
|
|
|
constructor(baseURL: string) {
|
|
this.instance = axios.create({
|
|
baseURL,
|
|
timeout: 15000,
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
|
|
this.setupInterceptors();
|
|
}
|
|
|
|
private setupInterceptors() {
|
|
this.instance.interceptors.request.use(
|
|
(config) => {
|
|
(config as any).metadata = { startTime: new Date() };
|
|
return config;
|
|
},
|
|
(error) => Promise.reject(error)
|
|
);
|
|
|
|
this.instance.interceptors.response.use(
|
|
(response: AxiosResponse) => {
|
|
const startTime = (response.config as any).metadata.startTime;
|
|
const endTime = new Date();
|
|
const duration = endTime.getTime() - startTime.getTime();
|
|
|
|
// Log request for debugging panel
|
|
console.log(`[API] ${response.config.method?.toUpperCase()} ${response.config.url} - ${duration}ms`);
|
|
|
|
return response.data;
|
|
},
|
|
(error) => {
|
|
const message = error.response?.data?.message || error.message || 'Request failed';
|
|
console.error(`[API Error]`, error);
|
|
return Promise.reject({
|
|
message,
|
|
status: error.response?.status,
|
|
data: error.response?.data
|
|
});
|
|
}
|
|
);
|
|
}
|
|
|
|
public get<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
|
|
return this.instance.get(url, config);
|
|
}
|
|
|
|
public post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<T> {
|
|
return this.instance.post(url, data, config);
|
|
}
|
|
|
|
public delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<T> {
|
|
return this.instance.delete(url, config);
|
|
}
|
|
}
|
|
|
|
export const ingestionApi = new Request(import.meta.env.VITE_API_INGESTION);
|
|
export const mcpApi = new Request(import.meta.env.VITE_API_MCP);
|
|
export const gatewayApi = new Request(import.meta.env.VITE_API_GATEWAY);
|