import { NextResponse } from "next/server"; import { archiveLookupAction } from "../../../channels/[slug]/waybackPagesActions"; import { OpsInputError, ops, opsFail, optPositiveInt, optString, reqString } from "../_lib"; export const dynamic = "force-dynamic"; // POST { url, source?, limit? } -> { ok: true, url, found, sources: [{ source, // label, ok, captures: [{ source, captureTs, originalUrl, mementoUrl, // status?, mimetype? }], refusal?, error? }] } // // THE COPIES OF A PAGE across archive sources (release 37 lane F, // common/lib/archives/): the Wayback Machine (its CDX API, archive.org's pace) // and archive.today (its timemap, one request at a time with a long gap), in // that order, or only `source` ("wayback" | "archive.today"). At most `limit` // per source (default 50, at most 500), oldest first. A source that refuses is // its own entry's sentence — `refusal: "excluded"` (the Wayback Machine // answered 403 for the URL: "excluded from the Wayback Machine (cause // unknown)"), "challenge" (archive.today put a check in front: Connect archive // session), "rate-limited" (a 429: the client rests) — and the others still // answer. Read only; nothing is stored. `wayback-lookup` stays as it is. const MAX_LIMIT = 500; export async function POST(request: Request) { return ops(request, ["url", "source", "limit"], async (body) => { const url = reqString(body, "url"); if (!/^https?:\/\/[^\s]+$/.test(url)) throw new OpsInputError(`"url" must be an http(s) URL`); const source = optString(body, "source")?.trim() || undefined; const limit = optPositiveInt(body, "limit") ?? 50; if (limit > MAX_LIMIT) throw new OpsInputError(`"limit" is at most ${MAX_LIMIT}`); const r = await archiveLookupAction(url, source, limit); if (!r.ok) return opsFail(r.error, 400); return NextResponse.json({ ok: true, ...r.lookup }); }); }