From a5ae67c241d8cdd452acd4d98800ec61740c041f Mon Sep 17 00:00:00 2001 From: Mohamed Bassem Date: Sun, 18 May 2025 19:12:27 +0000 Subject: feat(api): Expose the endpoint to create a new tag --- packages/trpc/routers/tags.ts | 36 +++++++++++++----------------------- 1 file changed, 13 insertions(+), 23 deletions(-) (limited to 'packages/trpc') diff --git a/packages/trpc/routers/tags.ts b/packages/trpc/routers/tags.ts index 7f75c16e..cade4b45 100644 --- a/packages/trpc/routers/tags.ts +++ b/packages/trpc/routers/tags.ts @@ -7,7 +7,9 @@ import { SqliteError } from "@karakeep/db"; import { bookmarkTags, tagsOnBookmarks } from "@karakeep/db/schema"; import { triggerSearchReindex } from "@karakeep/shared/queues"; import { + zCreateTagRequestSchema, zGetTagResponseSchema, + zTagBasicSchema, zUpdateTagRequestSchema, } from "@karakeep/shared/types/tags"; @@ -53,19 +55,8 @@ export const ensureTagOwnership = experimental_trpcMiddleware<{ export const tagsAppRouter = router({ create: authedProcedure - .input( - z.object({ - name: z.string().min(1), // Ensure the name is provided and not empty - }), - ) - .output( - z.object({ - id: z.string(), - name: z.string(), - userId: z.string(), - createdAt: z.date(), - }), - ) + .input(zCreateTagRequestSchema) + .output(zTagBasicSchema) .mutation(async ({ input, ctx }) => { try { const [newTag] = await ctx.db @@ -76,7 +67,10 @@ export const tagsAppRouter = router({ }) .returning(); - return newTag; + return { + id: newTag.id, + name: newTag.name, + }; } catch (e) { if (e instanceof SqliteError && e.code === "SQLITE_CONSTRAINT_UNIQUE") { throw new TRPCError({ @@ -195,14 +189,7 @@ export const tagsAppRouter = router({ }), update: authedProcedure .input(zUpdateTagRequestSchema) - .output( - z.object({ - id: z.string(), - name: z.string(), - userId: z.string(), - createdAt: z.date(), - }), - ) + .output(zTagBasicSchema) .use(ensureTagOwnership) .mutation(async ({ input, ctx }) => { try { @@ -242,7 +229,10 @@ export const tagsAppRouter = router({ console.error("Failed to reindex affected bookmarks", e); } - return res[0]; + return { + id: res[0].id, + name: res[0].name, + }; } catch (e) { if (e instanceof SqliteError) { if (e.code == "SQLITE_CONSTRAINT_UNIQUE") { -- cgit v1.2.3-70-g09d2