Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion packages/start-client-core/src/createMiddleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -771,8 +771,11 @@ export type RequestMiddlewareServerFnResult<
TMiddlewares,
TServerContext,
> =
| Promise<RequestServerResult<TRegister, TMiddlewares, TServerContext>>
| Promise<
RequestServerResult<TRegister, TMiddlewares, TServerContext> | Response
>
| RequestServerResult<TRegister, TMiddlewares, TServerContext>
| Response

export interface RequestServerResult<TRegister, TMiddlewares, TServerContext> {
request: Request
Expand Down
110 changes: 86 additions & 24 deletions packages/start-client-core/src/tests/createServerMiddleware.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -663,12 +663,15 @@ test('createMiddleware with type request, no middleware or context', () => {

const result = await options.next()

expectTypeOf(result).toEqualTypeOf<{
context: undefined
pathname: string
request: Request
response: Response
}>()
expectTypeOf(result).toEqualTypeOf<
| {
context: undefined
pathname: string
request: Request
response: Response
}
| Response
>()

return result
})
Expand All @@ -685,12 +688,15 @@ test('createMiddleware with type request, no middleware with context', () => {

const result = await options.next({ context: { a: 'a' } })

expectTypeOf(result).toEqualTypeOf<{
context: { a: string }
pathname: string
request: Request
response: Response
}>()
expectTypeOf(result).toEqualTypeOf<
| {
context: { a: string }
pathname: string
request: Request
response: Response
}
| Response
>()

return result
})
Expand All @@ -708,12 +714,15 @@ test('createMiddleware with type request, middleware and context', () => {

const result = await options.next({ context: { a: 'a' } })

expectTypeOf(result).toEqualTypeOf<{
context: { a: string }
pathname: string
request: Request
response: Response
}>()
expectTypeOf(result).toEqualTypeOf<
| {
context: { a: string }
pathname: string
request: Request
response: Response
}
| Response
>()

return result
},
Expand All @@ -731,13 +740,66 @@ test('createMiddleware with type request, middleware and context', () => {

const result = await options.next({ context: { b: 'b' } })

expectTypeOf(result).toEqualTypeOf<{
context: { a: string; b: string }
pathname: string
request: Request
response: Response
}>()
expectTypeOf(result).toEqualTypeOf<
| {
context: { a: string; b: string }
pathname: string
request: Request
response: Response
}
| Response
>()

return result
})
})

test('createMiddleware with type request can return Response directly', () => {
createMiddleware({ type: 'request' }).server(async (options) => {
expectTypeOf(options).toEqualTypeOf<{
request: Request
next: RequestServerNextFn<{}, undefined>
pathname: string
context: undefined
}>()

// Should be able to return a Response directly
if (Math.random() > 0.5) {
return new Response('Unauthorized', { status: 401 })
}

// Or return the result from next()
return options.next()
})
})

test('createMiddleware with type request can return Promise<Response>', () => {
createMiddleware({ type: 'request' }).server(async (options) => {
expectTypeOf(options).toEqualTypeOf<{
request: Request
next: RequestServerNextFn<{}, undefined>
pathname: string
context: undefined
}>()

// Should be able to return a Promise<Response>
return Promise.resolve(new Response('OK', { status: 200 }))
})
})

test('createMiddleware with type request can return sync Response', () => {
createMiddleware({ type: 'request' }).server((options) => {
expectTypeOf(options).toEqualTypeOf<{
request: Request
next: RequestServerNextFn<{}, undefined>
pathname: string
context: undefined
}>()

// Should be able to return a synchronous Response
return new Response(JSON.stringify({ error: 'Not Found' }), {
status: 404,
headers: { 'Content-Type': 'application/json' },
})
})
})
Loading