File
Blob: ui/operator/dns-api.ts
| 1 | import { RequestError, requestJSON } from '../shared/http'; |
| 2 | import type { DNSConfigSnapshot } from './types'; |
| 3 | |
| 4 | export async function requestDNSConfig(signal: AbortSignal, body?: string, after?: string): Promise<DNSConfigSnapshot> { |
| 5 | const data = await requestJSON<DNSConfigSnapshot>(`/_internal/api/config/dns${after ? `?after=${encodeURIComponent(after)}` : ''}`, { |
| 6 | signal, body, method: body === undefined ? 'GET' : 'PUT', timeout: body === undefined ? 12_000 : 15_000, |
| 7 | }); |
| 8 | const validConfig = data?.config === null || (data?.config && typeof data.config.zone === 'string' |
| 9 | && Array.isArray(data.config.nameservers) |
| 10 | && data.config.nameservers.every(ns => ns && typeof ns.host === 'string' |
| 11 | && Array.isArray(ns.addresses) && ns.addresses.every(address => typeof address === 'string'))); |
| 12 | if (!data || typeof data.version !== 'string' || typeof data.server?.address !== 'string' |
| 13 | || typeof data.observed_at !== 'string' || !validConfig) { |
| 14 | throw new RequestError('Unexpected server response.', body === undefined ? 'request_failed' : 'outcome_unknown'); |
| 15 | } |
| 16 | return data; |
| 17 | } |