// API Helper - Centralized API calls class API { constructor() { this.baseURL = '/api'; this.timeout = 10000; } async request(endpoint, options = {}) { const { method = 'GET', headers = {}, body = null, timeout = this.timeout } = options; const url = `${this.baseURL}${endpoint}`; const config = { method, headers: { 'Content-Type': 'application/json', ...headers } }; if (body) { config.body = JSON.stringify(body); } try { const controller = new AbortController(); const timeoutId = setTimeout(() => controller.abort(), timeout); const response = await fetch(url, { ...config, signal: controller.signal }); clearTimeout(timeoutId); if (!response.ok) { if (response.status === 401) { // Redirect to login if unauthorized window.location.hash = '#login'; throw new Error('Unauthorized. Please login again.'); } throw new Error(`HTTP ${response.status}: ${response.statusText}`); } return await response.json(); } catch (error) { console.error('API Error:', error); throw error; } } // Auth endpoints async login(email, password) { return this.request('/auth/login', { method: 'POST', body: { email, password } }); } async register(name, email, password) { return this.request('/auth/register', { method: 'POST', body: { name, email, password } }); } async logout() { return this.request('/auth/logout', { method: 'POST' }); } async getCurrentUser() { return this.request('/auth/me'); } // Users endpoints async getUsers() { return this.request('/users'); } async createUser(userData) { return this.request('/users', { method: 'POST', body: userData }); } async updateUser(userId, userData) { return this.request(`/users/${userId}`, { method: 'PATCH', body: userData }); } async deleteUser(userId) { return this.request(`/users/${userId}`, { method: 'DELETE' }); } // Database endpoints async getTables() { return this.request('/db/tables'); } async getTableData(tableName, limit = 100, offset = 0) { return this.request(`/db/tables/${tableName}/data?limit=${limit}&offset=${offset}`); } async executeQuery(sql) { return this.request('/db/query', { method: 'POST', body: { sql } }); } async getDatabaseStats() { return this.request('/db/stats'); } // Admin endpoints async getSystemStats() { return this.request('/admin/stats'); } async getLogs(limit = 100) { return this.request(`/admin/logs?limit=${limit}`); } async getBackups() { return this.request('/admin/backups'); } async createBackup() { return this.request('/admin/backups', { method: 'POST' }); } } // Global API instance const api = new API();