-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fixed infered type from useServerFn + added type test #5367
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Christopher96u
wants to merge
9
commits into
TanStack:main
Choose a base branch
from
Christopher96u:fix/use-server-fn-typing
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
−13
Open
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
008ee96
fixed infered type from useServerFn + added type test
Christopher96u 0cf8f24
fixed wrong import
Christopher96u ac1e342
fixed types to keep single-argument optionals optional
Christopher96u 7104281
ci: apply automated fixes
autofix-ci[bot] 1a7438d
clean up
Christopher96u 745afd3
added solid test
Christopher96u aea930f
ci: apply automated fixes
autofix-ci[bot] 37e55f3
removed unnecessary chaining operator
Christopher96u 2e28196
fixed nit picks
Christopher96u File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 26 additions & 2 deletions
28
examples/react/start-basic-react-query/src/routes/index.tsx
This file contains hidden or 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,12 +1,36 @@ | ||
import * as React from 'react' | ||
import { useMutation } from '@tanstack/react-query' | ||
import { createFileRoute } from '@tanstack/react-router' | ||
import { createServerFn, useServerFn } from '@tanstack/react-start' | ||
|
||
const greetUser = createServerFn({ method: 'POST' }) | ||
.inputValidator((input: { name: string }) => input) | ||
.handler(async ({ data }) => { | ||
return Promise.resolve({ message: `Hello ${data.name}!` }) | ||
}) | ||
|
||
export const Route = createFileRoute('/')({ | ||
component: Home, | ||
}) | ||
|
||
function Home() { | ||
const greetingMutation = useMutation({ | ||
mutationFn: useServerFn(greetUser), | ||
onSuccess: (data) => data, | ||
}) | ||
|
||
return ( | ||
<div className="p-2"> | ||
<h3>Welcome Home!!!</h3> | ||
<div className="p-2 flex flex-col gap-2"> | ||
<h3>useServerFn + useMutation demo</h3> | ||
<button | ||
className="rounded bg-blue-500 px-3 py-1 text-white" | ||
onClick={() => greetingMutation.mutate({ data: { name: 'TanStack' } })} | ||
> | ||
Say hi | ||
</button> | ||
{greetingMutation.data ? ( | ||
<p data-testid="greeted">{greetingMutation.data.message}</p> | ||
) : null} | ||
</div> | ||
) | ||
} |
15 changes: 15 additions & 0 deletions
15
packages/react-start/src/tests/useServerFnMutation.test-d.tsx
This file contains hidden or 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 |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { useMutation } from '@tanstack/react-query' | ||
import { createServerFn, useServerFn } from '../index' | ||
|
||
const serverFn = createServerFn({ method: 'POST' }) | ||
.inputValidator((input: { name: string }) => input) | ||
.handler(async ({ data }) => ({ | ||
message: `Hello ${data.name}!`, | ||
})) | ||
|
||
useMutation({ | ||
mutationFn: useServerFn(serverFn), | ||
onSuccess: (data) => { | ||
data.message | ||
}, | ||
}) |
This file contains hidden or 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,30 +1,45 @@ | ||
import * as React from 'react' | ||
import { isRedirect, useRouter } from '@tanstack/react-router' | ||
|
||
type AwaitedReturn<T extends (...args: Array<any>) => Promise<any>> = Awaited< | ||
ReturnType<T> | ||
> | ||
|
||
type UseServerFnReturn<T extends (...args: Array<any>) => Promise<any>> = | ||
Parameters<T> extends [] | ||
? () => Promise<AwaitedReturn<T>> | ||
: Parameters<T> extends [infer TVariables] | ||
? (variables: TVariables) => Promise<AwaitedReturn<T>> | ||
: (...args: Parameters<T>) => Promise<AwaitedReturn<T>> | ||
|
||
export function useServerFn<T extends (...deps: Array<any>) => Promise<any>>( | ||
serverFn: T, | ||
): (...args: Parameters<T>) => ReturnType<T> { | ||
): UseServerFnReturn<T> { | ||
const router = useRouter() | ||
|
||
return React.useCallback( | ||
async (...args: Array<any>) => { | ||
const handler = React.useCallback( | ||
async (...args: Parameters<T>) => { | ||
try { | ||
const res = await serverFn(...args) | ||
|
||
if (isRedirect(res)) { | ||
throw res | ||
} | ||
|
||
return res | ||
return res as AwaitedReturn<T> | ||
} catch (err) { | ||
if (isRedirect(err)) { | ||
err.options._fromLocation = router.state.location | ||
return router.navigate(router.resolveRedirect(err).options) | ||
return router.navigate( | ||
router.resolveRedirect(err).options, | ||
) as AwaitedReturn<T> | ||
} | ||
|
||
throw err | ||
} | ||
}, | ||
[router, serverFn], | ||
) as any | ||
) | ||
|
||
return handler as UseServerFnReturn<T> | ||
} |
This file contains hidden or 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,26 +1,41 @@ | ||
import { isRedirect, useRouter } from '@tanstack/solid-router' | ||
|
||
type AwaitedReturn<T extends (...args: Array<any>) => Promise<any>> = Awaited< | ||
ReturnType<T> | ||
> | ||
|
||
type UseServerFnReturn<T extends (...args: Array<any>) => Promise<any>> = | ||
Parameters<T> extends [] | ||
? () => Promise<AwaitedReturn<T>> | ||
: Parameters<T> extends [infer TVariables] | ||
? (variables: TVariables) => Promise<AwaitedReturn<T>> | ||
: (...args: Parameters<T>) => Promise<AwaitedReturn<T>> | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
export function useServerFn<T extends (...deps: Array<any>) => Promise<any>>( | ||
serverFn: T, | ||
): (...args: Parameters<T>) => ReturnType<T> { | ||
): UseServerFnReturn<T> { | ||
const router = useRouter() | ||
|
||
return (async (...args: Array<any>) => { | ||
const handler = async (...args: Parameters<T>) => { | ||
try { | ||
const res = await serverFn(...args) | ||
|
||
if (isRedirect(res)) { | ||
throw res | ||
} | ||
|
||
return res | ||
return res as AwaitedReturn<T> | ||
} catch (err) { | ||
if (isRedirect(err)) { | ||
err.options._fromLocation = router.state.location | ||
return router.navigate(router.resolveRedirect(err).options) | ||
return router.navigate( | ||
router.resolveRedirect(err).options, | ||
) as AwaitedReturn<T> | ||
} | ||
|
||
throw err | ||
} | ||
}) as any | ||
} | ||
|
||
return handler as UseServerFnReturn<T> | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.