Skip to content

Commit 3cb451f

Browse files
JBR-0100autofix-ci[bot]schiller-manuel
authored
fix(start): allow request middleware to return Response (#6006)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Manuel Schiller <manuel.schiller@caligano.de>
1 parent 52d7b88 commit 3cb451f

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

packages/start-client-core/src/createMiddleware.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -760,19 +760,27 @@ export type RequestServerNextFn<TRegister, TMiddlewares> = <
760760
TServerContext = undefined,
761761
>(
762762
options?: RequestServerNextFnOptions<TServerContext>,
763-
) => RequestMiddlewareServerFnResult<TRegister, TMiddlewares, TServerContext>
763+
) => RequestServerNextFnResult<TRegister, TMiddlewares, TServerContext>
764764

765765
export interface RequestServerNextFnOptions<TServerContext> {
766766
context?: TServerContext
767767
}
768768

769+
export type RequestServerNextFnResult<TRegister, TMiddlewares, TServerContext> =
770+
771+
| Promise<RequestServerResult<TRegister, TMiddlewares, TServerContext>>
772+
| RequestServerResult<TRegister, TMiddlewares, TServerContext>
773+
769774
export type RequestMiddlewareServerFnResult<
770775
TRegister,
771776
TMiddlewares,
772777
TServerContext,
773778
> =
774-
| Promise<RequestServerResult<TRegister, TMiddlewares, TServerContext>>
779+
| Promise<
780+
RequestServerResult<TRegister, TMiddlewares, TServerContext> | Response
781+
>
775782
| RequestServerResult<TRegister, TMiddlewares, TServerContext>
783+
| Response
776784

777785
export interface RequestServerResult<TRegister, TMiddlewares, TServerContext> {
778786
request: Request

packages/start-client-core/src/tests/createServerMiddleware.test-d.ts

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,3 +741,53 @@ test('createMiddleware with type request, middleware and context', () => {
741741
return result
742742
})
743743
})
744+
745+
test('createMiddleware with type request can return Response directly', () => {
746+
createMiddleware({ type: 'request' }).server(async (options) => {
747+
expectTypeOf(options).toEqualTypeOf<{
748+
request: Request
749+
next: RequestServerNextFn<{}, undefined>
750+
pathname: string
751+
context: undefined
752+
}>()
753+
754+
// Should be able to return a Response directly
755+
if (Math.random() > 0.5) {
756+
return new Response('Unauthorized', { status: 401 })
757+
}
758+
759+
// Or return the result from next()
760+
return options.next()
761+
})
762+
})
763+
764+
test('createMiddleware with type request can return Promise<Response>', () => {
765+
createMiddleware({ type: 'request' }).server(async (options) => {
766+
expectTypeOf(options).toEqualTypeOf<{
767+
request: Request
768+
next: RequestServerNextFn<{}, undefined>
769+
pathname: string
770+
context: undefined
771+
}>()
772+
773+
// Should be able to return a Promise<Response>
774+
return Promise.resolve(new Response('OK', { status: 200 }))
775+
})
776+
})
777+
778+
test('createMiddleware with type request can return sync Response', () => {
779+
createMiddleware({ type: 'request' }).server((options) => {
780+
expectTypeOf(options).toEqualTypeOf<{
781+
request: Request
782+
next: RequestServerNextFn<{}, undefined>
783+
pathname: string
784+
context: undefined
785+
}>()
786+
787+
// Should be able to return a synchronous Response
788+
return new Response(JSON.stringify({ error: 'Not Found' }), {
789+
status: 404,
790+
headers: { 'Content-Type': 'application/json' },
791+
})
792+
})
793+
})

0 commit comments

Comments
 (0)