import { existsSync } from "node:fs"; import { NextResponse } from "next/server"; import { getPaths } from "yt-dlp-transcript-common/lib/paths"; import { isValidSiteId, patchSite, siteConfigFile, sitePublishPolicy, type Site, } from "yt-dlp-transcript-common/lib/site"; import { PUBLISH_POLICIES, type PublishPolicy } from "yt-dlp-transcript-common/lib/settingsSchema"; import { channelExists, isValidChannelSlug } from "yt-dlp-transcript-common/controller/channels"; import { OpsInputError, ops, reqString } from "../_lib"; export const dynamic = "force-dynamic"; // POST { // siteId: string, // citedChannels?: string[], // the WHOLE cited scope; [] clears it // publishAuto?: "off" | "build" | "preview" | "production", // } // — at least one of the two. Only these keys for now (release 24 H6): a // session sets a site's cited-only channels, or its publish policy, without // the editor UI. // // THROUGH patchSite, so through writeSite: the same validation and the same // on-disk shape the site form's save gets. A slug the pool already holds is // dropped from `citedChannels`, as the parser drops it; a deploying policy // (`preview`, `production`) on a site with no Pages project is refused; a // private site's policy is clamped to `build`. The answer is what was saved. // // Every refusal is said before anything is written: an unknown key (ops()), // an unknown site, a slug that is not a channel, a policy that is not one. export async function POST(request: Request) { return ops(request, ["siteId", "citedChannels", "publishAuto"], async (body) => { const siteId = reqString(body, "siteId"); if (!isValidSiteId(siteId)) { throw new OpsInputError(`"${siteId}" is not a valid site id (lowercase letters, numbers and hyphens)`); } const cited = readCited(body.citedChannels); const publishAuto = readPolicy(body.publishAuto); if (cited === undefined && publishAuto === undefined) { throw new OpsInputError('nothing to change — give "citedChannels" or "publishAuto"'); } const paths = getPaths(); if (!existsSync(siteConfigFile(paths, siteId))) { throw new OpsInputError(`Site "${siteId}" not found`); } if (cited !== undefined) { const missing: string[] = []; for (const slug of cited) { if (!(await channelExists(paths, slug))) missing.push(slug); } if (missing.length > 0) { throw new OpsInputError( `not a channel: ${missing.map((s) => `"${s}"`).join(", ")} — "citedChannels" names channel slugs`, ); } } const changes: Partial> = {}; if (cited !== undefined) changes.citedChannels = cited.map((slug) => ({ slug })); if (publishAuto !== undefined) { changes.publish = publishAuto === "off" ? undefined : { auto: publishAuto }; } let saved: Site | null; try { saved = await patchSite(siteId, changes, paths); } catch (err) { // writeSite's refusals (a deploying policy with no Pages project) are // the request's fault, said in its words. throw new OpsInputError((err as Error).message); } if (!saved) throw new OpsInputError(`Site "${siteId}" not found`); return NextResponse.json({ ok: true, siteId, citedChannels: (saved.citedChannels ?? []).map((c) => c.slug), publishAuto: sitePublishPolicy(saved), }); }); } // `citedChannels`: absent, or an array of channel slugs (blank and repeated // entries are the parser's to drop; anything else is refused here). function readCited(raw: unknown): string[] | undefined { if (raw === undefined) return undefined; if (!Array.isArray(raw) || raw.some((s) => typeof s !== "string")) { throw new OpsInputError('"citedChannels" must be an array of channel slugs'); } const slugs = [...new Set((raw as string[]).map((s) => s.trim()).filter(Boolean))]; // A slug reaches a path.join under channelsDir: never one that traverses. for (const slug of slugs) { if (!isValidChannelSlug(slug)) { throw new OpsInputError( `"${slug}" is not a valid channel slug (letters, digits, ".", "_", "-"; must start with a letter or digit)`, ); } } return slugs; } function readPolicy(raw: unknown): PublishPolicy | undefined { if (raw === undefined) return undefined; if (typeof raw !== "string" || !(PUBLISH_POLICIES as readonly string[]).includes(raw)) { throw new OpsInputError(`"publishAuto" must be one of ${PUBLISH_POLICIES.join(", ")}`); } return raw as PublishPolicy; }