Skip to content

Commit 7d876bc

Browse files
fix: register createServerFn fetcher as serializable (#5268)
* fix: createServerFn fetcher has TSS_SERVER_FUNCTION symbol prop * add serializer extension, register server functions to be serializable * remove cast
1 parent 985f8c6 commit 7d876bc

File tree

6 files changed

+34
-11
lines changed

6 files changed

+34
-11
lines changed

e2e/react-start/server-functions/src/routes/middleware/send-serverFn.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const middleware = createMiddleware({ type: 'function' }).client(
66
async ({ next }) => {
77
return next({
88
sendContext: {
9-
serverFn: barFn as any,
9+
serverFn: barFn,
1010
},
1111
})
1212
},

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 & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { mergeHeaders } from '@tanstack/router-core/ssr/client'
44
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,
@@ -248,6 +249,7 @@ export type Fetcher<TRegister, TMiddlewares, TInputValidator, TResponse> =
248249
: RequiredFetcher<TRegister, TMiddlewares, TInputValidator, TResponse>
249250

250251
export interface FetcherBase {
252+
[TSS_SERVER_FUNCTION]: true
251253
url: string
252254
__executeServer: (opts: {
253255
method: Method

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createMiddleware } from './createMiddleware'
2+
import type { TSS_SERVER_FUNCTION } from './constants'
23
import type {
34
AnyFunctionMiddleware,
45
AnyRequestMiddleware,
@@ -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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
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'
67

@@ -586,3 +587,14 @@ test('createServerFn with inputValidator and request middleware', () => {
586587
Promise<string>
587588
>()
588589
})
590+
591+
test('createServerFn has TSS_SERVER_FUNCTION symbol set', () => {
592+
const fn = createServerFn().handler(() => ({}))
593+
expectTypeOf(fn).toHaveProperty(TSS_SERVER_FUNCTION)
594+
expectTypeOf(fn[TSS_SERVER_FUNCTION]).toEqualTypeOf<true>()
595+
})
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)