Files
rk-frontend/src/actions/visualisierung/api-library.ts
T

31 lines
2.4 KiB
TypeScript

'use server';
import { revalidatePath } from 'next/cache';
import type { GraphicLibrary, LibraryGraphic } from '@/types/visualisierung/library';
import type { GrafikGeneratorDaten } from '@/types/visualisierung/graphic';
const url = 'http://localhost:3000/visualisierung/grafik-bibliothek';
async function request<T>(path = '', init?: RequestInit): Promise<T> {
const response = await fetch(url + path, { cache: 'no-store', ...init });
const result = await response.json();
if (!response.ok) throw new Error(Array.isArray(result.message) ? result.message.join(', ') : result.message ?? 'Grafikbibliothek nicht verfügbar');
return result;
}
export async function api_graphicLibrary() { return request<GraphicLibrary>(); }
export async function api_updateLibraryGraphic(id: string, input: { anlageId: string; version: number; generator: GrafikGeneratorDaten; variant: string }) {
const body = JSON.stringify({ ...input, generator: { grafikTyp: input.generator.grafikTyp, config: input.generator.config, options: input.generator.options } });
if (new TextEncoder().encode(body).length > 9 * 1024 * 1024) throw new Error('Grafikparameter sind zu groß');
const asset = await request<LibraryGraphic>('/' + encodeURIComponent(id), { method: 'PUT', headers: { 'Content-Type': 'application/json' }, body });
revalidatePath('/', 'layout'); return asset;
}
export async function api_createLibraryGraphic(input: { anlageId: string; name: string; generator: GrafikGeneratorDaten; variant: string }) {
const body = JSON.stringify({ ...input, generator: { grafikTyp: input.generator.grafikTyp, config: input.generator.config, options: input.generator.options } });
if (new TextEncoder().encode(body).length > 9 * 1024 * 1024) throw new Error('Grafikparameter sind zu groß');
const asset = await request<LibraryGraphic>('', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body });
revalidatePath('/visualisierung/bibliothek'); return asset;
}
export async function api_deleteLibraryGraphic(id: string, anlageId: string) {
await request('/' + encodeURIComponent(id), { method: 'DELETE', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ anlageId }) });
revalidatePath('/visualisierung/bibliothek');
}
export async function api_exportLibrary(anlageId: string) { return request<{ count: number; directory: string }>('/xaml', {
method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ anlageId }) }); }