| |
| |
| |
| |
|
|
| const api = (() => { |
| const BASE = ''; |
|
|
| |
| const ON_LOGIN_PAGE = window.location.pathname.includes('auth-login'); |
|
|
| async function request(method, path, body = null) { |
| const opts = { |
| method, |
| headers: { 'Content-Type': 'application/json' }, |
| credentials: 'same-origin', |
| }; |
| if (body !== null) opts.body = JSON.stringify(body); |
|
|
| let data; |
| try { |
| const res = await fetch(BASE + path, opts); |
| data = await res.json(); |
| } catch (e) { |
| if (!ON_LOGIN_PAGE) window.location.href = '/auth-login.html'; |
| return { success: false, error: 'Error de conexión' }; |
| } |
|
|
| |
| if (data.redirect_to_login && !ON_LOGIN_PAGE) { |
| window.location.href = '/auth-login.html'; |
| } |
|
|
| return data; |
| } |
|
|
| const get = (path) => request('GET', path); |
| const post = (path, body) => request('POST', path, body); |
| const put = (path, body) => request('PUT', path, body); |
| const del = (path) => request('DELETE', path); |
|
|
| return { |
| |
| login: (username, password) => post('/api/login', { username, password }), |
| logout: () => post('/api/logout', {}), |
| getSession: () => get('/api/session'), |
|
|
| |
| getUsers: () => get('/api/users'), |
| createUser: (username, password, role) => post('/api/users', { username, password, role }), |
| updateUser: (userId, data) => put(`/api/users/${userId}`, data), |
| deleteUser: (userId) => del(`/api/users/${userId}`), |
|
|
| |
| getPatients: (search = '') => get(`/api/patients${search ? '?search=' + encodeURIComponent(search) : ''}`), |
| createPatient: (data) => post('/api/patients', data), |
| getPatient: (id) => get(`/api/patients/${id}`), |
| updatePatient: (id, data) => put(`/api/patients/${id}`, data), |
| deletePatient: (id) => del(`/api/patients/${id}`), |
|
|
| |
| getRiskFactors: () => get('/api/risk-factors'), |
| addPatientRiskFactor: (pid, rfid) => post(`/api/patients/${pid}/risk-factors`, { riskFactorID: rfid }), |
| removePatientRiskFactor: (pid, rfid) => del(`/api/patients/${pid}/risk-factors/${rfid}`), |
|
|
| |
| predict: (imageData, filename) => |
| post('/api/predict', { imageData, filename }), |
|
|
| gradcam: (imageData, filename, predictionResult) => |
| post('/api/gradcam', { imageData, filename, predictionResult }), |
|
|
| |
| getConsultations: (page = 1, perPage = 10, search = '', filter = 'all') => |
| get(`/api/consultations?page=${page}&per_page=${perPage}&search=${encodeURIComponent(search)}&filter=${filter}`), |
|
|
| getConsultation: (id) => get(`/api/consultations/${id}`), |
|
|
| saveConsultation: (data) => post('/api/consultations', data), |
|
|
| |
| getDashboardStats: () => get('/api/dashboard/stats'), |
| getModelInfo: () => get('/api/model/info'), |
|
|
| |
| getTasks: () => get('/api/tasks'), |
| addTask: (text) => post('/api/tasks', { text }), |
| toggleTask: (id) => post(`/api/tasks/${id}/toggle`, {}), |
| deleteTask: (id) => del(`/api/tasks/${id}`), |
| }; |
| })(); |
|
|
| if (typeof window !== 'undefined') window.api = api; |