import { prisma } from "@/lib/db"; import { signUrl } from "@/lib/r2"; import { decodeId } from "@/lib/hashids"; import { withGuards } from "@/lib/api-guards"; export const GET = withGuards( { rateLimit: { key: "pages", limit: 300, windowMs: 60_000 } }, async (request) => { const { searchParams } = new URL(request.url); const chapterId = decodeId(searchParams.get("chapter") ?? ""); const offset = Math.max(parseInt(searchParams.get("offset") ?? "0", 10), 0); const limit = Math.min( Math.max(parseInt(searchParams.get("limit") ?? "7", 10), 1), 20 ); if (chapterId === null) { return Response.json({ error: "Missing chapter" }, { status: 400 }); } const pages = await prisma.page.findMany({ where: { chapterId }, orderBy: { number: "asc" }, skip: offset, take: limit, select: { number: true, imageUrl: true }, }); const signedPages = await Promise.all( pages.map(async (p) => ({ number: p.number, imageUrl: await signUrl(p.imageUrl), })) ); return Response.json(signedPages); } );