Open History to see the last commit
1import { createSerializationAdapter } from '@tanstack/router-core'
2import { TSS_SERVER_FUNCTION } from '@tanstack/start-client-core'
3import { getServerFnById } from '../getServerFnById'
4β
5export const ServerFunctionSerializationAdapter = createSerializationAdapter({
6 key: '$TSS/serverfn',
7 test: (v): v is { serverFnMeta: { id: string } } => {
8 if (typeof v !== 'function') return false
9β
10 if (!(TSS_SERVER_FUNCTION in v)) return false
11β
12 return !!v[TSS_SERVER_FUNCTION]
13 },
14 toSerializable: ({ serverFnMeta }) => ({ functionId: serverFnMeta.id }),
15 fromSerializable: ({ functionId }) => {
16 const fn = async (opts: any, signal: any): Promise<any> => {
17 // When a function ID is received through serialization (e.g., as a parameter
18 // to another server function), it originates from the client and must be
19 // validated the same way as direct HTTP calls to server functions.
20 const serverFn = await getServerFnById(functionId, { origin: 'client' })
21 const result = await serverFn(
22 {
23 data: opts?.data,
24 context: opts?.context,
25 method: serverFn.method ?? 'GET',
26 },
27 signal,
28 )
29 if (result.error !== undefined) {
30 throw result.error
31 }
32 return result.result
33 }
34 return fn as never
35 },
36})