import { prisma } from "@/lib/db"; import { signUrl } from "@/lib/r2"; export async function GET(request: Request) { const { searchParams } = new URL(request.url); const chapterId = parseInt(searchParams.get("chapterId") ?? "", 10); 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 (isNaN(chapterId)) { return Response.json({ error: "Missing chapterId" }, { 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); }