aboutsummaryrefslogtreecommitdiffstats
path: root/apps/web/app/api/v1/lists
diff options
context:
space:
mode:
authorMohamed Bassem <me@mbassem.com>2024-10-20 15:40:51 +0000
committerMohamed Bassem <me@mbassem.com>2024-10-20 15:40:51 +0000
commit20e5225e0547978cab656e94a3519cd590a16d8a (patch)
tree729687533556348a3200c7b409177fbc7ad426b2 /apps/web/app/api/v1/lists
parent10dcf2e7429601fcbde42e3d10c46c074dc9a28a (diff)
downloadkarakeep-20e5225e0547978cab656e94a3519cd590a16d8a.tar.zst
feature(api): Add REST APIs to manipulate lists and tags for bookmarks
Diffstat (limited to 'apps/web/app/api/v1/lists')
-rw-r--r--apps/web/app/api/v1/lists/[listId]/bookmarks/[bookmarkId]/route.ts35
-rw-r--r--apps/web/app/api/v1/lists/[listId]/bookmarks/route.ts18
-rw-r--r--apps/web/app/api/v1/lists/[listId]/route.ts22
3 files changed, 58 insertions, 17 deletions
diff --git a/apps/web/app/api/v1/lists/[listId]/bookmarks/[bookmarkId]/route.ts b/apps/web/app/api/v1/lists/[listId]/bookmarks/[bookmarkId]/route.ts
new file mode 100644
index 00000000..6efe2055
--- /dev/null
+++ b/apps/web/app/api/v1/lists/[listId]/bookmarks/[bookmarkId]/route.ts
@@ -0,0 +1,35 @@
+import { NextRequest } from "next/server";
+import { buildHandler } from "@/app/api/v1/utils/handler";
+
+export const dynamic = "force-dynamic";
+
+export const PUT = (
+ req: NextRequest,
+ { params }: { params: { listId: string; bookmarkId: string } },
+) =>
+ buildHandler({
+ req,
+ handler: async ({ api }) => {
+ // TODO: PUT is supposed to be idempotent, but we currently fail if the bookmark is already in the list.
+ await api.lists.addToList({
+ listId: params.listId,
+ bookmarkId: params.bookmarkId,
+ });
+ return { status: 204 };
+ },
+ });
+
+export const DELETE = (
+ req: NextRequest,
+ { params }: { params: { listId: string; bookmarkId: string } },
+) =>
+ buildHandler({
+ req,
+ handler: async ({ api }) => {
+ await api.lists.removeFromList({
+ listId: params.listId,
+ bookmarkId: params.bookmarkId,
+ });
+ return { status: 204 };
+ },
+ });
diff --git a/apps/web/app/api/v1/lists/[listId]/bookmarks/route.ts b/apps/web/app/api/v1/lists/[listId]/bookmarks/route.ts
new file mode 100644
index 00000000..72d4aa5f
--- /dev/null
+++ b/apps/web/app/api/v1/lists/[listId]/bookmarks/route.ts
@@ -0,0 +1,18 @@
+import { NextRequest } from "next/server";
+import { buildHandler } from "@/app/api/v1/utils/handler";
+import { adaptPagination, zPagination } from "@/app/api/v1/utils/pagination";
+
+export const dynamic = "force-dynamic";
+
+export const GET = (req: NextRequest, params: { params: { listId: string } }) =>
+ buildHandler({
+ req,
+ searchParamsSchema: zPagination,
+ handler: async ({ api, searchParams }) => {
+ const bookmarks = await api.bookmarks.getBookmarks({
+ listId: params.params.listId,
+ ...searchParams,
+ });
+ return { status: 200, resp: adaptPagination(bookmarks) };
+ },
+ });
diff --git a/apps/web/app/api/v1/lists/[listId]/route.ts b/apps/web/app/api/v1/lists/[listId]/route.ts
index d3e1f17c..f5af286d 100644
--- a/apps/web/app/api/v1/lists/[listId]/route.ts
+++ b/apps/web/app/api/v1/lists/[listId]/route.ts
@@ -1,6 +1,5 @@
import { NextRequest } from "next/server";
import { buildHandler } from "@/app/api/v1/utils/handler";
-import { adaptPagination, zPagination } from "@/app/api/v1/utils/pagination";
export const dynamic = "force-dynamic";
@@ -10,24 +9,13 @@ export const GET = (
) =>
buildHandler({
req,
- searchParamsSchema: zPagination,
- handler: async ({ api, searchParams }) => {
- const [list, bookmarks] = await Promise.all([
- api.lists.get({
- listId: params.listId,
- }),
- api.bookmarks.getBookmarks({
- listId: params.listId,
- limit: searchParams.limit,
- cursor: searchParams.cursor,
- }),
- ]);
+ handler: async ({ api }) => {
+ const list = await api.lists.get({
+ listId: params.listId,
+ });
return {
status: 200,
- resp: {
- ...list,
- ...adaptPagination(bookmarks),
- },
+ resp: list,
};
},
});