{"version":3,"file":"query-options.mjs","sources":["../src/query-options.ts"],"sourcesContent":["import type {\n  DefaultError,\n  InitialDataFunction,\n  NonUndefinedGuard,\n  OmitKeyof,\n  QueryFunction,\n  QueryKey,\n  QueryKeyWithDataTag,\n  SkipToken,\n} from '@tanstack/query-core'\nimport type { CreateQueryOptions } from './types'\n\n/**\n * The options accepted by the `queryOptions` overload selected when no `initialData` is set — `data` may be\n * `undefined` while the query is `pending`.\n *\n * @template TQueryFnData - The type your `queryFn` resolves to.\n * @template TError - The type of errors your `queryFn` may throw.\n * @template TData - The type `data` ends up as after `select` runs.\n * @template TQueryKey - The type of your `queryKey`.\n */\nexport type UndefinedInitialDataOptions<\n  TQueryFnData = unknown,\n  TError = DefaultError,\n  TData = TQueryFnData,\n  TQueryKey extends QueryKey = QueryKey,\n> = CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {\n  /**\n   * If set, this value will be used as the initial data for the query cache (as long as the query hasn't been\n   * created or cached yet). If set to a function, the function will be called **once** during the shared/root\n   * query initialization, and be expected to synchronously return the initial data. Initial data is\n   * considered stale by default unless a `staleTime` has been set. `initialData` **is persisted** to the\n   * cache.\n   */\n  initialData?:\n    | undefined\n    | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>\n    | NonUndefinedGuard<TQueryFnData>\n}\n\n/**\n * The options accepted by the `queryOptions` overload selected when no `initialData` is set and `queryFn` is\n * not `skipToken` — same as {@link UndefinedInitialDataOptions}, but `queryFn` may not be `skipToken`.\n *\n * @template TQueryFnData - The type your `queryFn` resolves to.\n * @template TError - The type of errors your `queryFn` may throw.\n * @template TData - The type `data` ends up as after `select` runs.\n * @template TQueryKey - The type of your `queryKey`.\n */\nexport type UnusedSkipTokenOptions<\n  TQueryFnData = unknown,\n  TError = DefaultError,\n  TData = TQueryFnData,\n  TQueryKey extends QueryKey = QueryKey,\n> = OmitKeyof<\n  CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n  'queryFn'\n> & {\n  /**\n   * `skipToken` is not allowed as a value here — this overload is selected when no `initialData` is set. If\n   * you don't intend to run the query yet, set `enabled: false` — omitting `queryFn` alone still triggers a\n   * fetch that fails with \"Missing queryFn\" unless `enabled` is `false` or a default query function has been\n   * defined. A default query function only supplies `queryFn`; it doesn't defer the fetch on its own.\n   */\n  queryFn?: Exclude<\n    CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>['queryFn'],\n    SkipToken | undefined\n  >\n}\n\n/**\n * The options accepted by the `queryOptions` overload selected when `initialData` is set — `data` is never\n * `undefined` (unless a `select` changes `TData` to include `undefined`).\n *\n * @template TQueryFnData - The type your `queryFn` resolves to.\n * @template TError - The type of errors your `queryFn` may throw.\n * @template TData - The type `data` ends up as after `select` runs.\n * @template TQueryKey - The type of your `queryKey`.\n */\nexport type DefinedInitialDataOptions<\n  TQueryFnData = unknown,\n  TError = DefaultError,\n  TData = TQueryFnData,\n  TQueryKey extends QueryKey = QueryKey,\n> = Omit<\n  CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n  'queryFn'\n> & {\n  /**\n   * If set, this value will be used as the initial data for the query cache (as long as the query hasn't been\n   * created or cached yet). If set to a function, the function will be called **once** during the shared/root\n   * query initialization, and be expected to synchronously return the initial data. Initial data is\n   * considered stale by default unless a `staleTime` has been set. `initialData` **is persisted** to the\n   * cache.\n   */\n  initialData:\n    | NonUndefinedGuard<TQueryFnData>\n    | (() => NonUndefinedGuard<TQueryFnData>)\n  /**\n   * Optional here, but omitting it is only safe when no fetch will be attempted — for example with\n   * `enabled: false`, or when a default query function has been defined. Otherwise, an enabled query with no\n   * `queryFn` still tries to fetch and fails with a \"Missing queryFn\" error; `initialData` does not prevent this.\n   */\n  queryFn?: QueryFunction<TQueryFnData, TQueryKey>\n}\n\n/**\n * You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options\n * can be shared across functions and imperative APIs such as `queryClient.fetchQuery`. `options.queryKey` is\n * required and is the query key to generate options for.\n *\n * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined` (unless\n * a `select` changes `TData` to include `undefined`).\n *\n * @see {@link injectQuery} to run a query with these options.\n * @see [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.\n * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `injectQuery`,\n * with `initialData` set.\n * @returns The same options object, typed so that `queryKey` carries the inferred data type.\n *\n * @example\n * ```angular-ts\n * import { queryOptions, injectQuery } from '@tanstack/angular-query-experimental'\n *\n * export const postsOptions = queryOptions({\n *   queryKey: ['posts'],\n *   queryFn: fetchPosts,\n *   initialData: [],\n * })\n *\n * @Component({\n *   selector: 'posts',\n *   template: `\n *     <!-- `postsQuery.data()` is never `undefined`, thanks to `initialData` — even if a refetch\n *     fails, so the list stays visible alongside the error. -->\n *     @if (postsQuery.isError()) {\n *       <span>Error: {{ postsQuery.error()?.message }}</span>\n *     }\n *     <ul>\n *       @for (post of postsQuery.data(); track post.id) {\n *         <li>{{ post.title }}</li>\n *       }\n *     </ul>\n *   `,\n * })\n * export class Posts {\n *   readonly postsQuery = injectQuery(() => postsOptions)\n * }\n * ```\n */\nexport function queryOptions<\n  TQueryFnData = unknown,\n  TError = DefaultError,\n  TData = TQueryFnData,\n  TQueryKey extends QueryKey = QueryKey,\n>(\n  options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,\n): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> &\n  QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>\n\n/**\n * You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options\n * can be shared across functions and imperative APIs such as `queryClient.fetchQuery`. `options.queryKey` is\n * required and is the query key to generate options for.\n *\n * @see {@link injectQuery} to run a query with these options.\n * @see [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.\n * @param options - The {@link UnusedSkipTokenOptions} to use — everything you can pass to `injectQuery`.\n * @returns The same options object, typed so that `queryKey` carries the inferred data type.\n *\n * @example\n * A parameterized factory, so the same options object can be reused per `id`:\n * ```angular-ts\n * import { queryOptions, injectQuery } from '@tanstack/angular-query-experimental'\n *\n * export const postOptions = (id: string) =>\n *   queryOptions({\n *     queryKey: ['post', id],\n *     queryFn: () => fetchPost(id),\n *   })\n *\n * @Component({\n *   selector: 'post',\n *   template: `\n *     @if (postQuery.isPending()) {\n *       Loading...\n *     } @else if (postQuery.isError()) {\n *       <span>Error: {{ postQuery.error()?.message }}</span>\n *     } @else {\n *       <h1>{{ postQuery.data().title }}</h1>\n *     }\n *   `,\n * })\n * export class Post {\n *   readonly id = signal('1')\n *   readonly postQuery = injectQuery(() => postOptions(this.id()))\n * }\n * ```\n */\nexport function queryOptions<\n  TQueryFnData = unknown,\n  TError = DefaultError,\n  TData = TQueryFnData,\n  TQueryKey extends QueryKey = QueryKey,\n>(\n  options: UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey>,\n): UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey> &\n  QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>\n\n/**\n * You can generally pass everything to `queryOptions` that you can also pass to `injectQuery`. These options\n * can be shared across functions and imperative APIs such as `queryClient.fetchQuery`. `options.queryKey` is\n * required and is the query key to generate options for.\n *\n * @see {@link injectQuery} to run a query with these options.\n * @see [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.\n * @param options - The {@link UndefinedInitialDataOptions} to use — everything you can pass to `injectQuery`.\n * @returns The same options object, typed so that `queryKey` carries the inferred data type.\n * @remarks This is the only overload that accepts `queryFn: skipToken`, shown below.\n *\n * @example\n * A parameterized factory, so the same options object can be reused per `id`:\n * ```angular-ts\n * import { queryOptions, injectQuery } from '@tanstack/angular-query-experimental'\n *\n * export const postOptions = (id: string) =>\n *   queryOptions({\n *     queryKey: ['post', id],\n *     queryFn: () => fetchPost(id),\n *   })\n *\n * @Component({\n *   selector: 'post',\n *   template: `\n *     @if (postQuery.isPending()) {\n *       Loading...\n *     } @else if (postQuery.isError()) {\n *       <span>Error: {{ postQuery.error()?.message }}</span>\n *     } @else {\n *       <h1>{{ postQuery.data().title }}</h1>\n *     }\n *   `,\n * })\n * export class Post {\n *   readonly id = signal('1')\n *   readonly postQuery = injectQuery(() => postOptions(this.id()))\n * }\n * ```\n *\n * @example\n * A factory that disables the query, type safe, until `postId` is set:\n * ```angular-ts\n * import { queryOptions, skipToken, injectQuery } from '@tanstack/angular-query-experimental'\n *\n * export const postOptions = (postId: number | undefined) =>\n *   queryOptions({\n *     queryKey: ['post', postId],\n *     queryFn: postId != null ? () => fetchPost(postId) : skipToken,\n *   })\n *\n * @Component({\n *   selector: 'post',\n *   template: `\n *     @if (postId() == null) {\n *       Select a post\n *     } @else if (postQuery.isPending()) {\n *       Loading...\n *     } @else if (postQuery.isError()) {\n *       <span>Error: {{ postQuery.error()?.message }}</span>\n *     } @else {\n *       <h1>{{ postQuery.data().title }}</h1>\n *     }\n *   `,\n * })\n * export class Post {\n *   readonly postId = signal<number | undefined>(undefined)\n *   readonly postQuery = injectQuery(() => postOptions(this.postId()))\n * }\n * ```\n */\nexport function queryOptions<\n  TQueryFnData = unknown,\n  TError = DefaultError,\n  TData = TQueryFnData,\n  TQueryKey extends QueryKey = QueryKey,\n>(\n  options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,\n): UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> &\n  QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>\n\nexport function queryOptions(options: unknown) {\n  return options\n}\n"],"names":[],"mappings":"AAkSO,SAAS,aAAa,SAAkB;AAC7C,SAAO;AACT;"}