● Zion Boggan
repos/Docket/app/api/executive/route.ts
zionboggan.com ↗
28 lines · typescript
History for this file →
1
import { NextResponse, type NextRequest } from "next/server";
2
import { parsePaging, type ItemList } from "@/lib/db";
3
import { getExecutive, type ExecutiveAction } from "@/lib/feed";
4
 
5
export const dynamic = "force-dynamic";
6
 
7
export async function GET(req: NextRequest) {
8
  const sp = req.nextUrl.searchParams;
9
  const { limit, offset } = parsePaging(sp);
10
  const type = sp.get("type")?.toLowerCase();
11
  const q = sp.get("q")?.toLowerCase().trim();
12
 
13
  let items = getExecutive();
14
  if (type) {
15
    items = items.filter((e) => e.type.toLowerCase() === type);
16
  }
17
  if (q) {
18
    items = items.filter(
19
      (e) =>
20
        e.title.toLowerCase().includes(q) ||
21
        e.agency.toLowerCase().includes(q)
22
    );
23
  }
24
  const total = items.length;
25
  const paged = items.slice(offset, offset + limit);
26
  const body: ItemList<ExecutiveAction> = { items: paged, total, limit, offset };
27
  return NextResponse.json(body);
28
}