From a4cc33da1f524d6afcb95f32b10e64da40a283a7 Mon Sep 17 00:00:00 2001 From: Markus Blomqvist Date: Tue, 24 Oct 2023 18:23:52 +0300 Subject: [PATCH] Fix TypedNextResponse usage The custom typings do not implement the same constuctor as the original NextResponse class, so we apply the custom types to the original NextResponse class when exporting the module instead. --- packages/next-rest-framework/src/index.ts | 6 +++++- packages/next-rest-framework/src/types/route-handlers.ts | 2 +- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/next-rest-framework/src/index.ts b/packages/next-rest-framework/src/index.ts index 348b166..8648344 100644 --- a/packages/next-rest-framework/src/index.ts +++ b/packages/next-rest-framework/src/index.ts @@ -1,3 +1,5 @@ +import { NextResponse } from 'next/server'; +import { type TypedNextResponse as TypedNextResponseType } from './types'; export { routeHandler, routeOperation, @@ -5,4 +7,6 @@ export { apiRouteOperation } from './route-handlers'; export { docsRouteHandler, docsApiRouteHandler } from './docs-handlers'; -export { TypedNextResponse } from './types'; + +// @ts-expect-error - Keep the original NextResponse functionality with custom types. +export const TypedNextResponse: typeof TypedNextResponseType = NextResponse; diff --git a/packages/next-rest-framework/src/types/route-handlers.ts b/packages/next-rest-framework/src/types/route-handlers.ts index 9f2b5ae..5b68ea7 100644 --- a/packages/next-rest-framework/src/types/route-handlers.ts +++ b/packages/next-rest-framework/src/types/route-handlers.ts @@ -159,7 +159,7 @@ export type RouteOperation = ( export type NextRouteHandler = ( req: NextRequest, context: { params: BaseQuery } -) => Promise; +) => Promise | NextResponse | Promise | void; export interface RouteOperationDefinition { _config: {