@ahoo-wang/fetcher-react 2.6.10 → 2.6.13
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 +531 -9
- package/README.zh-CN.md +376 -22
- package/dist/core/useExecutePromise.d.ts +3 -2
- package/dist/core/useExecutePromise.d.ts.map +1 -1
- package/dist/core/usePromiseState.d.ts +4 -3
- package/dist/core/usePromiseState.d.ts.map +1 -1
- package/dist/fetcher/useFetcher.d.ts +4 -4
- package/dist/fetcher/useFetcher.d.ts.map +1 -1
- package/dist/index.es.js +457 -441
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +5 -5
- package/dist/index.umd.js.map +1 -1
- package/dist/wow/index.d.ts +1 -0
- package/dist/wow/index.d.ts.map +1 -1
- package/dist/wow/types.d.ts +7 -0
- package/dist/wow/types.d.ts.map +1 -0
- package/dist/wow/useCountQuery.d.ts +5 -7
- package/dist/wow/useCountQuery.d.ts.map +1 -1
- package/dist/wow/useListQuery.d.ts +5 -7
- package/dist/wow/useListQuery.d.ts.map +1 -1
- package/dist/wow/useListStreamQuery.d.ts +5 -7
- package/dist/wow/useListStreamQuery.d.ts.map +1 -1
- package/dist/wow/usePagedQuery.d.ts +5 -7
- package/dist/wow/usePagedQuery.d.ts.map +1 -1
- package/dist/wow/useSingleQuery.d.ts +5 -7
- package/dist/wow/useSingleQuery.d.ts.map +1 -1
- package/package.json +1 -1
package/README.zh-CN.md
CHANGED
|
@@ -18,18 +18,25 @@ Fetcher 生态的 React 集成包。提供 React Hooks 和组件,实现无缝
|
|
|
18
18
|
- ⚡ **性能优化**: 使用 useMemo、useCallback 和智能依赖管理进行优化
|
|
19
19
|
- 🎯 **选项灵活性**: 支持静态选项和动态选项供应商
|
|
20
20
|
- 🔧 **开发者体验**: 内置加载状态、错误处理和自动重新渲染
|
|
21
|
+
- 📊 **高级查询 Hooks**: 专门用于列表、分页、单个、计数和流查询的 hooks,具有状态管理功能
|
|
21
22
|
|
|
22
23
|
## 目录
|
|
23
24
|
|
|
24
25
|
- [安装](#安装)
|
|
25
26
|
- [快速开始](#快速开始)
|
|
26
27
|
- [使用方法](#使用方法)
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
- [useFetcher Hook](#usefetcher-hook)
|
|
29
|
+
- [useExecutePromise Hook](#useexecutepromise-hook)
|
|
30
|
+
- [usePromiseState Hook](#usepromisestate-hook)
|
|
31
|
+
- [useRequestId Hook](#userequestid-hook)
|
|
32
|
+
- [useLatest Hook](#uselatest-hook)
|
|
33
|
+
- [useKeyStorage Hook](#usekeystorage-hook)
|
|
34
|
+
- [Wow 查询 Hooks](#wow-查询-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 参考](#api-参考)
|
|
34
41
|
- [许可证](#许可证)
|
|
35
42
|
|
|
@@ -76,15 +83,34 @@ const MyComponent = () => {
|
|
|
76
83
|
|
|
77
84
|
const handleFetch = () => {
|
|
78
85
|
execute({ url: '/api/users', method: 'GET' });
|
|
79
|
-
|
|
86
|
+
};
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
#### 自动执行示例
|
|
90
|
+
|
|
91
|
+
```typescript jsx
|
|
92
|
+
import { useListQuery } from '@ahoo-wang/fetcher-react';
|
|
93
|
+
|
|
94
|
+
const MyComponent = () => {
|
|
95
|
+
const { result, loading, error, execute, setCondition } = useListQuery({
|
|
96
|
+
initialQuery: { condition: {}, projection: {}, sort: [], limit: 10 },
|
|
97
|
+
list: async (listQuery) => fetchListData(listQuery),
|
|
98
|
+
autoExecute: true, // 在组件挂载时自动执行
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
// 查询将在组件挂载时自动执行
|
|
102
|
+
// 您仍然可以通过 execute() 手动触发或更新条件
|
|
80
103
|
|
|
81
104
|
if (loading) return <div>加载中...</div>;
|
|
82
105
|
if (error) return <div>错误: {error.message}</div>;
|
|
83
106
|
|
|
84
107
|
return (
|
|
85
108
|
<div>
|
|
86
|
-
<
|
|
87
|
-
|
|
109
|
+
<ul>
|
|
110
|
+
{result?.map((item, index) => (
|
|
111
|
+
<li key={index}>{item.name}</li>
|
|
112
|
+
))}
|
|
113
|
+
</ul>
|
|
88
114
|
</div>
|
|
89
115
|
);
|
|
90
116
|
};
|
|
@@ -290,6 +316,206 @@ const userStorage = new KeyStorage<User>({ key: 'current-user' });
|
|
|
290
316
|
const [user, setUser] = useKeyStorage(userStorage);
|
|
291
317
|
```
|
|
292
318
|
|
|
319
|
+
## Wow 查询 Hooks
|
|
320
|
+
|
|
321
|
+
Wow 查询 Hooks 提供高级数据查询功能,具有内置的状态管理,用于条件、投影、排序、分页和限制。这些 hooks 专为与 `@ahoo-wang/fetcher-wow` 包配合使用而设计,用于复杂的查询操作。
|
|
322
|
+
|
|
323
|
+
### useListQuery Hook
|
|
324
|
+
|
|
325
|
+
`useListQuery` hook 管理列表查询,具有条件、投影、排序和限制的状态管理。
|
|
326
|
+
|
|
327
|
+
```typescript jsx
|
|
328
|
+
import { useListQuery } from '@ahoo-wang/fetcher-react';
|
|
329
|
+
|
|
330
|
+
const MyComponent = () => {
|
|
331
|
+
const { result, loading, error, execute, setCondition, setLimit } = useListQuery({
|
|
332
|
+
initialQuery: { condition: {}, projection: {}, sort: [], limit: 10 },
|
|
333
|
+
list: async (listQuery) => {
|
|
334
|
+
// 您的列表获取逻辑
|
|
335
|
+
return fetchListData(listQuery);
|
|
336
|
+
},
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
const handleSearch = (searchTerm: string) => {
|
|
340
|
+
setCondition({ name: { $regex: searchTerm } });
|
|
341
|
+
execute();
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
if (loading) return <div>加载中...</div>;
|
|
345
|
+
if (error) return <div>错误: {error.message}</div>;
|
|
346
|
+
|
|
347
|
+
return (
|
|
348
|
+
<div>
|
|
349
|
+
<input onChange={(e) => handleSearch(e.target.value)} placeholder="搜索..." />
|
|
350
|
+
<ul>
|
|
351
|
+
{result?.map((item, index) => (
|
|
352
|
+
<li key={index}>{item.name}</li>
|
|
353
|
+
))}
|
|
354
|
+
</ul>
|
|
355
|
+
</div>
|
|
356
|
+
);
|
|
357
|
+
};
|
|
358
|
+
```
|
|
359
|
+
|
|
360
|
+
### usePagedQuery Hook
|
|
361
|
+
|
|
362
|
+
`usePagedQuery` hook 管理分页查询,具有条件、投影、分页和排序的状态管理。
|
|
363
|
+
|
|
364
|
+
```typescript jsx
|
|
365
|
+
import { usePagedQuery } from '@ahoo-wang/fetcher-react';
|
|
366
|
+
|
|
367
|
+
const MyComponent = () => {
|
|
368
|
+
const { result, loading, error, execute, setCondition, setPagination } = usePagedQuery({
|
|
369
|
+
initialQuery: {
|
|
370
|
+
condition: {},
|
|
371
|
+
pagination: { index: 1, size: 10 },
|
|
372
|
+
projection: {},
|
|
373
|
+
sort: []
|
|
374
|
+
},
|
|
375
|
+
query: async (pagedQuery) => {
|
|
376
|
+
// 您的分页获取逻辑
|
|
377
|
+
return fetchPagedData(pagedQuery);
|
|
378
|
+
},
|
|
379
|
+
});
|
|
380
|
+
|
|
381
|
+
const handlePageChange = (page: number) => {
|
|
382
|
+
setPagination({ index: page, size: 10 });
|
|
383
|
+
execute();
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
if (loading) return <div>加载中...</div>;
|
|
387
|
+
if (error) return <div>错误: {error.message}</div>;
|
|
388
|
+
|
|
389
|
+
return (
|
|
390
|
+
<div>
|
|
391
|
+
<ul>
|
|
392
|
+
{result?.data?.map((item, index) => (
|
|
393
|
+
<li key={index}>{item.name}</li>
|
|
394
|
+
))}
|
|
395
|
+
</ul>
|
|
396
|
+
<button onClick={() => handlePageChange(result?.pagination?.index! - 1)} disabled={result?.pagination?.index === 1}>
|
|
397
|
+
上一页
|
|
398
|
+
</button>
|
|
399
|
+
<button onClick={() => handlePageChange(result?.pagination?.index! + 1)}>
|
|
400
|
+
下一页
|
|
401
|
+
</button>
|
|
402
|
+
</div>
|
|
403
|
+
);
|
|
404
|
+
};
|
|
405
|
+
```
|
|
406
|
+
|
|
407
|
+
### useSingleQuery Hook
|
|
408
|
+
|
|
409
|
+
`useSingleQuery` hook 管理单个查询,具有条件、投影和排序的状态管理。
|
|
410
|
+
|
|
411
|
+
```typescript jsx
|
|
412
|
+
import { useSingleQuery } from '@ahoo-wang/fetcher-react';
|
|
413
|
+
|
|
414
|
+
const MyComponent = () => {
|
|
415
|
+
const { result, loading, error, execute, setCondition } = useSingleQuery({
|
|
416
|
+
initialQuery: { condition: {}, projection: {}, sort: [] },
|
|
417
|
+
query: async (singleQuery) => {
|
|
418
|
+
// 您的单个获取逻辑
|
|
419
|
+
return fetchSingleData(singleQuery);
|
|
420
|
+
},
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
const handleFetchUser = (userId: string) => {
|
|
424
|
+
setCondition({ id: userId });
|
|
425
|
+
execute();
|
|
426
|
+
};
|
|
427
|
+
|
|
428
|
+
if (loading) return <div>加载中...</div>;
|
|
429
|
+
if (error) return <div>错误: {error.message}</div>;
|
|
430
|
+
|
|
431
|
+
return (
|
|
432
|
+
<div>
|
|
433
|
+
<button onClick={() => handleFetchUser('123')}>获取用户</button>
|
|
434
|
+
{result && <p>用户: {result.name}</p>}
|
|
435
|
+
</div>
|
|
436
|
+
);
|
|
437
|
+
};
|
|
438
|
+
```
|
|
439
|
+
|
|
440
|
+
### useCountQuery Hook
|
|
441
|
+
|
|
442
|
+
`useCountQuery` hook 管理计数查询,具有条件的状态管理。
|
|
443
|
+
|
|
444
|
+
```typescript jsx
|
|
445
|
+
import { useCountQuery } from '@ahoo-wang/fetcher-react';
|
|
446
|
+
|
|
447
|
+
const MyComponent = () => {
|
|
448
|
+
const { result, loading, error, execute, setCondition } = useCountQuery({
|
|
449
|
+
initialCondition: {},
|
|
450
|
+
count: async (condition) => {
|
|
451
|
+
// 您的计数获取逻辑
|
|
452
|
+
return fetchCount(condition);
|
|
453
|
+
},
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
const handleCountActive = () => {
|
|
457
|
+
setCondition({ status: 'active' });
|
|
458
|
+
execute();
|
|
459
|
+
};
|
|
460
|
+
|
|
461
|
+
if (loading) return <div>加载中...</div>;
|
|
462
|
+
if (error) return <div>错误: {error.message}</div>;
|
|
463
|
+
|
|
464
|
+
return (
|
|
465
|
+
<div>
|
|
466
|
+
<button onClick={handleCountActive}>计数活跃项目</button>
|
|
467
|
+
<p>总数: {result}</p>
|
|
468
|
+
</div>
|
|
469
|
+
);
|
|
470
|
+
};
|
|
471
|
+
```
|
|
472
|
+
|
|
473
|
+
### useListStreamQuery Hook
|
|
474
|
+
|
|
475
|
+
`useListStreamQuery` hook 管理列表流查询,返回服务器发送事件的 readable stream。
|
|
476
|
+
|
|
477
|
+
```typescript jsx
|
|
478
|
+
import { useListStreamQuery } from '@ahoo-wang/fetcher-react';
|
|
479
|
+
|
|
480
|
+
const MyComponent = () => {
|
|
481
|
+
const { result, loading, error, execute, setCondition } = useListStreamQuery({
|
|
482
|
+
initialQuery: { condition: {}, projection: {}, sort: [], limit: 100 },
|
|
483
|
+
listStream: async (listQuery) => {
|
|
484
|
+
// 您的流获取逻辑
|
|
485
|
+
return fetchListStream(listQuery);
|
|
486
|
+
},
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
useEffect(() => {
|
|
490
|
+
if (result) {
|
|
491
|
+
const reader = result.getReader();
|
|
492
|
+
const readStream = async () => {
|
|
493
|
+
try {
|
|
494
|
+
while (true) {
|
|
495
|
+
const { done, value } = await reader.read();
|
|
496
|
+
if (done) break;
|
|
497
|
+
console.log('接收到:', value);
|
|
498
|
+
// 处理流事件
|
|
499
|
+
}
|
|
500
|
+
} catch (error) {
|
|
501
|
+
console.error('流错误:', error);
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
readStream();
|
|
505
|
+
}
|
|
506
|
+
}, [result]);
|
|
507
|
+
|
|
508
|
+
if (loading) return <div>加载中...</div>;
|
|
509
|
+
if (error) return <div>错误: {error.message}</div>;
|
|
510
|
+
|
|
511
|
+
return (
|
|
512
|
+
<div>
|
|
513
|
+
<button onClick={execute}>开始流</button>
|
|
514
|
+
</div>
|
|
515
|
+
);
|
|
516
|
+
};
|
|
517
|
+
```
|
|
518
|
+
|
|
293
519
|
## API 参考
|
|
294
520
|
|
|
295
521
|
### useFetcher
|
|
@@ -305,15 +531,15 @@ function useFetcher<R = unknown, E = unknown>(
|
|
|
305
531
|
**类型参数:**
|
|
306
532
|
|
|
307
533
|
- `R`: 结果的类型
|
|
308
|
-
- `E`: 错误的类型(默认为 `
|
|
534
|
+
- `E`: 错误的类型(默认为 `FetcherError`)
|
|
309
535
|
|
|
310
536
|
**参数:**
|
|
311
537
|
|
|
312
538
|
- `options`: 配置选项或供应商函数
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
539
|
+
- `fetcher`: 要使用的自定义获取器实例。默认为默认获取器。
|
|
540
|
+
- `initialStatus`: 初始状态,默认为 IDLE
|
|
541
|
+
- `onSuccess`: 成功时调用的回调
|
|
542
|
+
- `onError`: 错误时调用的回调
|
|
317
543
|
|
|
318
544
|
**返回值:**
|
|
319
545
|
|
|
@@ -339,14 +565,14 @@ function useExecutePromise<R = unknown, E = unknown>(
|
|
|
339
565
|
**类型参数:**
|
|
340
566
|
|
|
341
567
|
- `R`: 结果的类型
|
|
342
|
-
- `E`: 错误的类型(默认为 `
|
|
568
|
+
- `E`: 错误的类型(默认为 `FetcherError`)
|
|
343
569
|
|
|
344
570
|
**参数:**
|
|
345
571
|
|
|
346
572
|
- `options`: 配置选项
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
573
|
+
- `initialStatus`: 初始状态,默认为 IDLE
|
|
574
|
+
- `onSuccess`: 成功时调用的回调
|
|
575
|
+
- `onError`: 错误时调用的回调
|
|
350
576
|
|
|
351
577
|
**返回值:**
|
|
352
578
|
|
|
@@ -372,14 +598,14 @@ function usePromiseState<R = unknown, E = unknown>(
|
|
|
372
598
|
**类型参数:**
|
|
373
599
|
|
|
374
600
|
- `R`: 结果的类型
|
|
375
|
-
- `E`: 错误的类型(默认为 `
|
|
601
|
+
- `E`: 错误的类型(默认为 `FetcherError`)
|
|
376
602
|
|
|
377
603
|
**参数:**
|
|
378
604
|
|
|
379
605
|
- `options`: 配置选项或供应商函数
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
606
|
+
- `initialStatus`: 初始状态,默认为 IDLE
|
|
607
|
+
- `onSuccess`: 成功时调用的回调(可以是异步的)
|
|
608
|
+
- `onError`: 错误时调用的回调(可以是异步的)
|
|
383
609
|
|
|
384
610
|
**返回值:**
|
|
385
611
|
|
|
@@ -450,6 +676,134 @@ function useKeyStorage<T>(
|
|
|
450
676
|
|
|
451
677
|
- 包含当前存储值和更新函数的元组
|
|
452
678
|
|
|
679
|
+
### useListQuery
|
|
680
|
+
|
|
681
|
+
```typescript
|
|
682
|
+
function useListQuery<R, FIELDS extends string = string, E = FetcherError>(
|
|
683
|
+
options: UseListQueryOptions<R, FIELDS, E>,
|
|
684
|
+
): UseListQueryReturn<R, FIELDS, E>;
|
|
685
|
+
```
|
|
686
|
+
|
|
687
|
+
用于管理列表查询的 React hook,具有条件、投影、排序和限制的状态管理。
|
|
688
|
+
|
|
689
|
+
**类型参数:**
|
|
690
|
+
|
|
691
|
+
- `R`: 列表中结果项的类型
|
|
692
|
+
- `FIELDS`: 用于条件和投影的字段类型
|
|
693
|
+
- `E`: 错误的类型(默认为 `FetcherError`)
|
|
694
|
+
|
|
695
|
+
**参数:**
|
|
696
|
+
|
|
697
|
+
- `options`: 包含 initialQuery 和 list 函数的配置选项
|
|
698
|
+
- `autoExecute`: 是否在组件挂载时自动执行查询(默认为 false)
|
|
699
|
+
|
|
700
|
+
**返回值:**
|
|
701
|
+
|
|
702
|
+
包含 promise 状态、execute 函数以及条件、投影、排序和限制设置器的对象。
|
|
703
|
+
|
|
704
|
+
### usePagedQuery
|
|
705
|
+
|
|
706
|
+
```typescript
|
|
707
|
+
function usePagedQuery<R, FIELDS extends string = string, E = unknown>(
|
|
708
|
+
options: UsePagedQueryOptions<R, FIELDS, E>,
|
|
709
|
+
): UsePagedQueryReturn<R, FIELDS, E>;
|
|
710
|
+
```
|
|
711
|
+
|
|
712
|
+
用于管理分页查询的 React hook,具有条件、投影、分页和排序的状态管理。
|
|
713
|
+
|
|
714
|
+
**类型参数:**
|
|
715
|
+
|
|
716
|
+
- `R`: 分页列表中结果项的类型
|
|
717
|
+
- `FIELDS`: 用于条件和投影的字段类型
|
|
718
|
+
- `E`: 错误的类型(默认为 `FetcherError`)
|
|
719
|
+
|
|
720
|
+
**参数:**
|
|
721
|
+
|
|
722
|
+
- `options`: 包含 initialQuery 和 query 函数的配置选项
|
|
723
|
+
- `autoExecute`: 是否在组件挂载时自动执行查询(默认为 false)
|
|
724
|
+
|
|
725
|
+
**返回值:**
|
|
726
|
+
|
|
727
|
+
包含 promise 状态、execute 函数以及条件、投影、分页和排序设置器的对象。
|
|
728
|
+
|
|
729
|
+
### useSingleQuery
|
|
730
|
+
|
|
731
|
+
```typescript
|
|
732
|
+
function useSingleQuery<R, FIELDS extends string = string, E = unknown>(
|
|
733
|
+
options: UseSingleQueryOptions<R, FIELDS, E>,
|
|
734
|
+
): UseSingleQueryReturn<R, FIELDS, E>;
|
|
735
|
+
```
|
|
736
|
+
|
|
737
|
+
用于管理单个查询的 React hook,具有条件、投影和排序的状态管理。
|
|
738
|
+
|
|
739
|
+
**类型参数:**
|
|
740
|
+
|
|
741
|
+
- `R`: 结果的类型
|
|
742
|
+
- `FIELDS`: 用于条件和投影的字段类型
|
|
743
|
+
- `E`: 错误的类型(默认为 `FetcherError`)
|
|
744
|
+
|
|
745
|
+
**参数:**
|
|
746
|
+
|
|
747
|
+
- `options`: 包含 initialQuery 和 query 函数的配置选项
|
|
748
|
+
- `autoExecute`: 是否在组件挂载时自动执行查询(默认为 false)
|
|
749
|
+
|
|
750
|
+
**返回值:**
|
|
751
|
+
|
|
752
|
+
包含 promise 状态、execute 函数以及条件、投影和排序设置器的对象。
|
|
753
|
+
|
|
754
|
+
### useCountQuery
|
|
755
|
+
|
|
756
|
+
```typescript
|
|
757
|
+
function useCountQuery<FIELDS extends string = string, E = FetcherError>(
|
|
758
|
+
options: UseCountQueryOptions<FIELDS, E>,
|
|
759
|
+
): UseCountQueryReturn<FIELDS, E>;
|
|
760
|
+
```
|
|
761
|
+
|
|
762
|
+
用于管理计数查询的 React hook,具有条件的状态管理。
|
|
763
|
+
|
|
764
|
+
**类型参数:**
|
|
765
|
+
|
|
766
|
+
- `FIELDS`: 用于条件的字段类型
|
|
767
|
+
- `E`: 错误的类型(默认为 `FetcherError`)
|
|
768
|
+
|
|
769
|
+
**参数:**
|
|
770
|
+
|
|
771
|
+
- `options`: 包含 initialCondition 和 count 函数的配置选项
|
|
772
|
+
- `autoExecute`: 是否在组件挂载时自动执行查询(默认为 false)
|
|
773
|
+
|
|
774
|
+
**返回值:**
|
|
775
|
+
|
|
776
|
+
包含 promise 状态、execute 函数以及条件设置器的对象。
|
|
777
|
+
|
|
778
|
+
### useListStreamQuery
|
|
779
|
+
|
|
780
|
+
```typescript
|
|
781
|
+
function useListStreamQuery<
|
|
782
|
+
R,
|
|
783
|
+
FIELDS extends string = string,
|
|
784
|
+
E = FetcherError,
|
|
785
|
+
>(
|
|
786
|
+
options: UseListStreamQueryOptions<R, FIELDS, E>,
|
|
787
|
+
): UseListStreamQueryReturn<R, FIELDS, E>;
|
|
788
|
+
```
|
|
789
|
+
|
|
790
|
+
用于管理列表流查询的 React hook,具有条件、投影、排序和限制的状态管理。返回 JSON 服务器发送事件的 readable stream。
|
|
791
|
+
|
|
792
|
+
**类型参数:**
|
|
793
|
+
|
|
794
|
+
- `R`: 流事件中结果项的类型
|
|
795
|
+
- `FIELDS`: 用于条件和投影的字段类型
|
|
796
|
+
- `E`: 错误的类型(默认为 `FetcherError`)
|
|
797
|
+
|
|
798
|
+
**参数:**
|
|
799
|
+
|
|
800
|
+
- `options`: 包含 initialQuery 和 listStream 函数的配置选项
|
|
801
|
+
- `autoExecute`: 是否在组件挂载时自动执行查询(默认为 false)
|
|
802
|
+
|
|
803
|
+
**返回值:**
|
|
804
|
+
|
|
805
|
+
包含 promise 状态、execute 函数以及条件、投影、排序和限制设置器的对象。
|
|
806
|
+
|
|
453
807
|
## 许可证
|
|
454
808
|
|
|
455
809
|
[Apache 2.0](https://github.com/Ahoo-Wang/fetcher/blob/main/LICENSE)
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { PromiseState, UsePromiseStateOptions } from './usePromiseState';
|
|
2
|
+
import { FetcherError } from '@ahoo-wang/fetcher';
|
|
2
3
|
export interface UseExecutePromiseOptions<R, E = unknown> extends UsePromiseStateOptions<R, E> {
|
|
3
4
|
/**
|
|
4
5
|
* Whether to propagate errors thrown by the promise.
|
|
@@ -16,7 +17,7 @@ export type PromiseSupplier<R> = () => Promise<R>;
|
|
|
16
17
|
* Interface defining the return type of useExecutePromise hook
|
|
17
18
|
* @template R - The type of the result value
|
|
18
19
|
*/
|
|
19
|
-
export interface UseExecutePromiseReturn<R, E =
|
|
20
|
+
export interface UseExecutePromiseReturn<R, E = FetcherError> extends PromiseState<R, E> {
|
|
20
21
|
/**
|
|
21
22
|
* Function to execute a promise supplier or promise.
|
|
22
23
|
* Returns a promise that resolves to the result on success, or the error if propagateError is false.
|
|
@@ -72,5 +73,5 @@ export interface UseExecutePromiseReturn<R, E = unknown> extends PromiseState<R,
|
|
|
72
73
|
* }
|
|
73
74
|
* ```
|
|
74
75
|
*/
|
|
75
|
-
export declare function useExecutePromise<R = unknown, E =
|
|
76
|
+
export declare function useExecutePromise<R = unknown, E = FetcherError>(options?: UseExecutePromiseOptions<R, E>): UseExecutePromiseReturn<R, E>;
|
|
76
77
|
//# sourceMappingURL=useExecutePromise.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useExecutePromise.d.ts","sourceRoot":"","sources":["../../src/core/useExecutePromise.ts"],"names":[],"mappings":"AAeA,OAAO,EAEL,YAAY,EACZ,sBAAsB,EACvB,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"useExecutePromise.d.ts","sourceRoot":"","sources":["../../src/core/useExecutePromise.ts"],"names":[],"mappings":"AAeA,OAAO,EAEL,YAAY,EACZ,sBAAsB,EACvB,MAAM,mBAAmB,CAAC;AAE3B,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD,MAAM,WAAW,wBAAwB,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO,CACtD,SAAQ,sBAAsB,CAAC,CAAC,EAAE,CAAC,CAAC;IACpC;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,CAAC;CAC1B;AAED;;;GAGG;AACH,MAAM,MAAM,eAAe,CAAC,CAAC,IAAI,MAAM,OAAO,CAAC,CAAC,CAAC,CAAC;AAElD;;;GAGG;AACH,MAAM,WAAW,uBAAuB,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,CAC1D,SAAQ,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC;IAC1B;;;OAGG;IACH,OAAO,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,KAAK,OAAO,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC;IACpE,oDAAoD;IACpD,KAAK,EAAE,MAAM,IAAI,CAAC;CACnB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,YAAY,EAC7D,OAAO,CAAC,EAAE,wBAAwB,CAAC,CAAC,EAAE,CAAC,CAAC,GACvC,uBAAuB,CAAC,CAAC,EAAE,CAAC,CAAC,CA0D/B"}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { FetcherError } from '@ahoo-wang/fetcher';
|
|
1
2
|
/**
|
|
2
3
|
* Enumeration of possible promise execution states
|
|
3
4
|
*/
|
|
@@ -39,7 +40,7 @@ export interface PromiseStateCallbacks<R, E = unknown> {
|
|
|
39
40
|
* };
|
|
40
41
|
* ```
|
|
41
42
|
*/
|
|
42
|
-
export interface UsePromiseStateOptions<R, E =
|
|
43
|
+
export interface UsePromiseStateOptions<R, E = FetcherError> extends PromiseStateCallbacks<R, E> {
|
|
43
44
|
/** Initial status, defaults to IDLE */
|
|
44
45
|
initialStatus?: PromiseStatus;
|
|
45
46
|
}
|
|
@@ -47,7 +48,7 @@ export interface UsePromiseStateOptions<R, E = unknown> extends PromiseStateCall
|
|
|
47
48
|
* Return type for usePromiseState hook
|
|
48
49
|
* @template R - The type of result
|
|
49
50
|
*/
|
|
50
|
-
export interface UsePromiseStateReturn<R, E =
|
|
51
|
+
export interface UsePromiseStateReturn<R, E = FetcherError> extends PromiseState<R, E> {
|
|
51
52
|
/** Set status to LOADING */
|
|
52
53
|
setLoading: () => void;
|
|
53
54
|
/** Set status to SUCCESS with result */
|
|
@@ -87,5 +88,5 @@ export interface UsePromiseStateReturn<R, E = unknown> extends PromiseState<R, E
|
|
|
87
88
|
* }
|
|
88
89
|
* ```
|
|
89
90
|
*/
|
|
90
|
-
export declare function usePromiseState<R = unknown, E =
|
|
91
|
+
export declare function usePromiseState<R = unknown, E = FetcherError>(options?: UsePromiseStateOptions<R, E>): UsePromiseStateReturn<R, E>;
|
|
91
92
|
//# sourceMappingURL=usePromiseState.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"usePromiseState.d.ts","sourceRoot":"","sources":["../../src/core/usePromiseState.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"usePromiseState.d.ts","sourceRoot":"","sources":["../../src/core/usePromiseState.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,YAAY,EAAE,MAAM,oBAAoB,CAAC;AAElD;;GAEG;AACH,oBAAY,aAAa;IACvB,IAAI,SAAS;IACb,OAAO,YAAY;IACnB,OAAO,YAAY;IACnB,KAAK,UAAU;CAChB;AAED,MAAM,WAAW,YAAY,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO;IAC1C,oCAAoC;IACpC,MAAM,EAAE,aAAa,CAAC;IACtB,qCAAqC;IACrC,OAAO,EAAE,OAAO,CAAC;IACjB,uBAAuB;IACvB,MAAM,EAAE,CAAC,GAAG,SAAS,CAAC;IACtB,sBAAsB;IACtB,KAAK,EAAE,CAAC,GAAG,SAAS,CAAC;CACtB;AAED,MAAM,WAAW,qBAAqB,CAAC,CAAC,EAAE,CAAC,GAAG,OAAO;IACnD,iDAAiD;IACjD,SAAS,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAChD,+CAA+C;IAC/C,OAAO,CAAC,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,IAAI,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC9C;AAED;;;;;;;;;;;;;;;GAeG;AACH,MAAM,WAAW,sBAAsB,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,CACzD,SAAQ,qBAAqB,CAAC,CAAC,EAAE,CAAC,CAAC;IACnC,uCAAuC;IACvC,aAAa,CAAC,EAAE,aAAa,CAAC;CAC/B;AAED;;;GAGG;AACH,MAAM,WAAW,qBAAqB,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,CACxD,SAAQ,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC;IAC1B,4BAA4B;IAC5B,UAAU,EAAE,MAAM,IAAI,CAAC;IACvB,wCAAwC;IACxC,UAAU,EAAE,CAAC,MAAM,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACzC,qCAAqC;IACrC,QAAQ,EAAE,CAAC,KAAK,EAAE,CAAC,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;IACtC,yBAAyB;IACzB,OAAO,EAAE,MAAM,IAAI,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,wBAAgB,eAAe,CAAC,CAAC,GAAG,OAAO,EAAE,CAAC,GAAG,YAAY,EAC3D,OAAO,CAAC,EAAE,sBAAsB,CAAC,CAAC,EAAE,CAAC,CAAC,GACrC,qBAAqB,CAAC,CAAC,EAAE,CAAC,CAAC,CAqE7B"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import { FetcherCapable, FetchExchange, FetchRequest, RequestOptions } from '@ahoo-wang/fetcher';
|
|
1
|
+
import { FetcherCapable, FetchExchange, FetchRequest, RequestOptions, FetcherError } from '@ahoo-wang/fetcher';
|
|
2
2
|
import { PromiseState, UsePromiseStateOptions } from '../core';
|
|
3
3
|
/**
|
|
4
4
|
* Configuration options for the useFetcher hook.
|
|
5
5
|
* Extends RequestOptions and FetcherCapable interfaces.
|
|
6
6
|
*/
|
|
7
|
-
export interface UseFetcherOptions<R, E =
|
|
7
|
+
export interface UseFetcherOptions<R, E = FetcherError> extends RequestOptions, FetcherCapable, UsePromiseStateOptions<R, E> {
|
|
8
8
|
}
|
|
9
|
-
export interface UseFetcherReturn<R, E =
|
|
9
|
+
export interface UseFetcherReturn<R, E = FetcherError> extends PromiseState<R, E> {
|
|
10
10
|
/** The FetchExchange object representing the ongoing fetch operation */
|
|
11
11
|
exchange?: FetchExchange;
|
|
12
12
|
execute: (request: FetchRequest) => Promise<void>;
|
|
@@ -38,5 +38,5 @@ export interface UseFetcherReturn<R, E = unknown> extends PromiseState<R, E> {
|
|
|
38
38
|
* }
|
|
39
39
|
* ```
|
|
40
40
|
*/
|
|
41
|
-
export declare function useFetcher<R, E =
|
|
41
|
+
export declare function useFetcher<R, E = FetcherError>(options?: UseFetcherOptions<R, E>): UseFetcherReturn<R, E>;
|
|
42
42
|
//# sourceMappingURL=useFetcher.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useFetcher.d.ts","sourceRoot":"","sources":["../../src/fetcher/useFetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAEL,cAAc,EACd,aAAa,EACb,YAAY,EAEZ,cAAc,
|
|
1
|
+
{"version":3,"file":"useFetcher.d.ts","sourceRoot":"","sources":["../../src/fetcher/useFetcher.ts"],"names":[],"mappings":"AAaA,OAAO,EAEL,cAAc,EACd,aAAa,EACb,YAAY,EAEZ,cAAc,EAAE,YAAY,EAC7B,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EACL,YAAY,EAGZ,sBAAsB,EAEvB,MAAM,SAAS,CAAC;AAEjB;;;GAGG;AACH,MAAM,WAAW,iBAAiB,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,CACpD,SAAQ,cAAc,EACpB,cAAc,EACd,sBAAsB,CAAC,CAAC,EAAE,CAAC,CAAC;CAC/B;AAED,MAAM,WAAW,gBAAgB,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,CAAE,SAAQ,YAAY,CAAC,CAAC,EAAE,CAAC,CAAC;IAC/E,wEAAwE;IACxE,QAAQ,CAAC,EAAE,aAAa,CAAC;IACzB,OAAO,EAAE,CAAC,OAAO,EAAE,YAAY,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACnD;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;GA0BG;AACH,wBAAgB,UAAU,CAAC,CAAC,EAAE,CAAC,GAAG,YAAY,EAC5C,OAAO,CAAC,EAAE,iBAAiB,CAAC,CAAC,EAAE,CAAC,CAAC,GAChC,gBAAgB,CAAC,CAAC,EAAE,CAAC,CAAC,CAsExB"}
|