Files
tsumugu/frontend/src/api.js
T
LarsandCursor 2558239bfd
Deploy Development / deploy (push) Successful in 23s
Stelle den ersten nutzbaren Ablauf zum Organisieren und Nachverfolgen bereit.
Anmeldung, Datenraeume, Akten, Vorgaenge, gemeinsame Notizen, Dateien, manuelle Paperless-Referenzen, Aufgaben und unabhaengige Fristen laufen damit in der Entwicklungsumgebung.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-10-16 22:09:08 +02:00

102 lines
2.7 KiB
JavaScript

let csrf = "";
let scope = null;
let epoch = 0;
let dirty = false;
export function sessionState() {
return { csrf, scope, epoch };
}
export function applySession(data) {
csrf = data.csrf_token || "";
scope = data.scope || null;
epoch = data.epoch || 0;
dirty = false;
return data;
}
export function clearSession() {
csrf = "";
scope = null;
epoch = 0;
dirty = false;
}
export function markDirty(value) {
dirty = value;
}
export function isDirty() {
return dirty;
}
function scopeHeaders() {
if (!scope) return {};
return { "X-Scope-Kind": scope.kind, "X-Scope-Id": scope.id };
}
export async function api(path, { method = "GET", body, form } = {}) {
const started = epoch;
const headers = { ...scopeHeaders() };
if (csrf && method !== "GET" && method !== "HEAD") {
headers["X-CSRF-Token"] = csrf;
}
let payload;
if (form) {
payload = form;
} else if (body !== undefined) {
headers["Content-Type"] = "application/json";
payload = JSON.stringify(body);
}
const response = await fetch(path, { method, headers, body: payload, credentials: "include" });
if (started !== epoch) {
const error = new Error("Der Datenraum wurde gewechselt.");
error.status = 0;
throw error;
}
if (response.status === 204) return null;
const text = await response.text();
const data = text ? JSON.parse(text) : null;
if (!response.ok) {
const error = new Error(data?.detail || "Die Anfrage ist fehlgeschlagen.");
error.status = response.status;
error.code = data?.code;
if (response.status === 401) clearSession();
throw error;
}
return data;
}
export async function downloadFile(elementId, context, filename) {
const params = new URLSearchParams();
if (context?.kind && context?.id) {
params.set("context_kind", context.kind);
params.set("context_id", context.id);
}
const response = await fetch(`/api/v1/files/${elementId}/content?${params}`, {
credentials: "include",
headers: scopeHeaders(),
});
if (!response.ok) {
throw new Error("Die Datei ist nicht verfügbar.");
}
const blob = await response.blob();
const url = URL.createObjectURL(blob);
const link = document.createElement("a");
link.href = url;
link.download = filename || "datei";
document.body.appendChild(link);
link.click();
link.remove();
URL.revokeObjectURL(url);
}
export function textToDocument(text) {
const blocks = text.split(/\n/).map((line) => ({ type: "paragraph", text: line }));
return { version: 1, blocks: blocks.length ? blocks : [{ type: "paragraph", text: "" }] };
}
export function documentToText(document) {
return (document?.blocks || []).map((block) => block.text || "").join("\n");
}