Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for type of route handler returned value at build time (via the TS plugin) and at runtime #51394

Merged
merged 14 commits into from Sep 8, 2023
Merged
Expand Up @@ -107,6 +107,19 @@ if ('${method}' in entry) {
'${method}'
>
>()
checkFields<
Diff<
{
__tag__: '${method}',
__return_type__: Response | Promise<Response>
joulev marked this conversation as resolved.
Show resolved Hide resolved
},
{
__tag__: '${method}',
__return_type__: ReturnType<MaybeField<TEntry, '${method}'>>
},
'${method}'
>
>()
}
`
).join('')
Expand Down
Expand Up @@ -70,7 +70,8 @@ type AppRouteHandlerFnContext = {
}

/**
* Handler function for app routes.
* Handler function for app routes. If a non-Response value is returned, an error
* will be thrown.
*/
export type AppRouteHandlerFn = (
/**
Expand All @@ -82,7 +83,7 @@ export type AppRouteHandlerFn = (
* dynamic route).
*/
ctx: AppRouteHandlerFnContext
) => Promise<Response> | Response
) => unknown

/**
* AppRouteHandlers describes the handlers for app routes that is provided by
Expand Down Expand Up @@ -368,6 +369,11 @@ export class AppRouteRouteModule extends RouteModule<
? parsedUrlQueryToParams(context.params)
: undefined,
})
if (!(res instanceof Response)) {
joulev marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(
`No response is returned from route handler '${this.resolvedPagePath}'. Ensure you return a \`Response\` or a \`NextResponse\` in all branches of your handler.`
)
}
;(context.staticGenerationContext as any).fetchMetrics =
staticGenerationStore.fetchMetrics

Expand Down