{"version":3,"file":"providers.mjs","sources":["../src/providers.ts"],"sourcesContent":["import { DestroyRef, InjectionToken, inject } from '@angular/core'\nimport { QueryClient } from '@tanstack/query-core'\nimport type { Provider } from '@angular/core'\n\n/**\n * Usually {@link provideTanStackQuery} is used once to set up TanStack Query and the\n * [`QueryClient`](https://tanstack.com/query/latest/docs/reference/QueryClient) for the entire application —\n * it calls `provideQueryClient` internally. Use `provideQueryClient` directly to provide a different\n * `QueryClient` instance for part of the application, or for unit testing.\n * @param queryClient - A `QueryClient` instance, or an `InjectionToken` which provides a `QueryClient`.\n * @returns A provider object that can be used to provide the `QueryClient` instance.\n *\n * @example\n * Providing a test-only `QueryClient` in a component test, without wiring up `provideTanStackQuery`'s other\n * defaults:\n * ```ts\n * TestBed.configureTestingModule({\n *   providers: [provideQueryClient(new QueryClient())],\n * })\n * ```\n */\nexport function provideQueryClient(\n  queryClient: QueryClient | InjectionToken<QueryClient>,\n): Provider {\n  return {\n    provide: QueryClient,\n    useFactory: () => {\n      const client =\n        queryClient instanceof InjectionToken\n          ? inject(queryClient)\n          : queryClient\n      // Unmount the query client on injector destroy\n      inject(DestroyRef).onDestroy(() => client.unmount())\n      client.mount()\n      return client\n    },\n  }\n}\n\n/**\n * Sets up providers necessary to enable TanStack Query functionality for Angular applications. Allows\n * configuring a `QueryClient` and optional features such as developer tools.\n *\n * @see https://tanstack.com/query/v5/docs/framework/angular/quick-start\n * @see {@link withDevtools}\n * @param queryClient - A `QueryClient` instance, or an `InjectionToken` which provides a `QueryClient`.\n * @param features - Optional features to configure additional Query functionality.\n * @returns A set of providers to set up TanStack Query.\n *\n * @example\n * ```ts\n * import { provideTanStackQuery, QueryClient } from '@tanstack/angular-query-experimental'\n *\n * bootstrapApplication(AppComponent, {\n *   providers: [provideTanStackQuery(new QueryClient())],\n * })\n * ```\n *\n * @example\n * The same, in an `NgModule`-based application:\n * ```ts\n * import { provideTanStackQuery, QueryClient } from '@tanstack/angular-query-experimental'\n *\n * @NgModule({\n *   declarations: [AppComponent],\n *   imports: [BrowserModule],\n *   providers: [provideTanStackQuery(new QueryClient())],\n *   bootstrap: [AppComponent],\n * })\n * export class AppModule {}\n * ```\n *\n * @example\n * Enabling optional developer tools by adding `withDevtools` — by default, the tools are then loaded when\n * your app is in development mode:\n * ```ts\n * import {\n *   provideTanStackQuery,\n *   withDevtools,\n *   QueryClient,\n * } from '@tanstack/angular-query-experimental'\n *\n * bootstrapApplication(AppComponent, {\n *   providers: [provideTanStackQuery(new QueryClient(), withDevtools())],\n * })\n * ```\n *\n * @example\n * Using an `InjectionToken` for the `QueryClient` — an advanced optimization that lets TanStack Query be\n * absent from the main application bundle, useful for including it on lazy-loaded routes only while still\n * sharing a `QueryClient`. This is a small optimization; for most applications it's preferable to provide\n * the `QueryClient` in the main application config, as in the examples above:\n * ```ts\n * export const MY_QUERY_CLIENT = new InjectionToken('', {\n *   factory: () => new QueryClient(),\n * })\n *\n * // In a lazy loaded route or lazy loaded component's providers array:\n * providers: [provideTanStackQuery(MY_QUERY_CLIENT)]\n * ```\n */\nexport function provideTanStackQuery(\n  queryClient: QueryClient | InjectionToken<QueryClient>,\n  ...features: Array<QueryFeatures>\n): Array<Provider> {\n  return [\n    provideQueryClient(queryClient),\n    features.map((feature) => feature.ɵproviders),\n  ]\n}\n\n/**\n * Sets up providers necessary to enable TanStack Query functionality for Angular applications.\n *\n * Allows configuring a `QueryClient`.\n * @see https://tanstack.com/query/v5/docs/framework/angular/quick-start\n * @param queryClient - A `QueryClient` instance.\n * @returns A set of providers to set up TanStack Query.\n * @deprecated Use `provideTanStackQuery` instead.\n */\nexport function provideAngularQuery(queryClient: QueryClient): Array<Provider> {\n  return provideTanStackQuery(queryClient)\n}\n\nconst queryFeatures = ['Devtools', 'PersistQueryClient'] as const\n\ntype QueryFeatureKind = (typeof queryFeatures)[number]\n\n/**\n * Helper type to represent a Query feature.\n */\nexport interface QueryFeature<TFeatureKind extends QueryFeatureKind> {\n  ɵkind: TFeatureKind\n  ɵproviders: Array<Provider>\n}\n\n/**\n * Helper function to create an object that represents a Query feature.\n * @param kind - The kind of feature, e.g. `'Devtools'`.\n * @param providers - The Angular providers this feature contributes to `provideTanStackQuery`.\n * @returns A Query feature.\n */\nexport function queryFeature<TFeatureKind extends QueryFeatureKind>(\n  kind: TFeatureKind,\n  providers: Array<Provider>,\n): QueryFeature<TFeatureKind> {\n  return { ɵkind: kind, ɵproviders: providers }\n}\n\n/**\n * A type alias that represents a feature which enables developer tools.\n * The type is used to describe the return value of the `withDevtools` function.\n * @see {@link withDevtools}\n */\nexport type DevtoolsFeature = QueryFeature<'Devtools'>\n\n/**\n * A type alias that represents a feature which enables persistence.\n * The type is used to describe the return value of the `withPersistQueryClient` function.\n */\nexport type PersistQueryClientFeature = QueryFeature<'PersistQueryClient'>\n\n/**\n * A type alias that represents all Query features available for use with `provideTanStackQuery`.\n * Features can be enabled by adding special functions to the `provideTanStackQuery` call.\n * See documentation for each symbol to find corresponding function name. See also `provideTanStackQuery`\n * documentation on how to use those functions.\n * @see {@link provideTanStackQuery}\n */\nexport type QueryFeatures = DevtoolsFeature | PersistQueryClientFeature\n"],"names":[],"mappings":";;AAqBO,SAAS,mBACd,aACU;AACV,SAAO;AAAA,IACL,SAAS;AAAA,IACT,YAAY,MAAM;AAChB,YAAM,SACJ,uBAAuB,iBACnB,OAAO,WAAW,IAClB;AAEN,aAAO,UAAU,EAAE,UAAU,MAAM,OAAO,SAAS;AACnD,aAAO,MAAA;AACP,aAAO;AAAA,IACT;AAAA,EAAA;AAEJ;AAgEO,SAAS,qBACd,gBACG,UACc;AACjB,SAAO;AAAA,IACL,mBAAmB,WAAW;AAAA,IAC9B,SAAS,IAAI,CAAC,YAAY,QAAQ,UAAU;AAAA,EAAA;AAEhD;AAWO,SAAS,oBAAoB,aAA2C;AAC7E,SAAO,qBAAqB,WAAW;AACzC;AAoBO,SAAS,aACd,MACA,WAC4B;AAC5B,SAAO,EAAE,OAAO,MAAM,YAAY,UAAA;AACpC;"}