aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/lib/api.ts
blob: 239c5983dafa319d9e25d0f43abe3c947a009863 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import type { Service, Heartbeat, ServiceStats, PaginatedResponse } from './types';

const BASE = '/api';

export async function listServices(): Promise<Service[]> {
	const res = await fetch(`${BASE}/services`);
	if (!res.ok) throw new Error('falha ao listar serviços');
	return res.json();
}

export async function createService(data: { name: string; url: string }): Promise<Service> {
	const res = await fetch(`${BASE}/services`, {
		method: 'POST',
		headers: { 'Content-Type': 'application/json' },
		body: JSON.stringify(data)
	});
	if (!res.ok) {
		const err = await res.json().catch(() => ({}));
		throw new Error(err.error || 'falha ao criar serviço');
	}
	return res.json();
}

export async function deleteService(id: number): Promise<void> {
	const res = await fetch(`${BASE}/services/${id}`, { method: 'DELETE' });
	if (!res.ok) throw new Error('falha ao remover serviço');
}

export async function getHistory(id: number, page = 1, perPage = 50): Promise<PaginatedResponse<Heartbeat>> {
	const res = await fetch(`${BASE}/services/${id}/history?page=${page}&per_page=${perPage}`);
	if (!res.ok) throw new Error('falha ao obter histórico');
	return res.json();
}

export async function getServiceStats(id: number): Promise<ServiceStats> {
	const res = await fetch(`${BASE}/services/${id}/stats`);
	if (!res.ok) throw new Error('falha ao obter estatísticas');
	return res.json();
}

export async function listMaintenances(): Promise<Maintenance[]> {
	const res = await fetch(`${BASE}/maintenances`);
	if (!res.ok) throw new Error('falha ao listar manutenções');
	return res.json();
}

export async function createMaintenance(data: { title: string; start_time: string; end_time: string }): Promise<Maintenance> {
	const res = await fetch(`${BASE}/maintenances`, {
		method: 'POST',
		headers: { 'Content-Type': 'application/json' },
		body: JSON.stringify(data)
	});
	if (!res.ok) {
		const err = await res.json().catch(() => ({}));
		throw new Error(err.error || 'falha ao criar manutenção');
	}
	return res.json();
}

export async function updateMaintenance(id: number, data: { title: string; start_time: string; end_time: string; is_active: boolean }): Promise<Maintenance> {
	const res = await fetch(`${BASE}/maintenances/${id}`, {
		method: 'PUT',
		headers: { 'Content-Type': 'application/json' },
		body: JSON.stringify(data)
	});
	if (!res.ok) {
		const err = await res.json().catch(() => ({}));
		throw new Error(err.error || 'falha ao atualizar manutenção');
	}
	return res.json();
}

export async function deleteMaintenance(id: number): Promise<void> {
	const res = await fetch(`${BASE}/maintenances/${id}`, { method: 'DELETE' });
	if (!res.ok) throw new Error('falha ao remover manutenção');
}