feat: add config preset manager
This commit is contained in:
@@ -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')) {
|
||||
|
||||
43
src/lib/api/server/presets.ts
Normal file
43
src/lib/api/server/presets.ts
Normal 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 ?? [];
|
||||
}
|
||||
Reference in New Issue
Block a user