feat(web): AuthContext + клиент под cookie-сессии, projectId из контекста
This commit is contained in:
+60
-27
@@ -1,15 +1,25 @@
|
||||
import { API_BASE } from "@/lib/config"
|
||||
import { API_ROOT } from "@/lib/config"
|
||||
import type {
|
||||
AuthState,
|
||||
Account, CreateAccountInput, Template, CreateTemplateInput,
|
||||
Domain, CreateDomainInput, ChangesetResponse, ApplyRequest,
|
||||
} from "./types"
|
||||
|
||||
export class UnauthorizedError extends Error {
|
||||
constructor() {
|
||||
super("Unauthorized")
|
||||
this.name = "UnauthorizedError"
|
||||
}
|
||||
}
|
||||
|
||||
async function req<T>(path: string, init?: RequestInit): Promise<T> {
|
||||
const res = await fetch(`${API_BASE}${path}`, {
|
||||
const res = await fetch(path, {
|
||||
headers: { "Content-Type": "application/json" },
|
||||
method: "GET",
|
||||
credentials: "include",
|
||||
...init,
|
||||
})
|
||||
if (res.status === 401) throw new UnauthorizedError()
|
||||
if (!res.ok) {
|
||||
let msg = `HTTP ${res.status}`
|
||||
try { const b = await res.json(); if (b?.error) msg = String(b.error) } catch { /* ignore */ }
|
||||
@@ -19,29 +29,52 @@ async function req<T>(path: string, init?: RequestInit): Promise<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) }),
|
||||
function projectPath(projectId: string, path: string): string {
|
||||
return `${API_ROOT}/projects/${projectId}${path}`
|
||||
}
|
||||
|
||||
export const api = {
|
||||
auth: {
|
||||
register: (email: string, password: string) =>
|
||||
req<AuthState>(`${API_ROOT}/auth/register`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ email, password }),
|
||||
}),
|
||||
login: (email: string, password: string) =>
|
||||
req<AuthState>(`${API_ROOT}/auth/login`, {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ email, password }),
|
||||
}),
|
||||
logout: () => req<void>(`${API_ROOT}/auth/logout`, { method: "POST" }),
|
||||
me: () => req<AuthState>(`${API_ROOT}/auth/me`),
|
||||
},
|
||||
|
||||
listAccounts: (projectId: string) => req<Account[]>(projectPath(projectId, "/accounts")),
|
||||
createAccount: (projectId: string, input: CreateAccountInput) =>
|
||||
req<Account>(projectPath(projectId, "/accounts"), { method: "POST", body: JSON.stringify(input) }),
|
||||
deleteAccount: (projectId: string, id: string) =>
|
||||
req<void>(projectPath(projectId, `/accounts/${id}`), { method: "DELETE" }),
|
||||
|
||||
listTemplates: (projectId: string) => req<Template[]>(projectPath(projectId, "/templates")),
|
||||
createTemplate: (projectId: string, input: CreateTemplateInput) =>
|
||||
req<Template>(projectPath(projectId, "/templates"), { method: "POST", body: JSON.stringify(input) }),
|
||||
updateTemplate: (projectId: string, id: string, input: CreateTemplateInput) =>
|
||||
req<Template>(projectPath(projectId, `/templates/${id}`), { method: "PUT", body: JSON.stringify(input) }),
|
||||
deleteTemplate: (projectId: string, id: string) =>
|
||||
req<void>(projectPath(projectId, `/templates/${id}`), { method: "DELETE" }),
|
||||
|
||||
listDomains: (projectId: string) => req<Domain[]>(projectPath(projectId, "/domains")),
|
||||
createDomain: (projectId: string, input: CreateDomainInput) =>
|
||||
req<Domain>(projectPath(projectId, "/domains"), { method: "POST", body: JSON.stringify(input) }),
|
||||
deleteDomain: (projectId: string, id: string) =>
|
||||
req<void>(projectPath(projectId, `/domains/${id}`), { method: "DELETE" }),
|
||||
importZones: (projectId: string, accountId: string) =>
|
||||
req<Domain[]>(projectPath(projectId, `/accounts/${accountId}/import`), { method: "POST" }),
|
||||
setDomainTemplate: (projectId: string, id: string, templateId: string | null) =>
|
||||
req<Domain>(projectPath(projectId, `/domains/${id}`), { method: "PATCH", body: JSON.stringify({ templateId }) }),
|
||||
|
||||
checkDomain: (projectId: string, id: string) =>
|
||||
req<ChangesetResponse>(projectPath(projectId, `/domains/${id}/check`)),
|
||||
applyDomain: (projectId: string, id: string, body: ApplyRequest) =>
|
||||
req<ChangesetResponse>(projectPath(projectId, `/domains/${id}/apply`), { method: "POST", body: JSON.stringify(body) }),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user