feat(web): типизированный API-клиент, типы DTO, TanStack Query хуки

This commit is contained in:
2026-07-03 17:14:11 +07:00
parent 41242973e1
commit 6f82036e38
5 changed files with 212 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
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 = 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) }),
}