From 5101db7fa01621328bb7087c7a2f4e7efb6792c2 Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Tue, 6 Feb 2024 11:58:02 +0000 Subject: Start using zod in the API --- app/api/v1/links/route.ts | 32 ++++++++++++++++++++------------ 1 file changed, 20 insertions(+), 12 deletions(-) (limited to 'app/api') diff --git a/app/api/v1/links/route.ts b/app/api/v1/links/route.ts index 9a1de10a..5be1018e 100644 --- a/app/api/v1/links/route.ts +++ b/app/api/v1/links/route.ts @@ -1,12 +1,9 @@ import { authOptions } from "@/lib/auth"; import prisma from "@/lib/prisma"; +import { ZNewBookmarkedLinkRequest, ZGetLinksResponse, ZBookmarkedLink } from "@/lib/types/api/links"; import { getServerSession } from "next-auth"; import { NextRequest, NextResponse } from "next/server"; -interface NewLinkRequest { - url: string, -} - 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); @@ -14,17 +11,24 @@ export async function POST(request: NextRequest) { return new Response(null, { status: 401 }); } - // TODO: We need proper type assertion here - const body: NewLinkRequest = await request.json(); + const linkRequest = ZNewBookmarkedLinkRequest.safeParse(await request.json()); + + if (!linkRequest.success) { + return NextResponse.json({ + error: linkRequest.error.toString(), + }, { status: 400 }); + } const link = await prisma.bookmarkedLink.create({ data: { - url: body.url, + url: linkRequest.data.url, userId: session.user.id, } - }) + }); + + let response: ZBookmarkedLink = { ...link }; - return NextResponse.json(link, { status: 201 }); + return NextResponse.json(response, { status: 201 }); } export async function GET() { @@ -37,7 +41,10 @@ export async function GET() { where: { userId: session.user.id, }, - include: { + select: { + id: true, + url: true, + createdAt: true, details: { select: { title: true, @@ -46,7 +53,8 @@ export async function GET() { } }, } - }) + }); - return NextResponse.json({links}); + let response: ZGetLinksResponse = { links }; + return NextResponse.json(response); } -- cgit v1.2.3-70-g09d2