import axios, { AxiosError } from 'axios'; import type { Project, Template } from '../store/useStore'; const apiClient = axios.create({ baseURL: 'http://192.168.3.11:8000', headers: { 'Content-Type': 'application/json', }, timeout: 30000, }); // Request interceptor: attach token apiClient.interceptors.request.use( (config) => { const token = localStorage.getItem('token'); if (token) { config.headers.Authorization = `Bearer ${token}`; } return config; }, (error) => Promise.reject(error) ); // Response interceptor: handle errors apiClient.interceptors.response.use( (response) => response, (error: AxiosError) => { if (error.response?.status === 401) { localStorage.removeItem('token'); window.location.reload(); } return Promise.reject(error); } ); // Auth export async function login(username: string, password: string): Promise<{ token: string }> { const response = await apiClient.post('/api/auth/login', { username, password }); return response.data; } // Projects export async function getProjects(): Promise { const response = await apiClient.get('/api/projects'); return response.data; } export async function createProject(payload: { name: string; description?: string; }): Promise { const response = await apiClient.post('/api/projects', payload); return response.data; } export async function updateProject(id: string, payload: Partial): Promise { const response = await apiClient.put(`/api/projects/${id}`, payload); return response.data; } export async function deleteProject(id: string): Promise { await apiClient.delete(`/api/projects/${id}`); } // Templates export async function getTemplates(): Promise { const response = await apiClient.get('/api/templates'); return response.data; } export async function createTemplate(payload: { name: string; description?: string; classes?: { name: string; color: string; zIndex: number; category?: string }[]; }): Promise