import axios from 'axios'; const API_URL = process.env.NEXT_PUBLIC_API_URL || 'http://localhost:3002'; export const api = axios.create({ baseURL: `${API_URL}/api`, headers: { 'Content-Type': 'application/json', }, }); // Add auth token to requests api.interceptors.request.use((config) => { const token = localStorage.getItem('token'); if (token) { config.headers.Authorization = `Bearer ${token}`; } return config; }); // Handle auth errors api.interceptors.response.use( (response) => response, (error) => { if (error.response?.status === 401) { localStorage.removeItem('token'); localStorage.removeItem('user'); window.location.href = '/login'; } return Promise.reject(error); } ); // Auth API export const authAPI = { register: async (email: string, password: string) => { const response = await api.post('/auth/register', { email, password }); return response.data; }, login: async (email: string, password: string) => { const response = await api.post('/auth/login', { email, password }); return response.data; }, }; // Monitor API export const monitorAPI = { list: async () => { const response = await api.get('/monitors'); return response.data; }, get: async (id: string) => { const response = await api.get(`/monitors/${id}`); return response.data; }, create: async (data: any) => { const response = await api.post('/monitors', data); return response.data; }, update: async (id: string, data: any) => { const response = await api.put(`/monitors/${id}`, data); return response.data; }, delete: async (id: string) => { const response = await api.delete(`/monitors/${id}`); return response.data; }, check: async (id: string) => { const response = await api.post(`/monitors/${id}/check`); return response.data; }, history: async (id: string, limit = 50) => { const response = await api.get(`/monitors/${id}/history`, { params: { limit }, }); return response.data; }, snapshot: async (id: string, snapshotId: string) => { const response = await api.get(`/monitors/${id}/history/${snapshotId}`); return response.data; }, };