Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions packages/router-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ export type {
SerializableExtensions,
DefaultSerializable,
Serializable,
TSR_SERIALIZABLE,
TsrSerializable,
} from './ssr/serializer/transformer'

export {
Expand Down
5 changes: 5 additions & 0 deletions packages/router-core/src/ssr/serializer/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import type {
import type { LooseReturnType } from '../../utils'
import type { AnyRoute, ResolveAllSSR } from '../../route'

declare const TSR_SERIALIZABLE: unique symbol
export type TSR_SERIALIZABLE = typeof TSR_SERIALIZABLE

export type TsrSerializable = { [TSR_SERIALIZABLE]: true }
export interface DefaultSerializable {
number: number
string: string
Expand All @@ -17,6 +21,7 @@ export interface DefaultSerializable {
undefined: undefined
bigint: bigint
Date: Date
TsrSerializable: TsrSerializable
}

export interface SerializableExtensions extends DefaultSerializable {}
Expand Down
6 changes: 0 additions & 6 deletions packages/router-core/src/ssr/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,3 @@ export {
transformReadableStreamWithRouter,
} from './transformStreamWithRouter'
export { attachRouterServerSsrUtils, getOrigin } from './ssr-server'

// declare module '../router' {
// export interface RegisterSsr {
// ssr: true
// }
// }
74 changes: 74 additions & 0 deletions packages/router-core/tests/serializer.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { describe, expectTypeOf, it } from 'vitest'

import {
Serializable,
TsrSerializable,
ValidateSerializable,
ValidateSerializableResult,
} from '../src/ssr/serializer/transformer'

describe('Serializer', () => {
describe('Default types are serializable: $name', () => {
it('string', () => {
const value: string = 'hello'
expectTypeOf<
ValidateSerializableResult<typeof value, Serializable>
>().toBeString()
})
it('number', () => {
const value: number = 123

expectTypeOf<
ValidateSerializableResult<typeof value, Serializable>
>().toBeNumber()
})
it('boolean', () => {
const value: boolean = true

expectTypeOf<
ValidateSerializableResult<typeof value, Serializable>
>().toBeBoolean()
})
it('null', () => {
const value = null

expectTypeOf<
ValidateSerializableResult<typeof value, Serializable>
>().toBeNull()
})
it('undefined', () => {
const value = undefined

expectTypeOf<
ValidateSerializableResult<typeof value, Serializable>
>().toBeUndefined()
})
it('bigint', () => {
const value = BigInt(123)

expectTypeOf<
ValidateSerializableResult<typeof value, Serializable>
>().toBeBigInt()
})
it('Date', () => {
const value = new Date()

expectTypeOf<
ValidateSerializableResult<typeof value, Serializable>
>().toEqualTypeOf<Date>()
})
})
it('fails for non-serializable types', () => {
const value = () => {}
expectTypeOf<
ValidateSerializable<typeof value, Serializable>
>().toEqualTypeOf<'Function is not serializable'>()
})

it('works for types extending TsrSerializable', () => {
type MyCustomType = { f: () => {} } & TsrSerializable
expectTypeOf<
ValidateSerializable<MyCustomType, Serializable>
>().toEqualTypeOf<MyCustomType>()
})
})
19 changes: 18 additions & 1 deletion packages/start-client-core/src/tests/createServerFn.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,12 @@ import { describe, expectTypeOf, test } from 'vitest'
import { createMiddleware } from '../createMiddleware'
import { createServerFn } from '../createServerFn'
import { TSS_SERVER_FUNCTION } from '../constants'
import type { Constrain, Register, Validator } from '@tanstack/router-core'
import type {
Constrain,
Register,
TsrSerializable,
Validator,
} from '@tanstack/router-core'
import type { ConstrainValidator } from '../createServerFn'

test('createServerFn method with autocomplete', () => {
Expand Down Expand Up @@ -638,3 +643,15 @@ test('createServerFn returns sync array', () => {

expectTypeOf(serverFn()).toEqualTypeOf<Promise<Array<{ a: number }>>>()
})

test('createServerFn respects TsrSerializable', () => {
type MyCustomType = { f: () => void; value: string }
type MyCustomTypeSerializable = MyCustomType & TsrSerializable
const fn1 = createServerFn().handler(() => {
const custom: MyCustomType = { f: () => {}, value: 'test' }
return { nested: { custom: custom as MyCustomTypeSerializable } }
})
expectTypeOf(fn1()).toEqualTypeOf<
Promise<{ nested: { custom: MyCustomTypeSerializable } }>
>()
})
Loading