aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/app/api/v1/bookmarks/search/route.ts
blob: f0c5417a35df32972d231687482fa44da77d0352 (plain) (blame)
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
import { NextRequest } from "next/server";
import { z } from "zod";

import { buildHandler } from "../../utils/handler";

export const dynamic = "force-dynamic";

export const GET = (req: NextRequest) =>
  buildHandler({
    req,
    searchParamsSchema: z.object({
      q: z.string(),
      limit: z.coerce.number().optional(),
      cursor: z
        .string()
        // Search cursor V1 is just a number
        .pipe(z.coerce.number())
        .transform((val) => {
          return { ver: 1 as const, offset: val };
        })
        .optional(),
    }),
    handler: async ({ api, searchParams }) => {
      const bookmarks = await api.bookmarks.searchBookmarks({
        text: searchParams.q,
        cursor: searchParams.cursor,
        limit: searchParams.limit,
      });
      return {
        status: 200,
        resp: {
          bookmarks: bookmarks.bookmarks,
          nextCursor: bookmarks.nextCursor
            ? `${bookmarks.nextCursor.offset}`
            : null,
        },
      };
    },
  });