Skip to content

Commit 62538cc

Browse files
strict: false for server functions
Add a `strict: false` server function handler option to opt out of type-level serialization checks for server function return values.
1 parent 1285422 commit 62538cc

5 files changed

Lines changed: 215 additions & 19 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@tanstack/start-client-core': minor
3+
'@tanstack/start-plugin-core': minor
4+
---
5+
6+
Add a `strict: false` server function handler option to opt out of type-level serialization checks for server function return values.

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

Lines changed: 74 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -414,9 +414,14 @@ export type RscStream<T> = {
414414

415415
export type Method = 'GET' | 'POST'
416416

417-
export type ServerFnReturnType<TRegister, TResponse> =
418-
TResponse extends PromiseLike<infer U>
419-
? Promise<ServerFnReturnType<TRegister, U>>
417+
export type ServerFnReturnType<
418+
TRegister,
419+
TResponse,
420+
TStrict extends boolean = true,
421+
> = TStrict extends false
422+
? TResponse
423+
: TResponse extends PromiseLike<infer U>
424+
? Promise<ServerFnReturnType<TRegister, U, TStrict>>
420425
: TResponse extends Response
421426
? TResponse
422427
: ValidateSerializableInput<TRegister, TResponse>
@@ -427,9 +432,45 @@ export type ServerFn<
427432
TMiddlewares,
428433
TInputValidator,
429434
TResponse,
435+
TStrict extends boolean = true,
430436
> = (
431437
ctx: ServerFnCtx<TRegister, TMethod, TMiddlewares, TInputValidator>,
432-
) => ServerFnReturnType<TRegister, TResponse>
438+
) => ServerFnReturnType<TRegister, TResponse, TStrict>
439+
440+
export interface ServerFnStrictHandlerOptions<
441+
TRegister,
442+
TMethod,
443+
TMiddlewares,
444+
TInputValidator,
445+
TResponse,
446+
> {
447+
strict?: true
448+
handler: ServerFn<
449+
TRegister,
450+
TMethod,
451+
TMiddlewares,
452+
TInputValidator,
453+
TResponse
454+
>
455+
}
456+
457+
export interface ServerFnLooseHandlerOptions<
458+
TRegister,
459+
TMethod,
460+
TMiddlewares,
461+
TInputValidator,
462+
TResponse,
463+
> {
464+
strict: false
465+
handler: ServerFn<
466+
TRegister,
467+
TMethod,
468+
TMiddlewares,
469+
TInputValidator,
470+
TResponse,
471+
false
472+
>
473+
}
433474

434475
export interface ServerFnCtx<
435476
TRegister,
@@ -626,15 +667,35 @@ export interface ServerFnHandler<
626667
TMiddlewares,
627668
TInputValidator,
628669
> {
629-
handler: <TNewResponse>(
630-
fn?: ServerFn<
631-
TRegister,
632-
TMethod,
633-
TMiddlewares,
634-
TInputValidator,
635-
TNewResponse
636-
>,
637-
) => Fetcher<TMiddlewares, TInputValidator, TNewResponse>
670+
handler: {
671+
<TNewResponse>(
672+
opts: ServerFnLooseHandlerOptions<
673+
TRegister,
674+
TMethod,
675+
TMiddlewares,
676+
TInputValidator,
677+
TNewResponse
678+
>,
679+
): Fetcher<TMiddlewares, TInputValidator, TNewResponse>
680+
<TNewResponse>(
681+
opts: ServerFnStrictHandlerOptions<
682+
TRegister,
683+
TMethod,
684+
TMiddlewares,
685+
TInputValidator,
686+
TNewResponse
687+
>,
688+
): Fetcher<TMiddlewares, TInputValidator, TNewResponse>
689+
<TNewResponse>(
690+
fn?: ServerFn<
691+
TRegister,
692+
TMethod,
693+
TMiddlewares,
694+
TInputValidator,
695+
TNewResponse
696+
>,
697+
): Fetcher<TMiddlewares, TInputValidator, TNewResponse>
698+
}
638699
}
639700

640701
export interface ServerFnBuilder<TRegister, TMethod extends Method = 'GET'>

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

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,43 @@ test('createServerFn cannot return function', () => {
518518
}>()
519519
})
520520

521+
test('createServerFn handler strict false can return function', () => {
522+
const fn = createServerFn().handler({
523+
strict: false,
524+
handler: () => ({
525+
func: () => 'func' as const,
526+
}),
527+
})
528+
529+
expectTypeOf(fn()).toEqualTypeOf<
530+
Promise<{
531+
func: () => 'func'
532+
}>
533+
>()
534+
535+
const promiseFn = createServerFn().handler({
536+
strict: false,
537+
handler: () =>
538+
Promise.resolve({
539+
func: () => 'func' as const,
540+
}),
541+
})
542+
543+
expectTypeOf(promiseFn()).toEqualTypeOf<
544+
Promise<{
545+
func: () => 'func'
546+
}>
547+
>()
548+
})
549+
550+
test('ServerFnReturnType skips serialization when strict is false', () => {
551+
expectTypeOf<
552+
ServerFnReturnType<Register, { func: () => 'func' }, false>
553+
>().toEqualTypeOf<{
554+
func: () => 'func'
555+
}>()
556+
})
557+
521558
test('createServerFn cannot validate function', () => {
522559
const validator = createServerFn().inputValidator<
523560
(input: { func: () => 'string' }) => { output: 'string' }

‎packages/start-plugin-core/src/start-compiler/handleCreateServerFn.ts‎

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,9 +277,9 @@ export function handleCreateServerFn(
277277
}
278278
}
279279

280-
const handlerFnPath = handler?.firstArgPath
280+
const handlerArgPath = handler?.firstArgPath
281281

282-
if (!handler || !handlerFnPath?.node) {
282+
if (!handler || !handlerArgPath?.node) {
283283
throw codeFrameError(
284284
context.code,
285285
candidatePath.node.callee.loc!,
@@ -288,14 +288,46 @@ export function handleCreateServerFn(
288288
}
289289

290290
// Validate the handler argument is an expression (not a SpreadElement, etc.)
291-
if (!t.isExpression(handlerFnPath.node)) {
291+
if (!t.isExpression(handlerArgPath.node)) {
292292
throw codeFrameError(
293293
context.code,
294-
handlerFnPath.node.loc!,
295-
`handler() must be called with an expression, not a ${handlerFnPath.node.type}`,
294+
handlerArgPath.node.loc!,
295+
`handler() must be called with an expression, not a ${handlerArgPath.node.type}`,
296296
)
297297
}
298298

299+
let handlerFnPath = handlerArgPath as babel.NodePath<t.Expression>
300+
301+
if (handlerArgPath.isObjectExpression()) {
302+
const handlerPropertyPath = handlerArgPath
303+
.get('properties')
304+
.find(
305+
(propertyPath) =>
306+
propertyPath.isObjectProperty() &&
307+
!propertyPath.node.computed &&
308+
t.isIdentifier(propertyPath.node.key, { name: 'handler' }),
309+
)
310+
311+
if (!handlerPropertyPath) {
312+
throw codeFrameError(
313+
context.code,
314+
handlerArgPath.node.loc!,
315+
`handler() object argument must have a "handler" property!`,
316+
)
317+
}
318+
319+
const valuePath = handlerPropertyPath.get('value')
320+
if (!valuePath.isExpression()) {
321+
throw codeFrameError(
322+
context.code,
323+
valuePath.node.loc!,
324+
`handler() object "handler" property must be an expression, not a ${valuePath.node.type}`,
325+
)
326+
}
327+
328+
handlerFnPath = valuePath
329+
}
330+
299331
const handlerFn = handlerFnPath.node
300332

301333
// Register function only from caller files (not provider files)
@@ -405,7 +437,7 @@ export function handleCreateServerFn(
405437

406438
// Replace ONLY the handler argument with the RPC stub
407439
// Keep the createServerFn().handler() wrapper intact for client middleware
408-
handlerFnPath.replaceWith(rpcStub)
440+
handlerArgPath.replaceWith(rpcStub)
409441
}
410442
}
411443

‎packages/start-plugin-core/tests/createServerFn/createServerFn.test.ts‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,66 @@ describe('createServerFn compiles correctly', async () => {
155155
`)
156156
})
157157

158+
test('should unwrap handler object form', async () => {
159+
const code = `
160+
import { createServerFn } from '@tanstack/react-start'
161+
const myServerFn = createServerFn().handler({
162+
strict: false,
163+
handler: async () => {
164+
return { func: () => 'hello from the server' }
165+
},
166+
})`
167+
168+
const compiledResultClient = await compile({
169+
code,
170+
env: 'client',
171+
isProviderFile: false,
172+
mode: 'build',
173+
})
174+
175+
const compiledResultServerCaller = await compile({
176+
code,
177+
env: 'server',
178+
isProviderFile: false,
179+
mode: 'build',
180+
})
181+
182+
const compiledResultServerProvider = await compile({
183+
code,
184+
env: 'server',
185+
isProviderFile: true,
186+
mode: 'build',
187+
})
188+
189+
expect(compiledResultClient!.code).toMatchInlineSnapshot(`
190+
"import { createClientRpc } from '@tanstack/react-start/client-rpc';
191+
import { createServerFn } from '@tanstack/react-start';
192+
const myServerFn = createServerFn().handler(createClientRpc("2c205add8e6755de551521133ddff3d48859b1631add5f1bbe5c48a5664f319b"));"
193+
`)
194+
195+
expect(compiledResultServerCaller!.code).toMatchInlineSnapshot(`
196+
"import { createSsrRpc } from '@tanstack/react-start/ssr-rpc';
197+
import { createServerFn } from '@tanstack/react-start';
198+
const myServerFn = createServerFn().handler(createSsrRpc("2c205add8e6755de551521133ddff3d48859b1631add5f1bbe5c48a5664f319b"));"
199+
`)
200+
201+
expect(compiledResultServerProvider!.code).toMatchInlineSnapshot(`
202+
"import { createServerRpc } from '@tanstack/react-start/server-rpc';
203+
import { createServerFn } from '@tanstack/react-start';
204+
const myServerFn_createServerFn_handler = createServerRpc({
205+
id: "2c205add8e6755de551521133ddff3d48859b1631add5f1bbe5c48a5664f319b",
206+
name: "myServerFn",
207+
filename: "src/test.ts"
208+
}, opts => myServerFn.__executeServer(opts));
209+
const myServerFn = createServerFn().handler(myServerFn_createServerFn_handler, async () => {
210+
return {
211+
func: () => 'hello from the server'
212+
};
213+
});
214+
export { myServerFn_createServerFn_handler };"
215+
`)
216+
})
217+
158218
test('should use dce by default', async () => {
159219
const code = `
160220
import { createServerFn } from '@tanstack/react-start'

0 commit comments

Comments
 (0)