Files
ui-tracker/frontend/src/api/client.ts

48 lines
1.7 KiB
TypeScript
Raw Normal View History

2026-03-27 23:33:31 -05:00
import type { WatchedItem, Settings } from '../types';
const BASE = '/api';
async function req<T>(path: string, options?: RequestInit): Promise<T> {
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<T>;
}
export const getItems = () => req<WatchedItem[]>('/items');
export const addItem = (url: string, check_interval: number) =>
req<WatchedItem>('/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<WatchedItem>(`/items/${id}`, {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
});
export const deleteItem = (id: number) => req<void>(`/items/${id}`, { method: 'DELETE' });
export const pauseItem = (id: number) => req<WatchedItem>(`/items/${id}/pause`, { method: 'POST' });
export const resumeItem = (id: number) => req<WatchedItem>(`/items/${id}/resume`, { method: 'POST' });
export const resetItem = (id: number) => req<WatchedItem>(`/items/${id}/reset`, { method: 'POST' });
export const getSettings = () => req<Settings>('/settings');
export const updateSettings = (data: Partial<Settings>) =>
req<Settings>('/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' });