-
-
Notifications
You must be signed in to change notification settings - Fork 353
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: force createServerFn usage with getFormData (#826)
- Loading branch information
1 parent
04fda1c
commit 3b91e6c
Showing
4 changed files
with
25 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
import { createServerFn } from '@tanstack/start' | ||
import { type FetchFn } from '@tanstack/start' | ||
import { _tanstackInternalsCookie } from './utils' | ||
import type { ServerFormState } from './types' | ||
|
||
type FetchFnCtx = Parameters<FetchFn<never, never>>[1] | ||
|
||
export const initialFormState = { | ||
errorMap: { | ||
onServer: undefined, | ||
}, | ||
errors: [], | ||
} | ||
|
||
export const getFormData = createServerFn('GET', async (_, ctx) => { | ||
export const getFormData = async (ctx: FetchFnCtx) => { | ||
const data = (await _tanstackInternalsCookie.parse( | ||
ctx.request.headers.get('Cookie'), | ||
)) as undefined | ServerFormState<any> | ||
// Delete the cookie before it hits the client again¸ | ||
ctx.request.headers.delete('Cookie') | ||
if (!data) return initialFormState | ||
return data | ||
}) | ||
} |