|
| 1 | +import { NextRequest, NextResponse } from "next/server" |
| 2 | + |
| 3 | +import { getArticlesByTopics } from "@/lib/articles" |
| 4 | + |
| 5 | +// Cache for 5 minutes |
| 6 | +export const revalidate = 300 |
| 7 | +export const dynamic = "force-dynamic" |
| 8 | + |
| 9 | +export async function GET(request: NextRequest) { |
| 10 | + try { |
| 11 | + const { searchParams } = request.nextUrl |
| 12 | + |
| 13 | + // Parse query parameters |
| 14 | + const topicsParam = searchParams.get("topics") |
| 15 | + const timeFilter = searchParams.get("timeFilter") || "7d" |
| 16 | + const topicTypesParam = searchParams.get("topicTypes") |
| 17 | + const sortBy = searchParams.get("sortBy") || "score" |
| 18 | + |
| 19 | + // Validate required parameters |
| 20 | + if (!topicsParam) { |
| 21 | + return NextResponse.json( |
| 22 | + { error: "topics parameter is required" }, |
| 23 | + { status: 400 } |
| 24 | + ) |
| 25 | + } |
| 26 | + |
| 27 | + // Parse topics array |
| 28 | + const topics = topicsParam |
| 29 | + .split(",") |
| 30 | + .map((t) => t.trim()) |
| 31 | + .filter(Boolean) |
| 32 | + if (topics.length === 0) { |
| 33 | + return NextResponse.json( |
| 34 | + { error: "At least one topic is required" }, |
| 35 | + { status: 400 } |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + // Parse topic types array |
| 40 | + const topicTypes = topicTypesParam |
| 41 | + ? topicTypesParam |
| 42 | + .split(",") |
| 43 | + .map((t) => t.trim()) |
| 44 | + .filter(Boolean) |
| 45 | + : [] |
| 46 | + |
| 47 | + // Validate timeFilter |
| 48 | + const validTimeFilters = ["24h", "7d", "30d", "all"] |
| 49 | + if (!validTimeFilters.includes(timeFilter)) { |
| 50 | + return NextResponse.json( |
| 51 | + { error: "timeFilter must be one of: 24h, 7d, 30d, all" }, |
| 52 | + { status: 400 } |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + // Validate sortBy |
| 57 | + const validSortBy = ["time", "score"] |
| 58 | + if (!validSortBy.includes(sortBy)) { |
| 59 | + return NextResponse.json( |
| 60 | + { error: "sortBy must be one of: time, score" }, |
| 61 | + { status: 400 } |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + // Convert timeFilter to hours |
| 66 | + let timeWindow: number | null = null |
| 67 | + switch (timeFilter) { |
| 68 | + case "24h": |
| 69 | + timeWindow = 24 |
| 70 | + break |
| 71 | + case "7d": |
| 72 | + timeWindow = 168 |
| 73 | + break |
| 74 | + case "30d": |
| 75 | + timeWindow = 720 |
| 76 | + break |
| 77 | + case "all": |
| 78 | + timeWindow = null |
| 79 | + break |
| 80 | + } |
| 81 | + |
| 82 | + // Get articles |
| 83 | + const articles = await getArticlesByTopics(topics, { |
| 84 | + timeWindow, |
| 85 | + topicTypes, |
| 86 | + limit: 50, |
| 87 | + }) |
| 88 | + |
| 89 | + // Sort results |
| 90 | + let sortedArticles = [...articles] |
| 91 | + if (sortBy === "time") { |
| 92 | + sortedArticles.sort( |
| 93 | + (a, b) => |
| 94 | + new Date(b.published_at).getTime() - |
| 95 | + new Date(a.published_at).getTime() |
| 96 | + ) |
| 97 | + } else { |
| 98 | + // Already sorted by relevance score from the query |
| 99 | + sortedArticles.sort((a, b) => b.relevanceScore - a.relevanceScore) |
| 100 | + } |
| 101 | + |
| 102 | + // Prepare response |
| 103 | + const response = { |
| 104 | + articles: sortedArticles, |
| 105 | + metadata: { |
| 106 | + totalResults: sortedArticles.length, |
| 107 | + timeFilter, |
| 108 | + searchType: "topic" as const, |
| 109 | + topics, |
| 110 | + topicTypes, |
| 111 | + sortBy, |
| 112 | + generatedAt: new Date().toISOString(), |
| 113 | + }, |
| 114 | + } |
| 115 | + |
| 116 | + return NextResponse.json(response, { |
| 117 | + headers: { |
| 118 | + "Cache-Control": "public, s-maxage=300, stale-while-revalidate=600", |
| 119 | + "CDN-Cache-Control": "public, s-maxage=300", |
| 120 | + "Vercel-CDN-Cache-Control": "public, s-maxage=300", |
| 121 | + }, |
| 122 | + }) |
| 123 | + } catch (error) { |
| 124 | + console.error("Error in GET /api/articles:", error) |
| 125 | + return NextResponse.json( |
| 126 | + { |
| 127 | + error: "Failed to retrieve articles", |
| 128 | + details: error instanceof Error ? error.message : "Unknown error", |
| 129 | + }, |
| 130 | + { status: 500 } |
| 131 | + ) |
| 132 | + } |
| 133 | +} |
0 commit comments