TypeScriptInjection & inputDifficulty 3/3
// lib/settings.ts
type Settings = Record<string, unknown>;
export function mergeDeep(target: Settings, source: Settings): Settings {
for (const key of Object.keys(source)) {
const value = source[key];
if (typeof value === "object" && value !== null) {
target[key] = mergeDeep((target[key] as Settings) ?? {}, value as Settings);
} else {
target[key] = value;
}
}
return target;
}
// app/api/settings/route.ts
import { getSession } from "@/lib/session";
import { loadSettings, saveSettings } from "@/lib/settings-store";
import { mergeDeep } from "@/lib/settings";
export async function PATCH(req: Request) {
const session = await getSession();
if (!session) {
return Response.json({ error: "Unauthorized" }, { status: 401 });
}
const patch = JSON.parse(await req.text());
const current = await loadSettings(session.userId);
const merged = mergeDeep(current, patch);
await saveSettings(session.userId, merged);
return Response.json(merged);
}