import type { WatchedItem, Settings } from '../types'; const BASE = '/api'; async function req(path: string, options?: RequestInit): Promise { const res = await fetch(`${BASE}${path}`, options); if (!res.ok) { const body = await res.json().catch(() => ({})) as { error?: string }; throw new Error(body.error || `Request failed: ${res.status}`); } if (res.status === 204) return undefined as T; return res.json() as Promise; } export const getItems = () => req('/items'); export const addItem = (url: string, check_interval: number) => req('/items', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ url, check_interval }), }); export const updateItem = (id: number, data: { check_interval?: number; is_active?: number }) => req(`/items/${id}`, { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data), }); export const deleteItem = (id: number) => req(`/items/${id}`, { method: 'DELETE' }); export const pauseItem = (id: number) => req(`/items/${id}/pause`, { method: 'POST' }); export const resumeItem = (id: number) => req(`/items/${id}/resume`, { method: 'POST' }); export const resetItem = (id: number) => req(`/items/${id}/reset`, { method: 'POST' }); export const getSettings = () => req('/settings'); export const updateSettings = (data: Partial) => req('/settings', { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(data), }); export const testTelegram = () => req<{ success: boolean; error?: string }>('/settings/test-telegram', { method: 'POST' });