build 1
This commit is contained in:
47
frontend/src/api/client.ts
Normal file
47
frontend/src/api/client.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
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' });
|
||||
Reference in New Issue
Block a user