@@ -19,7 +19,8 @@ Get rid of granular state management, manual refetching, and async spaghetti cod
19
19
✅   ; Request Cancellation
20
20
✅   ; Prefetching
21
21
✅   ; Offline Support
22
- ✅   ; Data Selectors
22
+ ✅   ; Data Selectors
23
+ ✅   ; SSR Support
23
24
24
25
<hr />
25
26
@@ -55,9 +56,14 @@ Get rid of granular state management, manual refetching, and async spaghetti cod
55
56
56
57
## Installation
57
58
59
+ npm
58
60
```
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
61
67
```
62
68
63
69
## Queries
@@ -67,11 +73,11 @@ yarn add @ngneat/query
67
73
Inject the ` QueryClientService ` provider to get access to the query client [ instance] ( https://tanstack.com/query/v4/docs/reference/QueryClient ) :
68
74
69
75
``` ts
70
- import { QueryClientService } from ' @ngneat/query' ;
76
+ import { injectQueryClient } from ' @ngneat/query' ;
71
77
72
78
@Injectable ({ providedIn: ' root' })
73
79
export class TodosService {
74
- private queryClient = inject ( QueryClientService );
80
+ private queryClient = injectQueryClient ( );
75
81
}
76
82
```
77
83
@@ -331,7 +337,7 @@ You can provide the `QUERY_CLIENT_OPTIONS` provider to set the global [options](
331
337
``` ts
332
338
import { provideQueryClientOptions } from ' @ngneat/query' ;
333
339
334
- {
340
+ bootstrapApplication ( AppComponent , {
335
341
providers: [
336
342
provideQueryClientOptions ({
337
343
defaultOptions: {
@@ -340,72 +346,99 @@ import { provideQueryClientOptions } from '@ngneat/query';
340
346
},
341
347
},
342
348
}),
343
- ];
344
- }
349
+ ]
350
+ });
345
351
```
346
352
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
348
361
349
362
``` 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
+ ```
360
365
361
- export class TodosPageComponent {
362
- public todos: Array <Todo >;
363
- todosService = inject (TodosService );
366
+ ### filterError()
364
367
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.
380
370
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
392
372
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
+ )
409
442
```
410
443
411
444
## Utils
@@ -449,25 +482,11 @@ MutationService.use(...)
449
482
Install the ` @ngneat / query - devtools ` package. Lazy load and use it only in ` development ` environment:
450
483
451
484
` ` ` 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' ;
456
486
457
487
bootstrapApplication (AppComponent , {
458
488
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 (),
471
490
],
472
491
});
473
492
` ` `
0 commit comments