From e5ee13d07beecb1b41780db0910850e8ea6a1aba Mon Sep 17 00:00:00 2001 From: Amruth Pillai Date: Thu, 24 Oct 2024 14:14:03 +0200 Subject: [PATCH 1/2] =?UTF-8?q?=F0=9F=90=9E=20fix=20(server):=20support=20?= =?UTF-8?q?for=20awaiting=20next.js=2015=20params?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- packages/server/src/next/app-route-handler.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/packages/server/src/next/app-route-handler.ts b/packages/server/src/next/app-route-handler.ts index 5c8cbe0e5..5e72e0378 100644 --- a/packages/server/src/next/app-route-handler.ts +++ b/packages/server/src/next/app-route-handler.ts @@ -6,7 +6,7 @@ import { AppRouteRequestHandlerOptions } from '.'; import { RPCApiHandler } from '../api'; import { loadAssets } from '../shared'; -type Context = { params: { path: string[] } }; +type Context = { params: Promise<{ path: string[] }> }; /** * Creates a Next.js 13 "app dir" API route request handler which encapsulates Prisma CRUD operations. @@ -28,9 +28,10 @@ export default function factory( } const url = new URL(req.url); + const params = await context.params; const query = Object.fromEntries(url.searchParams); - if (!context.params.path) { + if (!params.path) { return NextResponse.json( { message: 'missing path parameter' }, { @@ -38,7 +39,7 @@ export default function factory( } ); } - const path = context.params.path.join('/'); + const path = params.path.join('/'); let requestBody: unknown; if (req.body) { From 224f65f56f3d52ee391e8dc1e846dde6c5d71953 Mon Sep 17 00:00:00 2001 From: Amruth Pillai Date: Thu, 24 Oct 2024 17:44:12 +0200 Subject: [PATCH 2/2] refactor code to coderabbit's suggestions --- packages/server/src/next/app-route-handler.ts | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/packages/server/src/next/app-route-handler.ts b/packages/server/src/next/app-route-handler.ts index 5e72e0378..8347c5eb0 100644 --- a/packages/server/src/next/app-route-handler.ts +++ b/packages/server/src/next/app-route-handler.ts @@ -6,11 +6,12 @@ import { AppRouteRequestHandlerOptions } from '.'; import { RPCApiHandler } from '../api'; import { loadAssets } from '../shared'; -type Context = { params: Promise<{ path: string[] }> }; +type Context = { params: Promise<{ path: string[] }> | { path: string[] } }; /** * Creates a Next.js 13 "app dir" API route request handler which encapsulates Prisma CRUD operations. * + * @remarks Since Next.js 15, `context.params` is asynchronous and must be awaited. * @param options Options for initialization * @returns An API route request handler */ @@ -27,10 +28,16 @@ export default function factory( return NextResponse.json({ message: 'unable to get prisma from request context' }, { status: 500 }); } + let params: Context['params']; const url = new URL(req.url); - const params = await context.params; const query = Object.fromEntries(url.searchParams); + try { + params = await context.params; + } catch { + return NextResponse.json({ message: 'Failed to resolve request parameters' }, { status: 500 }); + } + if (!params.path) { return NextResponse.json( { message: 'missing path parameter' },