Skip to content

Commit 026aced

Browse files
committed
docs(general): 📘 update devtools and rxjs operator docs
1 parent 1408655 commit 026aced

File tree

1 file changed

+99
-80
lines changed

1 file changed

+99
-80
lines changed

README.md

+99-80
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Get rid of granular state management, manual refetching, and async spaghetti cod
1919
 Request Cancellation
2020
 Prefetching
2121
 Offline Support
22-
 Data Selectors
22+
 Data Selectors
23+
 SSR Support
2324

2425
<hr />
2526

@@ -55,9 +56,14 @@ Get rid of granular state management, manual refetching, and async spaghetti cod
5556

5657
## Installation
5758

59+
npm
5860
```
59-
npm i @ngneat/query
60-
yarn add @ngneat/query
61+
npm i -S @ngneat/query
62+
```
63+
64+
Yarn
65+
```
66+
yarn add --save @ngneat/query
6167
```
6268

6369
## Queries
@@ -67,11 +73,11 @@ yarn add @ngneat/query
6773
Inject the `QueryClientService` provider to get access to the query client [instance](https://tanstack.com/query/v4/docs/reference/QueryClient):
6874

6975
```ts
70-
import { QueryClientService } from '@ngneat/query';
76+
import { injectQueryClient } from '@ngneat/query';
7177

7278
@Injectable({ providedIn: 'root' })
7379
export class TodosService {
74-
private queryClient = inject(QueryClientService);
80+
private queryClient = injectQueryClient();
7581
}
7682
```
7783

@@ -331,7 +337,7 @@ You can provide the `QUERY_CLIENT_OPTIONS` provider to set the global [options](
331337
```ts
332338
import { provideQueryClientOptions } from '@ngneat/query';
333339

334-
{
340+
bootstrapApplication(AppComponent, {
335341
providers: [
336342
provideQueryClientOptions({
337343
defaultOptions: {
@@ -340,72 +346,99 @@ import { provideQueryClientOptions } from '@ngneat/query';
340346
},
341347
},
342348
}),
343-
];
344-
}
349+
]
350+
});
345351
```
346352

347-
## Operators
353+
## RxJS Operators
354+
355+
### filterSuccess()
356+
357+
The `filterSuccess` operator is a shortcut for `filter((result) => result.isSuccess)`.
358+
It's useful when you want to filter only successful results.
359+
360+
#### Example
348361

349362
```ts
350-
import {
351-
filterError,
352-
filterSuccess,
353-
selectResult,
354-
mapResultData,
355-
mapResultsData,
356-
tapSuccess,
357-
tapError,
358-
startWithQueryResult,
359-
} from '@ngneat/query';
363+
this.todosService.getTodos().result$.pipe(filterSuccess());
364+
```
360365

361-
export class TodosPageComponent {
362-
public todos: Array<Todo>;
363-
todosService = inject(TodosService);
366+
### filterError()
364367

365-
ngOnInit() {
366-
this.todosService.getTodos().result$.pipe(filterError());
367-
this.todosService.getTodos().result$.pipe(filterSuccess());
368-
this.todosService
369-
.getTodos()
370-
.result$.pipe(selectResult((result) => result.data.foo));
371-
372-
// Map the result `data`
373-
this.todosService.getTodos().pipe(
374-
mapResultData((data) => {
375-
return {
376-
todos: data.todos.filter(predicate),
377-
};
378-
})
379-
);
368+
The `filterError` operator is a shortcut for `filter((result) => result.status === 'error')`.
369+
It's useful when you want to filter only error results.
380370

381-
// Map the result `data` of multiple queries
382-
combineLatest([this.todosService.getTodos(), this.todosService.getTodos()])
383-
.pipe(
384-
mapResultsData(([todos, todos2]) => {
385-
return {
386-
todos: todos.data.todos.filter(predicate),
387-
todos2: todos2.data.todos.filter(predicate),
388-
};
389-
})
390-
)
391-
.subscribe(console.log); // { isLoading: boolean, isSuccess: boolean, isError: boolean, error: unknown, data: { todos: [], todos2: [] } }
371+
#### Example
392372

393-
// process error or success result directly
394-
this.todosService
395-
.getTodos()
396-
.result$.pipe(
397-
tapSuccess((data) => {
398-
// get result data directly if success
399-
this.todos = data;
400-
}),
401-
tapError((error) => {
402-
// do what you want with error (display modal, toast, etc)
403-
alert(error.message);
404-
})
405-
)
406-
.subscribe();
407-
}
408-
}
373+
```ts
374+
this.todosService.getTodos().result$.pipe(filterError());
375+
```
376+
377+
### tapSuccess(callback: (data) => void)
378+
379+
The `tapSuccess` operator is a shortcut for `tap((result) => result.isSuccess && callback(result.data))`.
380+
It's useful when you want to run a side effect only when the result is successful.
381+
382+
#### Example
383+
384+
```ts
385+
this.todosService.getTodos().result$.pipe(tapSuccess((data) => console.log(data)));
386+
```
387+
388+
### tapError(callback: (error) => void)
389+
390+
The `tapError` operator is a shortcut for `tap((result) => result.isError && callback(result.error))`.
391+
It's useful when you want to run a side effect only when the result is successful.
392+
393+
#### Example
394+
395+
```ts
396+
this.todosService.getTodos().result$.pipe(tapError((error) => console.log(error)));
397+
```
398+
399+
### mapResultData(mapFn)
400+
401+
The `mapResultData` operator like the name implies maps the `data` property of the `result` object.
402+
> **Note:** The data will only be mapped if the result is **successful** and otherwise just returned as is on **any other** state.
403+
404+
#### Example
405+
406+
```ts
407+
this.todosService.getTodos().pipe(
408+
mapResultData((data) => {
409+
return {
410+
todos: data.todos.filter(predicate),
411+
};
412+
})
413+
);
414+
```
415+
416+
### intersectResults(mapFn: (combinedQueries) => any)
417+
418+
The `intersectResults` operator is used to merge multiple queries into one.
419+
It will return a new base query result that will merge the results of all the queries.
420+
421+
> **Note:** The data will only be mapped if the result is **successful** and otherwise just returned as is on **any other** state.
422+
423+
#### Example
424+
425+
```ts
426+
const query = combineLatest({
427+
todos: todos.result$,
428+
posts: posts.result$,
429+
}).pipe(
430+
intersectResults(({ todos, posts }) => {
431+
return { ... }
432+
})
433+
)
434+
435+
// --- or ---
436+
437+
const query = combineLatest([todos.result$, posts.result$]).pipe(
438+
intersectResults(([todos, posts]) => {
439+
return { ... }
440+
})
441+
)
409442
```
410443

411444
## Utils
@@ -449,25 +482,11 @@ MutationService.use(...)
449482
Install the `@ngneat/query-devtools` package. Lazy load and use it only in `development` environment:
450483
451484
```ts
452-
import { ENVIRONMENT_INITIALIZER } from '@angular/core';
453-
import { environment } from './environments/environment';
454-
455-
import { QueryClientService } from '@ngneat/query';
485+
import { provideQueryDevTools } from '@ngneat/query';
456486

457487
bootstrapApplication(AppComponent, {
458488
providers: [
459-
environment.production
460-
? []
461-
: {
462-
provide: ENVIRONMENT_INITIALIZER,
463-
multi: true,
464-
useValue() {
465-
const queryClient = inject(QueryClientService);
466-
import('@ngneat/query-devtools').then((m) => {
467-
m.ngQueryDevtools({ queryClient });
468-
});
469-
},
470-
},
489+
provideQueryDevTools(),
471490
],
472491
});
473492
```

0 commit comments

Comments
 (0)