new file: backend/src/controllers/sqlInterfaceController.ts new file: backend/src/routes/sqlInterface.ts modified: backend/src/server.ts modified: docker-compose.external-db.yml modified: frontend/src/App.tsx modified: frontend/src/components/Sidebar.tsx new file: frontend/src/pages/SqlInterface.tsx modified: frontend/src/services/api.ts
191 lines
4.8 KiB
TypeScript
191 lines
4.8 KiB
TypeScript
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<AuthResponse>('/auth/login', { username, password }),
|
|
|
|
getMe: () =>
|
|
api.get<User>('/auth/me'),
|
|
};
|
|
|
|
// Users API (superadmin only)
|
|
export const usersApi = {
|
|
getAll: () =>
|
|
api.get<User[]>('/users'),
|
|
|
|
create: (data: { username: string; password: string; role?: string; is_superadmin?: boolean }) =>
|
|
api.post<User>('/users', data),
|
|
|
|
update: (id: string, data: Partial<User> & { password?: string }) =>
|
|
api.put<User>(`/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<any[]>('/db-management'),
|
|
|
|
getById: (id: string) =>
|
|
api.get<any>(`/db-management/${id}`),
|
|
|
|
create: (data: any) =>
|
|
api.post<any>('/db-management', data),
|
|
|
|
update: (id: string, data: any) =>
|
|
api.put<any>(`/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<Endpoint[]>('/endpoints', { params: { search, folder_id: folderId } }),
|
|
|
|
getById: (id: string) =>
|
|
api.get<Endpoint>(`/endpoints/${id}`),
|
|
|
|
create: (data: Partial<Endpoint>) =>
|
|
api.post<Endpoint>('/endpoints', data),
|
|
|
|
update: (id: string, data: Partial<Endpoint>) =>
|
|
api.put<Endpoint>(`/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<QueryTestResult>('/endpoints/test', data),
|
|
};
|
|
|
|
// Folders API
|
|
export const foldersApi = {
|
|
getAll: () =>
|
|
api.get<Folder[]>('/folders'),
|
|
|
|
getById: (id: string) =>
|
|
api.get<Folder>(`/folders/${id}`),
|
|
|
|
create: (name: string, parentId?: string) =>
|
|
api.post<Folder>('/folders', { name, parent_id: parentId }),
|
|
|
|
update: (id: string, name: string, parentId?: string) =>
|
|
api.put<Folder>(`/folders/${id}`, { name, parent_id: parentId }),
|
|
|
|
delete: (id: string) =>
|
|
api.delete(`/folders/${id}`),
|
|
};
|
|
|
|
// API Keys API
|
|
export const apiKeysApi = {
|
|
getAll: () =>
|
|
api.get<ApiKey[]>('/keys'),
|
|
|
|
create: (name: string, permissions: string[], expiresAt?: string, enableLogging?: boolean) =>
|
|
api.post<ApiKey>('/keys', { name, permissions, expires_at: expiresAt, enable_logging: enableLogging }),
|
|
|
|
update: (id: string, data: Partial<ApiKey>) =>
|
|
api.put<ApiKey>(`/keys/${id}`, data),
|
|
|
|
delete: (id: string) =>
|
|
api.delete(`/keys/${id}`),
|
|
};
|
|
|
|
// Databases API
|
|
export const databasesApi = {
|
|
getAll: () =>
|
|
api.get<Database[]>('/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<SqlQueryResult>('/sql/execute', { database_id: databaseId, query }),
|
|
};
|
|
|
|
export default api;
|