@ahoo-wang/fetcher-react 2.6.9 → 2.6.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -19,6 +19,7 @@ automatic re-rendering and loading states.
19
19
  - ⚡ **Performance**: Optimized with useMemo, useCallback, and smart dependency management
20
20
  - 🎯 **Options Flexibility**: Support for both static options and dynamic option suppliers
21
21
  - 🔧 **Developer Experience**: Built-in loading states, error handling, and automatic re-rendering
22
+ - 📊 **Advanced Query Hooks**: Specialized hooks for list, paged, single, count, and stream queries with state management
22
23
 
23
24
  ## Table of Contents
24
25
 
@@ -30,6 +31,12 @@ automatic re-rendering and loading states.
30
31
  - [useRequestId Hook](#userequestid-hook)
31
32
  - [useLatest Hook](#uselatest-hook)
32
33
  - [useKeyStorage Hook](#usekeystorage-hook)
34
+ - [Wow Query Hooks](#wow-query-hooks)
35
+ - [useListQuery Hook](#uselistquery-hook)
36
+ - [usePagedQuery Hook](#usepagedquery-hook)
37
+ - [useSingleQuery Hook](#usesinglequery-hook)
38
+ - [useCountQuery Hook](#usecountquery-hook)
39
+ - [useListStreamQuery Hook](#useliststreamquery-hook)
33
40
  - [API Reference](#api-reference)
34
41
  - [License](#license)
35
42
 
@@ -77,15 +84,34 @@ const MyComponent = () => {
77
84
 
78
85
  const handleFetch = () => {
79
86
  execute({ url: '/api/users', method: 'GET' });
80
- };
87
+ };
88
+ ```
89
+
90
+ #### Auto Execute Example
91
+
92
+ ```typescript jsx
93
+ import { useListQuery } from '@ahoo-wang/fetcher-react';
94
+
95
+ const MyComponent = () => {
96
+ const { result, loading, error, execute, setCondition } = useListQuery({
97
+ initialQuery: { condition: {}, projection: {}, sort: [], limit: 10 },
98
+ list: async (listQuery) => fetchListData(listQuery),
99
+ autoExecute: true, // Automatically execute on component mount
100
+ });
101
+
102
+ // The query will execute automatically when the component mounts
103
+ // You can still manually trigger it with execute() or update conditions
81
104
 
82
105
  if (loading) return <div>Loading...</div>;
83
106
  if (error) return <div>Error: {error.message}</div>;
84
107
 
85
108
  return (
86
109
  <div>
87
- <pre>{JSON.stringify(result, null, 2)}</pre>
88
- <button onClick={handleFetch}>Fetch Data</button>
110
+ <ul>
111
+ {result?.map((item, index) => (
112
+ <li key={index}>{item.name}</li>
113
+ ))}
114
+ </ul>
89
115
  </div>
90
116
  );
91
117
  };
@@ -298,6 +324,209 @@ const userStorage = new KeyStorage<User>({ key: 'current-user' });
298
324
  const [user, setUser] = useKeyStorage(userStorage);
299
325
  ```
300
326
 
327
+ ## Wow Query Hooks
328
+
329
+ The Wow Query Hooks provide advanced data querying capabilities with built-in state management for conditions,
330
+ projections, sorting, pagination, and limits. These hooks are designed to work with the `@ahoo-wang/fetcher-wow` package
331
+ for complex query operations.
332
+
333
+ ### useListQuery Hook
334
+
335
+ The `useListQuery` hook manages list queries with state management for conditions, projections, sorting, and limits.
336
+
337
+ ```typescript jsx
338
+ import { useListQuery } from '@ahoo-wang/fetcher-react';
339
+
340
+ const MyComponent = () => {
341
+ const { result, loading, error, execute, setCondition, setLimit } = useListQuery({
342
+ initialQuery: { condition: {}, projection: {}, sort: [], limit: 10 },
343
+ list: async (listQuery) => {
344
+ // Your list fetching logic here
345
+ return fetchListData(listQuery);
346
+ },
347
+ });
348
+
349
+ const handleSearch = (searchTerm: string) => {
350
+ setCondition({ name: { $regex: searchTerm } });
351
+ execute();
352
+ };
353
+
354
+ if (loading) return <div>Loading...</div>;
355
+ if (error) return <div>Error: {error.message}</div>;
356
+
357
+ return (
358
+ <div>
359
+ <input onChange={(e) => handleSearch(e.target.value)} placeholder="Search..." />
360
+ <ul>
361
+ {result?.map((item, index) => (
362
+ <li key={index}>{item.name}</li>
363
+ ))}
364
+ </ul>
365
+ </div>
366
+ );
367
+ };
368
+ ```
369
+
370
+ ### usePagedQuery Hook
371
+
372
+ The `usePagedQuery` hook manages paged queries with state management for conditions, projections, pagination, and
373
+ sorting.
374
+
375
+ ```typescript jsx
376
+ import { usePagedQuery } from '@ahoo-wang/fetcher-react';
377
+
378
+ const MyComponent = () => {
379
+ const { result, loading, error, execute, setCondition, setPagination } = usePagedQuery({
380
+ initialQuery: {
381
+ condition: {},
382
+ pagination: { index: 1, size: 10 },
383
+ projection: {},
384
+ sort: []
385
+ },
386
+ query: async (pagedQuery) => {
387
+ // Your paged fetching logic here
388
+ return fetchPagedData(pagedQuery);
389
+ },
390
+ });
391
+
392
+ const handlePageChange = (page: number) => {
393
+ setPagination({ index: page, size: 10 });
394
+ execute();
395
+ };
396
+
397
+ if (loading) return <div>Loading...</div>;
398
+ if (error) return <div>Error: {error.message}</div>;
399
+
400
+ return (
401
+ <div>
402
+ <ul>
403
+ {result?.data?.map((item, index) => (
404
+ <li key={index}>{item.name}</li>
405
+ ))}
406
+ </ul>
407
+ <button onClick={() => handlePageChange(result?.pagination?.index! - 1)} disabled={result?.pagination?.index === 1}>
408
+ Previous
409
+ </button>
410
+ <button onClick={() => handlePageChange(result?.pagination?.index! + 1)}>
411
+ Next
412
+ </button>
413
+ </div>
414
+ );
415
+ };
416
+ ```
417
+
418
+ ### useSingleQuery Hook
419
+
420
+ The `useSingleQuery` hook manages single item queries with state management for conditions, projections, and sorting.
421
+
422
+ ```typescript jsx
423
+ import { useSingleQuery } from '@ahoo-wang/fetcher-react';
424
+
425
+ const MyComponent = () => {
426
+ const { result, loading, error, execute, setCondition } = useSingleQuery({
427
+ initialQuery: { condition: {}, projection: {}, sort: [] },
428
+ query: async (singleQuery) => {
429
+ // Your single item fetching logic here
430
+ return fetchSingleData(singleQuery);
431
+ },
432
+ });
433
+
434
+ const handleFetchUser = (userId: string) => {
435
+ setCondition({ id: userId });
436
+ execute();
437
+ };
438
+
439
+ if (loading) return <div>Loading...</div>;
440
+ if (error) return <div>Error: {error.message}</div>;
441
+
442
+ return (
443
+ <div>
444
+ <button onClick={() => handleFetchUser('123')}>Fetch User</button>
445
+ {result && <p>User: {result.name}</p>}
446
+ </div>
447
+ );
448
+ };
449
+ ```
450
+
451
+ ### useCountQuery Hook
452
+
453
+ The `useCountQuery` hook manages count queries with state management for conditions.
454
+
455
+ ```typescript jsx
456
+ import { useCountQuery } from '@ahoo-wang/fetcher-react';
457
+
458
+ const MyComponent = () => {
459
+ const { result, loading, error, execute, setCondition } = useCountQuery({
460
+ initialCondition: {},
461
+ count: async (condition) => {
462
+ // Your count fetching logic here
463
+ return fetchCount(condition);
464
+ },
465
+ });
466
+
467
+ const handleCountActive = () => {
468
+ setCondition({ status: 'active' });
469
+ execute();
470
+ };
471
+
472
+ if (loading) return <div>Loading...</div>;
473
+ if (error) return <div>Error: {error.message}</div>;
474
+
475
+ return (
476
+ <div>
477
+ <button onClick={handleCountActive}>Count Active Items</button>
478
+ <p>Total: {result}</p>
479
+ </div>
480
+ );
481
+ };
482
+ ```
483
+
484
+ ### useListStreamQuery Hook
485
+
486
+ The `useListStreamQuery` hook manages list stream queries that return a readable stream of server-sent events.
487
+
488
+ ```typescript jsx
489
+ import { useListStreamQuery } from '@ahoo-wang/fetcher-react';
490
+
491
+ const MyComponent = () => {
492
+ const { result, loading, error, execute, setCondition } = useListStreamQuery({
493
+ initialQuery: { condition: {}, projection: {}, sort: [], limit: 100 },
494
+ listStream: async (listQuery) => {
495
+ // Your stream fetching logic here
496
+ return fetchListStream(listQuery);
497
+ },
498
+ });
499
+
500
+ useEffect(() => {
501
+ if (result) {
502
+ const reader = result.getReader();
503
+ const readStream = async () => {
504
+ try {
505
+ while (true) {
506
+ const { done, value } = await reader.read();
507
+ if (done) break;
508
+ console.log('Received:', value);
509
+ // Process the stream event
510
+ }
511
+ } catch (error) {
512
+ console.error('Stream error:', error);
513
+ }
514
+ };
515
+ readStream();
516
+ }
517
+ }, [result]);
518
+
519
+ if (loading) return <div>Loading...</div>;
520
+ if (error) return <div>Error: {error.message}</div>;
521
+
522
+ return (
523
+ <div>
524
+ <button onClick={execute}>Start Stream</button>
525
+ </div>
526
+ );
527
+ };
528
+ ```
529
+
301
530
  ## API Reference
302
531
 
303
532
  ### useFetcher
@@ -462,6 +691,131 @@ A React hook that provides state management for a KeyStorage instance.
462
691
 
463
692
  - A tuple containing the current stored value and a function to update it
464
693
 
694
+ ### useListQuery
695
+
696
+ ```typescript
697
+ function useListQuery<R, FIELDS extends string = string, E = unknown>(
698
+ options: UseListQueryOptions<R, FIELDS, E>,
699
+ ): UseListQueryReturn<R, FIELDS, E>;
700
+ ```
701
+
702
+ A React hook for managing list queries with state management for conditions, projections, sorting, and limits.
703
+
704
+ **Type Parameters:**
705
+
706
+ - `R`: The type of the result items in the list
707
+ - `FIELDS`: The type of the fields used in conditions and projections
708
+ - `E`: The type of the error (defaults to `unknown`)
709
+
710
+ **Parameters:**
711
+
712
+ - `options`: Configuration options including initialQuery and list function
713
+ - `autoExecute`: Whether to automatically execute the query on component mount (defaults to false)
714
+
715
+ **Returns:**
716
+
717
+ An object containing promise state, execute function, and setters for condition, projection, sort, and limit.
718
+
719
+ ### usePagedQuery
720
+
721
+ ```typescript
722
+ function usePagedQuery<R, FIELDS extends string = string, E = unknown>(
723
+ options: UsePagedQueryOptions<R, FIELDS, E>,
724
+ ): UsePagedQueryReturn<R, FIELDS, E>;
725
+ ```
726
+
727
+ A React hook for managing paged queries with state management for conditions, projections, pagination, and sorting.
728
+
729
+ **Type Parameters:**
730
+
731
+ - `R`: The type of the result items in the paged list
732
+ - `FIELDS`: The type of the fields used in conditions and projections
733
+ - `E`: The type of the error (defaults to `unknown`)
734
+
735
+ **Parameters:**
736
+
737
+ - `options`: Configuration options including initialQuery and query function
738
+ - `autoExecute`: Whether to automatically execute the query on component mount (defaults to false)
739
+
740
+ **Returns:**
741
+
742
+ An object containing promise state, execute function, and setters for condition, projection, pagination, and sort.
743
+
744
+ ### useSingleQuery
745
+
746
+ ```typescript
747
+ function useSingleQuery<R, FIELDS extends string = string, E = unknown>(
748
+ options: UseSingleQueryOptions<R, FIELDS, E>,
749
+ ): UseSingleQueryReturn<R, FIELDS, E>;
750
+ ```
751
+
752
+ A React hook for managing single queries with state management for conditions, projections, and sorting.
753
+
754
+ **Type Parameters:**
755
+
756
+ - `R`: The type of the result
757
+ - `FIELDS`: The type of the fields used in conditions and projections
758
+ - `E`: The type of the error (defaults to `unknown`)
759
+
760
+ **Parameters:**
761
+
762
+ - `options`: Configuration options including initialQuery and query function
763
+ - `autoExecute`: Whether to automatically execute the query on component mount (defaults to false)
764
+
765
+ **Returns:**
766
+
767
+ An object containing promise state, execute function, and setters for condition, projection, and sort.
768
+
769
+ ### useCountQuery
770
+
771
+ ```typescript
772
+ function useCountQuery<FIELDS extends string = string, E = unknown>(
773
+ options: UseCountQueryOptions<FIELDS, E>,
774
+ ): UseCountQueryReturn<FIELDS, E>;
775
+ ```
776
+
777
+ A React hook for managing count queries with state management for conditions.
778
+
779
+ **Type Parameters:**
780
+
781
+ - `FIELDS`: The type of the fields used in conditions
782
+ - `E`: The type of the error (defaults to `unknown`)
783
+
784
+ **Parameters:**
785
+
786
+ - `options`: Configuration options including initialCondition and count function
787
+ - `autoExecute`: Whether to automatically execute the query on component mount (defaults to false)
788
+
789
+ **Returns:**
790
+
791
+ An object containing promise state, execute function, and setter for condition.
792
+
793
+ ### useListStreamQuery
794
+
795
+ ```typescript
796
+ function useListStreamQuery<R, FIELDS extends string = string, E = unknown>(
797
+ options: UseListStreamQueryOptions<R, FIELDS, E>,
798
+ ): UseListStreamQueryReturn<R, FIELDS, E>;
799
+ ```
800
+
801
+ A React hook for managing list stream queries with state management for conditions, projections, sorting, and limits.
802
+ Returns a readable stream of JSON server-sent events.
803
+
804
+ **Type Parameters:**
805
+
806
+ - `R`: The type of the result items in the stream events
807
+ - `FIELDS`: The type of the fields used in conditions and projections
808
+ - `E`: The type of the error (defaults to `unknown`)
809
+
810
+ **Parameters:**
811
+
812
+ - `options`: Configuration options including initialQuery and listStream function
813
+ - `autoExecute`: Whether to automatically execute the query on component mount (defaults to false)
814
+
815
+ **Returns:**
816
+
817
+ An object containing promise state, execute function, and setters for condition, projection, sort, and limit.
818
+
465
819
  ## License
466
820
 
467
821
  [Apache 2.0](https://github.com/Ahoo-Wang/fetcher/blob/main/LICENSE)