import type { Status, HealthResponse, StatusResponse, NetworkInfo, DnsmasqStatus, DnsmasqConfigResponse } from '../types'; import { getApiBaseUrl } from './api/config'; const API_BASE = getApiBaseUrl(); class ApiService { private baseUrl: string; constructor(baseUrl: string = API_BASE) { this.baseUrl = baseUrl; } private async request(endpoint: string, options?: RequestInit): Promise { const url = `${this.baseUrl}${endpoint}`; const response = await fetch(url, options); if (!response.ok) { throw new Error(`HTTP error! status: ${response.status}`); } return response.json(); } async getStatus(): Promise { return this.request('/api/status'); } async getHealth(): Promise { return this.request('/api/v1/health'); } async getDnsmasqStatus(): Promise { return this.request('/api/v1/dnsmasq/status'); } async getDnsmasqConfig(): Promise { return this.request('/api/v1/dnsmasq/config'); } async writeDnsmasqConfig(content: string): Promise { return this.request('/api/v1/dnsmasq/config', { method: 'PUT', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ content }), }); } async generateDnsmasqConfig(overwrite: boolean = false): Promise { const url = overwrite ? '/api/v1/dnsmasq/generate?overwrite=true' : '/api/v1/dnsmasq/generate'; return this.request(url, { method: 'POST' }); } async restartDnsmasq(): Promise { return this.request('/api/v1/dnsmasq/restart', { method: 'POST' }); } async getNetworkInfo(): Promise { return this.request('/api/v1/network/info'); } async downloadPXEAssets(): Promise { return this.request('/api/v1/assets', { method: 'POST' }); } } export const apiService = new ApiService(); export default ApiService;