import type { DataTag, DefaultError, InitialDataFunction, NonUndefinedGuard, OmitKeyof, QueryFunction, QueryKey, SkipToken, } from '@tanstack/query-core' import type { CreateQueryOptions } from './types' export type UndefinedInitialDataOptions< TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, > = CreateQueryOptions & { initialData?: | undefined | InitialDataFunction> | NonUndefinedGuard } export type UnusedSkipTokenOptions< TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, > = OmitKeyof< CreateQueryOptions, 'queryFn' > & { queryFn?: Exclude< CreateQueryOptions['queryFn'], SkipToken | undefined > } export type DefinedInitialDataOptions< TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, > = Omit< CreateQueryOptions, 'queryFn' > & { initialData: | NonUndefinedGuard | (() => NonUndefinedGuard) queryFn?: QueryFunction } /** * Allows to share and re-use query options in a type-safe way. * * The `queryKey` will be tagged with the type from `queryFn`. * * **Example** * * ```ts * const { queryKey } = queryOptions({ * queryKey: ['key'], * queryFn: () => Promise.resolve(5), * // ^? Promise * }) * * const queryClient = new QueryClient() * const data = queryClient.getQueryData(queryKey) * // ^? number | undefined * ``` * @param options - The query options to tag with the type from `queryFn`. * @returns The tagged query options. * @public */ export function queryOptions< TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, >( options: DefinedInitialDataOptions, ): DefinedInitialDataOptions & { queryKey: DataTag } /** * Allows to share and re-use query options in a type-safe way. * * The `queryKey` will be tagged with the type from `queryFn`. * * **Example** * * ```ts * const { queryKey } = queryOptions({ * queryKey: ['key'], * queryFn: () => Promise.resolve(5), * // ^? Promise * }) * * const queryClient = new QueryClient() * const data = queryClient.getQueryData(queryKey) * // ^? number | undefined * ``` * @param options - The query options to tag with the type from `queryFn`. * @returns The tagged query options. * @public */ export function queryOptions< TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, >( options: UnusedSkipTokenOptions, ): UnusedSkipTokenOptions & { queryKey: DataTag } /** * Allows to share and re-use query options in a type-safe way. * * The `queryKey` will be tagged with the type from `queryFn`. * * **Example** * * ```ts * const { queryKey } = queryOptions({ * queryKey: ['key'], * queryFn: () => Promise.resolve(5), * // ^? Promise * }) * * const queryClient = new QueryClient() * const data = queryClient.getQueryData(queryKey) * // ^? number | undefined * ``` * @param options - The query options to tag with the type from `queryFn`. * @returns The tagged query options. * @public */ export function queryOptions< TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, >( options: UndefinedInitialDataOptions, ): UndefinedInitialDataOptions & { queryKey: DataTag } /** * Allows to share and re-use query options in a type-safe way. * * The `queryKey` will be tagged with the type from `queryFn`. * * **Example** * * ```ts * const { queryKey } = queryOptions({ * queryKey: ['key'], * queryFn: () => Promise.resolve(5), * // ^? Promise * }) * * const queryClient = new QueryClient() * const data = queryClient.getQueryData(queryKey) * // ^? number | undefined * ``` * @param options - The query options to tag with the type from `queryFn`. * @returns The tagged query options. * @public */ export function queryOptions(options: unknown) { return options }