Skip to content

Commit b732c8a

Browse files
authored
fix: server middleware type in solid-router (#7260)
1 parent d20c878 commit b732c8a

3 files changed

Lines changed: 93 additions & 1 deletion

File tree

‎.changeset/curvy-chairs-study.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@tanstack/solid-router': patch
3+
---
4+
5+
fix server middleware type in solid-router

‎packages/solid-router/src/fileRoute.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ export class FileRoute<
8181
TLoaderFn = undefined,
8282
TChildren = unknown,
8383
TSSR = unknown,
84-
TMiddlewares = unknown,
84+
const TMiddlewares = unknown,
8585
THandlers = undefined,
8686
>(
8787
options?: FileBaseRouteOptions<

‎packages/solid-router/tests/fileRoute.test-d.tsx‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,53 @@
11
import { expectTypeOf, test } from 'vitest'
22
import { createFileRoute, createRootRoute } from '../src'
3+
import type { Route } from '@tanstack/router-core'
4+
5+
declare module '@tanstack/router-core' {
6+
interface FilebaseRouteOptionsInterface<
7+
TRegister,
8+
TParentRoute,
9+
TId,
10+
TPath,
11+
TSearchValidator,
12+
TParams,
13+
TLoaderDeps,
14+
TLoaderFn,
15+
TRouterContext,
16+
TRouteContextFn,
17+
TBeforeLoadFn,
18+
TRemountDepsFn,
19+
TSSR,
20+
TServerMiddlewares,
21+
THandlers,
22+
> {
23+
server?: {
24+
middleware?: TServerMiddlewares
25+
}
26+
}
27+
28+
interface RouteTypes<
29+
TRegister,
30+
TParentRoute,
31+
TPath,
32+
TFullPath,
33+
TCustomId,
34+
TId,
35+
TSearchValidator,
36+
TParams,
37+
TRouterContext,
38+
TRouteContextFn,
39+
TBeforeLoadFn,
40+
TLoaderDeps,
41+
TLoaderFn,
42+
TChildren,
43+
TFileRouteTypes,
44+
TSSR,
45+
TServerMiddlewares,
46+
THandlers,
47+
> {
48+
middleware: TServerMiddlewares
49+
}
50+
}
351

452
const rootRoute = createRootRoute()
553

@@ -100,3 +148,42 @@ test('when creating a folder group', () => {
100148
expectTypeOf<'(auth)/protected'>(protectedRoute.path)
101149
expectTypeOf<'/protected'>(protectedRoute.id)
102150
})
151+
152+
test('when creating a file route with middleware options', () => {
153+
type Middleware = { readonly middleware: 'middleware' }
154+
const middleware = { middleware: 'middleware' } as const
155+
156+
const route = createFileRoute('/invoices')({
157+
server: {
158+
middleware: [middleware],
159+
},
160+
})
161+
162+
type ExtractMiddlewares<TRoute> =
163+
TRoute extends Route<
164+
any,
165+
any,
166+
any,
167+
any,
168+
any,
169+
any,
170+
any,
171+
any,
172+
any,
173+
any,
174+
any,
175+
any,
176+
any,
177+
any,
178+
any,
179+
any,
180+
infer TMiddlewares,
181+
any
182+
>
183+
? TMiddlewares
184+
: never
185+
186+
expectTypeOf<ExtractMiddlewares<typeof route>>().toEqualTypeOf<
187+
readonly [Middleware]
188+
>()
189+
})

0 commit comments

Comments
 (0)