aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/app/api/v1/bookmarks/search/route.ts
blob: e85c79541b47e84f987aeb9e81873fc385132fee (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
40
41
42
43
44
import { NextRequest } from "next/server";
import { z } from "zod";

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

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(),
      })
      .and(zGetBookmarkSearchParamsSchema),
    handler: async ({ api, searchParams }) => {
      const bookmarks = await api.bookmarks.searchBookmarks({
        text: searchParams.q,
        cursor: searchParams.cursor,
        sortOrder: searchParams.sortOrder,
        limit: searchParams.limit,
        includeContent: searchParams.includeContent,
      });
      return {
        status: 200,
        resp: {
          bookmarks: bookmarks.bookmarks,
          nextCursor: bookmarks.nextCursor
            ? `${bookmarks.nextCursor.offset}`
            : null,
        },
      };
    },
  });