| 1 | import { NextResponse, type NextRequest } from "next/server"; |
| 2 | import { parsePaging, paramArray, type ItemList } from "@/lib/db"; |
| 3 | import { getFeedItems, type FeedItem } from "@/lib/feed"; |
| 4 | |
| 5 | export const dynamic = "force-dynamic"; |
| 6 | |
| 7 | |
| 8 | export async function GET(req: NextRequest) { |
| 9 | const sp = req.nextUrl.searchParams; |
| 10 | const { limit, offset } = parsePaging(sp); |
| 11 | const types = paramArray(sp, "type"); |
| 12 | const state = sp.get("state")?.toUpperCase(); |
| 13 | const q = sp.get("q")?.toLowerCase().trim(); |
| 14 | |
| 15 | let items = getFeedItems(); |
| 16 | |
| 17 | if (types.length) { |
| 18 | const allow = new Set(types); |
| 19 | items = items.filter((i) => allow.has(i.kind)); |
| 20 | } |
| 21 | if (state && state !== "US") { |
| 22 | items = items.filter((i) => i.jurisdiction === state); |
| 23 | } |
| 24 | if (q) { |
| 25 | items = items.filter( |
| 26 | (i) => |
| 27 | i.title.toLowerCase().includes(q) || |
| 28 | (i.summary ?? "").toLowerCase().includes(q) || |
| 29 | i.actionType.toLowerCase().includes(q) |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | const total = items.length; |
| 34 | const paged = items.slice(offset, offset + limit); |
| 35 | const body: ItemList<FeedItem> = { items: paged, total, limit, offset }; |
| 36 | return NextResponse.json(body); |
| 37 | } |