diff options
Diffstat (limited to 'packages/web/app/api/v1/bookmarks/route.ts')
| -rw-r--r-- | packages/web/app/api/v1/bookmarks/route.ts | 47 |
1 files changed, 47 insertions, 0 deletions
diff --git a/packages/web/app/api/v1/bookmarks/route.ts b/packages/web/app/api/v1/bookmarks/route.ts new file mode 100644 index 00000000..b9305ca8 --- /dev/null +++ b/packages/web/app/api/v1/bookmarks/route.ts @@ -0,0 +1,47 @@ +import { authOptions } from "@/lib/auth"; +import { bookmarkLink, getBookmarks } from "@/lib/services/bookmarks"; + +import { + zNewBookmarkRequestSchema, + ZGetBookmarksResponse, + ZBookmark, +} from "@/lib/types/api/bookmarks"; +import { getServerSession } from "next-auth"; +import { NextRequest, NextResponse } from "next/server"; + +export async function POST(request: NextRequest) { + // TODO: We probably should be using an API key here instead of the session; + const session = await getServerSession(authOptions); + if (!session) { + return new Response(null, { status: 401 }); + } + + const linkRequest = zNewBookmarkRequestSchema.safeParse(await request.json()); + + if (!linkRequest.success) { + return NextResponse.json( + { + error: linkRequest.error.toString(), + }, + { status: 400 }, + ); + } + + const bookmark = await bookmarkLink(linkRequest.data.url, session.user.id); + + let response: ZBookmark = { ...bookmark }; + return NextResponse.json(response, { status: 201 }); +} + +export async function GET() { + // TODO: We probably should be using an API key here instead of the session; + const session = await getServerSession(authOptions); + if (!session) { + return new Response(null, { status: 401 }); + } + + const bookmarks = await getBookmarks(session.user.id); + + let response: ZGetBookmarksResponse = { bookmarks }; + return NextResponse.json(response); +} |
