diff --git a/packages/angular-query-experimental/src/index.ts b/packages/angular-query-experimental/src/index.ts index da68067d33c..360be005e66 100644 --- a/packages/angular-query-experimental/src/index.ts +++ b/packages/angular-query-experimental/src/index.ts @@ -8,24 +8,57 @@ export * from './types' export type { DefinedInitialDataOptions, UndefinedInitialDataOptions, + UnusedSkipTokenOptions, } from './query-options' export { queryOptions } from './query-options' -export { mutationOptions } from './mutation-options' + export type { CreateMutationOptions } from './mutation-options' +export { mutationOptions } from './mutation-options' export type { DefinedInitialDataInfiniteOptions, UndefinedInitialDataInfiniteOptions, + UnusedSkipTokenInfiniteOptions, } from './infinite-query-options' export { infiniteQueryOptions } from './infinite-query-options' -export * from './inject-infinite-query' -export * from './inject-is-fetching' -export * from './inject-is-mutating' -export * from './inject-is-restoring' -export * from './inject-mutation' -export * from './inject-mutation-state' -export * from './inject-queries' -export * from './inject-query' -export * from './inject-query-client' -export * from './providers' +export type { InjectInfiniteQueryOptions } from './inject-infinite-query' +export { injectInfiniteQuery } from './inject-infinite-query' + +export type { InjectIsFetchingOptions } from './inject-is-fetching' +export { injectIsFetching } from './inject-is-fetching' + +export type { InjectIsMutatingOptions } from './inject-is-mutating' +export { injectIsMutating } from './inject-is-mutating' + +export { injectIsRestoring, provideIsRestoring } from './inject-is-restoring' + +export type { InjectMutationOptions } from './inject-mutation' +export { injectMutation } from './inject-mutation' + +export type { InjectMutationStateOptions } from './inject-mutation-state' +export { injectMutationState } from './inject-mutation-state' + +export type { QueriesOptions, QueriesResults } from './inject-queries' +export { injectQueries } from './inject-queries' + +export type { InjectQueryOptions } from './inject-query' +export { injectQuery } from './inject-query' + +export { injectQueryClient } from './inject-query-client' + +export type { + DeveloperToolsFeature, + DevtoolsOptions, + PersistQueryClientFeature, + QueryFeature, + QueryFeatureKind, + QueryFeatures, +} from './providers' +export { + provideQueryClient, + provideTanStackQuery, + queryFeature, + queryFeatures, + withDevtools, +} from './providers' diff --git a/packages/angular-query-experimental/src/infinite-query-options.ts b/packages/angular-query-experimental/src/infinite-query-options.ts index 266df37910e..5b8844c78f6 100644 --- a/packages/angular-query-experimental/src/infinite-query-options.ts +++ b/packages/angular-query-experimental/src/infinite-query-options.ts @@ -2,13 +2,13 @@ import type { DataTag, DefaultError, InfiniteData, + InitialDataFunction, + OmitKeyof, QueryKey, + SkipToken, } from '@tanstack/query-core' -import type { CreateInfiniteQueryOptions, NonUndefinedGuard } from './types' +import type { CreateInfiniteQueryOptions } from './types' -/** - * @public - */ export type UndefinedInitialDataInfiniteOptions< TQueryFnData, TError = DefaultError, @@ -23,12 +23,46 @@ export type UndefinedInitialDataInfiniteOptions< TQueryKey, TPageParam > & { - initialData?: undefined + initialData?: + | undefined + | NonUndefinedGuard> + | InitialDataFunction< + NonUndefinedGuard> + > } -/** - * @public - */ +export type UnusedSkipTokenInfiniteOptions< + TQueryFnData, + TError = DefaultError, + TData = InfiniteData, + TQueryKey extends QueryKey = QueryKey, + TPageParam = unknown, +> = OmitKeyof< + CreateInfiniteQueryOptions< + TQueryFnData, + TError, + TData, + TQueryFnData, + TQueryKey, + TPageParam + >, + 'queryFn' +> & { + queryFn?: Exclude< + CreateInfiniteQueryOptions< + TQueryFnData, + TError, + TData, + TQueryFnData, + TQueryKey, + TPageParam + >['queryFn'], + SkipToken | undefined + > +} + +type NonUndefinedGuard = T extends undefined ? never : T + export type DefinedInitialDataInfiniteOptions< TQueryFnData, TError = DefaultError, @@ -46,6 +80,7 @@ export type DefinedInitialDataInfiniteOptions< initialData: | NonUndefinedGuard> | (() => NonUndefinedGuard>) + | undefined } /** @@ -77,7 +112,39 @@ export function infiniteQueryOptions< TQueryKey, TPageParam > & { - queryKey: DataTag> + queryKey: DataTag, TError> +} + +/** + * Allows to share and re-use infinite query options in a type-safe way. + * + * The `queryKey` will be tagged with the type from `queryFn`. + * @param options - The infinite query options to tag with the type from `queryFn`. + * @returns The tagged infinite query options. + * @public + */ +export function infiniteQueryOptions< + TQueryFnData, + TError = DefaultError, + TData = InfiniteData, + TQueryKey extends QueryKey = QueryKey, + TPageParam = unknown, +>( + options: UnusedSkipTokenInfiniteOptions< + TQueryFnData, + TError, + TData, + TQueryKey, + TPageParam + >, +): UnusedSkipTokenInfiniteOptions< + TQueryFnData, + TError, + TData, + TQueryKey, + TPageParam +> & { + queryKey: DataTag, TError> } /** @@ -109,7 +176,7 @@ export function infiniteQueryOptions< TQueryKey, TPageParam > & { - queryKey: DataTag> + queryKey: DataTag, TError> } /** diff --git a/packages/angular-query-experimental/src/query-options.ts b/packages/angular-query-experimental/src/query-options.ts index 3dc91a6d0aa..2c48f49711b 100644 --- a/packages/angular-query-experimental/src/query-options.ts +++ b/packages/angular-query-experimental/src/query-options.ts @@ -2,34 +2,55 @@ import type { DataTag, DefaultError, InitialDataFunction, + OmitKeyof, + QueryFunction, QueryKey, + SkipToken, } from '@tanstack/query-core' -import type { CreateQueryOptions, NonUndefinedGuard } from './types' +import type { CreateQueryOptions } from './types' -/** - * @public - */ export type UndefinedInitialDataOptions< TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, > = CreateQueryOptions & { - initialData?: undefined | InitialDataFunction> + initialData?: + | undefined + | InitialDataFunction> + | NonUndefinedGuard } -/** - * @public - */ +export type UnusedSkipTokenOptions< + TQueryFnData = unknown, + TError = DefaultError, + TData = TQueryFnData, + TQueryKey extends QueryKey = QueryKey, +> = OmitKeyof< + CreateQueryOptions, + 'queryFn' +> & { + queryFn?: Exclude< + CreateQueryOptions['queryFn'], + SkipToken | undefined + > +} + +type NonUndefinedGuard = T extends undefined ? never : T + export type DefinedInitialDataOptions< TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey, -> = CreateQueryOptions & { +> = Omit< + CreateQueryOptions, + 'queryFn' +> & { initialData: | NonUndefinedGuard | (() => NonUndefinedGuard) + queryFn?: QueryFunction } /** @@ -62,7 +83,40 @@ export function queryOptions< >( options: DefinedInitialDataOptions, ): DefinedInitialDataOptions & { - queryKey: DataTag + 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 } /** @@ -95,7 +149,7 @@ export function queryOptions< >( options: UndefinedInitialDataOptions, ): UndefinedInitialDataOptions & { - queryKey: DataTag + queryKey: DataTag } /** diff --git a/packages/angular-query-experimental/src/types.ts b/packages/angular-query-experimental/src/types.ts index 5c0e0964577..eb65ae5b565 100644 --- a/packages/angular-query-experimental/src/types.ts +++ b/packages/angular-query-experimental/src/types.ts @@ -308,8 +308,3 @@ export type CreateMutationResult< >, > = BaseMutationNarrowing & MapToSignals> - -/** - * @public - */ -export type NonUndefinedGuard = T extends undefined ? never : T