Guilherme Silberfarb Costa
Rebuild app with React, FastAPI and persistent JSON storage
ce583c4
Raw History Blame Contribute Delete
2.04 kB
import type { AppDocument, Space, StateResponse, Tpu } from "../types";
async function request(path: string, init?: RequestInit): Promise<StateResponse> {
const response = await fetch(path, {
...init,
headers: { "Content-Type": "application/json", ...(init?.headers || {}) },
});
const body = await response.json().catch(() => ({}));
if (!response.ok) {
const detail = typeof body.detail === "string" ? body.detail : body.detail?.message;
throw new Error(detail || "Não foi possível concluir a operação.");
}
return body;
}
export const api = {
state: () => request("/api/state"),
updateSpace: (revision: number, id: string, changes: Partial<Space>) =>
request(`/api/spaces/${encodeURIComponent(id)}`, { method: "PATCH", body: JSON.stringify({ expected_revision: revision, changes }) }),
updateSpaces: (revision: number, updates: Array<{ id: string; changes: Partial<Space> }>) =>
request("/api/spaces", { method: "PATCH", body: JSON.stringify({ expected_revision: revision, updates }) }),
createSpace: (revision: number, space: Space) =>
request("/api/spaces", { method: "POST", body: JSON.stringify({ expected_revision: revision, space }) }),
deleteSpace: (revision: number, id: string) =>
request(`/api/spaces/${encodeURIComponent(id)}?expected_revision=${revision}`, { method: "DELETE" }),
updateTpu: (revision: number, id: string, changes: Partial<Tpu>) =>
request(`/api/tpus/${encodeURIComponent(id)}`, { method: "PATCH", body: JSON.stringify({ expected_revision: revision, changes }) }),
createTpu: (revision: number, tpu: Tpu) =>
request("/api/tpus", { method: "POST", body: JSON.stringify({ expected_revision: revision, tpu }) }),
deleteTpu: (revision: number, id: string) =>
request(`/api/tpus/${encodeURIComponent(id)}?expected_revision=${revision}`, { method: "DELETE" }),
importState: (revision: number, document: AppDocument) =>
request("/api/import", { method: "POST", body: JSON.stringify({ expected_revision: revision, document }) }),
};