43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
import type Database from "bun:sqlite";
|
|
|
|
import type { RuntimeMode, SettingsData } from "../../shared/api";
|
|
import type { Logger } from "../logger";
|
|
|
|
import { getSettings, updateSettings } from "../db/settings";
|
|
import { createApiError, jsonResponse } from "../helpers";
|
|
|
|
export function handleGetSettings(_req: Request, db: Database, mode: RuntimeMode, _logger: Logger): Response {
|
|
const data = getSettings(db);
|
|
return jsonResponse(data, { mode });
|
|
}
|
|
|
|
export async function handleUpdateSettings(
|
|
req: Request,
|
|
db: Database,
|
|
mode: RuntimeMode,
|
|
logger: Logger,
|
|
): Promise<Response> {
|
|
let body: Partial<SettingsData>;
|
|
try {
|
|
body = (await req.json()) as Partial<SettingsData>;
|
|
} catch {
|
|
return jsonResponse(createApiError("Invalid JSON body", 400), { mode, status: 400 });
|
|
}
|
|
|
|
if (body.theme !== undefined && typeof body.theme !== "string") {
|
|
return jsonResponse(createApiError("theme must be a string", 400), { mode, status: 400 });
|
|
}
|
|
|
|
if (body.theme !== undefined && body.theme !== "dark" && body.theme !== "light" && body.theme !== "system") {
|
|
return jsonResponse(createApiError("theme 仅支持 dark、light、system", 400), { mode, status: 400 });
|
|
}
|
|
|
|
if (body.compact !== undefined && typeof body.compact !== "boolean") {
|
|
return jsonResponse(createApiError("compact 必须为布尔值", 400), { mode, status: 400 });
|
|
}
|
|
|
|
const result = updateSettings(db, body, logger);
|
|
logger.info({ data: result }, "设置已更新");
|
|
return jsonResponse(result, { mode });
|
|
}
|