@ahoo-wang/fetcher-react 3.4.0 → 3.4.2

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.zh-CN.md CHANGED
@@ -26,30 +26,39 @@
26
26
  - [安装](#安装)
27
27
  - [快速开始](#快速开始)
28
28
  - [使用方法](#使用方法)
29
- - [useFetcher Hook](#usefetcher-hook)
29
+ - [核心 Hooks](#核心-hooks)
30
+ - [useFetcher](#usefetcher-hook)
31
+ - [useExecutePromise](#useexecutepromise)
32
+ - [usePromiseState](#usepromisestate)
30
33
  - [防抖 Hooks](#防抖-hooks)
31
34
  - [useDebouncedCallback](#usedebouncedcallback)
32
35
  - [useDebouncedExecutePromise](#usedebouncedexecutepromise)
33
36
  - [useDebouncedFetcher](#usedebouncedfetcher)
34
- - [useExecutePromise Hook](#useexecutepromise-hook)
35
- - [usePromiseState Hook](#usepromisestate-hook)
36
- - [useRequestId Hook](#userequestid-hook)
37
- - [useLatest Hook](#uselatest-hook)
38
- - [useRefs Hook](#userefs-hook)
39
- - [useEventSubscription Hook](#useeventsubscription-hook)
40
- - [useKeyStorage Hook](#usekeystorage-hook)
41
- - [useImmerKeyStorage Hook](#useimmerkeystorage-hook)
42
- - [Wow 查询 Hooks](#wow-查询-hooks)
43
- - [useListQuery Hook](#uselistquery-hook)
44
- - [usePagedQuery Hook](#usepagedquery-hook)
45
- - [useSingleQuery Hook](#usesinglequery-hook)
46
- - [useCountQuery Hook](#usecountquery-hook)
47
- - [useFetcherCountQuery Hook](#usefetchercountquery-hook)
48
- - [useFetcherPagedQuery Hook](#usefetcherpagedquery-hook)
49
- - [useFetcherListQuery Hook](#usefetcherlistquery-hook)
50
- - [useFetcherListStreamQuery Hook](#usefetcherliststreamquery-hook)
51
- - [useFetcherSingleQuery Hook](#usefetchersinglequery-hook)
52
- - [useListStreamQuery Hook](#useliststreamquery-hook)
37
+ - [useDebouncedFetcherQuery](#usedebouncedfetcherquery)
38
+ - [useDebouncedQuery](#usedebouncedquery)
39
+ - [工具 Hooks](#工具-hooks)
40
+ - [useRequestId](#userequestid)
41
+ - [useLatest](#uselatest)
42
+ - [useRefs](#userefs)
43
+ - [存储 Hooks](#存储-hooks)
44
+ - [useKeyStorage](#usekeystorage)
45
+ - [useImmerKeyStorage](#useimmerkeystorage-hook)
46
+ - [事件 Hooks](#事件-hooks)
47
+ - [useEventSubscription](#useeventsubscription-hook)
48
+ - [Wow 查询 Hooks](#wow-查询-hooks)
49
+ - [基础查询 Hooks](#基础查询-hooks)
50
+ - [useListQuery](#uselistquery-hook)
51
+ - [usePagedQuery](#usepagedquery-hook)
52
+ - [useSingleQuery](#usesinglequery-hook)
53
+ - [useCountQuery](#usecountquery-hook)
54
+ - [获取查询 Hooks](#获取查询-hooks)
55
+ - [useFetcherListQuery](#usefetcherlistquery-hook)
56
+ - [useFetcherPagedQuery](#usefetcherpagedquery-hook)
57
+ - [useFetcherSingleQuery](#usefetchersinglequery-hook)
58
+ - [useFetcherCountQuery](#usefetchercountquery-hook)
59
+ - [流查询 Hooks](#流查询-hooks)
60
+ - [useListStreamQuery](#useliststreamquery-hook)
61
+ - [useFetcherListStreamQuery](#usefetcherliststreamquery-hook)
53
62
  - [最佳实践](#最佳实践)
54
63
  - [API 参考](#api-参考)
55
64
  - [许可证](#许可证)
@@ -62,7 +71,7 @@ npm install @ahoo-wang/fetcher-react
62
71
 
63
72
  ### 要求
64
73
 
65
- - React 16.8+ (hooks 支持)
74
+ - React 19.0+ (hooks 支持)
66
75
  - TypeScript 4.0+ (完整类型安全)
67
76
 
68
77
  ## 快速开始
@@ -90,7 +99,9 @@ function App() {
90
99
 
91
100
  ## 使用方法
92
101
 
93
- ### useFetcher Hook
102
+ ### 核心 Hooks
103
+
104
+ #### useFetcher Hook
94
105
 
95
106
  `useFetcher` hook 提供完整的数据获取功能,具有自动状态管理、竞态条件保护和灵活的配置选项。
96
107
 
@@ -367,7 +378,9 @@ const [prefs, updatePrefs] = useImmerKeyStorage(prefsStorage);
367
378
 
368
379
  Wow 查询 Hooks 提供高级数据查询功能,具有内置的状态管理,用于条件、投影、排序、分页和限制。这些 hooks 专为与 `@ahoo-wang/fetcher-wow` 包配合使用而设计,用于复杂的查询操作。
369
380
 
370
- ### useListQuery Hook
381
+ ### 基础查询 Hooks
382
+
383
+ #### useListQuery Hook
371
384
 
372
385
  `useListQuery` hook 管理列表查询,具有条件、投影、排序和限制的状态管理。
373
386
 
@@ -517,7 +530,9 @@ const MyComponent = () => {
517
530
  };
518
531
  ```
519
532
 
520
- ### useFetcherCountQuery Hook
533
+ ### 获取查询 Hooks
534
+
535
+ #### useFetcherCountQuery Hook
521
536
 
522
537
  `useFetcherCountQuery` hook 是使用 Fetcher 库执行计数查询的专用 React hook。它专为需要检索匹配特定条件的记录数量的场景而设计,返回表示计数的数字。
523
538
 
@@ -965,7 +980,9 @@ const MyComponent = () => {
965
980
  };
966
981
  ```
967
982
 
968
- ### useListStreamQuery Hook
983
+ ### 流查询 Hooks
984
+
985
+ #### useListStreamQuery Hook
969
986
 
970
987
  `useListStreamQuery` hook 管理列表流查询,返回服务器发送事件的 readable stream。
971
988
 
@@ -1140,6 +1157,246 @@ function useDebouncedFetcher<R, E = FetcherError>(
1140
1157
  - `cancel`: 取消任何待处理防抖执行的函数
1141
1158
  - `isPending`: 布尔值,表示防抖调用是否待处理
1142
1159
 
1160
+ #### useDebouncedFetcherQuery
1161
+
1162
+ ```typescript
1163
+ function useDebouncedFetcherQuery<Q, R, E = FetcherError>(
1164
+ options: UseDebouncedFetcherQueryOptions<Q, R, E>,
1165
+ ): UseDebouncedFetcherQueryReturn<Q, R, E>;
1166
+ ```
1167
+
1168
+ 将基于查询的 HTTP 获取与防抖相结合,非常适合搜索输入和动态查询场景,您希望根据查询参数防抖 API 调用。
1169
+
1170
+ ```typescript jsx
1171
+ import { useDebouncedFetcherQuery } from '@ahoo-wang/fetcher-react';
1172
+
1173
+ interface SearchQuery {
1174
+ keyword: string;
1175
+ limit: number;
1176
+ filters?: { category?: string };
1177
+ }
1178
+
1179
+ interface SearchResult {
1180
+ items: Array<{ id: string; title: string }>;
1181
+ total: number;
1182
+ }
1183
+
1184
+ const SearchComponent = () => {
1185
+ const {
1186
+ loading,
1187
+ result,
1188
+ error,
1189
+ run,
1190
+ cancel,
1191
+ isPending,
1192
+ setQuery,
1193
+ getQuery,
1194
+ } = useDebouncedFetcherQuery<SearchQuery, SearchResult>({
1195
+ url: '/api/search',
1196
+ initialQuery: { keyword: '', limit: 10 },
1197
+ debounce: { delay: 300 }, // 防抖 300ms
1198
+ autoExecute: false, // 挂载时不执行
1199
+ });
1200
+
1201
+ const handleSearch = (keyword: string) => {
1202
+ setQuery({ keyword, limit: 10 }); // 如果 autoExecute 为 true,这将触发防抖执行
1203
+ };
1204
+
1205
+ const handleManualSearch = () => {
1206
+ run(); // 使用当前查询手动防抖执行
1207
+ };
1208
+
1209
+ const handleCancel = () => {
1210
+ cancel(); // 取消任何待处理的防抖执行
1211
+ };
1212
+
1213
+ if (loading) return <div>搜索中...</div>;
1214
+ if (error) return <div>错误: {error.message}</div>;
1215
+
1216
+ return (
1217
+ <div>
1218
+ <input
1219
+ type="text"
1220
+ onChange={(e) => handleSearch(e.target.value)}
1221
+ placeholder="搜索..."
1222
+ />
1223
+ <button onClick={handleManualSearch} disabled={isPending()}>
1224
+ {isPending() ? '搜索中...' : '搜索'}
1225
+ </button>
1226
+ <button onClick={handleCancel}>取消</button>
1227
+ {result && (
1228
+ <div>
1229
+ 找到 {result.total} 项:
1230
+ {result.items.map(item => (
1231
+ <div key={item.id}>{item.title}</div>
1232
+ ))}
1233
+ </div>
1234
+ )}
1235
+ </div>
1236
+ );
1237
+ };
1238
+ ```
1239
+
1240
+ **主要特性:**
1241
+
1242
+ - **查询状态管理**: 使用 `setQuery` 和 `getQuery` 自动查询参数处理
1243
+ - **防抖执行**: 在快速用户输入期间防止过多 API 调用
1244
+ - **自动执行**: 可选的在查询参数更改时自动执行
1245
+ - **手动控制**: `run()` 用于手动执行,`cancel()` 用于取消
1246
+ - **待处理状态**: `isPending()` 检查防抖调用是否排队
1247
+
1248
+ **类型参数:**
1249
+
1250
+ - `Q`: 查询参数的类型
1251
+ - `R`: 获取结果的类型
1252
+ - `E`: 错误的类型(默认为 FetcherError)
1253
+
1254
+ **参数:**
1255
+
1256
+ - `options`: 扩展 `UseFetcherQueryOptions` 和 `DebounceCapable` 的配置对象
1257
+ - `url`: API 端点 URL(必需)
1258
+ - `initialQuery`: 初始查询参数(必需)
1259
+ - `autoExecute?`: 查询参数更改时是否自动执行
1260
+ - `debounce`: 防抖配置(delay、leading、trailing)
1261
+ - HTTP 请求选项(method、headers、timeout 等)
1262
+
1263
+ **返回:**
1264
+
1265
+ 包含以下内容的对象:
1266
+
1267
+ - `loading`: 布尔值,表示获取当前是否正在执行
1268
+ - `result`: 获取的解析值
1269
+ - `error`: 执行期间发生的任何错误
1270
+ - `status`: 当前执行状态
1271
+ - `reset`: 重置获取器状态的函数
1272
+ - `abort`: 中止当前操作的函数
1273
+ - `getQuery`: 获取当前查询参数的函数
1274
+ - `setQuery`: 更新查询参数的函数
1275
+ - `run`: 使用当前查询执行防抖获取的函数
1276
+ - `cancel`: 取消任何待处理防抖执行的函数
1277
+ - `isPending`: 返回布尔值表示防抖执行当前是否待处理的函数
1278
+
1279
+ #### useDebouncedQuery
1280
+
1281
+ 将通用查询执行与防抖相结合,非常适合自定义查询操作,您希望根据查询参数防抖执行。
1282
+
1283
+ ```typescript jsx
1284
+ import { useDebouncedQuery } from '@ahoo-wang/fetcher-react';
1285
+
1286
+ interface SearchQuery {
1287
+ keyword: string;
1288
+ limit: number;
1289
+ filters?: { category?: string };
1290
+ }
1291
+
1292
+ interface SearchResult {
1293
+ items: Array<{ id: string; title: string }>;
1294
+ total: number;
1295
+ }
1296
+
1297
+ const SearchComponent = () => {
1298
+ const {
1299
+ loading,
1300
+ result,
1301
+ error,
1302
+ run,
1303
+ cancel,
1304
+ isPending,
1305
+ setQuery,
1306
+ getQuery,
1307
+ } = useDebouncedQuery<SearchQuery, SearchResult>({
1308
+ initialQuery: { keyword: '', limit: 10 },
1309
+ execute: async (query) => {
1310
+ const response = await fetch('/api/search', {
1311
+ method: 'POST',
1312
+ body: JSON.stringify(query),
1313
+ headers: { 'Content-Type': 'application/json' },
1314
+ });
1315
+ return response.json();
1316
+ },
1317
+ debounce: { delay: 300 }, // 防抖 300ms
1318
+ autoExecute: false, // 挂载时不执行
1319
+ });
1320
+
1321
+ const handleSearch = (keyword: string) => {
1322
+ setQuery({ keyword, limit: 10 }); // 如果 autoExecute 为 true,这将触发防抖执行
1323
+ };
1324
+
1325
+ const handleManualSearch = () => {
1326
+ run(); // 使用当前查询手动防抖执行
1327
+ };
1328
+
1329
+ const handleCancel = () => {
1330
+ cancel(); // 取消任何待处理的防抖执行
1331
+ };
1332
+
1333
+ if (loading) return <div>搜索中...</div>;
1334
+ if (error) return <div>错误: {error.message}</div>;
1335
+
1336
+ return (
1337
+ <div>
1338
+ <input
1339
+ type="text"
1340
+ onChange={(e) => handleSearch(e.target.value)}
1341
+ placeholder="搜索..."
1342
+ />
1343
+ <button onClick={handleManualSearch} disabled={isPending()}>
1344
+ {isPending() ? '搜索中...' : '搜索'}
1345
+ </button>
1346
+ <button onClick={handleCancel}>取消</button>
1347
+ {result && (
1348
+ <div>
1349
+ 找到 {result.total} 项:
1350
+ {result.items.map(item => (
1351
+ <div key={item.id}>{item.title}</div>
1352
+ ))}
1353
+ </div>
1354
+ )}
1355
+ </div>
1356
+ );
1357
+ };
1358
+ ```
1359
+
1360
+ **主要特性:**
1361
+
1362
+ - **查询状态管理**: 使用 `setQuery` 和 `getQuery` 自动查询参数处理
1363
+ - **防抖执行**: 在快速查询更改期间防止过多操作
1364
+ - **自动执行**: 可选的在查询参数更改时自动执行
1365
+ - **手动控制**: `run()` 用于手动执行,`cancel()` 用于取消
1366
+ - **待处理状态**: `isPending()` 检查防抖调用是否排队
1367
+ - **自定义执行**: 灵活的 execute 函数用于任何查询操作
1368
+
1369
+ **类型参数:**
1370
+
1371
+ - `Q`: 查询参数的类型
1372
+ - `R`: 结果的类型
1373
+ - `E`: 错误的类型(默认为 FetcherError)
1374
+
1375
+ **参数:**
1376
+
1377
+ - `options`: 扩展 `UseQueryOptions` 和 `DebounceCapable` 的配置对象
1378
+ - `initialQuery`: 初始查询参数(必需)
1379
+ - `execute`: 使用参数执行查询的函数
1380
+ - `autoExecute?`: 挂载时或查询参数更改时是否自动执行
1381
+ - `debounce`: 防抖配置(delay、leading、trailing)
1382
+ - 所有来自 `UseExecutePromiseOptions` 的选项
1383
+
1384
+ **返回:**
1385
+
1386
+ 包含以下内容的对象:
1387
+
1388
+ - `loading`: 布尔值,表示查询当前是否正在执行
1389
+ - `result`: 查询的解析值
1390
+ - `error`: 执行期间发生的任何错误
1391
+ - `status`: 当前执行状态
1392
+ - `reset`: 重置查询状态的函数
1393
+ - `abort`: 中止当前操作的函数
1394
+ - `getQuery`: 获取当前查询参数的函数
1395
+ - `setQuery`: 更新查询参数的函数
1396
+ - `run`: 使用当前参数执行防抖查询的函数
1397
+ - `cancel`: 取消任何待处理防抖执行的函数
1398
+ - `isPending`: 返回布尔值表示防抖执行当前是否待处理的函数
1399
+
1143
1400
  ### useFetcher
1144
1401
 
1145
1402
  ```typescript
@@ -1242,7 +1499,9 @@ function usePromiseState<R = unknown, E = unknown>(
1242
1499
  - `setError`: 设置状态为 ERROR 并提供错误
1243
1500
  - `setIdle`: 设置状态为 IDLE
1244
1501
 
1245
- ### useRequestId
1502
+ ### 工具 Hooks
1503
+
1504
+ #### useRequestId
1246
1505
 
1247
1506
  ```typescript
1248
1507
  function useRequestId(): UseRequestIdReturn;
@@ -1313,7 +1572,9 @@ React hook,用于使用 Map-like 接口管理多个 refs,允许通过键动
1313
1572
  - `RefKey = string | number | symbol`
1314
1573
  - `UseRefsReturn<T> extends Iterable<[RefKey, T]>`
1315
1574
 
1316
- ### useEventSubscription Hook
1575
+ ### 事件 Hooks
1576
+
1577
+ #### useEventSubscription Hook
1317
1578
 
1318
1579
  `useEventSubscription` hook 为类型化事件总线提供了 React 接口。它自动管理订阅生命周期,同时提供手动控制功能以增加灵活性。
1319
1580
 
@@ -1354,7 +1615,9 @@ function MyComponent() {
1354
1615
  - **错误处理**: 对失败的订阅尝试记录警告
1355
1616
  - **事件总线集成**: 与 `@ahoo-wang/fetcher-eventbus` TypedEventBus 实例无缝配合
1356
1617
 
1357
- ### useKeyStorage
1618
+ ### 存储 Hooks
1619
+
1620
+ #### useKeyStorage
1358
1621
 
1359
1622
  ```typescript jsx
1360
1623
  function useKeyStorage<T>(