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 --- packages/trpc/routers/bookmarks.ts | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) (limited to 'packages/trpc') diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts index f3884053..15e4cb7c 100644 --- a/packages/trpc/routers/bookmarks.ts +++ b/packages/trpc/routers/bookmarks.ts @@ -45,6 +45,8 @@ import { zGetBookmarksResponseSchema, zManipulatedTagSchema, zNewBookmarkRequestSchema, + zSearchBookmarksCursor, + zSearchBookmarksRequestSchema, zUpdateBookmarksRequestSchema, } from "@hoarder/shared/types/bookmarks"; @@ -521,29 +523,17 @@ export const bookmarksAppRouter = router({ return await getBookmark(ctx, input.bookmarkId); }), searchBookmarks: authedProcedure - .input( - z.object({ - text: z.string(), - cursor: z - .object({ - offset: z.number(), - limit: z.number(), - }) - .nullish(), - }), - ) + .input(zSearchBookmarksRequestSchema) .output( z.object({ bookmarks: z.array(zBookmarkSchema), - nextCursor: z - .object({ - offset: z.number(), - limit: z.number(), - }) - .nullable(), + nextCursor: zSearchBookmarksCursor.nullable(), }), ) .query(async ({ input, ctx }) => { + if (!input.limit) { + input.limit = DEFAULT_NUM_BOOKMARKS_PER_PAGE; + } const client = await getSearchIdxClient(); if (!client) { throw new TRPCError({ @@ -571,10 +561,10 @@ export const bookmarksAppRouter = router({ showRankingScore: true, attributesToRetrieve: ["id"], sort: ["createdAt:desc"], + limit: input.limit, ...(input.cursor ? { offset: input.cursor.offset, - limit: input.cursor.limit, } : {}), }); @@ -614,8 +604,8 @@ export const bookmarksAppRouter = router({ resp.hits.length + resp.offset >= resp.estimatedTotalHits ? null : { + ver: 1 as const, offset: resp.hits.length + resp.offset, - limit: resp.limit, }, }; }), -- cgit v1.2.3-70-g09d2