● Zion Boggan
repos/Docket/app/api/laws/route.ts
zionboggan.com ↗
36 lines · typescript
History for this file →
1
import { NextResponse, type NextRequest } from "next/server";
2
import { parsePaging } from "@/lib/db";
3
import {
4
  getPublicLaws,
5
  getPublicLawsCount,
6
  getPrivateLawsCount,
7
  type LawListItem,
8
  type LawSort,
9
} from "@/lib/feed";
10
 
11
export const dynamic = "force-dynamic";
12
 
13
type LawsResponse = {
14
  items: LawListItem[];
15
  total: number;
16
  limit: number;
17
  offset: number;
18
  privateCount: number;
19
};
20
 
21
export async function GET(req: NextRequest) {
22
  const sp = req.nextUrl.searchParams;
23
  const { limit, offset } = parsePaging(sp);
24
  const q = sp.get("q") || undefined;
25
  const sortParam = sp.get("sort");
26
  const sort: LawSort =
27
    sortParam === "oldest" ? "oldest" :
28
    sortParam === "number" ? "number" :
29
    "recent";
30
 
31
  const items = getPublicLaws({ q, sort }, limit, offset);
32
  const total = getPublicLawsCount(q);
33
  const privateCount = getPrivateLawsCount();
34
  const body: LawsResponse = { items, total, limit, offset, privateCount };
35
  return NextResponse.json(body);
36
}