104 lines
3.3 KiB
JavaScript
104 lines
3.3 KiB
JavaScript
const API_BASE = (import.meta.env.VITE_API_URL || '').replace(/\/$/, '');
|
|
|
|
export function getToken() {
|
|
return localStorage.getItem('authToken');
|
|
}
|
|
|
|
export function clearSession() {
|
|
localStorage.removeItem('authToken');
|
|
localStorage.removeItem('usuario');
|
|
localStorage.removeItem('rolId');
|
|
}
|
|
|
|
export async function api(endpoint, options = {}) {
|
|
const token = getToken();
|
|
const headers = {
|
|
'Content-Type': 'application/json',
|
|
...(token ? { Authorization: `Bearer ${token}` } : {}),
|
|
...options.headers,
|
|
};
|
|
|
|
const url = `${API_BASE}${endpoint.startsWith('/') ? endpoint : `/${endpoint}`}`;
|
|
const res = await fetch(url, { ...options, headers });
|
|
|
|
if (res.status === 401) {
|
|
clearSession();
|
|
if (!window.location.pathname.endsWith('/login')) {
|
|
window.location.href = `${import.meta.env.BASE_URL}login`.replace('//', '/');
|
|
}
|
|
throw new Error('Sesión expirada o no autorizado');
|
|
}
|
|
|
|
if (res.status === 204) return null;
|
|
|
|
const text = await res.text();
|
|
let data = null;
|
|
if (text) {
|
|
try {
|
|
data = JSON.parse(text);
|
|
} catch {
|
|
data = text;
|
|
}
|
|
}
|
|
|
|
if (!res.ok) {
|
|
const msg = data?.error || data?.message || `Error ${res.status}`;
|
|
throw new Error(typeof msg === 'string' ? msg : JSON.stringify(msg));
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
export const authApi = {
|
|
login: (username, password) =>
|
|
api('/api/usuarios/login', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ username, password }),
|
|
}),
|
|
me: () => api('/api/usuarios/me'),
|
|
};
|
|
|
|
export const usuariosApi = {
|
|
list: () => api('/api/usuarios'),
|
|
pendientes: () => api('/api/usuarios/pendientes'),
|
|
aceptar: (id) => api(`/api/usuarios/aceptar/${id}`, { method: 'PATCH' }),
|
|
update: (id, body) => api(`/api/usuarios/${id}`, { method: 'PUT', body: JSON.stringify(body) }),
|
|
remove: (id) => api(`/api/usuarios/${id}`, { method: 'DELETE' }),
|
|
registrar: (body) => api('/api/usuarios/registrar', { method: 'POST', body: JSON.stringify(body) }),
|
|
};
|
|
|
|
export const rematesApi = {
|
|
list: () => api('/api/remates'),
|
|
get: (id) => api(`/api/remates/${id}`),
|
|
create: (body) => api('/api/remates', { method: 'POST', body: JSON.stringify(body) }),
|
|
update: (id, body) => api(`/api/remates/${id}`, { method: 'PUT', body: JSON.stringify(body) }),
|
|
remove: (id) => api(`/api/remates/${id}`, { method: 'DELETE' }),
|
|
finalizar: (remateId, loteId) =>
|
|
api(`/api/remates/${remateId}/finalizar/${loteId}`, { method: 'PUT' }),
|
|
};
|
|
|
|
export const lotesApi = {
|
|
list: () => api('/api/lotes'),
|
|
get: (id) => api(`/api/lotes/${id}`),
|
|
create: (body) => api('/api/lotes', { method: 'POST', body: JSON.stringify(body) }),
|
|
update: (id, body) => api(`/api/lotes/${id}`, { method: 'PUT', body: JSON.stringify(body) }),
|
|
remove: (id) => api(`/api/lotes/${id}`, { method: 'DELETE' }),
|
|
};
|
|
|
|
export const cabanasApi = {
|
|
list: () => api('/api/cabanas'),
|
|
get: (id) => api(`/api/cabanas/${id}`),
|
|
create: (body) => api('/api/cabanas', { method: 'POST', body: JSON.stringify(body) }),
|
|
update: (id, body) => api(`/api/cabanas/${id}`, { method: 'PUT', body: JSON.stringify(body) }),
|
|
remove: (id) => api(`/api/cabanas/${id}`, { method: 'DELETE' }),
|
|
};
|
|
|
|
export const pujasApi = {
|
|
list: () => api('/api/pujas'),
|
|
remove: (id) => api(`/api/pujas/${id}`, { method: 'DELETE' }),
|
|
};
|
|
|
|
export const rolesApi = {
|
|
list: () => api('/api/roles'),
|
|
};
|