import { Todo } from '../types'; const API_URL = '/todos'; export const fetchTodos = async (): Promise => { const res = await fetch(API_URL); if (!res.ok) { throw new Error('Failed to fetch todos'); } return res.json(); }; export const createTodo = async (data: { title: string; completed?: boolean }): Promise => { const res = await fetch(API_URL, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data), }); if (!res.ok) { throw new Error('Failed to create todo'); } return res.json(); }; export const updateTodo = async (id: number, data: Partial): Promise => { const res = await fetch(`${API_URL}/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data), }); if (!res.ok) { throw new Error('Failed to update todo'); } return res.json(); }; export const deleteTodo = async (id: number): Promise => { const res = await fetch(`${API_URL}/${id}`, { method: 'DELETE', }); if (!res.ok) { throw new Error('Failed to delete todo'); } };