From 8eb890520c96219c07a372032947bf575d9cb334 Mon Sep 17 00:00:00 2001 From: Sasha <64744993+r1tsuu@users.noreply.github.com> Date: Thu, 23 Jan 2025 20:00:36 +0200 Subject: [PATCH 1/2] fix: next.js rewrites were not respected for api --- packages/next/src/routes/rest/index.ts | 11 +++++++++-- packages/payload/src/utilities/handleEndpoints.ts | 14 +++++++++----- 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/next/src/routes/rest/index.ts b/packages/next/src/routes/rest/index.ts index 5929dc7e5fc..419bd72e414 100644 --- a/packages/next/src/routes/rest/index.ts +++ b/packages/next/src/routes/rest/index.ts @@ -6,7 +6,12 @@ let initedOGEndpoint = false const handlerBuilder = (config: Promise | SanitizedConfig) => - async (request: Request): Promise => { + async ( + request: Request, + args: { + params: Promise<{ slug: string[] }> + }, + ): Promise => { const awaitedConfig = await config // Add this endpoint only when using Next.js, still can be overriden. @@ -25,9 +30,11 @@ const handlerBuilder = initedOGEndpoint = true + const awaitedParams = await args.params + const response = await handleEndpoints({ - basePath: process.env.NEXT_BASE_PATH, config, + path: `${awaitedConfig.routes.api}/${awaitedParams.slug.join('/')}`, request, }) diff --git a/packages/payload/src/utilities/handleEndpoints.ts b/packages/payload/src/utilities/handleEndpoints.ts index 5c5db7897b3..24777af45ae 100644 --- a/packages/payload/src/utilities/handleEndpoints.ts +++ b/packages/payload/src/utilities/handleEndpoints.ts @@ -11,10 +11,10 @@ import { headersWithCors } from './headersWithCors.js' import { mergeHeaders } from './mergeHeaders.js' import { routeError } from './routeError.js' -const notFoundResponse = (req: PayloadRequest) => { +const notFoundResponse = (req: PayloadRequest, pathname?: string) => { return Response.json( { - message: `Route not found "${new URL(req.url).pathname}"`, + message: `Route not found "${pathname ?? new URL(req.url).pathname}"`, }, { headers: headersWithCors({ @@ -61,10 +61,13 @@ const notFoundResponse = (req: PayloadRequest) => { export const handleEndpoints = async ({ basePath = '', config: incomingConfig, + path, request, }: { basePath?: string config: Promise | SanitizedConfig + /** Override path from the reques */ + path?: string request: Request }): Promise => { let handler: PayloadHandler @@ -85,6 +88,7 @@ export const handleEndpoints = async ({ const response = await handleEndpoints({ basePath, config: incomingConfig, + path, request: new Request(url, { cache: request.cache, credentials: request.credentials, @@ -116,10 +120,10 @@ export const handleEndpoints = async ({ const { payload } = req const { config } = payload - const pathname = `${basePath}${new URL(req.url).pathname}` + const pathname = `${basePath}${path}` if (!pathname.startsWith(config.routes.api)) { - return notFoundResponse(req) + return notFoundResponse(req, pathname) } // /api/posts/route -> /posts/route @@ -213,7 +217,7 @@ export const handleEndpoints = async ({ } if (!handler) { - return notFoundResponse(req) + return notFoundResponse(req, pathname) } const response = await handler(req) From d2949890066a61b4f81cf4f7d2312479df7eafea Mon Sep 17 00:00:00 2001 From: Sasha <64744993+r1tsuu@users.noreply.github.com> Date: Thu, 23 Jan 2025 20:02:22 +0200 Subject: [PATCH 2/2] fix typo --- packages/payload/src/utilities/handleEndpoints.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/payload/src/utilities/handleEndpoints.ts b/packages/payload/src/utilities/handleEndpoints.ts index 24777af45ae..25cd16ccb24 100644 --- a/packages/payload/src/utilities/handleEndpoints.ts +++ b/packages/payload/src/utilities/handleEndpoints.ts @@ -66,7 +66,7 @@ export const handleEndpoints = async ({ }: { basePath?: string config: Promise | SanitizedConfig - /** Override path from the reques */ + /** Override path from the request */ path?: string request: Request }): Promise => { @@ -120,7 +120,7 @@ export const handleEndpoints = async ({ const { payload } = req const { config } = payload - const pathname = `${basePath}${path}` + const pathname = `${basePath}${path ?? new URL(req.url).pathname}` if (!pathname.startsWith(config.routes.api)) { return notFoundResponse(req, pathname)