Init Commit
This commit is contained in:
129
frontend/src/services/api.ts
Normal file
129
frontend/src/services/api.ts
Normal file
@@ -0,0 +1,129 @@
|
||||
import axios from 'axios';
|
||||
import type {
|
||||
Configuracion,
|
||||
Evento,
|
||||
PagedResult,
|
||||
Estadisticas,
|
||||
ProbarConexionDto,
|
||||
ProbarSMTPDto,
|
||||
EjecucionManualDto,
|
||||
ApiResponse,
|
||||
} from '../types';
|
||||
import { clearAuthData, getRefreshToken, getToken, updateTokens } from '../utils/storage';
|
||||
|
||||
const API_BASE_URL = import.meta.env.VITE_API_URL || 'http://localhost:5000/api';
|
||||
|
||||
const api = axios.create({
|
||||
baseURL: API_BASE_URL,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
});
|
||||
|
||||
// Interceptor Request
|
||||
api.interceptors.request.use((config) => {
|
||||
const token = getToken(); // Usa la utilidad que busca en ambos
|
||||
if (token) {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
return config;
|
||||
});
|
||||
|
||||
// Interceptor Response
|
||||
api.interceptors.response.use(
|
||||
(response) => response,
|
||||
async (error) => {
|
||||
const originalRequest = error.config;
|
||||
|
||||
if (error.response?.status === 401 && !originalRequest._retry) {
|
||||
originalRequest._retry = true;
|
||||
|
||||
try {
|
||||
const refreshToken = getRefreshToken(); // Usa la utilidad
|
||||
|
||||
if (!refreshToken) throw new Error('No refresh token');
|
||||
|
||||
const { data } = await axios.post(`${API_BASE_URL}/auth/refresh-token`, {
|
||||
token: refreshToken
|
||||
});
|
||||
|
||||
// IMPORTANTE: Actualizamos en el storage correspondiente
|
||||
updateTokens(data.token, data.refreshToken);
|
||||
|
||||
originalRequest.headers.Authorization = `Bearer ${data.token}`;
|
||||
return api(originalRequest);
|
||||
|
||||
} catch (refreshError) {
|
||||
console.error('Sesión expirada', refreshError);
|
||||
clearAuthData();
|
||||
window.location.href = '/';
|
||||
return Promise.reject(refreshError);
|
||||
}
|
||||
}
|
||||
return Promise.reject(error);
|
||||
}
|
||||
);
|
||||
|
||||
// ===== CONFIGURACIÓN =====
|
||||
export const configuracionApi = {
|
||||
obtener: async (): Promise<Configuracion> => {
|
||||
const { data } = await api.get<Configuracion>('/configuracion');
|
||||
return data;
|
||||
},
|
||||
|
||||
actualizar: async (config: Configuracion): Promise<Configuracion> => {
|
||||
const { data } = await api.put<Configuracion>('/configuracion', config);
|
||||
return data;
|
||||
},
|
||||
|
||||
probarConexionSQL: async (dto: ProbarConexionDto): Promise<ApiResponse> => {
|
||||
const { data } = await api.post<ApiResponse>('/configuracion/probar-conexion-sql', dto);
|
||||
return data;
|
||||
},
|
||||
|
||||
probarSMTP: async (dto: ProbarSMTPDto): Promise<ApiResponse> => {
|
||||
const { data } = await api.post<ApiResponse>('/configuracion/probar-smtp', dto);
|
||||
return data;
|
||||
},
|
||||
};
|
||||
|
||||
// ===== OPERACIONES =====
|
||||
export const operacionesApi = {
|
||||
ejecutarManual: async (dto: EjecucionManualDto): Promise<ApiResponse> => {
|
||||
const { data } = await api.post<ApiResponse>('/operaciones/ejecutar-manual', dto);
|
||||
return data;
|
||||
},
|
||||
|
||||
obtenerLogs: async (
|
||||
pageNumber: number = 1,
|
||||
pageSize: number = 20,
|
||||
tipo?: string
|
||||
): Promise<PagedResult<Evento>> => {
|
||||
const params: any = { pageNumber, pageSize };
|
||||
if (tipo) params.tipo = tipo;
|
||||
|
||||
const { data } = await api.get<PagedResult<Evento>>('/operaciones/logs', { params });
|
||||
return data;
|
||||
},
|
||||
|
||||
obtenerEstadisticas: async (): Promise<Estadisticas> => {
|
||||
const { data } = await api.get<Estadisticas>('/operaciones/estadisticas');
|
||||
return data;
|
||||
},
|
||||
|
||||
limpiarLogs: async (diasAntiguedad: number = 30): Promise<ApiResponse> => {
|
||||
const { data } = await api.delete<ApiResponse>('/operaciones/logs/limpiar', {
|
||||
params: { diasAntiguedad },
|
||||
});
|
||||
return data;
|
||||
},
|
||||
};
|
||||
|
||||
export const authApi = {
|
||||
logout: async (refreshToken: string) => {
|
||||
// No esperamos respuesta, es un "fire and forget" para el usuario
|
||||
return api.post('/auth/revoke', { token: refreshToken });
|
||||
}
|
||||
};
|
||||
|
||||
export default api;
|
||||
Reference in New Issue
Block a user