Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

serverRoute.test-d.ts

289 lines7.0 KB

1import { test } from 'vitest'
2​
3test('dummy', () => {})
4/*
5import { expectTypeOf, test } from 'vitest'
6import { json } from '../../router-core/src/ssr/json'
7import '../../start-client-core/src/serverRoute'
8import { createMiddleware } from '../../start-client-core/src/createMiddleware'
9import { createFileRoute as defaultCreateFileRoute } from '../../react-router/src/fileRoute'
10import type { CreateFileRoute } from '../../router-core/src/fileRoute'
11​
12test('createServerFileRoute with methods with no middleware', () => {
13 type Path = '$detailId'
14 const createFileRoute: CreateFileRoute<Path, any, Path, Path, Path> =
15 defaultCreateFileRoute as never
16​
17 const options: Parameters<typeof createFileRoute>[0] = {
18 methods: {
19 GET: (ctx) => {
20 expectTypeOf(ctx).toEqualTypeOf<{
21 context: undefined
22 params: { detailId: string }
23 pathname: '$detailId'
24 request: Request
25 }>()
26 },
27 },
28 }
29​
30 const fileRoute = createFileRoute()
31​
32 expectTypeOf(fileRoute.methods).toBeFunction()
33 expectTypeOf(fileRoute.middleware).toBeFunction()
34​
35 fileRoute.methods({
36 GET: (ctx) => {
37 expectTypeOf(ctx).toEqualTypeOf<{
38 context: undefined
39 params: { detailId: string }
40 pathname: '$detailId'
41 request: Request
42 }>()
43​
44 return json({
45 test: 'test',
46 })
47 },
48 })
49​
50 fileRoute.methods((r) => ({
51 GET: r.handler((ctx) => {
52 expectTypeOf(ctx).toEqualTypeOf<{
53 context: undefined
54 params: { detailId: string }
55 pathname: '$detailId'
56 request: Request
57 }>()
58​
59 return json({
60 test: 'test',
61 })
62 }),
63 }))
64})
65​
66test('createServerFileRoute with methods and route middleware context', () => {
67 type Path = '$detailId'
68 const createFileRoute: CreateFileRoute<Path, any, Path, Path, Path> =
69 defaultCreateFileRoute as never
70​
71 const routeMiddleware = createMiddleware({ type: 'request' }).server(
72 ({ next }) => next({ context: { a: 'a' } }),
73 )
74​
75 const serverFileRoute = createFileRoute().middleware([routeMiddleware])
76​
77 fileRoute.methods({
78 GET: (ctx) => {
79 expectTypeOf(ctx).toEqualTypeOf<{
80 context: { a: string }
81 params: { detailId: string }
82 pathname: '$detailId'
83 request: Request
84 }>()
85​
86 return json({
87 test: 'test',
88 })
89 },
90 })
91​
92 fileRoute.methods((r) => ({
93 GET: r.handler((ctx) => {
94 expectTypeOf(ctx).toEqualTypeOf<{
95 context: { a: string }
96 params: { detailId: string }
97 pathname: '$detailId'
98 request: Request
99 }>()
100​
101 return json({
102 test: 'test',
103 })
104 }),
105 }))
106})
107​
108test('createServerFileRoute with methods middleware and route middleware', () => {
109 type Path = '$detailId'
110 const createFileRoute: CreateFileRoute<Path, any, Path, Path, Path> =
111 defaultCreateFileRoute as never
112​
113 const routeMiddleware = createMiddleware({ type: 'request' }).server(
114 ({ next }) => next({ context: { a: 'a' } }),
115 )
116​
117 const fileRoute = createFileRoute().middleware([routeMiddleware])
118​
119 const methodMiddleware = createMiddleware({ type: 'request' }).server(
120 ({ next }) => next({ context: { b: 'b' } }),
121 )
122​
123 fileRoute.methods((r) => ({
124 GET: r.middleware([methodMiddleware]).handler((ctx) => {
125 expectTypeOf(ctx).toEqualTypeOf<{
126 context: { a: string; b: string }
127 params: { detailId: string }
128 pathname: '$detailId'
129 request: Request
130 }>()
131​
132 return json({
133 test: 'test',
134 })
135 }),
136 }))
137})
138​
139test('createServerFileRoute with a parent middleware context', () => {
140 const createDetailsFileRoute: CreateFileRoute<
141 'details',
142 any,
143 'details',
144 'details',
145 'details'
146 > = defaultCreateFileRoute as never
147​
148 const routeMiddleware1 = createMiddleware({ type: 'request' }).server(
149 ({ next }) => {
150 return next({ context: { a: 'a' } })
151 },
152 )
153​
154 const _detailsRoute = createDetailsFileRoute().middleware([routeMiddleware1])
155​
156 const createDetailFileRoute: CreateFileRoute<
157 'details/$detailId',
158 typeof _detailsRoute,
159 '$detailId',
160 'details/$detailId',
161 'details/$detailId'
162 > = defaultCreateFileRoute as never
163​
164 const routeMiddleware2 = createMiddleware({ type: 'request' }).server(
165 ({ next }) => {
166 return next({ context: { b: 'b' } })
167 },
168 )
169​
170 createDetailFileRoute()
171 .middleware([routeMiddleware2])
172 .methods({
173 GET: (ctx) => {
174 expectTypeOf(ctx).toEqualTypeOf<{
175 context: { a: string; b: string }
176 params: { detailId: string }
177 pathname: 'details/$detailId'
178 request: Request
179 }>()
180​
181 return json({ test: 'test' })
182 },
183 })
184​
185 const methodMiddleware = createMiddleware({ type: 'request' }).server(
186 ({ next }) => {
187 return next({ context: { c: 'c' } })
188 },
189 )
190​
191 createDetailFileRoute()
192 .middleware([routeMiddleware2])
193 .methods((r) => ({
194 GET: r.middleware([methodMiddleware]).handler((ctx) => {
195 expectTypeOf(ctx).toEqualTypeOf<{
196 context: { a: string; b: string; c: string }
197 params: { detailId: string }
198 pathname: 'details/$detailId'
199 request: Request
200 }>()
201​
202 return json({ test: 'test' })
203 }),
204 }))
205})
206​
207test('createServerFileRoute with parent middleware params', () => {
208 const createDetailsFileRoute: CreateFileRoute<
209 '$userId',
210 any,
211 '$userId',
212 '$userId',
213 '$userId'
214 > = defaultCreateFileRoute as never
215​
216 const _detailsServerRoute = createDetailsFileRoute()
217​
218 const createDetailFileRoute: CreateFileRoute<
219 '$userId/$detailId',
220 typeof _detailsServerRoute,
221 '$detailId',
222 '$userId/$detailId',
223 '$userId/$detailId'
224 > = defaultCreateFileRoute as never
225​
226 createDetailFileRoute().methods({
227 GET: (ctx) => {
228 expectTypeOf(ctx).toEqualTypeOf<{
229 context: undefined
230 params: { userId: string; detailId: string }
231 pathname: '$userId/$detailId'
232 request: Request
233 }>()
234​
235 return json({ test: 'test' })
236 },
237 })
238​
239 createDetailFileRoute().methods((r) => ({
240 GET: r.handler((ctx) => {
241 expectTypeOf(ctx).toEqualTypeOf<{
242 context: undefined
243 params: { userId: string; detailId: string }
244 pathname: '$userId/$detailId'
245 request: Request
246 }>()
247​
248 return json({ test: 'test' })
249 }),
250 }))
251})
252​
253test('createServerFileRoute with no params', () => {
254 const createDetailsFileRoute: CreateFileRoute<
255 'details',
256 any,
257 'details',
258 'details',
259 'details'
260 > = defaultCreateFileRoute as never
261​
262 createDetailsFileRoute().methods({
263 GET: (ctx) => {
264 expectTypeOf(ctx).toEqualTypeOf<{
265 context: undefined
266 params: {}
267 pathname: 'details'
268 request: Request
269 }>()
270​
271 return json({ test: 'test' })
272 },
273 })
274​
275 createDetailsFileRoute().methods((r) => ({
276 GET: r.handler((ctx) => {
277 expectTypeOf(ctx).toEqualTypeOf<{
278 context: undefined
279 params: {}
280 pathname: 'details'
281 request: Request
282 }>()
283​
284 return json({ test: 'test' })
285 }),
286 }))
287})
288​
289*/