@tanstack/start-client-core 1.132.0-alpha.7 → 1.132.0-alpha.9

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.
@@ -1,7 +1,7 @@
1
1
  import { describe, expectTypeOf, test } from 'vitest'
2
2
  import { createMiddleware } from '../createMiddleware'
3
3
  import { createServerFn } from '../createServerFn'
4
- import type { Constrain, Validator } from '@tanstack/router-core'
4
+ import type { Constrain, Register, Validator } from '@tanstack/router-core'
5
5
  import type { ConstrainValidator } from '../createServerFn'
6
6
 
7
7
  test('createServerFn method with autocomplete', () => {
@@ -97,7 +97,6 @@ test('createServerFn with middleware and context', () => {
97
97
 
98
98
  expectTypeOf(fnWithMiddleware).toHaveProperty('handler')
99
99
  expectTypeOf(fnWithMiddleware).toHaveProperty('validator')
100
- expectTypeOf(fnWithMiddleware).not.toHaveProperty('middleware')
101
100
 
102
101
  fnWithMiddleware.handler((options) => {
103
102
  expectTypeOf(options).toEqualTypeOf<{
@@ -398,7 +397,9 @@ test('createServerFn can validate Date', () => {
398
397
 
399
398
  expectTypeOf(validator)
400
399
  .parameter(0)
401
- .toEqualTypeOf<ConstrainValidator<(input: Date) => { output: 'string' }>>()
400
+ .toEqualTypeOf<
401
+ ConstrainValidator<Register, (input: Date) => { output: 'string' }>
402
+ >()
402
403
  })
403
404
 
404
405
  test('createServerFn can validate FormData', () => {
@@ -409,7 +410,7 @@ test('createServerFn can validate FormData', () => {
409
410
  expectTypeOf(validator)
410
411
  .parameter(0)
411
412
  .toEqualTypeOf<
412
- ConstrainValidator<(input: FormData) => { output: 'string' }>
413
+ ConstrainValidator<Register, (input: FormData) => { output: 'string' }>
413
414
  >()
414
415
  })
415
416
 
@@ -511,3 +512,88 @@ test('createServerFn validator infers unknown for default input type', () => {
511
512
 
512
513
  expectTypeOf(fn()).toEqualTypeOf<Promise<'failed' | 'success'>>()
513
514
  })
515
+
516
+ test('incrementally building createServerFn with multiple middleware calls', () => {
517
+ const middleware1 = createMiddleware({ type: 'function' }).server(
518
+ ({ next }) => {
519
+ return next({ context: { a: 'a' } as const })
520
+ },
521
+ )
522
+
523
+ const middleware2 = createMiddleware({ type: 'function' }).server(
524
+ ({ next }) => {
525
+ return next({ context: { b: 'b' } as const })
526
+ },
527
+ )
528
+
529
+ const middleware3 = createMiddleware({ type: 'function' }).server(
530
+ ({ next }) => {
531
+ return next({ context: { c: 'c' } as const })
532
+ },
533
+ )
534
+
535
+ const builderWithMw1 = createServerFn({ method: 'GET' }).middleware([
536
+ middleware1,
537
+ ])
538
+
539
+ expectTypeOf(builderWithMw1).toHaveProperty('handler')
540
+ expectTypeOf(builderWithMw1).toHaveProperty('validator')
541
+ expectTypeOf(builderWithMw1).toHaveProperty('middleware')
542
+
543
+ builderWithMw1.handler((options) => {
544
+ expectTypeOf(options).toEqualTypeOf<{
545
+ method: 'GET'
546
+ context: {
547
+ readonly a: 'a'
548
+ }
549
+ data: undefined
550
+ signal: AbortSignal
551
+ response: 'data'
552
+ }>()
553
+ })
554
+
555
+ // overrides method
556
+ const builderWithMw2 = builderWithMw1({ method: 'POST' }).middleware([
557
+ middleware2,
558
+ ])
559
+
560
+ expectTypeOf(builderWithMw2).toHaveProperty('handler')
561
+ expectTypeOf(builderWithMw2).toHaveProperty('validator')
562
+ expectTypeOf(builderWithMw2).toHaveProperty('middleware')
563
+
564
+ builderWithMw2.handler((options) => {
565
+ expectTypeOf(options).toEqualTypeOf<{
566
+ method: 'POST'
567
+ context: {
568
+ readonly a: 'a'
569
+ readonly b: 'b'
570
+ }
571
+ data: undefined
572
+ signal: AbortSignal
573
+ response: 'data'
574
+ }>()
575
+ })
576
+
577
+ // overrides method again
578
+ const builderWithMw3 = builderWithMw2({ method: 'GET' }).middleware([
579
+ middleware3,
580
+ ])
581
+
582
+ expectTypeOf(builderWithMw3).toHaveProperty('handler')
583
+ expectTypeOf(builderWithMw3).toHaveProperty('validator')
584
+ expectTypeOf(builderWithMw3).toHaveProperty('middleware')
585
+
586
+ builderWithMw3.handler((options) => {
587
+ expectTypeOf(options).toEqualTypeOf<{
588
+ method: 'GET'
589
+ context: {
590
+ readonly a: 'a'
591
+ readonly b: 'b'
592
+ readonly c: 'c'
593
+ }
594
+ data: undefined
595
+ signal: AbortSignal
596
+ response: 'data'
597
+ }>()
598
+ })
599
+ })
@@ -1,8 +1,8 @@
1
1
  import { expectTypeOf, test } from 'vitest'
2
2
  import { createMiddleware } from '../createMiddleware'
3
3
  import type { RequestServerNextFn } from '../createMiddleware'
4
- import type { Constrain, Validator } from '@tanstack/router-core'
5
4
  import type { ConstrainValidator } from '../createServerFn'
5
+ import type { Register } from '@tanstack/router-core'
6
6
 
7
7
  test('createServeMiddleware removes middleware after middleware,', () => {
8
8
  const middleware = createMiddleware({ type: 'function' })
@@ -211,7 +211,7 @@ test('createMiddleware merges client context and sends to the server', () => {
211
211
  expectTypeOf(result).toEqualTypeOf<{
212
212
  'use functions must return the result of next()': true
213
213
  context: { a: boolean; b: string; c: number }
214
- sendContext: { a: boolean; b: string; c: number; d: number }
214
+ sendContext: { a: boolean; b: string; c: number; d: 5 }
215
215
  headers: HeadersInit
216
216
  }>()
217
217
 
@@ -225,7 +225,7 @@ test('createMiddleware merges client context and sends to the server', () => {
225
225
  a: boolean
226
226
  b: string
227
227
  c: number
228
- d: number
228
+ d: 5
229
229
  }>()
230
230
 
231
231
  const result = await options.next({
@@ -242,7 +242,7 @@ test('createMiddleware merges client context and sends to the server', () => {
242
242
  }
243
243
  sendContext: undefined
244
244
  }
245
- context: { a: boolean; b: string; c: number; d: number; e: string }
245
+ context: { a: boolean; b: string; c: number; d: 5; e: string }
246
246
  sendContext: undefined
247
247
  }>()
248
248
 
@@ -586,9 +586,9 @@ test('createMiddleware cannot validate function', () => {
586
586
  expectTypeOf(validator)
587
587
  .parameter(0)
588
588
  .toEqualTypeOf<
589
- Constrain<
590
- (input: { func: () => 'string' }) => { output: 'string' },
591
- Validator<{ func: 'Function is not serializable' }, any>
589
+ ConstrainValidator<
590
+ Register,
591
+ (input: { func: () => 'string' }) => { output: 'string' }
592
592
  >
593
593
  >()
594
594
  })
@@ -600,7 +600,9 @@ test('createMiddleware can validate Date', () => {
600
600
 
601
601
  expectTypeOf(validator)
602
602
  .parameter(0)
603
- .toEqualTypeOf<ConstrainValidator<(input: Date) => { output: 'string' }>>()
603
+ .toEqualTypeOf<
604
+ ConstrainValidator<Register, (input: Date) => { output: 'string' }>
605
+ >()
604
606
  })
605
607
 
606
608
  test('createMiddleware can validate FormData', () => {
@@ -611,7 +613,7 @@ test('createMiddleware can validate FormData', () => {
611
613
  expectTypeOf(validator)
612
614
  .parameter(0)
613
615
  .toEqualTypeOf<
614
- ConstrainValidator<(input: FormData) => { output: 'string' }>
616
+ ConstrainValidator<Register, (input: FormData) => { output: 'string' }>
615
617
  >()
616
618
  })
617
619
 
@@ -1,16 +0,0 @@
1
- export type SerializerStringifyBy<T, TSerializable> = T extends TSerializable ? T : T extends (...args: Array<any>) => any ? 'Function is not serializable' : {
2
- [K in keyof T]: SerializerStringifyBy<T[K], TSerializable>;
3
- };
4
- export type SerializerParseBy<T, TSerializable> = T extends TSerializable ? T : unknown extends SerializerExtensions['ReadableStream'] ? {
5
- [K in keyof T]: SerializerParseBy<T[K], TSerializable>;
6
- } : T extends SerializerExtensions['ReadableStream'] ? ReadableStream : {
7
- [K in keyof T]: SerializerParseBy<T[K], TSerializable>;
8
- };
9
- export interface DefaultSerializerExtensions {
10
- ReadableStream: unknown;
11
- }
12
- export interface SerializerExtensions extends DefaultSerializerExtensions {
13
- }
14
- export type Serializable = Date | undefined | Error | FormData | bigint;
15
- export type SerializerStringify<T> = SerializerStringifyBy<T, Serializable>;
16
- export type SerializerParse<T> = SerializerParseBy<T, Serializable>;
package/src/serializer.ts DELETED
@@ -1,25 +0,0 @@
1
- export type SerializerStringifyBy<T, TSerializable> = T extends TSerializable
2
- ? T
3
- : T extends (...args: Array<any>) => any
4
- ? 'Function is not serializable'
5
- : { [K in keyof T]: SerializerStringifyBy<T[K], TSerializable> }
6
-
7
- export type SerializerParseBy<T, TSerializable> = T extends TSerializable
8
- ? T
9
- : unknown extends SerializerExtensions['ReadableStream']
10
- ? { [K in keyof T]: SerializerParseBy<T[K], TSerializable> }
11
- : T extends SerializerExtensions['ReadableStream']
12
- ? ReadableStream
13
- : { [K in keyof T]: SerializerParseBy<T[K], TSerializable> }
14
-
15
- export interface DefaultSerializerExtensions {
16
- ReadableStream: unknown
17
- }
18
-
19
- export interface SerializerExtensions extends DefaultSerializerExtensions {}
20
-
21
- export type Serializable = Date | undefined | Error | FormData | bigint
22
-
23
- export type SerializerStringify<T> = SerializerStringifyBy<T, Serializable>
24
-
25
- export type SerializerParse<T> = SerializerParseBy<T, Serializable>