Skip to content

Commit d3beb47

Browse files
refactor: types for server routes (#4272)
Co-authored-by: chorobin <chrishorobin@hotmail.com>
1 parent 423f6c0 commit d3beb47

File tree

3 files changed

+89
-38
lines changed

3 files changed

+89
-38
lines changed

packages/start-server-core/src/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ export { handleServerAction } from './server-functions-handler'
1919
export * from './h3'
2020

2121
export { createServerRoute, createServerFileRoute } from './serverRoute'
22-
export type { CreateServerFileRoute } from './serverRoute'
22+
export type { CreateServerFileRoute, FileRoutesByPath } from './serverRoute'

packages/start-server-core/src/serverRoute.ts

Lines changed: 80 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,23 @@ import type {
1212
AssignAllServerContext,
1313
} from '@tanstack/start-client-core'
1414

15-
type TODO = any
16-
1715
export function createServerFileRoute<
18-
TParentRoute extends AnyServerRouteWithTypes,
19-
TId extends RouteConstraints['TId'],
20-
TPath extends RouteConstraints['TPath'],
21-
TFullPath extends RouteConstraints['TFullPath'],
22-
TChildren,
23-
>(__?: never): ServerRoute<TParentRoute, TId, TPath, TFullPath, TChildren> {
16+
TFilePath extends keyof FileRoutesByPath,
17+
TParentRoute extends
18+
AnyServerRouteWithTypes = FileRoutesByPath[TFilePath]['parentRoute'],
19+
TId extends RouteConstraints['TId'] = FileRoutesByPath[TFilePath]['id'],
20+
TPath extends RouteConstraints['TPath'] = FileRoutesByPath[TFilePath]['path'],
21+
TFullPath extends
22+
RouteConstraints['TFullPath'] = FileRoutesByPath[TFilePath]['fullPath'],
23+
TChildren = FileRoutesByPath[TFilePath]['children'],
24+
>(_: TFilePath): ServerRoute<TParentRoute, TId, TPath, TFullPath, TChildren> {
2425
return createServerRoute<TParentRoute, TId, TPath, TFullPath, TChildren>(
2526
undefined,
2627
)
2728
}
2829

30+
export interface FileRoutesByPath {}
31+
2932
export interface ServerRouteOptions<
3033
TParentRoute extends AnyServerRouteWithTypes,
3134
TId extends RouteConstraints['TId'],
@@ -70,16 +73,29 @@ export function createServerRoute<
7073
id: '' as TId,
7174
fullPath: '' as TFullPath,
7275
to: '' as TrimPathRight<TFullPath>,
73-
options: options as TODO,
76+
options: options as ServerRouteOptions<
77+
TParentRoute,
78+
TId,
79+
TPath,
80+
TFullPath,
81+
any
82+
>,
7483
parentRoute: undefined as unknown as TParentRoute,
75-
_types: {} as TODO,
84+
_types: {} as ServerRouteTypes<
85+
TParentRoute,
86+
TId,
87+
TPath,
88+
TFullPath,
89+
undefined,
90+
undefined
91+
>,
7692
// children: undefined as TChildren,
77-
middleware: (middlewares: TODO) =>
93+
middleware: (middlewares) =>
7894
createServerRoute(undefined, {
7995
...options,
8096
middleware: middlewares,
81-
}) as TODO,
82-
methods: (methodsOrGetMethods: TODO) => {
97+
}) as never,
98+
methods: (methodsOrGetMethods) => {
8399
const methods = (() => {
84100
if (typeof methodsOrGetMethods === 'function') {
85101
return methodsOrGetMethods(createMethodBuilder())
@@ -90,14 +106,14 @@ export function createServerRoute<
90106

91107
return createServerRoute(undefined, {
92108
...__opts,
93-
methods,
94-
}) as TODO
109+
methods: methods as never,
110+
}) as never
95111
},
96112
update: (opts) =>
97113
createServerRoute(undefined, {
98114
...options,
99115
...opts,
100-
}) as TODO,
116+
}),
101117
init: (opts: { originalIndex: number }): void => {
102118
options.originalIndex = opts.originalIndex
103119

@@ -149,14 +165,14 @@ export function createServerRoute<
149165

150166
_addFileChildren: (children) => {
151167
if (Array.isArray(children)) {
152-
route.children = children as TChildren as TODO
168+
route.children = children as TChildren
153169
}
154170

155171
if (typeof children === 'object' && children !== null) {
156-
route.children = Object.values(children) as TChildren as TODO
172+
route.children = Object.values(children) as TChildren
157173
}
158174

159-
return route as any
175+
return route
160176
},
161177

162178
_addFileTypes: <TFileTypes>() => route,
@@ -190,33 +206,57 @@ const createMethodBuilder = <
190206
TFullPath extends string,
191207
TMiddlewares,
192208
>(
193-
__opts?: TODO,
209+
__opts?: ServerRouteMethodBuilderOptions<
210+
TParentRoute,
211+
TFullPath,
212+
TMiddlewares,
213+
unknown,
214+
unknown
215+
>,
194216
): ServerRouteMethodBuilder<TParentRoute, TFullPath, TMiddlewares> => {
195217
return {
196-
_options: __opts || {},
197-
_types: {} as TODO,
218+
_options: (__opts || {}) as never,
219+
_types: {} as never,
198220
middleware: (middlewares) =>
199221
createMethodBuilder({
200222
...__opts,
201223
middlewares,
202-
}) as TODO,
224+
}) as never,
203225
handler: (handler) =>
204226
createMethodBuilder({
205227
...__opts,
206-
handler,
207-
}) as TODO,
228+
handler: handler as never,
229+
}) as never,
208230
}
209231
}
210232

233+
export interface ServerRouteMethodBuilderOptions<
234+
TParentRoute extends AnyServerRouteWithTypes,
235+
TFullPath extends string,
236+
TMiddlewares,
237+
TMethodMiddlewares,
238+
TResponse,
239+
> {
240+
handler?: ServerRouteMethodHandlerFn<
241+
TParentRoute,
242+
TFullPath,
243+
TMiddlewares,
244+
TMethodMiddlewares,
245+
TResponse
246+
>
247+
middlewares?: Constrain<
248+
TMethodMiddlewares,
249+
ReadonlyArray<AnyRequestMiddleware>
250+
>
251+
}
252+
211253
export type CreateServerFileRoute<
212254
TParentRoute extends AnyServerRouteWithTypes,
213255
TId extends RouteConstraints['TId'],
214256
TPath extends RouteConstraints['TPath'],
215257
TFullPath extends RouteConstraints['TFullPath'],
216258
TChildren,
217-
> = (
218-
options?: undefined,
219-
) => ServerRoute<TParentRoute, TId, TPath, TFullPath, TChildren>
259+
> = () => ServerRoute<TParentRoute, TId, TPath, TFullPath, TChildren>
220260

221261
export type AnyServerRouteWithTypes = ServerRouteWithTypes<
222262
any,
@@ -497,6 +537,7 @@ export type AnyRouteMethodsBuilder = ServerRouteMethodBuilderWithTypes<
497537
any,
498538
any,
499539
any,
540+
any,
500541
any
501542
>
502543

@@ -505,6 +546,7 @@ export interface ServerRouteMethodBuilder<
505546
TFullPath extends string,
506547
TMiddlewares,
507548
> extends ServerRouteMethodBuilderWithTypes<
549+
TParentRoute,
508550
TFullPath,
509551
TMiddlewares,
510552
undefined,
@@ -519,12 +561,19 @@ export interface ServerRouteMethodBuilder<
519561
> {}
520562

521563
export interface ServerRouteMethodBuilderWithTypes<
564+
TParentRoute extends AnyServerRouteWithTypes,
522565
TFullPath extends string,
523566
TMiddlewares,
524567
TMethodMiddlewares,
525568
TResponse,
526569
> {
527-
_options: TODO
570+
_options: ServerRouteMethodBuilderOptions<
571+
TParentRoute,
572+
TFullPath,
573+
TMiddlewares,
574+
TMethodMiddlewares,
575+
TResponse
576+
>
528577
_types: ServerRouteMethodBuilderTypes<
529578
TFullPath,
530579
TMiddlewares,
@@ -569,6 +618,7 @@ export interface ServerRouteMethodBuilderAfterMiddleware<
569618
TMiddlewares,
570619
TMethodMiddlewares,
571620
> extends ServerRouteMethodBuilderWithTypes<
621+
TParentRoute,
572622
TFullPath,
573623
TMiddlewares,
574624
TMethodMiddlewares,
@@ -611,6 +661,7 @@ export interface ServerRouteMethodBuilderAfterHandler<
611661
TMethodMiddlewares,
612662
TResponse,
613663
> extends ServerRouteMethodBuilderWithTypes<
664+
TParentRoute,
614665
TFullPath,
615666
TMiddlewares,
616667
TMethodMiddlewares,

packages/start-server-core/tests/serverRoute.test-d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ test('createServerFileRoute with methods with no middleware', () => {
1212
Path,
1313
Path,
1414
unknown
15-
> = defaultCreateServerFileRoute
15+
> = defaultCreateServerFileRoute as never
1616

1717
const serverFileRoute = createServerFileRoute()
1818

@@ -58,7 +58,7 @@ test('createServerFileRoute with methods and route middleware context', () => {
5858
Path,
5959
Path,
6060
unknown
61-
> = defaultCreateServerFileRoute
61+
> = defaultCreateServerFileRoute as never
6262

6363
const routeMiddleware = createMiddleware({ type: 'request' }).server(
6464
({ next }) => next({ context: { a: 'a' } }),
@@ -105,7 +105,7 @@ test('createServerFileRoute with methods middleware and route middleware', () =>
105105
Path,
106106
Path,
107107
unknown
108-
> = defaultCreateServerFileRoute
108+
> = defaultCreateServerFileRoute as never
109109

110110
const routeMiddleware = createMiddleware({ type: 'request' }).server(
111111
({ next }) => next({ context: { a: 'a' } }),
@@ -140,7 +140,7 @@ test('createServerFileRoute with a parent middleware context', () => {
140140
'details',
141141
'details',
142142
unknown
143-
> = defaultCreateServerFileRoute
143+
> = defaultCreateServerFileRoute as never
144144

145145
const routeMiddleware1 = createMiddleware({ type: 'request' }).server(
146146
({ next }) => {
@@ -158,7 +158,7 @@ test('createServerFileRoute with a parent middleware context', () => {
158158
'$detailId',
159159
'details/$detailId',
160160
unknown
161-
> = defaultCreateServerFileRoute
161+
> = defaultCreateServerFileRoute as never
162162

163163
const routeMiddleware2 = createMiddleware({ type: 'request' }).server(
164164
({ next }) => {
@@ -210,7 +210,7 @@ test('createServerFileRoute with parent middleware params', () => {
210210
'$userId',
211211
'$userId',
212212
unknown
213-
> = defaultCreateServerFileRoute
213+
> = defaultCreateServerFileRoute as never
214214

215215
const _detailsServerRoute = createDetailsServerFileRoute()
216216

@@ -220,7 +220,7 @@ test('createServerFileRoute with parent middleware params', () => {
220220
'$detailId',
221221
'$userId/$detailId',
222222
unknown
223-
> = defaultCreateServerFileRoute
223+
> = defaultCreateServerFileRoute as never
224224

225225
createDetailServerFileRoute().methods({
226226
GET: (ctx) => {
@@ -256,7 +256,7 @@ test('createServerFileRoute with no params', () => {
256256
'details',
257257
'details',
258258
unknown
259-
> = defaultCreateServerFileRoute
259+
> = defaultCreateServerFileRoute as never
260260

261261
createDetailsServerFileRoute().methods({
262262
GET: (ctx) => {

0 commit comments

Comments
 (0)