32 lines
956 B
TypeScript
32 lines
956 B
TypeScript
import { requireDm } from "@/lib/session";
|
|
import { json, err, asResponse } from "@/lib/api";
|
|
import { applyPlan, parseDonjon } from "@/lib/import";
|
|
import type { KindId } from "@/lib/kinds";
|
|
|
|
export const dynamic = "force-dynamic";
|
|
|
|
export async function POST(req: Request) {
|
|
try {
|
|
await requireDm();
|
|
const url = new URL(req.url);
|
|
const action = url.searchParams.get("action") ?? "preview"; // preview | apply
|
|
const b = (await req.json()) as Partial<{
|
|
input: string; kind: KindId; title: string; isPublic: boolean;
|
|
}>;
|
|
if (!b.input || typeof b.input !== "string")
|
|
return err("input (string) required");
|
|
const plan = parseDonjon(b.input, {
|
|
kind: b.kind,
|
|
title: b.title,
|
|
isPublic: b.isPublic,
|
|
});
|
|
if (action === "apply") {
|
|
const result = await applyPlan(plan, {});
|
|
return json({ plan, result });
|
|
}
|
|
return json({ plan });
|
|
} catch (e) {
|
|
return asResponse(e);
|
|
}
|
|
}
|