- Migration 010: test_* columns on databases table, environment column on request_logs - DatabasePoolManager: dual-pool strategy (prod + test), getPool(id, env) with fallback - All executors (SQL, Script, AQL): environment param threaded through execution paths - dynamicApiController: X-Environment header detection, environment in logging - databaseManagementController: CRUD for test credentials, testConnection with ?env=test - Frontend: test env form in DatabaseModal, env toggle in EndpointEditor test panel Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
252 lines
6.4 KiB
TypeScript
252 lines
6.4 KiB
TypeScript
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<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, 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<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' | '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<string, string>;
|
|
environment?: 'prod' | 'test';
|
|
}) =>
|
|
api.post<QueryTestResult>('/endpoints/test', data),
|
|
|
|
exportEndpoint: (id: string) =>
|
|
api.get(`/endpoints/${id}/export`, { responseType: 'blob' }),
|
|
|
|
importPreview: (file: File) =>
|
|
file.arrayBuffer().then(buffer =>
|
|
api.post<ImportPreviewResponse>('/endpoints/import/preview', buffer, {
|
|
headers: { 'Content-Type': 'application/octet-stream' },
|
|
})
|
|
),
|
|
|
|
importConfirm: (data: {
|
|
file_data: string;
|
|
database_mapping: Record<string, string>;
|
|
folder_id?: string | null;
|
|
override_path?: string;
|
|
}) =>
|
|
api.post<Endpoint>('/endpoints/import', 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>('/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;
|