// THE DELETE ROUTES' ONE ANSWER (release 27 follow-up): an article, an // iteration, a project and a take are each deleted by a plan and then the // delete — common's lib/report/delete.ts for the first two, lib/report/ // delete.mjs for the others — and answered the same way, so one page component // (components/DeletePlan.tsx) shows every plan and asks before it acts. // // 200 { ok, applied: false, plan } a dry run // 200 { ok, applied: true, plan, removed, failed } done // 409 { error, plan } refused: nothing removed // 400/404 { error } no such thing // // Types only, and the answer: no node: import, so a client component can take // the types. export type DeletePlanEntry = { path: string; label: string; what: string; kind: "dir" | "file" | "link" | "absent"; bytes: number; files: number; parts?: { name: string; bytes: number }[]; }; export type DeletePlanView = { refusals: { reason: string; forceable: boolean }[]; steps: string[]; remove: DeletePlanEntry[]; kept: { what: string; path?: string; why: string }[]; bytes: number; files: number; }; export type DeleteAnswer = | { ok: boolean; applied: false; plan: DeletePlanView } | { ok: boolean; applied: true; plan: DeletePlanView; removed: string[]; failed: { path: string; error: string }[] }; export const blockedBy = (plan: Pick, force: boolean) => plan.refusals.filter((r) => !force || !r.forceable); /** Answer a plan: a refusal is a 409 carrying it; a dry run answers it; else `apply` runs. */ export async function answerDelete( plan: DeletePlanView, { dryRun, force }: { dryRun: boolean; force: boolean }, apply: () => Promise<{ plan: DeletePlanView; removed: string[]; failed: { path: string; error: string }[] }>, ): Promise { const blocked = blockedBy(plan, force); const headers = { "cache-control": "no-store" }; if (blocked.length) return Response.json({ error: `refused: ${blocked.map((r) => r.reason).join("; ")}`, plan }, { status: 409, headers }); if (dryRun) return Response.json({ ok: true, applied: false, plan }, { headers }); const r = await apply(); return Response.json({ ok: r.failed.length === 0, applied: true, plan: r.plan, removed: r.removed, failed: r.failed }, { headers }); }