aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMohamedBassem <me@mbassem.com>2024-02-06 11:58:02 +0000
committerMohamedBassem <me@mbassem.com>2024-02-06 11:58:02 +0000
commit5101db7fa01621328bb7087c7a2f4e7efb6792c2 (patch)
treec5054290306b0d575bc98471f530bef543bbba82
parente5b79e8ff0de22f86387a46c2155044adeb19fc4 (diff)
downloadkarakeep-5101db7fa01621328bb7087c7a2f4e7efb6792c2.tar.zst
Start using zod in the API
-rw-r--r--app/api/v1/links/route.ts32
-rwxr-xr-xbun.lockbbin151999 -> 152334 bytes
-rw-r--r--lib/types/api/links.ts26
-rw-r--r--package.json3
4 files changed, 48 insertions, 13 deletions
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);
}
diff --git a/bun.lockb b/bun.lockb
index 9bdccc98..bfd618dc 100755
--- a/bun.lockb
+++ b/bun.lockb
Binary files differ
diff --git a/lib/types/api/links.ts b/lib/types/api/links.ts
new file mode 100644
index 00000000..81cde053
--- /dev/null
+++ b/lib/types/api/links.ts
@@ -0,0 +1,26 @@
+import { z } from "zod";
+
+export const ZBookmarkedLink = z.object({
+ id: z.string(),
+ url: z.string().url(),
+ createdAt: z.coerce.date(),
+
+ details: z.object({
+ title: z.string(),
+ description: z.string(),
+ imageUrl: z.string().url(),
+ }).nullish(),
+
+});
+export type ZBookmarkedLink = z.infer<typeof ZBookmarkedLink>;
+
+
+// POST /v1/links
+export const ZNewBookmarkedLinkRequest = ZBookmarkedLink.pick({ url: true });
+
+
+// GET /v1/links
+export const ZGetLinksResponse = z.object({
+ links: z.array(ZBookmarkedLink),
+});
+export type ZGetLinksResponse = z.infer<typeof ZGetLinksResponse>;
diff --git a/package.json b/package.json
index 86517ef6..1323e456 100644
--- a/package.json
+++ b/package.json
@@ -19,7 +19,8 @@
"react": "^18",
"react-dom": "^18",
"tailwind-merge": "^2.2.1",
- "tailwindcss-animate": "^1.0.7"
+ "tailwindcss-animate": "^1.0.7",
+ "zod": "^3.22.4"
},
"devDependencies": {
"typescript": "^5",