@tanstack/start-client-core 1.120.7 → 1.121.0-alpha.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/README.md +6 -27
  2. package/dist/cjs/createMiddleware.cjs +8 -5
  3. package/dist/cjs/createMiddleware.cjs.map +1 -1
  4. package/dist/cjs/createMiddleware.d.cts +125 -81
  5. package/dist/cjs/createServerFn.cjs +1 -0
  6. package/dist/cjs/createServerFn.cjs.map +1 -1
  7. package/dist/cjs/createServerFn.d.cts +6 -5
  8. package/dist/cjs/index.cjs +1 -0
  9. package/dist/cjs/index.cjs.map +1 -1
  10. package/dist/cjs/index.d.cts +2 -2
  11. package/dist/cjs/registerGlobalMiddleware.cjs.map +1 -1
  12. package/dist/cjs/registerGlobalMiddleware.d.cts +3 -3
  13. package/dist/cjs/ssr-client.cjs +62 -61
  14. package/dist/cjs/ssr-client.cjs.map +1 -1
  15. package/dist/cjs/ssr-client.d.cts +2 -1
  16. package/dist/esm/createMiddleware.d.ts +125 -81
  17. package/dist/esm/createMiddleware.js +8 -5
  18. package/dist/esm/createMiddleware.js.map +1 -1
  19. package/dist/esm/createServerFn.d.ts +6 -5
  20. package/dist/esm/createServerFn.js +1 -0
  21. package/dist/esm/createServerFn.js.map +1 -1
  22. package/dist/esm/index.d.ts +2 -2
  23. package/dist/esm/index.js +2 -1
  24. package/dist/esm/registerGlobalMiddleware.d.ts +3 -3
  25. package/dist/esm/registerGlobalMiddleware.js.map +1 -1
  26. package/dist/esm/ssr-client.d.ts +2 -1
  27. package/dist/esm/ssr-client.js +62 -61
  28. package/dist/esm/ssr-client.js.map +1 -1
  29. package/package.json +2 -2
  30. package/src/createMiddleware.ts +474 -363
  31. package/src/createServerFn.ts +31 -16
  32. package/src/index.tsx +22 -21
  33. package/src/registerGlobalMiddleware.ts +3 -3
  34. package/src/ssr-client.tsx +75 -69
  35. package/src/tests/createServerFn.test-d.ts +20 -13
  36. package/src/tests/createServerMiddleware.test-d.ts +186 -73
@@ -1,10 +1,11 @@
1
1
  import { expectTypeOf, test } from 'vitest'
2
2
  import { createMiddleware } from '../createMiddleware'
3
+ import type { RequestServerNextFn } from '../createMiddleware'
3
4
  import type { Constrain, Validator } from '@tanstack/router-core'
4
5
  import type { ConstrainValidator } from '../createServerFn'
5
6
 
6
7
  test('createServeMiddleware removes middleware after middleware,', () => {
7
- const middleware = createMiddleware()
8
+ const middleware = createMiddleware({ type: 'function' })
8
9
 
9
10
  expectTypeOf(middleware).toHaveProperty('middleware')
10
11
  expectTypeOf(middleware).toHaveProperty('server')
@@ -45,51 +46,55 @@ test('createServeMiddleware removes middleware after middleware,', () => {
45
46
  })
46
47
 
47
48
  test('createMiddleware merges server context', () => {
48
- const middleware1 = createMiddleware().server(async (options) => {
49
- expectTypeOf(options.context).toEqualTypeOf<undefined>()
50
- expectTypeOf(options.data).toEqualTypeOf<undefined>()
51
- expectTypeOf(options.method).toEqualTypeOf<'GET' | 'POST'>()
49
+ const middleware1 = createMiddleware({ type: 'function' }).server(
50
+ async (options) => {
51
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
52
+ expectTypeOf(options.data).toEqualTypeOf<undefined>()
53
+ expectTypeOf(options.method).toEqualTypeOf<'GET' | 'POST'>()
52
54
 
53
- const result = await options.next({ context: { a: true } })
55
+ const result = await options.next({ context: { a: true } })
54
56
 
55
- expectTypeOf(result).toEqualTypeOf<{
56
- 'use functions must return the result of next()': true
57
- _types: {
58
- context: {
59
- a: boolean
57
+ expectTypeOf(result).toEqualTypeOf<{
58
+ 'use functions must return the result of next()': true
59
+ _types: {
60
+ context: {
61
+ a: boolean
62
+ }
63
+ sendContext: undefined
60
64
  }
65
+ context: { a: boolean }
61
66
  sendContext: undefined
62
- }
63
- context: { a: boolean }
64
- sendContext: undefined
65
- }>()
67
+ }>()
66
68
 
67
- return result
68
- })
69
+ return result
70
+ },
71
+ )
69
72
 
70
- const middleware2 = createMiddleware().server(async (options) => {
71
- expectTypeOf(options.context).toEqualTypeOf<undefined>()
72
- expectTypeOf(options.data).toEqualTypeOf<undefined>()
73
- expectTypeOf(options.method).toEqualTypeOf<'GET' | 'POST'>()
73
+ const middleware2 = createMiddleware({ type: 'function' }).server(
74
+ async (options) => {
75
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
76
+ expectTypeOf(options.data).toEqualTypeOf<undefined>()
77
+ expectTypeOf(options.method).toEqualTypeOf<'GET' | 'POST'>()
74
78
 
75
- const result = await options.next({ context: { b: 'test' } })
79
+ const result = await options.next({ context: { b: 'test' } })
76
80
 
77
- expectTypeOf(result).toEqualTypeOf<{
78
- 'use functions must return the result of next()': true
79
- _types: {
80
- context: {
81
- b: string
81
+ expectTypeOf(result).toEqualTypeOf<{
82
+ 'use functions must return the result of next()': true
83
+ _types: {
84
+ context: {
85
+ b: string
86
+ }
87
+ sendContext: undefined
82
88
  }
89
+ context: { b: string }
83
90
  sendContext: undefined
84
- }
85
- context: { b: string }
86
- sendContext: undefined
87
- }>()
91
+ }>()
88
92
 
89
- return result
90
- })
93
+ return result
94
+ },
95
+ )
91
96
 
92
- const middleware3 = createMiddleware()
97
+ const middleware3 = createMiddleware({ type: 'function' })
93
98
  .middleware([middleware1, middleware2])
94
99
  .server(async (options) => {
95
100
  expectTypeOf(options.context).toEqualTypeOf<{ a: boolean; b: string }>()
@@ -111,7 +116,7 @@ test('createMiddleware merges server context', () => {
111
116
  return result
112
117
  })
113
118
 
114
- createMiddleware()
119
+ createMiddleware({ type: 'function' })
115
120
  .middleware([middleware3])
116
121
  .server(async (options) => {
117
122
  expectTypeOf(options.context).toEqualTypeOf<{
@@ -139,37 +144,41 @@ test('createMiddleware merges server context', () => {
139
144
  })
140
145
 
141
146
  test('createMiddleware merges client context and sends to the server', () => {
142
- const middleware1 = createMiddleware().client(async (options) => {
143
- expectTypeOf(options.context).toEqualTypeOf<undefined>()
147
+ const middleware1 = createMiddleware({ type: 'function' }).client(
148
+ async (options) => {
149
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
144
150
 
145
- const result = await options.next({ context: { a: true } })
151
+ const result = await options.next({ context: { a: true } })
146
152
 
147
- expectTypeOf(result).toEqualTypeOf<{
148
- 'use functions must return the result of next()': true
149
- context: { a: boolean }
150
- sendContext: undefined
151
- headers: HeadersInit
152
- }>()
153
+ expectTypeOf(result).toEqualTypeOf<{
154
+ 'use functions must return the result of next()': true
155
+ context: { a: boolean }
156
+ sendContext: undefined
157
+ headers: HeadersInit
158
+ }>()
153
159
 
154
- return result
155
- })
160
+ return result
161
+ },
162
+ )
156
163
 
157
- const middleware2 = createMiddleware().client(async (options) => {
158
- expectTypeOf(options.context).toEqualTypeOf<undefined>()
164
+ const middleware2 = createMiddleware({ type: 'function' }).client(
165
+ async (options) => {
166
+ expectTypeOf(options.context).toEqualTypeOf<undefined>()
159
167
 
160
- const result = await options.next({ context: { b: 'test' } })
168
+ const result = await options.next({ context: { b: 'test' } })
161
169
 
162
- expectTypeOf(result).toEqualTypeOf<{
163
- 'use functions must return the result of next()': true
164
- context: { b: string }
165
- sendContext: undefined
166
- headers: HeadersInit
167
- }>()
170
+ expectTypeOf(result).toEqualTypeOf<{
171
+ 'use functions must return the result of next()': true
172
+ context: { b: string }
173
+ sendContext: undefined
174
+ headers: HeadersInit
175
+ }>()
168
176
 
169
- return result
170
- })
177
+ return result
178
+ },
179
+ )
171
180
 
172
- const middleware3 = createMiddleware()
181
+ const middleware3 = createMiddleware({ type: 'function' })
173
182
  .middleware([middleware1, middleware2])
174
183
  .client(async (options) => {
175
184
  expectTypeOf(options.context).toEqualTypeOf<{ a: boolean; b: string }>()
@@ -186,7 +195,7 @@ test('createMiddleware merges client context and sends to the server', () => {
186
195
  return result
187
196
  })
188
197
 
189
- const middleware4 = createMiddleware()
198
+ const middleware4 = createMiddleware({ type: 'function' })
190
199
  .middleware([middleware3])
191
200
  .client(async (options) => {
192
201
  expectTypeOf(options.context).toEqualTypeOf<{
@@ -209,7 +218,7 @@ test('createMiddleware merges client context and sends to the server', () => {
209
218
  return result
210
219
  })
211
220
 
212
- createMiddleware()
221
+ createMiddleware({ type: 'function' })
213
222
  .middleware([middleware4])
214
223
  .server(async (options) => {
215
224
  expectTypeOf(options.context).toEqualTypeOf<{
@@ -242,7 +251,7 @@ test('createMiddleware merges client context and sends to the server', () => {
242
251
  })
243
252
 
244
253
  test('createMiddleware merges input', () => {
245
- const middleware1 = createMiddleware()
254
+ const middleware1 = createMiddleware({ type: 'function' })
246
255
  .validator(() => {
247
256
  return {
248
257
  a: 'a',
@@ -253,7 +262,7 @@ test('createMiddleware merges input', () => {
253
262
  return next()
254
263
  })
255
264
 
256
- const middleware2 = createMiddleware()
265
+ const middleware2 = createMiddleware({ type: 'function' })
257
266
  .middleware([middleware1])
258
267
  .validator(() => {
259
268
  return {
@@ -265,7 +274,7 @@ test('createMiddleware merges input', () => {
265
274
  return next()
266
275
  })
267
276
 
268
- createMiddleware()
277
+ createMiddleware({ type: 'function' })
269
278
  .middleware([middleware2])
270
279
  .validator(() => ({ c: 'c' }) as const)
271
280
  .server(({ next, data }) => {
@@ -279,7 +288,7 @@ test('createMiddleware merges input', () => {
279
288
  })
280
289
 
281
290
  test('createMiddleware merges server context and client context, sends server context to the client and merges ', () => {
282
- const middleware1 = createMiddleware()
291
+ const middleware1 = createMiddleware({ type: 'function' })
283
292
  .client(async (options) => {
284
293
  expectTypeOf(options.context).toEqualTypeOf<undefined>()
285
294
 
@@ -318,7 +327,7 @@ test('createMiddleware merges server context and client context, sends server co
318
327
  return result
319
328
  })
320
329
 
321
- const middleware2 = createMiddleware()
330
+ const middleware2 = createMiddleware({ type: 'function' })
322
331
  .client(async (options) => {
323
332
  expectTypeOf(options.context).toEqualTypeOf<undefined>()
324
333
 
@@ -357,7 +366,7 @@ test('createMiddleware merges server context and client context, sends server co
357
366
  return result
358
367
  })
359
368
 
360
- const middleware3 = createMiddleware()
369
+ const middleware3 = createMiddleware({ type: 'function' })
361
370
  .middleware([middleware1, middleware2])
362
371
  .client(async (options) => {
363
372
  expectTypeOf(options.context).toEqualTypeOf<{
@@ -411,7 +420,7 @@ test('createMiddleware merges server context and client context, sends server co
411
420
  return result
412
421
  })
413
422
 
414
- const middleware4 = createMiddleware()
423
+ const middleware4 = createMiddleware({ type: 'function' })
415
424
  .middleware([middleware3])
416
425
  .client(async (options) => {
417
426
  expectTypeOf(options.context).toEqualTypeOf<{
@@ -475,7 +484,7 @@ test('createMiddleware merges server context and client context, sends server co
475
484
  return result
476
485
  })
477
486
 
478
- createMiddleware()
487
+ createMiddleware({ type: 'function' })
479
488
  .middleware([middleware4])
480
489
  .client(async (options) => {
481
490
  expectTypeOf(options.context).toEqualTypeOf<{
@@ -548,7 +557,7 @@ test('createMiddleware merges server context and client context, sends server co
548
557
  })
549
558
 
550
559
  test('createMiddleware sendContext cannot send a function', () => {
551
- createMiddleware()
560
+ createMiddleware({ type: 'function' })
552
561
  .client(({ next }) => {
553
562
  expectTypeOf(next<{ func: () => 'func' }>)
554
563
  .parameter(0)
@@ -570,7 +579,7 @@ test('createMiddleware sendContext cannot send a function', () => {
570
579
  })
571
580
 
572
581
  test('createMiddleware cannot validate function', () => {
573
- const validator = createMiddleware().validator<
582
+ const validator = createMiddleware({ type: 'function' }).validator<
574
583
  (input: { func: () => 'string' }) => { output: 'string' }
575
584
  >
576
585
 
@@ -585,7 +594,7 @@ test('createMiddleware cannot validate function', () => {
585
594
  })
586
595
 
587
596
  test('createMiddleware can validate Date', () => {
588
- const validator = createMiddleware().validator<
597
+ const validator = createMiddleware({ type: 'function' }).validator<
589
598
  (input: Date) => { output: 'string' }
590
599
  >
591
600
 
@@ -595,7 +604,7 @@ test('createMiddleware can validate Date', () => {
595
604
  })
596
605
 
597
606
  test('createMiddleware can validate FormData', () => {
598
- const validator = createMiddleware().validator<
607
+ const validator = createMiddleware({ type: 'function' }).validator<
599
608
  (input: FormData) => { output: 'string' }
600
609
  >
601
610
 
@@ -606,8 +615,22 @@ test('createMiddleware can validate FormData', () => {
606
615
  >()
607
616
  })
608
617
 
618
+ test('createMiddleware merging from parent with undefined validator', () => {
619
+ const middleware1 = createMiddleware({ type: 'function' }).validator(
620
+ (input: { test: string }) => input.test,
621
+ )
622
+
623
+ createMiddleware({ type: 'function' })
624
+ .middleware([middleware1])
625
+ .server((ctx) => {
626
+ expectTypeOf(ctx.data).toEqualTypeOf<string>()
627
+
628
+ return ctx.next()
629
+ })
630
+ })
631
+
609
632
  test('createMiddleware validator infers unknown for default input type', () => {
610
- createMiddleware()
633
+ createMiddleware({ type: 'function' })
611
634
  .validator((input) => {
612
635
  expectTypeOf(input).toEqualTypeOf<unknown>()
613
636
 
@@ -621,3 +644,93 @@ test('createMiddleware validator infers unknown for default input type', () => {
621
644
  return next()
622
645
  })
623
646
  })
647
+
648
+ test('createMiddleware with type request, no middleware or context', () => {
649
+ createMiddleware({ type: 'request' }).server(async (options) => {
650
+ expectTypeOf(options).toEqualTypeOf<{
651
+ request: Request
652
+ next: RequestServerNextFn<undefined>
653
+ pathname: string
654
+ context: undefined
655
+ }>()
656
+
657
+ const result = await options.next()
658
+
659
+ expectTypeOf(result).toEqualTypeOf<{
660
+ context: undefined
661
+ pathname: string
662
+ request: Request
663
+ response: Response
664
+ }>()
665
+
666
+ return result
667
+ })
668
+ })
669
+
670
+ test('createMiddleware with type request, no middleware with context', () => {
671
+ createMiddleware({ type: 'request' }).server(async (options) => {
672
+ expectTypeOf(options).toEqualTypeOf<{
673
+ request: Request
674
+ next: RequestServerNextFn<undefined>
675
+ pathname: string
676
+ context: undefined
677
+ }>()
678
+
679
+ const result = await options.next({ context: { a: 'a' } })
680
+
681
+ expectTypeOf(result).toEqualTypeOf<{
682
+ context: { a: string }
683
+ pathname: string
684
+ request: Request
685
+ response: Response
686
+ }>()
687
+
688
+ return result
689
+ })
690
+ })
691
+
692
+ test('createMiddleware with type request, middleware and context', () => {
693
+ const middleware1 = createMiddleware({ type: 'request' }).server(
694
+ async (options) => {
695
+ expectTypeOf(options).toEqualTypeOf<{
696
+ request: Request
697
+ next: RequestServerNextFn<undefined>
698
+ pathname: string
699
+ context: undefined
700
+ }>()
701
+
702
+ const result = await options.next({ context: { a: 'a' } })
703
+
704
+ expectTypeOf(result).toEqualTypeOf<{
705
+ context: { a: string }
706
+ pathname: string
707
+ request: Request
708
+ response: Response
709
+ }>()
710
+
711
+ return result
712
+ },
713
+ )
714
+
715
+ createMiddleware({ type: 'request' })
716
+ .middleware([middleware1])
717
+ .server(async (options) => {
718
+ expectTypeOf(options).toEqualTypeOf<{
719
+ request: Request
720
+ next: RequestServerNextFn<undefined>
721
+ pathname: string
722
+ context: { a: string }
723
+ }>()
724
+
725
+ const result = await options.next({ context: { b: 'b' } })
726
+
727
+ expectTypeOf(result).toEqualTypeOf<{
728
+ context: { a: string; b: string }
729
+ pathname: string
730
+ request: Request
731
+ response: Response
732
+ }>()
733
+
734
+ return result
735
+ })
736
+ })