diff options
| author | MohamedBassem <me@mbassem.com> | 2024-02-08 15:14:23 +0000 |
|---|---|---|
| committer | MohamedBassem <me@mbassem.com> | 2024-02-08 15:15:21 +0000 |
| commit | 80bb8a108f29331cdb2f2695f6801beee104dc89 (patch) | |
| tree | b1ae2a512963a9c916c4bfed71f7633f508de131 /packages/web/app/api/v1/links/route.ts | |
| parent | 333429adbaaa592cc96b480a5228f0e3f1de4cc2 (diff) | |
| download | karakeep-80bb8a108f29331cdb2f2695f6801beee104dc89.tar.zst | |
[refactor] Move the different packages to the package subdir
Diffstat (limited to 'packages/web/app/api/v1/links/route.ts')
| -rw-r--r-- | packages/web/app/api/v1/links/route.ts | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/packages/web/app/api/v1/links/route.ts b/packages/web/app/api/v1/links/route.ts new file mode 100644 index 00000000..87541634 --- /dev/null +++ b/packages/web/app/api/v1/links/route.ts @@ -0,0 +1,49 @@ +import { authOptions } from "@/lib/auth"; +import { bookmarkLink, getLinks } from "@/lib/services/links"; + +import { + zNewBookmarkedLinkRequestSchema, + ZGetLinksResponse, + ZBookmarkedLink, +} from "@/lib/types/api/links"; +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 = zNewBookmarkedLinkRequestSchema.safeParse( + await request.json(), + ); + + if (!linkRequest.success) { + return NextResponse.json( + { + error: linkRequest.error.toString(), + }, + { status: 400 }, + ); + } + + const link = await bookmarkLink(linkRequest.data.url, session.user.id); + + let response: ZBookmarkedLink = { ...link }; + 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 links = await getLinks(session.user.id); + + let response: ZGetLinksResponse = { links }; + return NextResponse.json(response); +} |
