import axios from 'axios'; import { AuthResponse, User, Endpoint, Folder, ApiKey, Database, QueryTestResult, ImportPreviewResponse } 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, env?: 'prod' | 'test') => api.get<{ success: boolean; message: string }>(`/db-management/${id}/test${env === 'test' ? '?env=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' | 'aql'; sql_query?: string; parameters?: any[]; endpoint_parameters?: any[]; script_language?: 'javascript' | 'python'; script_code?: string; script_queries?: any[]; aql_method?: string; aql_endpoint?: string; aql_body?: string; aql_query_params?: Record; environment?: 'prod' | 'test'; }) => api.post('/endpoints/test', data), exportEndpoint: (id: string) => api.get(`/endpoints/${id}/export`, { responseType: 'blob' }), importPreview: (file: File) => file.arrayBuffer().then(buffer => api.post('/endpoints/import/preview', buffer, { headers: { 'Content-Type': 'application/octet-stream' }, }) ), importConfirm: (data: { file_data: string; database_mapping: Record; folder_id?: string | null; override_path?: string; }) => api.post('/endpoints/import', 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('/workbench/execute', { database_id: databaseId, query }), }; // Schema API export interface ColumnInfo { name: string; type: string; nullable: boolean; default_value: string | null; is_primary: boolean; comment: string | null; } export interface ForeignKey { column: string; references_table: string; references_column: string; constraint_name: string; } export interface TableInfo { name: string; schema: string; comment: string | null; columns: ColumnInfo[]; foreign_keys: ForeignKey[]; } export interface SchemaData { tables: TableInfo[]; updated_at: string; } export const schemaApi = { getSchema: (databaseId: string) => api.get<{ success: boolean; data: SchemaData }>(`/workbench/schema/${databaseId}`), refreshSchema: (databaseId: string) => api.post<{ success: boolean; data: SchemaData }>(`/workbench/schema/${databaseId}/refresh`), }; export default api;