import axios from 'axios'; import { AuthResponse, User, Endpoint, Folder, ApiKey, Database, QueryTestResult } from '@/types'; const api = axios.create({ baseURL: '/api', headers: { 'Content-Type': 'application/json', }, }); // Request interceptor to add auth token api.interceptors.request.use((config) => { const token = localStorage.getItem('auth_token'); if (token) { config.headers.Authorization = `Bearer ${token}`; } return config; }); // Response interceptor for error handling api.interceptors.response.use( (response) => response, (error) => { if (error.response?.status === 401) { localStorage.removeItem('auth_token'); window.location.href = '/login'; } return Promise.reject(error); } ); // Auth API export const authApi = { login: (username: string, password: string) => api.post('/auth/login', { username, password }), getMe: () => api.get('/auth/me'), }; // Users API (superadmin only) export const usersApi = { getAll: () => api.get('/users'), create: (data: { username: string; password: string; role?: string; is_superadmin?: boolean }) => api.post('/users', data), update: (id: string, data: Partial & { password?: string }) => api.put(`/users/${id}`, data), delete: (id: string) => api.delete(`/users/${id}`), }; // Database Management API (admin only) // Logs API export const logsApi = { getAll: (filters?: any) => api.get('/logs', { params: filters }), getById: (id: string) => api.get(`/logs/${id}`), delete: (id: string) => api.delete(`/logs/${id}`), clear: (data: any) => api.post('/logs/clear', data), }; // Database Management API (admin only) export const dbManagementApi = { getAll: () => api.get('/db-management'), getById: (id: string) => api.get(`/db-management/${id}`), create: (data: any) => api.post('/db-management', data), update: (id: string, data: any) => api.put(`/db-management/${id}`, data), delete: (id: string) => api.delete(`/db-management/${id}`), test: (id: string) => api.get<{ success: boolean; message: string }>(`/db-management/${id}/test`), }; // Endpoints API export const endpointsApi = { getAll: (search?: string, folderId?: string) => api.get('/endpoints', { params: { search, folder_id: folderId } }), getById: (id: string) => api.get(`/endpoints/${id}`), create: (data: Partial) => api.post('/endpoints', data), update: (id: string, data: Partial) => api.put(`/endpoints/${id}`, data), delete: (id: string) => api.delete(`/endpoints/${id}`), test: (data: { database_id: string; execution_type?: 'sql' | 'script'; sql_query?: string; parameters?: any[]; endpoint_parameters?: any[]; script_language?: 'javascript' | 'python'; script_code?: string; script_queries?: any[]; }) => api.post('/endpoints/test', data), }; // Folders API export const foldersApi = { getAll: () => api.get('/folders'), getById: (id: string) => api.get(`/folders/${id}`), create: (name: string, parentId?: string) => api.post('/folders', { name, parent_id: parentId }), update: (id: string, name: string, parentId?: string) => api.put(`/folders/${id}`, { name, parent_id: parentId }), delete: (id: string) => api.delete(`/folders/${id}`), }; // API Keys API export const apiKeysApi = { getAll: () => api.get('/keys'), create: (name: string, permissions: string[], expiresAt?: string, enableLogging?: boolean) => api.post('/keys', { name, permissions, expires_at: expiresAt, enable_logging: enableLogging }), update: (id: string, data: Partial) => api.put(`/keys/${id}`, data), delete: (id: string) => api.delete(`/keys/${id}`), }; // Databases API export const databasesApi = { getAll: () => api.get('/databases'), test: (databaseId: string) => api.get<{ success: boolean; message: string }>(`/databases/${databaseId}/test`), getTables: (databaseId: string) => api.get<{ tables: string[] }>(`/databases/${databaseId}/tables`), getTableSchema: (databaseId: string, tableName: string) => api.get<{ schema: any[] }>(`/databases/${databaseId}/tables/${tableName}/schema`), }; // SQL Interface API export interface SqlQueryResult { success: boolean; data?: any[]; rowCount?: number; fields?: { name: string; dataTypeID: number }[]; executionTime?: number; command?: string; error?: string; position?: number; detail?: string; hint?: string; } export const sqlInterfaceApi = { execute: (databaseId: string, query: string) => api.post('/sql/execute', { database_id: databaseId, query }), }; export default api;