48 lines
1.7 KiB
TypeScript
48 lines
1.7 KiB
TypeScript
|
|
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' });
|