import { NextResponse } from "next/server"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { ArticleRegistryError, configureArticle, isReportAudience, type ArticleConfigChange, } from "yt-dlp-transcript-common/lib/report/registry"; import { REPORT_AUDIENCES } from "yt-dlp-transcript-common/lib/report/schema"; import { OpsInputError, ops, optBool, optNonNegativeInt, reqString } from "../_lib"; export const dynamic = "force-dynamic"; // POST { // id: string, // the article: transcripts/reports// // audience?: "public" | "private", // report.json `audience` // published?: boolean, // in reports/index.json, or a draft // position?: number, // its place in the published list, 0 = first // } // — at least one of the three (release 26 A2). The article store's one writer // over HTTP: THROUGH configureArticle (lib/report/registry.ts), so locked, // validated and atomic, as a generator calling the registry is. Publishing // with no position keeps a published article where it is, else puts it last; // a position alone moves a published article. // // Every refusal is said before anything is written: an unknown key (ops()), // an id that is not one, an article that is not there, an audience that is // not one, a position out of range or on a draft, publishing an article whose // report.json has problems (each listed). The answer is the article as saved. export async function POST(request: Request) { return ops(request, ["id", "audience", "published", "position"], async (body) => { const id = reqString(body, "id"); const audience = body.audience; if (audience !== undefined && !isReportAudience(audience)) { throw new OpsInputError(`"audience" must be one of ${REPORT_AUDIENCES.join(", ")}`); } const published = optBool(body, "published"); const position = optNonNegativeInt(body, "position"); if (audience === undefined && published === undefined && position === undefined) { throw new OpsInputError('nothing to change — give "audience", "published" or "position"'); } const change: ArticleConfigChange = { ...(audience !== undefined ? { audience } : {}), ...(published !== undefined ? { published } : {}), ...(position !== undefined ? { position } : {}), }; let saved; try { saved = await configureArticle(id, change, getPaths()); } catch (err) { if (err instanceof ArticleRegistryError) throw new OpsInputError(err.message); throw err; } return NextResponse.json({ ok: true, id: saved.id, audience: saved.audience, published: saved.published, position: saved.position, }); }); }