(path: string, options: globalThis.RequestInit = {})
| 63 | } |
| 64 | |
| 65 | async function apiRequest<T>(path: string, options: globalThis.RequestInit = {}): Promise<T> { |
| 66 | const response = await fetch(`${API_BASE}${path}`, { |
| 67 | ...options, |
| 68 | credentials: 'include', |
| 69 | headers: { |
| 70 | 'Content-Type': 'application/json', |
| 71 | ...options.headers, |
| 72 | }, |
| 73 | } as globalThis.RequestInit); |
| 74 | |
| 75 | if (response.status === 401) { |
| 76 | throw new AuthError('Unauthorized - please log in via Cloudflare Access'); |
| 77 | } |
| 78 | |
| 79 | const data = (await response.json()) as T & { error?: string }; |
| 80 | |
| 81 | if (!response.ok) { |
| 82 | throw new Error(data.error || `API error: ${response.status}`); |
| 83 | } |
| 84 | |
| 85 | return data; |
| 86 | } |
| 87 | |
| 88 | export async function listDevices(): Promise<DeviceListResponse> { |
| 89 | return apiRequest<DeviceListResponse>('/devices'); |
no outgoing calls
no test coverage detected