50 lines
1.8 KiB
TypeScript
50 lines
1.8 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { revalidatePath } from 'next/cache';
|
|
import { setSection, resetSection, getSection } from '@/lib/db/store';
|
|
import { isEditableKey } from '@/lib/content/sections';
|
|
|
|
export const runtime = 'nodejs';
|
|
|
|
// GET ?key=hero -> current stored override (or null)
|
|
export async function GET(req: Request) {
|
|
const key = new URL(req.url).searchParams.get('key') ?? '';
|
|
if (!isEditableKey(key)) {
|
|
return NextResponse.json({ error: 'unknown section' }, { status: 400 });
|
|
}
|
|
return NextResponse.json({ key, override: getSection(key) });
|
|
}
|
|
|
|
// POST { key, data: { fa, en } } -> save override
|
|
export async function POST(req: Request) {
|
|
let body: { key?: string; data?: { fa?: unknown; en?: unknown } };
|
|
try {
|
|
body = await req.json();
|
|
} catch {
|
|
return NextResponse.json({ error: 'bad json' }, { status: 400 });
|
|
}
|
|
|
|
const key = body.key ?? '';
|
|
if (!isEditableKey(key)) {
|
|
return NextResponse.json({ error: 'unknown section' }, { status: 400 });
|
|
}
|
|
if (!body.data || typeof body.data !== 'object' || !('fa' in body.data) || !('en' in body.data)) {
|
|
return NextResponse.json({ error: 'expected { fa, en } payload' }, { status: 400 });
|
|
}
|
|
|
|
setSection(key, { fa: body.data.fa, en: body.data.en });
|
|
// (site) layout is force-dynamic, but revalidate keeps any cached routes fresh.
|
|
revalidatePath('/', 'layout');
|
|
return NextResponse.json({ ok: true });
|
|
}
|
|
|
|
// DELETE ?key=hero -> revert to in-code default
|
|
export async function DELETE(req: Request) {
|
|
const key = new URL(req.url).searchParams.get('key') ?? '';
|
|
if (!isEditableKey(key)) {
|
|
return NextResponse.json({ error: 'unknown section' }, { status: 400 });
|
|
}
|
|
resetSection(key);
|
|
revalidatePath('/', 'layout');
|
|
return NextResponse.json({ ok: true });
|
|
}
|