Skip to content

Commit 2459701

Browse files
authored
feat(angular-query): add withDevtools and injectDevtoolsPanel
1 parent 5ce9959 commit 2459701

File tree

67 files changed

+1620
-154
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+1620
-154
lines changed

docs/framework/angular/devtools.md

Lines changed: 77 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,62 +3,102 @@ id: devtools
33
title: Devtools
44
---
55

6-
## Install and Import the Devtools
6+
## Enable devtools
77

8-
The devtools are a separate package that you need to install:
8+
The devtools help you debug and inspect your queries and mutations. You can enable the devtools by adding `withDevtools` to `provideTanStackQuery`.
99

10-
```bash
11-
npm i @tanstack/angular-query-devtools-experimental
12-
```
13-
14-
or
10+
By default, the devtools are enabled when Angular [`isDevMode`](https://angular.dev/api/core/isDevMode) returns true. So you don't need to worry about excluding them during a production build. The core tools are lazily loaded and excluded from bundled code. In most cases, all you'll need to do is add `withDevtools()` to `provideTanStackQuery` without any additional configuration.
1511

16-
```bash
17-
pnpm add @tanstack/angular-query-devtools-experimental
12+
```ts
13+
import {
14+
QueryClient,
15+
provideTanStackQuery,
16+
withDevtools,
17+
} from '@tanstack/angular-query-experimental'
18+
19+
export const appConfig: ApplicationConfig = {
20+
providers: [provideTanStackQuery(new QueryClient(), withDevtools())],
21+
}
1822
```
1923

20-
or
24+
## Configuring if devtools are loaded
2125

22-
```bash
23-
yarn add @tanstack/angular-query-devtools-experimental
24-
```
26+
If you need more control over when devtools are loaded, you can use the `loadDevtools` option. This is particularly useful if you want to load devtools based on environment configurations. For instance, you might have a test environment running in production mode but still require devtools to be available.
27+
28+
When not setting the option or setting it to 'auto', the devtools will be loaded when Angular is in development mode.
2529

26-
or
30+
```ts
31+
provideTanStackQuery(new QueryClient(), withDevtools())
2732

28-
```bash
29-
bun add @tanstack/angular-query-devtools-experimental
33+
// which is equivalent to
34+
provideTanStackQuery(
35+
new QueryClient(),
36+
withDevtools(() => ({ loadDevtools: 'auto' })),
37+
)
3038
```
3139

32-
You can import the devtools like this:
40+
When setting the option to true, the devtools will be loaded in both development and production mode.
3341

3442
```ts
35-
import { AngularQueryDevtools } from '@tanstack/angular-query-devtools-experimental'
43+
provideTanStackQuery(
44+
new QueryClient(),
45+
withDevtools(() => ({ loadDevtools: true })),
46+
)
3647
```
3748

38-
## Floating Mode
49+
When setting the option to false, the devtools will not be loaded.
3950

40-
Floating Mode will mount the devtools as a fixed, floating element in your app and provide a toggle in the corner of the screen to show and hide the devtools. This toggle state will be stored and remembered in localStorage across reloads.
41-
42-
Place the following code as high in your Angular app as you can. The closer it is to the root of the page, the better it will work!
51+
```ts
52+
provideTanStackQuery(
53+
new QueryClient(),
54+
withDevtools(() => ({ loadDevtools: false })),
55+
)
56+
```
4357

44-
```angular-ts
45-
import { AngularQueryDevtools } from '@tanstack/angular-query-devtools-experimental'
46-
import { Component } from '@angular/core';
58+
The `withDevtools` options are returned from a callback function to support reactivity through signals. In the following example
59+
a signal is created from a RxJS observable that listens for a keyboard shortcut. When the event is triggered, the devtools are lazily loaded.
60+
Using this technique allows you to support on-demand loading of the devtools even in production mode, without including the full tools in the bundled code.
4761

48-
@Component({
49-
selector: 'app-root',
50-
standalone: true,
51-
imports: [AngularQueryDevtools],
52-
template: `
53-
<angular-query-devtools initialIsOpen />
54-
`,
55-
})
62+
```ts
63+
@Injectable({ providedIn: 'root' })
64+
class DevtoolsOptionsManager {
65+
loadDevtools = toSignal(
66+
fromEvent<KeyboardEvent>(document, 'keydown').pipe(
67+
map(
68+
(event): boolean =>
69+
event.metaKey && event.ctrlKey && event.shiftKey && event.key === 'D',
70+
),
71+
scan((acc, curr) => acc || curr, false),
72+
),
73+
{
74+
initialValue: false,
75+
},
76+
)
77+
}
78+
79+
export const appConfig: ApplicationConfig = {
80+
providers: [
81+
provideHttpClient(),
82+
provideTanStackQuery(
83+
new QueryClient(),
84+
withDevtools(() => ({
85+
initialIsOpen: true,
86+
loadDevtools: inject(DevtoolsOptionsManager).loadDevtools(),
87+
})),
88+
),
89+
],
90+
}
5691
```
5792

5893
### Options
5994

60-
- `initialIsOpen: Boolean`
61-
- Set this `true` if you want the dev tools to default to being open
95+
Of these options `client`, `position`, `errorTypes`, `buttonPosition`, and `initialIsOpen` support reactivity through signals.
96+
97+
- `loadDevtools?: 'auto' | boolean`
98+
- Defaults to `auto`: lazily loads devtools when in development mode. Skips loading in production mode.
99+
- Use this to control if the devtools are loaded.
100+
- `initialIsOpen?: Boolean`
101+
- Set this to `true` if you want the tools to default to being open
62102
- `buttonPosition?: "top-left" | "top-right" | "bottom-left" | "bottom-right" | "relative"`
63103
- Defaults to `bottom-right`
64104
- The position of the TanStack logo to open and close the devtools panel
@@ -67,8 +107,8 @@ import { Component } from '@angular/core';
67107
- Defaults to `bottom`
68108
- The position of the Angular Query devtools panel
69109
- `client?: QueryClient`,
70-
- Use this to use a custom QueryClient. Otherwise, the QueryClient provided through provideAngularQuery() will be injected.
71-
- `errorTypes?: { name: string; initializer: (query: Query) => TError}`
110+
- Use this to use a custom QueryClient. Otherwise, the QueryClient provided through `provideTanStackQuery` will be injected.
111+
- `errorTypes?: { name: string; initializer: (query: Query) => TError}[]`
72112
- Use this to predefine some errors that can be triggered on your queries. Initializer will be called (with the specific query) when that error is toggled on from the UI. It must return an Error.
73113
- `styleNonce?: string`
74114
- Use this to pass a nonce to the style tag that is added to the document head. This is useful if you are using a Content Security Policy (CSP) nonce to allow inline styles.

docs/framework/angular/guides/default-query-function.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const queryClient = new QueryClient({
2525
})
2626

2727
bootstrapApplication(MyAppComponent, {
28-
providers: [provideAngularQuery(queryClient)],
28+
providers: [provideTanStackQuery(queryClient)],
2929
})
3030

3131
export class PostsComponent {

docs/framework/angular/guides/query-retries.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ const queryClient = new QueryClient({
4545
})
4646

4747
bootstrapApplication(AppComponent, {
48-
providers: [provideAngularQuery(queryClient)],
48+
providers: [provideTanStackQuery(queryClient)],
4949
})
5050
```
5151

docs/framework/angular/guides/window-focus-refetching.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ replace: { '@tanstack/react-query': '@tanstack/angular-query-experimental' }
1010
```ts
1111
bootstrapApplication(AppComponent, {
1212
providers: [
13-
provideAngularQuery(
13+
provideTanStackQuery(
1414
new QueryClient({
1515
defaultOptions: {
1616
queries: {

docs/framework/angular/overview.md

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ The `@tanstack/angular-query-experimental` package offers a 1st-class API for us
99

1010
## Feedback welcome!
1111

12-
We are in the process of getting to a stable API for Angular Query. If you have any feedback, please contact us at the [TanStack Discord](https://tlinz.com/discord) server or [visit this discussion](https://github.com/TanStack/query/discussions/6293) on Github.
12+
We are in the process of getting to a stable API for TanStack Query on Angular. If you have any feedback, please contact us at the [TanStack Discord](https://tlinz.com/discord) server or [visit this discussion](https://github.com/TanStack/query/discussions/6293) on Github.
1313

1414
## Supported Angular Versions
1515

16-
Angular Query is compatible with Angular v16 and higher.
16+
TanStack Query is compatible with Angular v16 and higher.
1717

1818
TanStack Query (FKA React Query) is often described as the missing data-fetching library for web applications, but in more technical terms, it makes **fetching, caching, synchronizing and updating server state** in your web applications a breeze.
1919

@@ -41,11 +41,11 @@ Once you grasp the nature of server state in your application, **even more chall
4141

4242
If you're not overwhelmed by that list, then that must mean that you've probably solved all of your server state problems already and deserve an award. However, if you are like a vast majority of people, you either have yet to tackle all or most of these challenges and we're only scratching the surface!
4343

44-
Angular Query is hands down one of the _best_ libraries for managing server state. It works amazingly well **out-of-the-box, with zero-config, and can be customized** to your liking as your application grows.
44+
TanStack Query is hands down one of the _best_ libraries for managing server state. It works amazingly well **out-of-the-box, with zero-config, and can be customized** to your liking as your application grows.
4545

46-
Angular Query allows you to defeat and overcome the tricky challenges and hurdles of _server state_ and control your app data before it starts to control you.
46+
TanStack Query allows you to defeat and overcome the tricky challenges and hurdles of _server state_ and control your app data before it starts to control you.
4747

48-
On a more technical note, Angular Query will likely:
48+
On a more technical note, TanStack Query will likely:
4949

5050
- Help you remove **many** lines of complicated and misunderstood code from your application and replace with just a handful of lines of Angular Query logic.
5151
- Make your application more maintainable and easier to build new features without worrying about wiring up new server state data sources
@@ -56,12 +56,11 @@ On a more technical note, Angular Query will likely:
5656

5757
## Enough talk, show me some code already!
5858

59-
In the example below, you can see Angular Query in its most basic and simple form being used to fetch the GitHub stats for the TanStack Query GitHub project itself:
59+
In the example below, you can see TanStack Query in its most basic and simple form being used to fetch the GitHub stats for the TanStack Query GitHub project itself:
6060

6161
[Open in StackBlitz](https://stackblitz.com/github/TanStack/query/tree/main/examples/angular/simple)
6262

6363
```angular-ts
64-
import { AngularQueryDevtools } from '@tanstack/angular-query-devtools-experimental'
6564
import { ChangeDetectionStrategy, Component, inject } from '@angular/core'
6665
import { HttpClient } from '@angular/common/http'
6766
import { CommonModule } from '@angular/common'
@@ -86,10 +85,7 @@ import { lastValueFrom } from 'rxjs'
8685
<strong>✨ {{ data.stargazers_count }}</strong>
8786
<strong>🍴 {{ data.forks_count }}</strong>
8887
}
89-
90-
<angular-query-devtools initialIsOpen />
91-
`,
92-
imports: [AngularQueryDevtools],
88+
`
9389
})
9490
export class SimpleExampleComponent {
9591
http = inject(HttpClient)
@@ -103,7 +99,7 @@ export class SimpleExampleComponent {
10399
}))
104100
}
105101
106-
type Response = {
102+
interface Response {
107103
name: string
108104
description: string
109105
subscribers_count: number
@@ -114,4 +110,4 @@ type Response = {
114110

115111
## You talked me into it, so what now?
116112

117-
- Learn Angular Query at your own pace with our amazingly thorough [Walkthrough Guide](../installation) and [API Reference](../reference/functions/injectquery)
113+
- Learn TanStack Query at your own pace with our amazingly thorough [Walkthrough Guide](../installation) and [API Reference](../reference/functions/injectquery)

docs/framework/angular/quick-start.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ If you're looking for a fully functioning example, please have a look at our [ba
1414
```ts
1515
import { provideHttpClient } from '@angular/common/http'
1616
import {
17-
provideAngularQuery,
17+
provideTanStackQuery,
1818
QueryClient,
1919
} from '@tanstack/angular-query-experimental'
2020

2121
bootstrapApplication(AppComponent, {
22-
providers: [provideHttpClient(), provideAngularQuery(new QueryClient())],
22+
providers: [provideHttpClient(), provideTanStackQuery(new QueryClient())],
2323
})
2424
```
2525

@@ -28,14 +28,14 @@ or in a NgModule-based app
2828
```ts
2929
import { provideHttpClient } from '@angular/common/http'
3030
import {
31-
provideAngularQuery,
31+
provideTanStackQuery,
3232
QueryClient,
3333
} from '@tanstack/angular-query-experimental'
3434

3535
@NgModule({
3636
declarations: [AppComponent],
3737
imports: [BrowserModule],
38-
providers: [provideAngularQuery(new QueryClient())],
38+
providers: [provideTanStackQuery(new QueryClient())],
3939
bootstrap: [AppComponent],
4040
})
4141
export class AppModule {}

examples/angular/basic/package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
"@angular-devkit/build-angular": "^17.3.8",
2424
"@angular/cli": "^17.3.8",
2525
"@angular/compiler-cli": "^17.3.12",
26-
"@tanstack/angular-query-devtools-experimental": "^5.59.20",
2726
"typescript": "5.3.3"
2827
}
2928
}

examples/angular/basic/src/app/app.component.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
sequences)
99
</strong>
1010
</p>
11-
<angular-query-devtools initialIsOpen />
1211
@if (postId() > -1) {
1312
<post [postId]="postId()" (setPostId)="postId.set($event)"></post>
1413
} @else {

examples/angular/basic/src/app/app.component.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { AngularQueryDevtools } from '@tanstack/angular-query-devtools-experimental'
21
import { ChangeDetectionStrategy, Component, signal } from '@angular/core'
32
import { PostComponent } from './components/post.component'
43
import { PostsComponent } from './components/posts.component'
@@ -8,7 +7,7 @@ import { PostsComponent } from './components/posts.component'
87
selector: 'basic-example',
98
standalone: true,
109
templateUrl: './app.component.html',
11-
imports: [AngularQueryDevtools, PostComponent, PostsComponent],
10+
imports: [PostComponent, PostsComponent],
1211
})
1312
export class BasicExampleComponent {
1413
postId = signal(-1)
Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
import { provideHttpClient, withFetch } from '@angular/common/http'
22
import {
33
QueryClient,
4-
provideAngularQuery,
4+
provideTanStackQuery,
5+
withDevtools,
56
} from '@tanstack/angular-query-experimental'
67
import type { ApplicationConfig } from '@angular/core'
78

89
export const appConfig: ApplicationConfig = {
910
providers: [
10-
provideAngularQuery(
11+
provideHttpClient(withFetch()),
12+
provideTanStackQuery(
1113
new QueryClient({
1214
defaultOptions: {
1315
queries: {
1416
gcTime: 1000 * 60 * 60 * 24, // 24 hours
1517
},
1618
},
1719
}),
20+
withDevtools(),
1821
),
19-
provideHttpClient(withFetch()),
2022
],
2123
}

0 commit comments

Comments
 (0)