1 import { NextRequest, NextResponse } from "next/server"
2 import {
3 CHANGELOG_MAX_PAGE_SIZE,
4 CHANGELOG_PAGE_SIZE,
5 getChangelogPage,
6 } from "../../../utils/changelog"
7 import {
8 PUBLIC_API_CACHE_CONTROL,
9 PUBLIC_API_CORS_HEADERS,
10 } from "../../../utils/public-api"
11
12 function parseNumber(value: string | null, fallback: number): number {
13 const parsed = parseInt(value ?? "", 10)
14
15 return Number.isFinite(parsed) ? parsed : fallback
16 }
17
18 /**
19 * A public, paginated endpoint for the Cloud changelog. Each entry's content is
20 * Markdown, with links resolved to absolute URLs so external consumers can
21 * follow them.
22 *
23 * Alongside the content, an entry carries a `summary` and an `image` — the
24 * entry's banner, rendered by the `release-banner` package and hosted on
25 * Cloudinary. Neither is rendered on the changelog page; both exist for
26 * consumers of this endpoint, and are `null` on entries that don't have them.
27 *
28 * @example
29 * GET /cloud/api/changelog?page=2&limit=10
30 */
31 export async function GET(req: NextRequest) {
32 const searchParams = req.nextUrl.searchParams
33 const page = parseNumber(searchParams.get("page"), 1)
34 const limit = parseNumber(searchParams.get("limit"), CHANGELOG_PAGE_SIZE)
35
36 if (page < 1 || limit < 1 || limit > CHANGELOG_MAX_PAGE_SIZE) {
37 return NextResponse.json(
38 {
39 message: `\`page\` must be at least 1, and \`limit\` must be between 1 and ${CHANGELOG_MAX_PAGE_SIZE}.`,
40 },
41 { status: 400, headers: PUBLIC_API_CORS_HEADERS }
42 )
43 }
44
45 const data = await getChangelogPage({
46 page,
47 limit,
48 baseUrl: process.env.NEXT_PUBLIC_BASE_URL || new URL(req.url).origin,
49 })
50
51 return NextResponse.json(data, {
52 headers: {
53 ...PUBLIC_API_CORS_HEADERS,
54 "Cache-Control": PUBLIC_API_CACHE_CONTROL,
55 },
56 })
57 }
58
59 export async function OPTIONS() {
60 return new NextResponse(null, {
61 status: 204,
62 headers: PUBLIC_API_CORS_HEADERS,
63 })
64 }