diff --git a/examples/react/start-basic-react-query/app/routes/posts.$postId.tsx b/examples/react/start-basic-react-query/app/routes/posts.$postId.tsx index 542a12a9f6..66c01ab280 100644 --- a/examples/react/start-basic-react-query/app/routes/posts.$postId.tsx +++ b/examples/react/start-basic-react-query/app/routes/posts.$postId.tsx @@ -1,13 +1,13 @@ import { ErrorComponent, Link, createFileRoute } from '@tanstack/react-router' import { useSuspenseQuery } from '@tanstack/react-query' -import { postQueryOptions } from '../utils/posts' +import { postFnQuery } from '../utils/posts' import type { ErrorComponentProps } from '@tanstack/react-router' import { NotFound } from '~/components/NotFound' export const Route = createFileRoute('/posts/$postId')({ loader: async ({ params: { postId }, context }) => { const data = await context.queryClient.ensureQueryData( - postQueryOptions(postId), + postFnQuery.queryOptions({ data: postId }), ) return { @@ -30,7 +30,7 @@ export function PostErrorComponent({ error }: ErrorComponentProps) { function PostComponent() { const { postId } = Route.useParams() - const postQuery = useSuspenseQuery(postQueryOptions(postId)) + const postQuery = useSuspenseQuery(postFnQuery.queryOptions({ data: postId })) return (
diff --git a/examples/react/start-basic-react-query/app/routes/posts.tsx b/examples/react/start-basic-react-query/app/routes/posts.tsx index b545663be2..4ec87dbb67 100644 --- a/examples/react/start-basic-react-query/app/routes/posts.tsx +++ b/examples/react/start-basic-react-query/app/routes/posts.tsx @@ -1,10 +1,10 @@ -import { useSuspenseQuery } from '@tanstack/react-query' +import { useQuery, useSuspenseQuery } from '@tanstack/react-query' import { Link, Outlet, createFileRoute } from '@tanstack/react-router' -import { postsQueryOptions } from '../utils/posts' +import { postsFnQuery } from '../utils/posts' export const Route = createFileRoute('/posts')({ loader: async ({ context }) => { - await context.queryClient.ensureQueryData(postsQueryOptions()) + await context.queryClient.ensureQueryData(postsFnQuery.queryOptions()) }, head: () => ({ meta: [{ title: 'Posts' }], @@ -13,7 +13,7 @@ export const Route = createFileRoute('/posts')({ }) function PostsComponent() { - const postsQuery = useSuspenseQuery(postsQueryOptions()) + const postsQuery = useSuspenseQuery(postsFnQuery.queryOptions()) return (
diff --git a/examples/react/start-basic-react-query/app/utils/posts.tsx b/examples/react/start-basic-react-query/app/utils/posts.tsx index cf52ce70ee..1307ed6119 100644 --- a/examples/react/start-basic-react-query/app/utils/posts.tsx +++ b/examples/react/start-basic-react-query/app/utils/posts.tsx @@ -1,7 +1,8 @@ import { queryOptions } from '@tanstack/react-query' import { notFound } from '@tanstack/react-router' -import { createServerFn } from '@tanstack/start' +import { createServerFn, Fetcher } from '@tanstack/start' import axios from 'redaxios' +import { serverFnQuery } from './serverFnQuery' export type PostType = { id: string @@ -18,11 +19,7 @@ export const fetchPosts = createServerFn({ method: 'GET' }).handler( }, ) -export const postsQueryOptions = () => - queryOptions({ - queryKey: ['posts'], - queryFn: () => fetchPosts(), - }) +export const postsFnQuery = serverFnQuery(fetchPosts) export const fetchPost = createServerFn({ method: 'GET' }) .validator((d: string) => d) @@ -42,8 +39,4 @@ export const fetchPost = createServerFn({ method: 'GET' }) return post }) -export const postQueryOptions = (postId: string) => - queryOptions({ - queryKey: ['post', postId], - queryFn: () => fetchPost({ data: postId }), - }) +export const postFnQuery = serverFnQuery(fetchPost) diff --git a/examples/react/start-basic-react-query/app/utils/serverFnQuery.ts b/examples/react/start-basic-react-query/app/utils/serverFnQuery.ts new file mode 100644 index 0000000000..77c857121c --- /dev/null +++ b/examples/react/start-basic-react-query/app/utils/serverFnQuery.ts @@ -0,0 +1,87 @@ +import { + DataTag, + queryOptions, + UnusedSkipTokenOptions, + UseQueryOptions, +} from '@tanstack/react-query' +import { Fetcher, FetcherParameters, FetcherResult } from '@tanstack/start' + +export type AnyFetcher = Fetcher + +export type ServerFnQueryData< + TMiddlewares, + TResponse, + TFullResponse extends boolean, +> = Awaited> + +export type ServerFnQueryOptions< + TMiddlewares, + TResponse, + TFullResponse extends boolean, +> = Omit< + UseQueryOptions< + ServerFnQueryData, + Error, + ServerFnQueryData, + [string, ServerFnQueryData] + >, + 'queryKey' | 'queryFn' +> + +export type ServerFnQueryOptionsResult< + TMiddlewares, + TResponse, + TFullResponse extends boolean, +> = ServerFnQueryOptions & { + queryKey: DataTag< + [string, ServerFnQueryData], + ServerFnQueryData, + Error + > +} + +export type ServerFnQueryParameters< + TMiddlewares, + TValidator, + TResponse, + TFullResponse extends boolean, + TParams = FetcherParameters, +> = { + [K in keyof TParams]: TParams[K] & + ServerFnQueryOptions +} + +export interface ServerFnQuery { + queryOptions: ( + ...args: ServerFnQueryParameters< + TMiddlewares, + TValidator, + TResponse, + TFullResponse + > + ) => ServerFnQueryOptionsResult +} + +export const serverFnQuery = ( + fetcher: Fetcher, +): ServerFnQuery => { + if (!('functionId' in fetcher) || typeof fetcher.functionId !== 'string') { + throw new Error() + } + + return { + queryOptions: (options) => { + const queryKey = [fetcher.functionId] + + if (options?.data != null) { + queryKey.push(options.data) + } + + return queryOptions({ + ...options, + queryKey: queryKey as any, + queryFn: fetcher as any, + }) + }, + } as ServerFnQuery +} diff --git a/packages/start-client/src/createServerFn.ts b/packages/start-client/src/createServerFn.ts index 3783fc2775..522b6719f9 100644 --- a/packages/start-client/src/createServerFn.ts +++ b/packages/start-client/src/createServerFn.ts @@ -38,10 +38,12 @@ export type CompiledFetcherFnOptions = { context?: any } -export type Fetcher = - undefined extends IntersectAllValidatorInputs - ? OptionalFetcher - : RequiredFetcher +export interface Fetcher + extends FetcherBase { + ( + ...args: FetcherParameters + ): FetcherResult +} export interface FetcherBase { url: string @@ -53,7 +55,7 @@ export interface FetcherBase { }) => Promise } -export type FetchResult< +export type FetcherResult< TMiddlewares, TResponse, TFullResponse extends boolean, @@ -61,6 +63,27 @@ export type FetchResult< ? Promise> : Promise> +export type FetcherParameters< + TMiddlewares, + TValidator, + TFullResponse extends boolean, +> = + undefined extends IntersectAllValidatorInputs + ? [ + options?: OptionalFetcherDataOptions< + TMiddlewares, + TValidator, + TFullResponse + >, + ] + : [ + options: RequiredFetcherDataOptions< + TMiddlewares, + TValidator, + TFullResponse + >, + ] + export interface OptionalFetcher extends FetcherBase { ( @@ -69,14 +92,14 @@ export interface OptionalFetcher TValidator, TFullResponse >, - ): FetchResult + ): FetcherResult } export interface RequiredFetcher extends FetcherBase { ( opts: RequiredFetcherDataOptions, - ): FetchResult + ): FetcherResult } export type FetcherBaseOptions = { diff --git a/packages/start-client/src/index.tsx b/packages/start-client/src/index.tsx index 736b4892bc..ca35384591 100644 --- a/packages/start-client/src/index.tsx +++ b/packages/start-client/src/index.tsx @@ -11,6 +11,8 @@ export { type JsonResponse, type ServerFn as FetchFn, type ServerFnCtx as FetchFnCtx, + type FetcherParameters, + type FetcherResult, type CompiledFetcherFnOptions, type CompiledFetcherFn, type Fetcher,