feat: add config preset manager

This commit is contained in:
2026-03-26 22:09:01 +01:00
parent 1dad80e557
commit e8cb877497
11 changed files with 587 additions and 29 deletions

View File

@@ -30,7 +30,14 @@ export async function fetchServerAPI<T>(
if (response.status == 401) {
redirect('/login?expired=true');
}
throw new Error(`API Error: ${response.statusText} - ${method} - ${BASE_URL}${endpoint}`);
let message = response.statusText;
try {
const body = await response.json();
message = body?.error ?? body?.message ?? message;
} catch {
// non-JSON error body, keep statusText
}
throw new Error(message);
}
if (response.headers.get('Content-Type')?.includes('application/json')) {

View File

@@ -0,0 +1,43 @@
import { fetchServerAPI } from './base';
import { configPresetSchema, type ConfigPreset } from '@/lib/schemas/preset';
import type { ConfigAuditRecord } from './configuration';
const route = '/presets';
export type ConfigPresetCreateRequest = {
name: string;
description?: string;
configuration?: Record<string, unknown> | null;
assistRules?: Record<string, unknown> | null;
event?: Record<string, unknown> | null;
eventRules?: Record<string, unknown> | null;
settings?: Record<string, unknown> | null;
};
export async function createPreset(
token: string,
data: ConfigPresetCreateRequest
): Promise<ConfigPreset> {
const response = await fetchServerAPI<ConfigPreset>(route, token, 'POST', data);
return configPresetSchema.parse(response.data);
}
export async function listPresets(token: string, name?: string): Promise<ConfigPreset[]> {
const query = name ? `?name=${encodeURIComponent(name)}` : '';
const response = await fetchServerAPI<ConfigPreset[]>(`${route}${query}`, token);
return configPresetSchema.array().parse(response.data);
}
export async function applyPreset(
token: string,
serverId: string,
presetId: string,
restart = false
): Promise<ConfigAuditRecord[]> {
const response = await fetchServerAPI<ConfigAuditRecord[]>(
`/server/${serverId}/config/presets/${presetId}/apply?restart=${restart}`,
token,
'POST'
);
return response.data ?? [];
}