Open History to see the last commit
1import { expectTypeOf, test } from 'vitest'
2import { createMiddleware } from '../createMiddleware'
3import type { RequestServerNextFn } from '../createMiddleware'
4import type { ConstrainValidator, CustomFetch } from '../createServerFn'
5import type { Register, SerializationError } from '@tanstack/router-core'
6import type { ServerFnMeta } from '../constants'
7β
8test('createServeMiddleware removes middleware after middleware,', () => {
9 const middleware = createMiddleware({ type: 'function' })
10β
11 expectTypeOf(middleware).toHaveProperty('middleware')
12 expectTypeOf(middleware).toHaveProperty('server')
13 expectTypeOf(middleware).toHaveProperty('validator')
14 // TODO remove upon stable
15 expectTypeOf(middleware).toHaveProperty('inputValidator')
16β
17 const middlewareAfterMiddleware = middleware.middleware([])
18β
19 expectTypeOf(middlewareAfterMiddleware).toHaveProperty('validator')
20 expectTypeOf(middlewareAfterMiddleware).toHaveProperty('server')
21 expectTypeOf(middlewareAfterMiddleware).not.toHaveProperty('middleware')
22β
23 const middlewareAfterInput = middleware.validator(() => {})
24β
25 expectTypeOf(middlewareAfterInput).toHaveProperty('server')
26 expectTypeOf(middlewareAfterInput).not.toHaveProperty('validator')
27 expectTypeOf(middlewareAfterInput).not.toHaveProperty('middleware')
28β
29 const middlewareAfterServer = middleware.server(async (options) => {
30 expectTypeOf(options.context).toEqualTypeOf<undefined>()
31 expectTypeOf(options.data).toEqualTypeOf<undefined>()
32 expectTypeOf(options.method).toEqualTypeOf<'GET' | 'POST'>()
33β
34 const result = await options.next({
35 context: {
36 a: 'a',
37 },
38 })
39β
40 expectTypeOf(result.context).toEqualTypeOf<{ a: string }>()
41β
42 expectTypeOf(result.sendContext).toEqualTypeOf<undefined>()
43β
44 return result
45 })
46β
47 expectTypeOf(middlewareAfterServer).not.toHaveProperty('server')
48 expectTypeOf(middlewareAfterServer).not.toHaveProperty('input')
49 expectTypeOf(middlewareAfterServer).not.toHaveProperty('validator')
50 expectTypeOf(middlewareAfterServer).not.toHaveProperty('middleware')
51})
52β
53test('createMiddleware merges server context', () => {
54 const middleware1 = createMiddleware({ type: 'function' }).server(
55 async (options) => {
56 expectTypeOf(options.context).toEqualTypeOf<undefined>()
57 expectTypeOf(options.data).toEqualTypeOf<undefined>()
58 expectTypeOf(options.method).toEqualTypeOf<'GET' | 'POST'>()
59β
60 const result = await options.next({ context: { a: true } })
61β
62 expectTypeOf(result).toEqualTypeOf<{
63 'use functions must return the result of next()': true
64 '~types': {
65 context: {
66 a: boolean
67 }
68 sendContext: undefined
69 }
70 context: { a: boolean }
71 sendContext: undefined
72 }>()
73β
74 return result
75 },
76 )
77β
78 const middleware2 = createMiddleware({ type: 'function' }).server(
79 async (options) => {
80 expectTypeOf(options.context).toEqualTypeOf<undefined>()
81 expectTypeOf(options.data).toEqualTypeOf<undefined>()
82 expectTypeOf(options.method).toEqualTypeOf<'GET' | 'POST'>()
83β
84 const result = await options.next({ context: { b: 'test' } })
85β
86 expectTypeOf(result).toEqualTypeOf<{
87 'use functions must return the result of next()': true
88 '~types': {
89 context: {
90 b: string
91 }
92 sendContext: undefined
93 }
94 context: { b: string }
95 sendContext: undefined
96 }>()
97β
98 return result
99 },
100 )
101β
102 const middleware3 = createMiddleware({ type: 'function' })
103 .middleware([middleware1, middleware2])
104 .server(async (options) => {
105 expectTypeOf(options.context).toEqualTypeOf<{ a: boolean; b: string }>()
106β
107 const result = await options.next({ context: { c: 0 } })
108β
109 expectTypeOf(result).toEqualTypeOf<{
110 'use functions must return the result of next()': true
111 '~types': {
112 context: {
113 c: number
114 }
115 sendContext: undefined
116 }
117 context: { a: boolean; b: string; c: number }
118 sendContext: undefined
119 }>()
120β
121 return result
122 })
123β
124 createMiddleware({ type: 'function' })
125 .middleware([middleware3])
126 .server(async (options) => {
127 expectTypeOf(options.context).toEqualTypeOf<{
128 a: boolean
129 b: string
130 c: number
131 }>()
132β
133 const result = await options.next({ context: { d: 5 } })
134β
135 expectTypeOf(result).toEqualTypeOf<{
136 'use functions must return the result of next()': true
137 '~types': {
138 context: {
139 d: number
140 }
141 sendContext: undefined
142 }
143 context: { a: boolean; b: string; c: number; d: number }
144 sendContext: undefined
145 }>()
146β
147 return result
148 })
149})
150β
151test('createMiddleware merges client context and sends to the server', () => {
152 const middleware1 = createMiddleware({ type: 'function' }).client(
153 async (options) => {
154 expectTypeOf(options.context).toEqualTypeOf<undefined>()
155β
156 const result = await options.next({ context: { a: true } })
157β
158 expectTypeOf(result).toEqualTypeOf<{
159 'use functions must return the result of next()': true
160 context: { a: boolean }
161 sendContext: undefined
162 headers: HeadersInit
163 fetch?: CustomFetch
164 }>()
165β
166 return result
167 },
168 )
169β
170 const middleware2 = createMiddleware({ type: 'function' }).client(
171 async (options) => {
172 expectTypeOf(options.context).toEqualTypeOf<undefined>()
173β
174 const result = await options.next({ context: { b: 'test' } })
175β
176 expectTypeOf(result).toEqualTypeOf<{
177 'use functions must return the result of next()': true
178 context: { b: string }
179 sendContext: undefined
180 headers: HeadersInit
181 fetch?: CustomFetch
182 }>()
183β
184 return result
185 },
186 )
187β
188 const middleware3 = createMiddleware({ type: 'function' })
189 .middleware([middleware1, middleware2])
190 .client(async (options) => {
191 expectTypeOf(options.context).toEqualTypeOf<{ a: boolean; b: string }>()
192β
193 const result = await options.next({ context: { c: 0 } })
194β
195 expectTypeOf(result).toEqualTypeOf<{
196 'use functions must return the result of next()': true
197 context: { a: boolean; b: string; c: number }
198 sendContext: undefined
199 headers: HeadersInit
200 fetch?: CustomFetch
201 }>()
202β
203 return result
204 })
205β
206 const middleware4 = createMiddleware({ type: 'function' })
207 .middleware([middleware3])
208 .client(async (options) => {
209 expectTypeOf(options.context).toEqualTypeOf<{
210 a: boolean
211 b: string
212 c: number
213 }>()
214β
215 const result = await options.next({
216 sendContext: { ...options.context, d: 5 },
217 })
218β
219 expectTypeOf(result).toEqualTypeOf<{
220 'use functions must return the result of next()': true
221 context: { a: boolean; b: string; c: number }
222 sendContext: { a: boolean; b: string; c: number; d: 5 }
223 headers: HeadersInit
224 fetch?: CustomFetch
225 }>()
226β
227 return result
228 })
229β
230 createMiddleware({ type: 'function' })
231 .middleware([middleware4])
232 .server(async (options) => {
233 expectTypeOf(options.context).toEqualTypeOf<{
234 a: boolean
235 b: string
236 c: number
237 d: 5
238 }>()
239β
240 const result = await options.next({
241 context: {
242 e: 'e',
243 },
244 })
245β
246 expectTypeOf(result).toEqualTypeOf<{
247 'use functions must return the result of next()': true
248 '~types': {
249 context: {
250 e: string
251 }
252 sendContext: undefined
253 }
254 context: { a: boolean; b: string; c: number; d: 5; e: string }
255 sendContext: undefined
256 }>()
257β
258 return result
259 })
260})
261β
262test('createMiddleware merges input', () => {
263 const middleware1 = createMiddleware({ type: 'function' })
264 .validator(() => {
265 return {
266 a: 'a',
267 } as const
268 })
269 .server(({ data, next }) => {
270 expectTypeOf(data).toEqualTypeOf<{ readonly a: 'a' }>()
271 return next()
272 })
273β
274 const middleware2 = createMiddleware({ type: 'function' })
275 .middleware([middleware1])
276 .validator(() => {
277 return {
278 b: 'b',
279 } as const
280 })
281 .server(({ data, next }) => {
282 expectTypeOf(data).toEqualTypeOf<{ readonly a: 'a'; readonly b: 'b' }>
283 return next()
284 })
285β
286 createMiddleware({ type: 'function' })
287 .middleware([middleware2])
288 .validator(() => ({ c: 'c' }) as const)
289 .server(({ next, data }) => {
290 expectTypeOf(data).toEqualTypeOf<{
291 readonly a: 'a'
292 readonly b: 'b'
293 readonly c: 'c'
294 }>
295 return next()
296 })
297})
298β
299test('createMiddleware merges server context and client context, sends server context to the client and merges ', () => {
300 const middleware1 = createMiddleware({ type: 'function' })
301 .client(async (options) => {
302 expectTypeOf(options.context).toEqualTypeOf<undefined>()
303β
304 const result = await options.next({
305 context: { fromClient1: 'fromClient1' },
306 })
307β
308 expectTypeOf(result).toEqualTypeOf<{
309 'use functions must return the result of next()': true
310 context: { fromClient1: string }
311 sendContext: undefined
312 headers: HeadersInit
313 fetch?: CustomFetch
314 }>()
315β
316 return result
317 })
318 .server(async (options) => {
319 expectTypeOf(options.context).toEqualTypeOf<undefined>()
320β
321 const result = await options.next({
322 context: { fromServer1: 'fromServer1' },
323 })
324β
325 expectTypeOf(result).toEqualTypeOf<{
326 'use functions must return the result of next()': true
327 '~types': {
328 context: {
329 fromServer1: string
330 }
331 sendContext: undefined
332 }
333 context: { fromServer1: string }
334 sendContext: undefined
335 }>()
336β
337 return result
338 })
339β
340 const middleware2 = createMiddleware({ type: 'function' })
341 .client(async (options) => {
342 expectTypeOf(options.context).toEqualTypeOf<undefined>()
343β
344 const result = await options.next({
345 context: { fromClient2: 'fromClient2' },
346 })
347β
348 expectTypeOf(result).toEqualTypeOf<{
349 'use functions must return the result of next()': true
350 context: { fromClient2: string }
351 sendContext: undefined
352 headers: HeadersInit
353 fetch?: CustomFetch
354 }>()
355β
356 return result
357 })
358 .server(async (options) => {
359 expectTypeOf(options.context).toEqualTypeOf<undefined>()
360β
361 const result = await options.next({
362 context: { fromServer2: 'fromServer2' },
363 })
364β
365 expectTypeOf(result).toEqualTypeOf<{
366 'use functions must return the result of next()': true
367 '~types': {
368 context: {
369 fromServer2: string
370 }
371 sendContext: undefined
372 }
373 context: { fromServer2: string }
374 sendContext: undefined
375 }>()
376β
377 return result
378 })
379β
380 const middleware3 = createMiddleware({ type: 'function' })
381 .middleware([middleware1, middleware2])
382 .client(async (options) => {
383 expectTypeOf(options.context).toEqualTypeOf<{
384 fromClient1: string
385 fromClient2: string
386 }>()
387β
388 const result = await options.next({
389 context: { fromClient3: 'fromClient3' },
390 })
391β
392 expectTypeOf(result).toEqualTypeOf<{
393 'use functions must return the result of next()': true
394 context: {
395 fromClient1: string
396 fromClient2: string
397 fromClient3: string
398 }
399 sendContext: undefined
400 headers: HeadersInit
401 fetch?: CustomFetch
402 }>()
403β
404 return result
405 })
406 .server(async (options) => {
407 expectTypeOf(options.context).toEqualTypeOf<{
408 fromServer1: string
409 fromServer2: string
410 }>()
411β
412 const result = await options.next({
413 context: { fromServer3: 'fromServer3' },
414 })
415β
416 expectTypeOf(result).toEqualTypeOf<{
417 'use functions must return the result of next()': true
418 '~types': {
419 context: {
420 fromServer3: string
421 }
422 sendContext: undefined
423 }
424 context: {
425 fromServer1: string
426 fromServer2: string
427 fromServer3: string
428 }
429 sendContext: undefined
430 }>()
431β
432 return result
433 })
434β
435 const middleware4 = createMiddleware({ type: 'function' })
436 .middleware([middleware3])
437 .client(async (options) => {
438 expectTypeOf(options.context).toEqualTypeOf<{
439 fromClient1: string
440 fromClient2: string
441 fromClient3: string
442 }>()
443β
444 const result = await options.next({
445 context: { fromClient4: 'fromClient4' },
446 sendContext: { toServer1: 'toServer1' },
447 })
448β
449 expectTypeOf(result).toEqualTypeOf<{
450 'use functions must return the result of next()': true
451 context: {
452 fromClient1: string
453 fromClient2: string
454 fromClient3: string
455 fromClient4: string
456 }
457 sendContext: { toServer1: 'toServer1' }
458 headers: HeadersInit
459 fetch?: CustomFetch
460 }>()
461β
462 return result
463 })
464 .server(async (options) => {
465 expectTypeOf(options.context).toEqualTypeOf<{
466 fromServer1: string
467 fromServer2: string
468 fromServer3: string
469 toServer1: 'toServer1'
470 }>()
471β
472 const result = await options.next({
473 context: { fromServer4: 'fromServer4' },
474 sendContext: { toClient1: 'toClient1' },
475 })
476β
477 expectTypeOf(result).toEqualTypeOf<{
478 'use functions must return the result of next()': true
479 '~types': {
480 context: {
481 fromServer4: string
482 }
483 sendContext: {
484 toClient1: 'toClient1'
485 }
486 }
487 context: {
488 fromServer1: string
489 fromServer2: string
490 fromServer3: string
491 fromServer4: string
492 toServer1: 'toServer1'
493 }
494 sendContext: { toClient1: 'toClient1' }
495 }>()
496β
497 return result
498 })
499β
500 createMiddleware({ type: 'function' })
501 .middleware([middleware4])
502 .client(async (options) => {
503 expectTypeOf(options.context).toEqualTypeOf<{
504 fromClient1: string
505 fromClient2: string
506 fromClient3: string
507 fromClient4: string
508 }>()
509β
510 const result = await options.next({
511 context: { fromClient5: 'fromClient5' },
512 sendContext: { toServer2: 'toServer2' },
513 })
514β
515 expectTypeOf(result).toEqualTypeOf<{
516 'use functions must return the result of next()': true
517 context: {
518 fromClient1: string
519 fromClient2: string
520 fromClient3: string
521 fromClient4: string
522 fromClient5: string
523 toClient1: 'toClient1'
524 }
525 sendContext: { toServer1: 'toServer1'; toServer2: 'toServer2' }
526 headers: HeadersInit
527 fetch?: CustomFetch
528 }>()
529β
530 return result
531 })
532 .server(async (options) => {
533 expectTypeOf(options.context).toEqualTypeOf<{
534 fromServer1: string
535 fromServer2: string
536 fromServer3: string
537 fromServer4: string
538 toServer1: 'toServer1'
539 toServer2: 'toServer2'
540 }>()
541β
542 const result = await options.next({
543 context: { fromServer5: 'fromServer5' },
544 sendContext: { toClient2: 'toClient2' },
545 })
546β
547 expectTypeOf(result).toEqualTypeOf<{
548 'use functions must return the result of next()': true
549 '~types': {
550 context: {
551 fromServer5: string
552 }
553 sendContext: {
554 toClient2: 'toClient2'
555 }
556 }
557 context: {
558 fromServer1: string
559 fromServer2: string
560 fromServer3: string
561 fromServer4: string
562 fromServer5: string
563 toServer1: 'toServer1'
564 toServer2: 'toServer2'
565 }
566 sendContext: { toClient1: 'toClient1'; toClient2: 'toClient2' }
567 }>()
568β
569 return result
570 })
571})
572β
573test('createMiddleware sendContext cannot send a function', () => {
574 createMiddleware({ type: 'function' })
575 .client(({ next }) => {
576 expectTypeOf(next<{ func: () => 'func' }>)
577 .parameter(0)
578 .exclude<undefined>()
579 .toHaveProperty('sendContext')
580 .toEqualTypeOf<
581 | { func: SerializationError<'Function may not be serializable'> }
582 | undefined
583 >()
584β
585 return next()
586 })
587 .server(({ next }) => {
588 expectTypeOf(next<undefined, { func: () => 'func' }>)
589 .parameter(0)
590 .exclude<undefined>()
591 .toHaveProperty('sendContext')
592 .toEqualTypeOf<
593 | { func: SerializationError<'Function may not be serializable'> }
594 | undefined
595 >()
596β
597 return next()
598 })
599})
600β
601test('createMiddleware cannot validate function', () => {
602 const validator = createMiddleware({ type: 'function' }).validator<
603 (input: { func: () => 'string' }) => { output: 'string' }
604 >
605β
606 expectTypeOf(validator)
607 .parameter(0)
608 .toEqualTypeOf<
609 ConstrainValidator<
610 Register,
611 'GET',
612 (input: { func: () => 'string' }) => { output: 'string' }
613 >
614 >()
615})
616β
617test('createMiddleware can validate Date', () => {
618 const validator = createMiddleware({ type: 'function' }).validator<
619 (input: Date) => { output: 'string' }
620 >
621β
622 expectTypeOf(validator)
623 .parameter(0)
624 .toEqualTypeOf<
625 ConstrainValidator<Register, 'GET', (input: Date) => { output: 'string' }>
626 >()
627})
628β
629test('createMiddleware can validate FormData', () => {
630 const validator = createMiddleware({ type: 'function' }).validator<
631 (input: FormData) => { output: 'string' }
632 >
633β
634 expectTypeOf(validator)
635 .parameter(0)
636 .toEqualTypeOf<
637 ConstrainValidator<
638 Register,
639 'GET',
640 (input: FormData) => { output: 'string' }
641 >
642 >()
643})
644β
645test('createMiddleware merging from parent with undefined validator', () => {
646 const middleware1 = createMiddleware({ type: 'function' }).validator(
647 (input: { test: string }) => input.test,
648 )
649β
650 createMiddleware({ type: 'function' })
651 .middleware([middleware1])
652 .server((ctx) => {
653 expectTypeOf(ctx.data).toEqualTypeOf<string>()
654β
655 return ctx.next()
656 })
657})
658β
659test('createMiddleware validator infers unknown for default input type', () => {
660 createMiddleware({ type: 'function' })
661 .validator((input) => {
662 expectTypeOf(input).toEqualTypeOf<unknown>()
663β
664 if (typeof input === 'number') return 'success' as const
665β
666 return 'failed' as const
667 })
668 .server(({ data, next }) => {
669 expectTypeOf(data).toEqualTypeOf<'success' | 'failed'>()
670β
671 return next()
672 })
673})
674β
675test('createMiddleware with type request, no middleware or context', () => {
676 createMiddleware({ type: 'request' }).server(async (options) => {
677 expectTypeOf(options).toEqualTypeOf<{
678 request: Request
679 next: RequestServerNextFn<{}, undefined>
680 pathname: string
681 context: undefined
682 handlerType: 'serverFn' | 'router'
683 serverFnMeta?: ServerFnMeta
684 }>()
685β
686 const result = await options.next()
687β
688 expectTypeOf(result).toEqualTypeOf<{
689 context: undefined
690 pathname: string
691 request: Request
692 response: Response
693 }>()
694β
695 return result
696 })
697})
698β
699test('createMiddleware with type request, no middleware with context', () => {
700 createMiddleware({ type: 'request' }).server(async (options) => {
701 expectTypeOf(options).toEqualTypeOf<{
702 request: Request
703 next: RequestServerNextFn<{}, undefined>
704 pathname: string
705 context: undefined
706 handlerType: 'serverFn' | 'router'
707 serverFnMeta?: ServerFnMeta
708 }>()
709β
710 const result = await options.next({ context: { a: 'a' } })
711β
712 expectTypeOf(result).toEqualTypeOf<{
713 context: { a: string }
714 pathname: string
715 request: Request
716 response: Response
717 }>()
718β
719 return result
720 })
721})
722β
723test('createMiddleware with type request, middleware and context', () => {
724 const middleware1 = createMiddleware({ type: 'request' }).server(
725 async (options) => {
726 expectTypeOf(options).toEqualTypeOf<{
727 request: Request
728 next: RequestServerNextFn<{}, undefined>
729 pathname: string
730 context: undefined
731 handlerType: 'serverFn' | 'router'
732 serverFnMeta?: ServerFnMeta
733 }>()
734β
735 const result = await options.next({ context: { a: 'a' } })
736β
737 expectTypeOf(result).toEqualTypeOf<{
738 context: { a: string }
739 pathname: string
740 request: Request
741 response: Response
742 }>()
743β
744 return result
745 },
746 )
747β
748 createMiddleware({ type: 'request' })
749 .middleware([middleware1])
750 .server(async (options) => {
751 expectTypeOf(options).toEqualTypeOf<{
752 request: Request
753 next: RequestServerNextFn<{}, undefined>
754 pathname: string
755 context: { a: string }
756 handlerType: 'serverFn' | 'router'
757 serverFnMeta?: ServerFnMeta
758 }>()
759β
760 const result = await options.next({ context: { b: 'b' } })
761β
762 expectTypeOf(result).toEqualTypeOf<{
763 context: { a: string; b: string }
764 pathname: string
765 request: Request
766 response: Response
767 }>()
768β
769 return result
770 })
771})
772β
773test('createMiddleware with type request can return Response directly', () => {
774 createMiddleware({ type: 'request' }).server(async (options) => {
775 expectTypeOf(options).toEqualTypeOf<{
776 request: Request
777 next: RequestServerNextFn<{}, undefined>
778 pathname: string
779 context: undefined
780 handlerType: 'serverFn' | 'router'
781 serverFnMeta?: ServerFnMeta
782 }>()
783β
784 // Should be able to return a Response directly
785 if (Math.random() > 0.5) {
786 return new Response('Unauthorized', { status: 401 })
787 }
788β
789 // Or return the result from next()
790 return options.next()
791 })
792})
793β
794test('createMiddleware with type request can return Promise<Response>', () => {
795 createMiddleware({ type: 'request' }).server(async (options) => {
796 expectTypeOf(options).toEqualTypeOf<{
797 request: Request
798 next: RequestServerNextFn<{}, undefined>
799 pathname: string
800 context: undefined
801 handlerType: 'serverFn' | 'router'
802 serverFnMeta?: ServerFnMeta
803 }>()
804β
805 // Should be able to return a Promise<Response>
806 return Promise.resolve(new Response('OK', { status: 200 }))
807 })
808})
809β
810test('createMiddleware with type request can return sync Response', () => {
811 createMiddleware({ type: 'request' }).server((options) => {
812 expectTypeOf(options).toEqualTypeOf<{
813 request: Request
814 next: RequestServerNextFn<{}, undefined>
815 pathname: string
816 context: undefined
817 handlerType: 'serverFn' | 'router'
818 serverFnMeta?: ServerFnMeta
819 }>()
820β
821 // Should be able to return a synchronous Response
822 return new Response(JSON.stringify({ error: 'Not Found' }), {
823 status: 404,
824 headers: { 'Content-Type': 'application/json' },
825 })
826 })
827})