From 1f5d5668b7558ec4d0a77129041cba3ba6d72cb7 Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Sun, 5 Jan 2025 12:01:42 +0000 Subject: feat: Expose the search functionality in the REST API --- apps/web/app/api/v1/bookmarks/search/route.ts | 39 +++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 apps/web/app/api/v1/bookmarks/search/route.ts (limited to 'apps/web/app') diff --git a/apps/web/app/api/v1/bookmarks/search/route.ts b/apps/web/app/api/v1/bookmarks/search/route.ts new file mode 100644 index 00000000..f0c5417a --- /dev/null +++ b/apps/web/app/api/v1/bookmarks/search/route.ts @@ -0,0 +1,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, + }, + }; + }, + }); -- cgit v1.2.3-70-g09d2