File size: 1,414 Bytes
9580dce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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 });
});