UNPKG

@tanstack/angular-query-experimental

Version:

Signals for managing, caching and syncing asynchronous and remote data in Angular

124 lines (123 loc) 5.17 kB
import { Injector } from '@angular/core'; import { DefaultError, QueryKey } from '@tanstack/query-core'; import { CreateQueryOptions, CreateQueryResult, DefinedCreateQueryResult } from './types.js'; import { DefinedInitialDataOptions, UndefinedInitialDataOptions } from './query-options.js'; export interface InjectQueryOptions { /** * The `Injector` in which to create the query. * * If this is not provided, the current injection context will be used instead (via `inject`). */ injector?: Injector; } /** * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key. * * **Basic example** * ```ts * class ServiceOrComponent { * query = injectQuery(() => ({ * queryKey: ['repoData'], * queryFn: () => * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'), * })) * } * ``` * * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context. * In the example below, the query will be automatically enabled and executed when the filter signal changes * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled. * * **Reactive example** * ```ts * class ServiceOrComponent { * filter = signal('') * * todosQuery = injectQuery(() => ({ * queryKey: ['todos', this.filter()], * queryFn: () => fetchTodos(this.filter()), * // Signals can be combined with expressions * enabled: !!this.filter(), * })) * } * ``` * @param injectQueryFn - A function that returns query options. * @param options - Additional configuration * @returns The query result. * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries */ export declare function injectQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(injectQueryFn: () => DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, options?: InjectQueryOptions): DefinedCreateQueryResult<TData, TError>; /** * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key. * * **Basic example** * ```ts * class ServiceOrComponent { * query = injectQuery(() => ({ * queryKey: ['repoData'], * queryFn: () => * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'), * })) * } * ``` * * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context. * In the example below, the query will be automatically enabled and executed when the filter signal changes * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled. * * **Reactive example** * ```ts * class ServiceOrComponent { * filter = signal('') * * todosQuery = injectQuery(() => ({ * queryKey: ['todos', this.filter()], * queryFn: () => fetchTodos(this.filter()), * // Signals can be combined with expressions * enabled: !!this.filter(), * })) * } * ``` * @param injectQueryFn - A function that returns query options. * @param options - Additional configuration * @returns The query result. * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries */ export declare function injectQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(injectQueryFn: () => UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, options?: InjectQueryOptions): CreateQueryResult<TData, TError>; /** * Injects a query: a declarative dependency on an asynchronous source of data that is tied to a unique key. * * **Basic example** * ```ts * class ServiceOrComponent { * query = injectQuery(() => ({ * queryKey: ['repoData'], * queryFn: () => * this.#http.get<Response>('https://api.github.com/repos/tanstack/query'), * })) * } * ``` * * Similar to `computed` from Angular, the function passed to `injectQuery` will be run in the reactive context. * In the example below, the query will be automatically enabled and executed when the filter signal changes * to a truthy value. When the filter signal changes back to a falsy value, the query will be disabled. * * **Reactive example** * ```ts * class ServiceOrComponent { * filter = signal('') * * todosQuery = injectQuery(() => ({ * queryKey: ['todos', this.filter()], * queryFn: () => fetchTodos(this.filter()), * // Signals can be combined with expressions * enabled: !!this.filter(), * })) * } * ``` * @param injectQueryFn - A function that returns query options. * @param options - Additional configuration * @returns The query result. * @see https://tanstack.com/query/latest/docs/framework/angular/guides/queries */ export declare function injectQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(injectQueryFn: () => CreateQueryOptions<TQueryFnData, TError, TData, TQueryKey>, options?: InjectQueryOptions): CreateQueryResult<TData, TError>;