From 20e5225e0547978cab656e94a3519cd590a16d8a Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Sun, 20 Oct 2024 15:40:51 +0000 Subject: feature(api): Add REST APIs to manipulate lists and tags for bookmarks --- packages/shared/types/bookmarks.ts | 12 ++++++++++++ packages/trpc/routers/bookmarks.ts | 27 +++------------------------ 2 files changed, 15 insertions(+), 24 deletions(-) (limited to 'packages') diff --git a/packages/shared/types/bookmarks.ts b/packages/shared/types/bookmarks.ts index a9708f73..c8ab96f9 100644 --- a/packages/shared/types/bookmarks.ts +++ b/packages/shared/types/bookmarks.ts @@ -157,3 +157,15 @@ export const zUpdateBookmarksRequestSchema = z.object({ export type ZUpdateBookmarksRequest = z.infer< typeof zUpdateBookmarksRequestSchema >; + +// The schema that's used to for attachig/detaching tags +export const zManipulatedTagSchema = z + .object({ + // At least one of the two must be set + tagId: z.string().optional(), // If the tag already exists and we know its id we should pass it + tagName: z.string().optional(), + }) + .refine((val) => !!val.tagId || !!val.tagName, { + message: "You must provide either a tagId or a tagName", + path: ["tagId", "tagName"], + }); diff --git a/packages/trpc/routers/bookmarks.ts b/packages/trpc/routers/bookmarks.ts index f272433a..6439111a 100644 --- a/packages/trpc/routers/bookmarks.ts +++ b/packages/trpc/routers/bookmarks.ts @@ -36,6 +36,7 @@ import { zBookmarkSchema, zGetBookmarksRequestSchema, zGetBookmarksResponseSchema, + zManipulatedTagSchema, zNewBookmarkRequestSchema, zUpdateBookmarksRequestSchema, } from "@hoarder/shared/types/bookmarks"; @@ -732,30 +733,8 @@ export const bookmarksAppRouter = router({ .input( z.object({ bookmarkId: z.string(), - attach: z.array( - z - .object({ - // At least one of the two must be set - tagId: z.string().optional(), // If the tag already exists and we know its id we should pass it - tagName: z.string().optional(), - }) - .refine((val) => !!val.tagId || !!val.tagName, { - message: "You must provide either a tagId or a tagName", - path: ["tagId", "tagName"], - }), - ), - detach: z.array( - z - .object({ - // At least one of the two must be set - tagId: z.string().optional(), - tagName: z.string().optional(), // Also allow removing by tagName, to make CLI usage easier - }) - .refine((val) => !!val.tagId || !!val.tagName, { - message: "You must provide either a tagId or a tagName", - path: ["tagId", "tagName"], - }), - ), + attach: z.array(zManipulatedTagSchema), + detach: z.array(zManipulatedTagSchema), }), ) .output( -- cgit v1.2.3-70-g09d2