Skip to content

Commit

Permalink
feature: Add APIs to create new lists and bookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedBassem committed Oct 20, 2024
1 parent f5fd3c4 commit 10dcf2e
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
12 changes: 12 additions & 0 deletions apps/web/app/api/v1/bookmarks/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { NextRequest } from "next/server";
import { z } from "zod";

import { zNewBookmarkRequestSchema } from "@hoarder/shared/types/bookmarks";

import { buildHandler } from "../utils/handler";
import { adaptPagination, zPagination } from "../utils/pagination";
import { zStringBool } from "../utils/types";
Expand All @@ -23,3 +25,13 @@ export const GET = (req: NextRequest) =>
return { status: 200, resp: adaptPagination(bookmarks) };
},
});

export const POST = (req: NextRequest) =>
buildHandler({
req,
bodySchema: zNewBookmarkRequestSchema,
handler: async ({ api, body }) => {
const bookmark = await api.bookmarks.createBookmark(body!);
return { status: 201, resp: bookmark };
},
});
12 changes: 12 additions & 0 deletions apps/web/app/api/v1/lists/route.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { NextRequest } from "next/server";

import { zNewBookmarkListSchema } from "@hoarder/shared/types/lists";

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

export const dynamic = "force-dynamic";
Expand All @@ -12,3 +14,13 @@ export const GET = (req: NextRequest) =>
return { status: 200, resp: lists };
},
});

export const POST = (req: NextRequest) =>
buildHandler({
req,
bodySchema: zNewBookmarkListSchema,
handler: async ({ api, body }) => {
const list = await api.lists.create(body!);
return { status: 201, resp: list };
},
});
9 changes: 9 additions & 0 deletions packages/shared/types/lists.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { z } from "zod";

export const zNewBookmarkListSchema = z.object({
name: z
.string()
.min(1, "List name can't be empty")
.max(40, "List name is at most 40 chars"),
icon: z.string(),
parentId: z.string().nullish(),
});

export const zBookmarkListSchema = z.object({
id: z.string(),
name: z.string(),
Expand Down
14 changes: 4 additions & 10 deletions packages/trpc/routers/lists.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@ import { z } from "zod";

import { SqliteError } from "@hoarder/db";
import { bookmarkLists, bookmarksInLists } from "@hoarder/db/schema";
import { zBookmarkListSchema } from "@hoarder/shared/types/lists";
import {
zBookmarkListSchema,
zNewBookmarkListSchema,
} from "@hoarder/shared/types/lists";

import type { Context } from "../index";
import { authedProcedure, router } from "../index";
import { ensureBookmarkOwnership } from "./bookmarks";

const zNewBookmarkListSchema = z.object({
name: z
.string()
.min(1, "List name can't be empty")
.max(40, "List name is at most 40 chars"),
icon: z.string(),
parentId: z.string().nullish(),
});

export const ensureListOwnership = experimental_trpcMiddleware<{
ctx: Context;
input: { listId: string };
Expand Down

0 comments on commit 10dcf2e

Please sign in to comment.