Skip to content

Commit 6489023

Browse files
committed
fix(react-form-start): fix infered return type of createServerFn
1 parent 254f157 commit 6489023

File tree

3 files changed

+45
-17
lines changed

3 files changed

+45
-17
lines changed

examples/react/tanstack-start/src/routes/index.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
import { createFileRoute } from '@tanstack/react-router'
2-
import { mergeForm, useForm, useTransform } from '@tanstack/react-form-start'
3-
import { useStore } from '@tanstack/react-store'
4-
import { getFormDataFromServer, handleForm } from 'src/utils/form'
2+
import {
3+
mergeForm,
4+
useForm,
5+
useStore,
6+
useTransform,
7+
} from '@tanstack/react-form-start'
8+
59
import { formOpts } from 'src/utils/form-isomorphic'
10+
import { getFormDataFromServer, handleForm } from '~/server/form'
611

712
export const Route = createFileRoute('/')({
813
component: Home,

examples/react/tanstack-start/src/utils/form.tsx renamed to examples/react/tanstack-start/src/server/form.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,15 @@ import {
44
createServerValidate,
55
getFormData,
66
} from '@tanstack/react-form-start'
7+
78
import { setResponseStatus } from '@tanstack/react-start/server'
8-
import { formOpts } from './form-isomorphic'
9+
import { formOpts } from '../utils/form-isomorphic'
10+
11+
export const getFormDataFromServer = createServerFn({ method: 'GET' }).handler(
12+
async () => {
13+
return getFormData()
14+
},
15+
)
916

1017
const serverValidate = createServerValidate({
1118
...formOpts,
@@ -39,9 +46,3 @@ export const handleForm = createServerFn({ method: 'POST' })
3946

4047
return 'Form has validated successfully'
4148
})
42-
43-
export const getFormDataFromServer = createServerFn({ method: 'GET' }).handler(
44-
async () => {
45-
return getFormData()
46-
},
47-
)

packages/react-form-start/src/createServerValidate.tsx

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,16 @@ import type {
1515
FormValidateAsyncFn,
1616
FormValidateOrFn,
1717
ServerFormState,
18+
StandardSchemaV1,
1819
UnwrapFormAsyncValidateOrFn,
1920
} from '@tanstack/react-form'
2021
import type { FormDataInfo } from 'decode-formdata'
2122

23+
type InferServerValidatorOutput<
24+
TOnServer extends undefined | FormAsyncValidateOrFn<any>,
25+
TFormData,
26+
> = TOnServer extends StandardSchemaV1<any, infer Output> ? Output : TFormData
27+
2228
interface CreateServerValidateOptions<
2329
TFormData,
2430
TOnMount extends undefined | FormValidateOrFn<TFormData>,
@@ -82,18 +88,32 @@ const serverFn = createServerFn({ method: 'POST' })
8288
}: {
8389
value: any
8490
validationSource: 'form'
85-
}) => {
91+
}): Promise<{
92+
error: UnwrapFormAsyncValidateOrFn<any> | undefined
93+
validatedValue: any
94+
}> => {
8695
if (isStandardSchemaValidator(onServerValidate)) {
87-
return await standardSchemaValidators.validateAsync(
96+
const rawResult = await onServerValidate['~standard'].validate(value)
97+
98+
if (!rawResult.issues) {
99+
return { error: undefined, validatedValue: rawResult.value }
100+
}
101+
102+
const error = await standardSchemaValidators.validateAsync(
88103
{ value, validationSource },
89104
onServerValidate,
90105
)
106+
107+
return { error, validatedValue: undefined }
91108
}
92-
return (onServerValidate as FormValidateAsyncFn<any>)({
109+
110+
const error = await (onServerValidate as FormValidateAsyncFn<any>)({
93111
value,
94112
signal: undefined as never,
95113
formApi: undefined as never,
96114
})
115+
116+
return { error, validatedValue: value }
97117
}
98118

99119
const referer = getRequestHeader('referer')!
@@ -102,12 +122,12 @@ const serverFn = createServerFn({ method: 'POST' })
102122
? decode(formData, info)
103123
: decode(formData)) as never as any
104124

105-
const onServerError = (await runValidator({
125+
const { error: onServerError, validatedValue } = await runValidator({
106126
value: decodedData,
107127
validationSource: 'form',
108-
})) as UnwrapFormAsyncValidateOrFn<any> | undefined
128+
})
109129

110-
if (!onServerError) return decodedData
130+
if (!onServerError) return validatedValue
111131

112132
const onServerErrorVal = (
113133
isGlobalFormValidationError(onServerError)
@@ -167,4 +187,6 @@ export const createServerValidate =
167187
>,
168188
) =>
169189
(formData: FormData, info?: Parameters<typeof decode>[1]) =>
170-
serverFn({ data: { defaultOpts, formData, info } })
190+
serverFn({ data: { defaultOpts, formData, info } }) as Promise<
191+
InferServerValidatorOutput<TOnServer, TFormData>
192+
>

0 commit comments

Comments
 (0)