From 2c2d05fd0a2c3c26d765f8a6beb88d907a097c1d Mon Sep 17 00:00:00 2001 From: MohamedBassem Date: Sun, 11 Feb 2024 14:54:52 +0000 Subject: refactor: Migrating to trpc instead of next's route handers --- packages/web/app/api/auth/[...nextauth]/route.tsx | 2 +- packages/web/app/api/trpc/[trpc]/route.ts | 10 ++-- .../web/app/api/v1/bookmarks/[bookmarkId]/route.ts | 70 ---------------------- packages/web/app/api/v1/bookmarks/route.ts | 55 ----------------- 4 files changed, 7 insertions(+), 130 deletions(-) delete mode 100644 packages/web/app/api/v1/bookmarks/[bookmarkId]/route.ts delete mode 100644 packages/web/app/api/v1/bookmarks/route.ts (limited to 'packages/web/app/api') diff --git a/packages/web/app/api/auth/[...nextauth]/route.tsx b/packages/web/app/api/auth/[...nextauth]/route.tsx index e722926b..2f7f1cb0 100644 --- a/packages/web/app/api/auth/[...nextauth]/route.tsx +++ b/packages/web/app/api/auth/[...nextauth]/route.tsx @@ -1,3 +1,3 @@ -import { authHandler } from "@/lib/auth"; +import { authHandler } from "@/server/auth"; export { authHandler as GET, authHandler as POST }; diff --git a/packages/web/app/api/trpc/[trpc]/route.ts b/packages/web/app/api/trpc/[trpc]/route.ts index 872da79a..4d108604 100644 --- a/packages/web/app/api/trpc/[trpc]/route.ts +++ b/packages/web/app/api/trpc/[trpc]/route.ts @@ -1,10 +1,12 @@ -import { fetchRequestHandler } from '@trpc/server/adapters/fetch'; -import { appRouter } from '@/server/routers/_app'; +import { fetchRequestHandler } from "@trpc/server/adapters/fetch"; +import { appRouter } from "@/server/api/routers/_app"; +import { createContext } from "@/server/api/client"; + const handler = (req: Request) => fetchRequestHandler({ - endpoint: '/api/trpc', + endpoint: "/api/trpc", req, router: appRouter, - createContext: () => ({}) + createContext, }); export { handler as GET, handler as POST }; diff --git a/packages/web/app/api/v1/bookmarks/[bookmarkId]/route.ts b/packages/web/app/api/v1/bookmarks/[bookmarkId]/route.ts deleted file mode 100644 index 3e57fa65..00000000 --- a/packages/web/app/api/v1/bookmarks/[bookmarkId]/route.ts +++ /dev/null @@ -1,70 +0,0 @@ -import { authOptions } from "@/lib/auth"; -import { deleteBookmark, updateBookmark } from "@/lib/services/bookmarks"; -import { - ZBookmark, - zUpdateBookmarksRequestSchema, -} from "@/lib/types/api/bookmarks"; -import { Prisma } from "@remember/db"; - -import { getServerSession } from "next-auth"; -import { NextRequest, NextResponse } from "next/server"; - -export async function PATCH( - request: NextRequest, - { params }: { params: { bookmarkId: string } }, -) { - const session = await getServerSession(authOptions); - if (!session) { - return new Response(null, { status: 401 }); - } - - const updateJson = await request.json(); - const update = zUpdateBookmarksRequestSchema.safeParse(updateJson); - if (!update.success) { - return new Response(null, { status: 400 }); - } - - try { - const bookmark: ZBookmark = await updateBookmark( - params.bookmarkId, - session.user.id, - update.data, - ); - return NextResponse.json(bookmark); - } catch (e: unknown) { - if ( - e instanceof Prisma.PrismaClientKnownRequestError && - e.code === "P2025" // RecordNotFound - ) { - return new Response(null, { status: 404 }); - } else { - throw e; - } - } -} - -export async function DELETE( - _request: NextRequest, - { params }: { params: { bookmarkId: string } }, -) { - // 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 }); - } - - try { - await deleteBookmark(params.bookmarkId, session.user.id); - } catch (e: unknown) { - if ( - e instanceof Prisma.PrismaClientKnownRequestError && - e.code === "P2025" // RecordNotFound - ) { - return new Response(null, { status: 404 }); - } else { - throw e; - } - } - - return new Response(null, { status: 204 }); -} diff --git a/packages/web/app/api/v1/bookmarks/route.ts b/packages/web/app/api/v1/bookmarks/route.ts deleted file mode 100644 index 98e01080..00000000 --- a/packages/web/app/api/v1/bookmarks/route.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { authOptions } from "@/lib/auth"; -import { bookmarkLink, getBookmarks } from "@/lib/services/bookmarks"; - -import { - zNewBookmarkRequestSchema, - ZGetBookmarksResponse, - ZBookmark, - zGetBookmarksRequestSchema, -} 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); - - const response: ZBookmark = { ...bookmark }; - return NextResponse.json(response, { status: 201 }); -} - -export async function GET(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 query = request.nextUrl.searchParams; - const params = zGetBookmarksRequestSchema.safeParse(query); - - if (!params.success) { - return new Response(null, { status: 400 }); - } - - const bookmarks = await getBookmarks(session.user.id, params.data); - - const response: ZGetBookmarksResponse = { bookmarks }; - return NextResponse.json(response); -} -- cgit v1.2.3-70-g09d2