import { Channel, Playlist, Video, Config, SchedulerStatus } from '../types'; const API_BASE = '/api'; async function fetchJSON(url: string, options?: RequestInit): Promise { const response = await fetch(url, { ...options, headers: { 'Content-Type': 'application/json', ...options?.headers, }, }); if (!response.ok) { const error = await response.json().catch(() => ({ error: 'Request failed' })); throw new Error(error.error || `HTTP ${response.status}`); } if (response.status === 204) { return {} as T; } return response.json(); } // Channels API export const channelsAPI = { getAll: () => fetchJSON(`${API_BASE}/channels`), getById: (id: number) => fetchJSON(`${API_BASE}/channels/${id}`), getVideos: (id: number) => fetchJSON(`${API_BASE}/channels/${id}/videos`), create: (url: string) => fetchJSON(`${API_BASE}/channels`, { method: 'POST', body: JSON.stringify({ url }), }), update: (id: number, active: boolean) => fetchJSON(`${API_BASE}/channels/${id}`, { method: 'PATCH', body: JSON.stringify({ active }), }), delete: (id: number) => fetchJSON(`${API_BASE}/channels/${id}`, { method: 'DELETE', }), refresh: (id: number) => fetchJSON<{ message: string }>(`${API_BASE}/channels/${id}/refresh`, { method: 'POST', }), }; // Playlists API export const playlistsAPI = { getAll: () => fetchJSON(`${API_BASE}/playlists`), getById: (id: number) => fetchJSON(`${API_BASE}/playlists/${id}`), getVideos: (id: number) => fetchJSON(`${API_BASE}/playlists/${id}/videos`), create: (url: string) => fetchJSON(`${API_BASE}/playlists`, { method: 'POST', body: JSON.stringify({ url }), }), update: (id: number, active: boolean) => fetchJSON(`${API_BASE}/playlists/${id}`, { method: 'PATCH', body: JSON.stringify({ active }), }), delete: (id: number) => fetchJSON(`${API_BASE}/playlists/${id}`, { method: 'DELETE', }), refresh: (id: number) => fetchJSON<{ message: string }>(`${API_BASE}/playlists/${id}/refresh`, { method: 'POST', }), }; // Videos API export const videosAPI = { getAll: (status?: string, search?: string) => { const params = new URLSearchParams(); if (status) params.append('status', status); if (search) params.append('search', search); const queryString = params.toString(); const url = queryString ? `${API_BASE}/videos?${queryString}` : `${API_BASE}/videos`; return fetchJSON(url); }, getById: (id: number) => fetchJSON