import { NextResponse } from "next/server"; import { amendRelease, amendVersionProblem, dateProblem, describeAmend, RELEASE_WORKSPACES, } from "yt-dlp-transcript-common/controller/cutRelease"; import { revalidateAfterReleaseCut } from "../../../sites/lib/revalidateAfterReleaseCut"; import { OpsInputError, oneOf, ops, opsFail, optBool, optString, reqString, } from "../_lib"; export const dynamic = "force-dynamic"; // POST { workspace: "editor" | "export", // release: "X.Y.Z" (the LATEST release, as you saw it), // version?: "patch" | "minor" | "major" | "X.Y.Z" (default: kept), // date?: "YYYY-MM-DD" (default: kept), // fold?: boolean (fold [Unreleased] into it), // uncut?: boolean (withdraw it back to [Unreleased] instead), // commit?: boolean (default false) } // // SYNCHRONOUS, like cut-release: one heading rewrite and at most one // path-limited commit. The writer is the one the Amend form on /changelog and // /sites and `archilyzer release amend` call (common/controller/cutRelease.ts, // amendRelease); every refusal is its sentence. Only the newest release can // be amended — `release` naming an older one is a 400. // // 200 { ok: true, message, result } — `result.changed` false when the file // already said this (nothing written, nothing committed). A refusal is a 400 // { ok: false, error, written }: `written` true means the changelog was // rewritten and only its commit failed. export async function POST(request: Request) { return ops( request, ["workspace", "release", "version", "date", "fold", "uncut", "commit"], async (body) => { const workspace = oneOf(body, "workspace", RELEASE_WORKSPACES); const release = reqString(body, "release"); const version = optString(body, "version"); if (version !== undefined) { const problem = amendVersionProblem(version); if (problem) throw new OpsInputError(problem); } const date = optString(body, "date"); const badDate = date === undefined ? null : dateProblem(date); if (badDate) throw new OpsInputError(badDate); const fold = optBool(body, "fold") ?? false; const uncut = optBool(body, "uncut") ?? false; if (uncut && (version !== undefined || date !== undefined || fold)) { throw new OpsInputError("A withdrawal takes no version, date or fold."); } const commit = optBool(body, "commit") ?? false; const result = await amendRelease({ workspace, release, version, date, fold, uncut, commit, }); if (result.ok) { if (result.changed) revalidateAfterReleaseCut(); return NextResponse.json({ ok: true, message: describeAmend(result), result }); } if (result.written) revalidateAfterReleaseCut(); return opsFail(result.error, 400, { written: result.written === true }); }, ); }