// src/services/api.js // Vite automatically injects the variable defined in .env.* based on the current mode. // `import.meta.env.VITE_BASE_URL` is the single source of truth for the API root. const BASE_URL = import.meta.env.VITE_BASE_URL + '/todos'; // ----------------------------------------------------------------------------- // API helpers // ----------------------------------------------------------------------------- export const getTodos = async () => { const response = await fetch(BASE_URL); if (!response.ok) throw new Error('Failed to fetch todos'); return response.json(); }; export const createTodo = async (todo) => { const response = await fetch(BASE_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(todo), }); if (!response.ok) throw new Error('Failed to create todo'); return response.json(); }; export const updateTodo = async (id, todo) => { const response = await fetch(`${BASE_URL}/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(todo), }); if (!response.ok) throw new Error('Failed to update todo'); return response.json(); }; export const deleteTodo = async (id) => { const response = await fetch(`${BASE_URL}/${id}`, { method: 'DELETE', }); if (!response.ok) throw new Error('Failed to delete todo'); return true; }; export const getVersion = async () => { const VERSION_URL = BASE_URL.replace('/todos', '/version'); const response = await fetch(VERSION_URL); if (!response.ok) throw new Error('Failed to fetch version'); return response.json(); };