Skip to content

Commit 6f7731f

Browse files
add serializer extension, register server functions to be serializable
1 parent d02c271 commit 6f7731f

File tree

5 files changed

+27
-12
lines changed

5 files changed

+27
-12
lines changed

packages/router-core/src/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,9 @@ export type {
420420
SerializerExtensions,
421421
ValidateSerializable,
422422
RegisteredSerializableInput,
423+
SerializableExtensions,
424+
DefaultSerializable,
425+
Serializable,
423426
} from './ssr/serializer/transformer'
424427

425428
export {

packages/router-core/src/ssr/serializer/transformer.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,19 @@ import type {
99
import type { LooseReturnType } from '../../utils'
1010
import type { AnyRoute, ResolveAllSSR } from '../../route'
1111

12-
export type Serializable =
13-
| number
14-
| string
15-
| boolean
16-
| null
17-
| undefined
18-
| bigint
19-
| Date
12+
export interface DefaultSerializable {
13+
number: number
14+
string: string
15+
boolean: boolean
16+
null: null
17+
undefined: undefined
18+
bigint: bigint
19+
Date: Date
20+
}
21+
22+
export interface SerializableExtensions extends DefaultSerializable {}
23+
24+
export type Serializable = SerializableExtensions[keyof SerializableExtensions]
2025

2126
export function createSerializationAdapter<
2227
TInput = unknown,

packages/start-client-core/src/createServerFn.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { isNotFound, isRedirect } from '@tanstack/router-core'
22
import { mergeHeaders } from '@tanstack/router-core/ssr/client'
33

4-
import { TSS_SERVER_FUNCTION, TSS_SERVER_FUNCTION_FACTORY } from './constants'
4+
import { TSS_SERVER_FUNCTION_FACTORY } from './constants'
55
import { getServerContextAfterGlobalMiddlewares } from './getServerContextAfterGlobalMiddlewares'
66
import { getStartOptions } from './getStartOptions'
7+
import type { TSS_SERVER_FUNCTION } from './constants'
78
import type {
89
AnyValidator,
910
Constrain,

packages/start-client-core/src/createStart.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { TSS_SERVER_FUNCTION } from './constants'
12
import { createMiddleware } from './createMiddleware'
23
import type {
34
AnyFunctionMiddleware,
@@ -116,7 +117,7 @@ export type AnyStartInstance = StartInstance<any, any, any, any>
116117
export type AnyStartInstanceOptions = StartInstanceOptions<any, any, any, any>
117118

118119
declare module '@tanstack/router-core' {
119-
interface Register {
120-
ssr: true
120+
interface SerializableExtensions {
121+
serverFn: { [TSS_SERVER_FUNCTION]: true }
121122
}
122123
}

packages/start-client-core/src/tests/createServerFn.test-d.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { describe, expectTypeOf, test } from 'vitest'
22
import { createMiddleware } from '../createMiddleware'
33
import { createServerFn } from '../createServerFn'
4+
import { TSS_SERVER_FUNCTION } from '../constants'
45
import type { Constrain, Register, Validator } from '@tanstack/router-core'
56
import type { ConstrainValidator } from '../createServerFn'
6-
import { TSS_SERVER_FUNCTION } from '../constants'
77

88
test('createServerFn method with autocomplete', () => {
99
createServerFn().handler((options) => {
@@ -593,3 +593,8 @@ test('createServerFn has TSS_SERVER_FUNCTION symbol set', () => {
593593
expectTypeOf(fn).toHaveProperty(TSS_SERVER_FUNCTION)
594594
expectTypeOf(fn[TSS_SERVER_FUNCTION]).toEqualTypeOf<true>()
595595
})
596+
597+
test('createServerFn fetcher itself is serializable', () => {
598+
const fn1 = createServerFn().handler(() => ({}))
599+
const fn2 = createServerFn().handler(() => fn1)
600+
})

0 commit comments

Comments
 (0)