import { auth } from './auth' const BASE_URL = import.meta.env.PROD ? '/api' : 'http://localhost:8000/api' export const api = { get: async (endpoint) => { const headers = {} if (auth.token.value) { headers['Authorization'] = `Bearer ${auth.token.value}` } const response = await fetch(`${BASE_URL}${endpoint}`, { headers }) if (response.status === 401) auth.logout() return response.json() }, post: async (endpoint, data) => { const headers = { 'Content-Type': 'application/json' } if (auth.token.value) { headers['Authorization'] = `Bearer ${auth.token.value}` } const response = await fetch(`${BASE_URL}${endpoint}`, { method: 'POST', headers, body: JSON.stringify(data) }) if (response.status === 401) auth.logout() return response.json() }, put: async (endpoint, data) => { const headers = { 'Content-Type': 'application/json' } if (auth.token.value) { headers['Authorization'] = `Bearer ${auth.token.value}` } const response = await fetch(`${BASE_URL}${endpoint}`, { method: 'PUT', headers, body: JSON.stringify(data) }) if (response.status === 401) auth.logout() return response.json() }, delete: async (endpoint) => { const headers = {} if (auth.token.value) { headers['Authorization'] = `Bearer ${auth.token.value}` } const response = await fetch(`${BASE_URL}${endpoint}`, { method: 'DELETE', headers }) if (response.status === 401) auth.logout() return response.json() } }