@tanstack/angular-query-experimental
Version:
Signals for managing, caching and syncing asynchronous and remote data in Angular
1 lines • 7.53 kB
Source Map (JSON)
{"version":3,"file":"inject-query.mjs","sources":["../src/inject-query.ts"],"sourcesContent":["import { QueryObserver } from '@tanstack/query-core'\nimport {\n Injector,\n assertInInjectionContext,\n inject,\n runInInjectionContext,\n} from '@angular/core'\nimport { createBaseQuery } from './create-base-query'\nimport type { DefaultError, QueryKey } from '@tanstack/query-core'\nimport type {\n CreateQueryOptions,\n CreateQueryResult,\n DefinedCreateQueryResult,\n} from './types'\nimport type {\n DefinedInitialDataOptions,\n UndefinedInitialDataOptions,\n} from './query-options'\n\nexport interface InjectQueryOptions {\n /**\n * The `Injector` in which to create the query.\n *\n * If this is not provided, the current injection context will be used instead (via `inject`).\n */\n injector?: Injector\n}\n\n/**\n * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.\n *\n * **Basic example**\n * ```ts\n * class ServiceOrComponent {\n * query = injectQuery(() => ({\n * queryKey: ['repoData'],\n * queryFn: () =>\n * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),\n * }))\n * }\n * ```\n *\n * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.\n * In the example below, the query will be automatically enabled and executed when the filter signal changes\n * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.\n *\n * **Reactive example**\n * ```ts\n * class ServiceOrComponent {\n * filter = signal('')\n *\n * todosQuery = injectQuery(() => ({\n * queryKey: ['todos', this.filter()],\n * queryFn: () => fetchTodos(this.filter()),\n * // Signals can be combined with expressions\n * enabled: !!this.filter(),\n * }))\n * }\n * ```\n * @param injectQueryFn - A function that returns query options.\n * @param options - Additional configuration\n * @returns The query result.\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries\n */\nexport function injectQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n injectQueryFn: () => DefinedInitialDataOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >,\n options?: InjectQueryOptions,\n): DefinedCreateQueryResult<TData, TError>\n\n/**\n * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.\n *\n * **Basic example**\n * ```ts\n * class ServiceOrComponent {\n * query = injectQuery(() => ({\n * queryKey: ['repoData'],\n * queryFn: () =>\n * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),\n * }))\n * }\n * ```\n *\n * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.\n * In the example below, the query will be automatically enabled and executed when the filter signal changes\n * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.\n *\n * **Reactive example**\n * ```ts\n * class ServiceOrComponent {\n * filter = signal('')\n *\n * todosQuery = injectQuery(() => ({\n * queryKey: ['todos', this.filter()],\n * queryFn: () => fetchTodos(this.filter()),\n * // Signals can be combined with expressions\n * enabled: !!this.filter(),\n * }))\n * }\n * ```\n * @param injectQueryFn - A function that returns query options.\n * @param options - Additional configuration\n * @returns The query result.\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries\n */\nexport function injectQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n injectQueryFn: () => UndefinedInitialDataOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >,\n options?: InjectQueryOptions,\n): CreateQueryResult<TData, TError>\n\n/**\n * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.\n *\n * **Basic example**\n * ```ts\n * class ServiceOrComponent {\n * query = injectQuery(() => ({\n * queryKey: ['repoData'],\n * queryFn: () =>\n * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),\n * }))\n * }\n * ```\n *\n * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.\n * In the example below, the query will be automatically enabled and executed when the filter signal changes\n * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.\n *\n * **Reactive example**\n * ```ts\n * class ServiceOrComponent {\n * filter = signal('')\n *\n * todosQuery = injectQuery(() => ({\n * queryKey: ['todos', this.filter()],\n * queryFn: () => fetchTodos(this.filter()),\n * // Signals can be combined with expressions\n * enabled: !!this.filter(),\n * }))\n * }\n * ```\n * @param injectQueryFn - A function that returns query options.\n * @param options - Additional configuration\n * @returns The query result.\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries\n */\nexport function injectQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n injectQueryFn: () => CreateQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey\n >,\n options?: InjectQueryOptions,\n): CreateQueryResult<TData, TError>\n\n/**\n * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key.\n *\n * **Basic example**\n * ```ts\n * class ServiceOrComponent {\n * query = injectQuery(() => ({\n * queryKey: ['repoData'],\n * queryFn: () =>\n * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'),\n * }))\n * }\n * ```\n *\n * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context.\n * In the example below, the query will be automatically enabled and executed when the filter signal changes\n * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled.\n *\n * **Reactive example**\n * ```ts\n * class ServiceOrComponent {\n * filter = signal('')\n *\n * todosQuery = injectQuery(() => ({\n * queryKey: ['todos', this.filter()],\n * queryFn: () => fetchTodos(this.filter()),\n * // Signals can be combined with expressions\n * enabled: !!this.filter(),\n * }))\n * }\n * ```\n * @param injectQueryFn - A function that returns query options.\n * @param options - Additional configuration\n * @returns The query result.\n * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries\n */\nexport function injectQuery(\n injectQueryFn: () => CreateQueryOptions,\n options?: InjectQueryOptions,\n) {\n !options?.injector && assertInInjectionContext(injectQuery)\n return runInInjectionContext(options?.injector ?? inject(Injector), () =>\n createBaseQuery(injectQueryFn, QueryObserver),\n ) as unknown as CreateQueryResult\n}\n"],"names":[],"mappings":";;;AAyNgB,SAAA,YACd,eACA,SACA;AACC,IAAA,mCAAS,aAAY,yBAAyB,WAAW;AACnD,SAAA;AAAA,KAAsB,mCAAS,aAAY,OAAO,QAAQ;AAAA,IAAG,MAClE,gBAAgB,eAAe,aAAa;AAAA,EAC9C;AACF;"}