40 lines
1.3 KiB
TypeScript
40 lines
1.3 KiB
TypeScript
import type { Settings, SettingsPatch } from "@/lib/types";
|
|
|
|
const SECTIONS = [
|
|
"upstream",
|
|
"dns",
|
|
"blocking",
|
|
"cache",
|
|
"web",
|
|
"doh_server",
|
|
"dot_server",
|
|
"edns",
|
|
"logging",
|
|
"disk",
|
|
"blocklist_update",
|
|
] as const;
|
|
|
|
/**
|
|
* Minimal partial patch for PUT /api/settings: only fields whose edited value
|
|
* differs from the original, grouped by section. The derived `web.auth_enabled`
|
|
* is never emitted. A non-empty `password` passes through as `web.password`.
|
|
* Returns null when nothing changed and no password was given.
|
|
*/
|
|
export function buildSettingsPatch(original: Settings, edited: Settings, password?: string): SettingsPatch | null {
|
|
const patch: Record<string, Record<string, unknown>> = {};
|
|
for (const section of SECTIONS) {
|
|
const before = original[section] as Record<string, unknown>;
|
|
const after = edited[section] as Record<string, unknown>;
|
|
let changed: Record<string, unknown> | undefined;
|
|
for (const key of Object.keys(after)) {
|
|
if (section === "web" && key === "auth_enabled") continue;
|
|
if (before[key] !== after[key]) (changed ??= {})[key] = after[key];
|
|
}
|
|
if (changed !== undefined) patch[section] = changed;
|
|
}
|
|
if (password !== undefined && password !== "") {
|
|
patch["web"] = { ...patch["web"], password };
|
|
}
|
|
return Object.keys(patch).length === 0 ? null : (patch as SettingsPatch);
|
|
}
|