import { NextRequest, NextResponse } from "next/server"; import { ARTICLES, getArticleBySlug, getRandomArticle, type ArticleDifficulty, } from "@/lib/reading/articles"; import { withApiError } from "@/lib/api-error"; /** * GET /api/reading/content * * Query params: * slug – return a specific article * difficulty – filter random pick by difficulty (beginner | intermediate | advanced) * list – if "1", return article metadata (no body) */ export const GET = withApiError(async (request: NextRequest) => { const { searchParams } = new URL(request.url); const slug = searchParams.get("slug"); const difficulty = searchParams.get("difficulty") as ArticleDifficulty | null; const list = searchParams.get("list"); // List mode: return metadata only if (list === "1") { // eslint-disable-next-line @typescript-eslint/no-unused-vars const articles = ARTICLES.map(({ body: _body, ...meta }) => meta); return NextResponse.json({ articles }); } // Specific slug if (slug) { const article = getArticleBySlug(slug); if (!article) { return NextResponse.json({ error: "Article not found", status: 404 }, { status: 404 }); } return NextResponse.json({ article }); } // Random article (optionally filtered by difficulty) const article = getRandomArticle(difficulty ?? undefined); return NextResponse.json({ article }); });