267ffc4ed9
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
48 lines
2.1 KiB
TypeScript
48 lines
2.1 KiB
TypeScript
import { API_BASE } from "@/lib/config"
|
|
import type {
|
|
Account, CreateAccountInput, Template, CreateTemplateInput,
|
|
Domain, CreateDomainInput, ChangesetResponse, ApplyRequest,
|
|
} from "./types"
|
|
|
|
async function req<T>(path: string, init?: RequestInit): Promise<T> {
|
|
const res = await fetch(`${API_BASE}${path}`, {
|
|
headers: { "Content-Type": "application/json" },
|
|
method: "GET",
|
|
...init,
|
|
})
|
|
if (!res.ok) {
|
|
let msg = `HTTP ${res.status}`
|
|
try { const b = await res.json(); if (b?.error) msg = String(b.error) } catch { /* ignore */ }
|
|
throw new Error(msg)
|
|
}
|
|
if (res.status === 204) return undefined as T
|
|
return (await res.json()) as T
|
|
}
|
|
|
|
export const api = {
|
|
listAccounts: () => req<Account[]>("/accounts"),
|
|
createAccount: (input: CreateAccountInput) =>
|
|
req<Account>("/accounts", { method: "POST", body: JSON.stringify(input) }),
|
|
deleteAccount: (id: string) => req<void>(`/accounts/${id}`, { method: "DELETE" }),
|
|
|
|
listTemplates: () => req<Template[]>("/templates"),
|
|
createTemplate: (input: CreateTemplateInput) =>
|
|
req<Template>("/templates", { method: "POST", body: JSON.stringify(input) }),
|
|
updateTemplate: (id: string, input: CreateTemplateInput) =>
|
|
req<Template>(`/templates/${id}`, { method: "PUT", body: JSON.stringify(input) }),
|
|
deleteTemplate: (id: string) => req<void>(`/templates/${id}`, { method: "DELETE" }),
|
|
|
|
listDomains: () => req<Domain[]>("/domains"),
|
|
createDomain: (input: CreateDomainInput) =>
|
|
req<Domain>("/domains", { method: "POST", body: JSON.stringify(input) }),
|
|
deleteDomain: (id: string) => req<void>(`/domains/${id}`, { method: "DELETE" }),
|
|
importZones: (accountId: string) =>
|
|
req<Domain[]>(`/accounts/${accountId}/import`, { method: "POST" }),
|
|
setDomainTemplate: (id: string, templateId: string | null) =>
|
|
req<Domain>(`/domains/${id}`, { method: "PATCH", body: JSON.stringify({ templateId }) }),
|
|
|
|
checkDomain: (id: string) => req<ChangesetResponse>(`/domains/${id}/check`),
|
|
applyDomain: (id: string, body: ApplyRequest) =>
|
|
req<ChangesetResponse>(`/domains/${id}/apply`, { method: "POST", body: JSON.stringify(body) }),
|
|
}
|