rxtutils 1.1.1 → 1.1.2-beta.1
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 +404 -0
- package/dist/cjs/index.cjs +4 -1
- package/dist/cjs/store/createGetter/index.cjs +45 -0
- package/dist/es/index.mjs +2 -1
- package/dist/es/store/createGetter/index.mjs +42 -0
- package/dist/types/index.d.ts +2 -1
- package/dist/types/store/createGetter/index.d.ts +30 -0
- package/package.json +5 -4
- /package/dist/cjs/{createStateStore → store/createStateStore}/index.cjs +0 -0
- /package/dist/es/{createStateStore → store/createStateStore}/index.mjs +0 -0
- /package/dist/types/{createStateStore → store/createStateStore}/index.d.ts +0 -0
package/README.md
ADDED
|
@@ -0,0 +1,404 @@
|
|
|
1
|
+
# RUtils
|
|
2
|
+
|
|
3
|
+
一个功能丰富的 TypeScript 工具库,提供缓存管理、HTTP 请求和状态管理等功能。
|
|
4
|
+
|
|
5
|
+
## 📦 安装
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install rxtutils
|
|
9
|
+
# 或
|
|
10
|
+
yarn add rxtutils
|
|
11
|
+
# 或
|
|
12
|
+
pnpm add rxtutils
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## 🚀 功能特性
|
|
16
|
+
|
|
17
|
+
- **缓存管理** - 支持内存、localStorage、sessionStorage 和 IndexedDB 多种存储方式
|
|
18
|
+
- **HTTP 请求** - 基于 axios 的请求封装,支持错误处理和缓存
|
|
19
|
+
- **状态管理** - 轻量级的状态管理解决方案,支持 React Hook
|
|
20
|
+
- **TypeScript** - 完整的 TypeScript 类型支持
|
|
21
|
+
|
|
22
|
+
## 📚 模块介绍
|
|
23
|
+
|
|
24
|
+
### 1. 缓存管理 (Cache)
|
|
25
|
+
|
|
26
|
+
提供灵活的缓存管理功能,支持多种存储方式。
|
|
27
|
+
|
|
28
|
+
#### 基本用法
|
|
29
|
+
|
|
30
|
+
```typescript
|
|
31
|
+
import { Cache } from 'rxtutils';
|
|
32
|
+
|
|
33
|
+
// 创建内存缓存
|
|
34
|
+
const memoryCache = new Cache<string, any>();
|
|
35
|
+
|
|
36
|
+
// 创建 localStorage 缓存
|
|
37
|
+
const localCache = new Cache<string, any>('localStorage', 'myCache', 300);
|
|
38
|
+
|
|
39
|
+
// 创建 IndexedDB 缓存
|
|
40
|
+
const dbCache = new Cache<string, any>('indexedDB', 'dbCache', 600, 'myDatabase');
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
#### 缓存操作
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
// 设置缓存
|
|
47
|
+
cache.setCache('key1', { data: 'value1' });
|
|
48
|
+
cache.setCache('key2', { data: 'value2' }, { cacheTime: 120 }); // 2分钟过期
|
|
49
|
+
|
|
50
|
+
// 获取缓存
|
|
51
|
+
const data = cache.getCache('key1');
|
|
52
|
+
console.log(data); // { data: 'value1' }
|
|
53
|
+
|
|
54
|
+
// 清空缓存
|
|
55
|
+
cache.clear();
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
#### 高级配置
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import { Cache } from 'rxtutils';
|
|
62
|
+
|
|
63
|
+
// 自定义比较函数
|
|
64
|
+
const customCache = new Cache<{ id: number; name: string }, any>(
|
|
65
|
+
'localStorage',
|
|
66
|
+
'customCache',
|
|
67
|
+
300,
|
|
68
|
+
'customDB',
|
|
69
|
+
(prev, next) => prev.id === next.id // 只比较 id 字段
|
|
70
|
+
);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### 2. HTTP 请求 (createBaseRequest)
|
|
74
|
+
|
|
75
|
+
基于 axios 的请求封装,支持错误处理、缓存和自定义配置。
|
|
76
|
+
|
|
77
|
+
#### 基本用法
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
import { createBaseRequest } from 'rxtutils';
|
|
81
|
+
|
|
82
|
+
// 创建请求实例
|
|
83
|
+
const request = createBaseRequest({
|
|
84
|
+
baseURL: 'https://api.example.com',
|
|
85
|
+
throwError: true,
|
|
86
|
+
defaultMessageShower: (message) => console.log(message)
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// 创建具体请求
|
|
90
|
+
const getUserInfo = request<{ id: number }, { name: string; email: string }>({
|
|
91
|
+
method: 'GET',
|
|
92
|
+
url: '/user/:id'
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// 发送请求
|
|
96
|
+
const userInfo = await getUserInfo({ params: { id: 123 } });
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### 错误处理
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
const request = createBaseRequest({
|
|
103
|
+
baseURL: 'https://api.example.com',
|
|
104
|
+
errorCodeMap: {
|
|
105
|
+
'400': '请求参数错误',
|
|
106
|
+
'401': '未授权,请重新登录',
|
|
107
|
+
'500': (code, data, res) => ({
|
|
108
|
+
replaceResData: { error: '服务器内部错误' },
|
|
109
|
+
throwError: false
|
|
110
|
+
})
|
|
111
|
+
},
|
|
112
|
+
httpErrorCodeMap: {
|
|
113
|
+
404: '资源不存在',
|
|
114
|
+
500: '服务器错误'
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
#### 缓存功能
|
|
120
|
+
|
|
121
|
+
```typescript
|
|
122
|
+
const request = createBaseRequest({
|
|
123
|
+
baseURL: 'https://api.example.com',
|
|
124
|
+
enableCache: true,
|
|
125
|
+
cacheData: true,
|
|
126
|
+
cacheDataInStorage: 'localStorage',
|
|
127
|
+
cacheDataKey: 'api-cache',
|
|
128
|
+
cacheTime: 300
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
// 第一次请求会从服务器获取数据
|
|
132
|
+
const data1 = await getUserInfo({ params: { id: 123 } });
|
|
133
|
+
|
|
134
|
+
// 第二次请求会从缓存获取数据
|
|
135
|
+
const data2 = await getUserInfo({ params: { id: 123 } });
|
|
136
|
+
|
|
137
|
+
// 清空缓存
|
|
138
|
+
getUserInfo.clearCache();
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
### 3. 状态管理 (createStateStore)
|
|
142
|
+
|
|
143
|
+
轻量级的状态管理解决方案,支持组件间状态共享。
|
|
144
|
+
|
|
145
|
+
#### 基本用法
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
import { createStateStore } from 'rxtutils';
|
|
149
|
+
|
|
150
|
+
// 创建状态存储
|
|
151
|
+
const userStore = createStateStore({
|
|
152
|
+
name: '',
|
|
153
|
+
email: '',
|
|
154
|
+
isLoggedIn: false
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
// 在组件中使用
|
|
158
|
+
function UserComponent() {
|
|
159
|
+
const [user, setUser] = userStore.use();
|
|
160
|
+
|
|
161
|
+
const handleLogin = () => {
|
|
162
|
+
setUser({
|
|
163
|
+
name: 'John Doe',
|
|
164
|
+
email: 'john@example.com',
|
|
165
|
+
isLoggedIn: true
|
|
166
|
+
});
|
|
167
|
+
};
|
|
168
|
+
|
|
169
|
+
return (
|
|
170
|
+
<div>
|
|
171
|
+
<p>用户名: {user.name}</p>
|
|
172
|
+
<p>邮箱: {user.email}</p>
|
|
173
|
+
<button onClick={handleLogin}>登录</button>
|
|
174
|
+
</div>
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
#### 状态监听
|
|
180
|
+
|
|
181
|
+
```typescript
|
|
182
|
+
// 监听状态变化
|
|
183
|
+
const unsubscribe = userStore.watch((state) => {
|
|
184
|
+
console.log('状态已更新:', state);
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
// 取消监听
|
|
188
|
+
unsubscribe();
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
#### 直接访问和设置
|
|
192
|
+
|
|
193
|
+
```typescript
|
|
194
|
+
// 直接获取状态
|
|
195
|
+
const currentUser = userStore.get();
|
|
196
|
+
|
|
197
|
+
// 直接设置状态
|
|
198
|
+
userStore.set({ name: 'Jane Doe', email: 'jane@example.com', isLoggedIn: true });
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
### 4. 状态计算器 (createStoreGetter)
|
|
202
|
+
|
|
203
|
+
为状态存储提供计算属性和派生状态。
|
|
204
|
+
|
|
205
|
+
#### 基本用法
|
|
206
|
+
|
|
207
|
+
```typescript
|
|
208
|
+
import { createStoreGetter, createStoreGetterMemo } from 'rxtutils';
|
|
209
|
+
|
|
210
|
+
const userStore = createStateStore({
|
|
211
|
+
firstName: 'John',
|
|
212
|
+
lastName: 'Doe',
|
|
213
|
+
age: 30
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
// 创建 getter 函数
|
|
217
|
+
const getters = {
|
|
218
|
+
fullName: (state) => `${state.firstName} ${state.lastName}`,
|
|
219
|
+
isAdult: (state) => state.age >= 18,
|
|
220
|
+
displayName: (state) => state.firstName
|
|
221
|
+
};
|
|
222
|
+
|
|
223
|
+
// 创建 getter 名称映射
|
|
224
|
+
const getterNameMaps = {
|
|
225
|
+
fullName: 'fullName',
|
|
226
|
+
isAdult: 'isAdult',
|
|
227
|
+
displayName: 'displayName'
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
// 创建 getter 对象(非响应式)
|
|
231
|
+
const userGetters = createStoreGetter(userStore, getters, getterNameMaps);
|
|
232
|
+
console.log(userGetters.fullName); // "John Doe"
|
|
233
|
+
|
|
234
|
+
// 创建 getter memo hook(响应式)
|
|
235
|
+
const useUserGetters = createStoreGetterMemo(userStore, getters, getterNameMaps);
|
|
236
|
+
|
|
237
|
+
function UserProfile() {
|
|
238
|
+
const { fullName, isAdult, displayName } = useUserGetters();
|
|
239
|
+
|
|
240
|
+
return (
|
|
241
|
+
<div>
|
|
242
|
+
<h1>{fullName}</h1>
|
|
243
|
+
<p>成年人: {isAdult ? '是' : '否'}</p>
|
|
244
|
+
<p>显示名: {displayName}</p>
|
|
245
|
+
</div>
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
```
|
|
249
|
+
|
|
250
|
+
## 🔧 配置选项
|
|
251
|
+
|
|
252
|
+
### Cache 配置
|
|
253
|
+
|
|
254
|
+
| 参数 | 类型 | 默认值 | 说明 |
|
|
255
|
+
|------|------|--------|------|
|
|
256
|
+
| `storageType` | `'sessionStorage' \| 'localStorage' \| 'indexedDB'` | `undefined` | 存储类型 |
|
|
257
|
+
| `cacheKey` | `string` | `undefined` | 缓存键名 |
|
|
258
|
+
| `cacheTime` | `number` | `60` | 缓存时间(秒) |
|
|
259
|
+
| `indexDBName` | `string` | `'__apiCacheDatabase__'` | IndexedDB 数据库名称 |
|
|
260
|
+
| `cacheKeyEquals` | `function` | `defaultEquals` | 缓存键比较函数 |
|
|
261
|
+
|
|
262
|
+
### Request 配置
|
|
263
|
+
|
|
264
|
+
| 参数 | 类型 | 默认值 | 说明 |
|
|
265
|
+
|------|------|--------|------|
|
|
266
|
+
| `baseURL` | `string` | `''` | 请求基础URL |
|
|
267
|
+
| `throwError` | `boolean` | `true` | 是否抛出错误 |
|
|
268
|
+
| `enableCache` | `boolean` | `false` | 是否启用缓存 |
|
|
269
|
+
| `cacheData` | `boolean` | `false` | 是否缓存数据 |
|
|
270
|
+
| `cacheTime` | `number` | `60` | 缓存时间(秒) |
|
|
271
|
+
| `cacheDataInStorage` | `StorageType` | `undefined` | 缓存存储类型 |
|
|
272
|
+
| `errorCodePath` | `string` | `'code'` | 错误码路径 |
|
|
273
|
+
| `successCodes` | `string[]` | `['0', '200']` | 成功状态码 |
|
|
274
|
+
|
|
275
|
+
## 📝 类型定义
|
|
276
|
+
|
|
277
|
+
### 缓存相关类型
|
|
278
|
+
|
|
279
|
+
```typescript
|
|
280
|
+
// 缓存项接口
|
|
281
|
+
interface ICache<Param, Data> {
|
|
282
|
+
params: Param;
|
|
283
|
+
data: Data;
|
|
284
|
+
expireTime: string;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
// 缓存选项接口
|
|
288
|
+
interface ICacheOptions<Param> {
|
|
289
|
+
storageType?: StorageType;
|
|
290
|
+
cacheKey?: string;
|
|
291
|
+
cacheTime?: number;
|
|
292
|
+
cacheKeyEquals: (prev: Param, next: Param) => boolean;
|
|
293
|
+
indexDBName?: string;
|
|
294
|
+
}
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
### 请求相关类型
|
|
298
|
+
|
|
299
|
+
```typescript
|
|
300
|
+
// 请求配置接口
|
|
301
|
+
interface Options<Params, Data> {
|
|
302
|
+
baseURL?: string;
|
|
303
|
+
throwError?: boolean;
|
|
304
|
+
enableCache?: boolean;
|
|
305
|
+
cacheData?: boolean;
|
|
306
|
+
cacheTime?: number;
|
|
307
|
+
errorCodeMap?: Record<string, string | Function>;
|
|
308
|
+
httpErrorCodeMap?: Record<string, string | Function>;
|
|
309
|
+
// ... 更多配置选项
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// 请求参数接口
|
|
313
|
+
interface RequestOptions<Param> {
|
|
314
|
+
method: Method;
|
|
315
|
+
url: string;
|
|
316
|
+
data?: Param;
|
|
317
|
+
params?: Param;
|
|
318
|
+
}
|
|
319
|
+
```
|
|
320
|
+
|
|
321
|
+
### 状态管理相关类型
|
|
322
|
+
|
|
323
|
+
```typescript
|
|
324
|
+
// 状态存储接口
|
|
325
|
+
interface StateStore<S> {
|
|
326
|
+
use: () => [S, (state: IHookStateSetAction<S>) => void];
|
|
327
|
+
get: () => S;
|
|
328
|
+
set: (state: IHookStateSetAction<S>) => void;
|
|
329
|
+
watch: (callback: (state: S) => void) => () => void;
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// 状态设置动作类型
|
|
333
|
+
type IHookStateSetAction<S> = S | IHookStateSetter<S>;
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
## 🎯 使用场景
|
|
337
|
+
|
|
338
|
+
### 1. API 数据缓存
|
|
339
|
+
|
|
340
|
+
```typescript
|
|
341
|
+
// 创建带缓存的 API 请求
|
|
342
|
+
const apiRequest = createBaseRequest({
|
|
343
|
+
baseURL: 'https://api.example.com',
|
|
344
|
+
enableCache: true,
|
|
345
|
+
cacheData: true,
|
|
346
|
+
cacheDataInStorage: 'localStorage',
|
|
347
|
+
cacheTime: 300
|
|
348
|
+
});
|
|
349
|
+
|
|
350
|
+
const getProductList = apiRequest<{ page: number }, { products: Product[] }>({
|
|
351
|
+
method: 'GET',
|
|
352
|
+
url: '/products'
|
|
353
|
+
});
|
|
354
|
+
```
|
|
355
|
+
|
|
356
|
+
### 2. 用户状态管理
|
|
357
|
+
|
|
358
|
+
```typescript
|
|
359
|
+
// 创建用户状态存储
|
|
360
|
+
const userStore = createStateStore({
|
|
361
|
+
user: null,
|
|
362
|
+
permissions: [],
|
|
363
|
+
theme: 'light'
|
|
364
|
+
});
|
|
365
|
+
|
|
366
|
+
// 创建用户相关的计算属性
|
|
367
|
+
const userGetters = createStoreGetter(userStore, {
|
|
368
|
+
isLoggedIn: (state) => !!state.user,
|
|
369
|
+
canEdit: (state) => state.permissions.includes('edit'),
|
|
370
|
+
isDarkTheme: (state) => state.theme === 'dark'
|
|
371
|
+
}, {
|
|
372
|
+
isLoggedIn: 'isLoggedIn',
|
|
373
|
+
canEdit: 'canEdit',
|
|
374
|
+
isDarkTheme: 'isDarkTheme'
|
|
375
|
+
});
|
|
376
|
+
```
|
|
377
|
+
|
|
378
|
+
### 3. 表单数据缓存
|
|
379
|
+
|
|
380
|
+
```typescript
|
|
381
|
+
// 创建表单缓存
|
|
382
|
+
const formCache = new Cache<string, FormData>('sessionStorage', 'form-cache', 1800);
|
|
383
|
+
|
|
384
|
+
// 保存表单数据
|
|
385
|
+
formCache.setCache('user-form', formData);
|
|
386
|
+
|
|
387
|
+
// 恢复表单数据
|
|
388
|
+
const savedData = formCache.getCache('user-form');
|
|
389
|
+
```
|
|
390
|
+
|
|
391
|
+
## 🤝 贡献
|
|
392
|
+
|
|
393
|
+
欢迎提交 Issue 和 Pull Request 来帮助改进这个项目。
|
|
394
|
+
|
|
395
|
+
## 📄 许可证
|
|
396
|
+
|
|
397
|
+
MIT License
|
|
398
|
+
|
|
399
|
+
## 🔗 相关链接
|
|
400
|
+
|
|
401
|
+
- [TypeScript](https://www.typescriptlang.org/)
|
|
402
|
+
- [React](https://reactjs.org/)
|
|
403
|
+
- [Axios](https://axios-http.com/)
|
|
404
|
+
- [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API)
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -2,10 +2,13 @@
|
|
|
2
2
|
|
|
3
3
|
var index = require('./cache/index.cjs');
|
|
4
4
|
var index$1 = require('./request/index.cjs');
|
|
5
|
-
var index$
|
|
5
|
+
var index$3 = require('./store/createGetter/index.cjs');
|
|
6
|
+
var index$2 = require('./store/createStateStore/index.cjs');
|
|
6
7
|
|
|
7
8
|
|
|
8
9
|
|
|
9
10
|
exports.Cache = index.default;
|
|
10
11
|
exports.createBaseRequest = index$1;
|
|
12
|
+
exports.createStoreGetter = index$3.createStoreGetter;
|
|
13
|
+
exports.createStoreGetterMemo = index$3.createStoreGetterMemo;
|
|
11
14
|
exports.createStateStore = index$2.default;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var react = require('react');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 创建 store getter
|
|
7
|
+
* @param store store实例
|
|
8
|
+
* @param getters getter函数
|
|
9
|
+
* @param getterNameMaps 将 getter 函数和 getter 名称一一映射
|
|
10
|
+
* @returns getter object
|
|
11
|
+
*/
|
|
12
|
+
var createStoreGetter = function (store, getters, getterNameMaps) {
|
|
13
|
+
var gettersObj = {};
|
|
14
|
+
Object.keys(getters).forEach(function (key) {
|
|
15
|
+
Object.defineProperty(gettersObj, getterNameMaps[key], {
|
|
16
|
+
get: function () { return getters[key](store.get()); }
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
return gettersObj;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
*
|
|
23
|
+
* @param store store实例
|
|
24
|
+
* @param getters getter函数
|
|
25
|
+
* @param getterNameMaps 将 getter 函数和 getter 名称一一映射
|
|
26
|
+
* @returns getter memo hook
|
|
27
|
+
*/
|
|
28
|
+
var createStoreGetterMemo = function (store, getters, getterNameMaps) {
|
|
29
|
+
// 创建缓存数据 hook
|
|
30
|
+
return function () {
|
|
31
|
+
var storeData = store.use()[0];
|
|
32
|
+
var reducedData = react.useMemo(function () {
|
|
33
|
+
return Object.keys(getters).reduce(function (acc, key) {
|
|
34
|
+
var mappedKey = getterNameMaps[key];
|
|
35
|
+
var getterValue = getters[key](storeData);
|
|
36
|
+
acc[mappedKey] = getterValue;
|
|
37
|
+
return acc;
|
|
38
|
+
}, {});
|
|
39
|
+
}, [storeData]);
|
|
40
|
+
return reducedData;
|
|
41
|
+
};
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
exports.createStoreGetter = createStoreGetter;
|
|
45
|
+
exports.createStoreGetterMemo = createStoreGetterMemo;
|
package/dist/es/index.mjs
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
1
|
export { default as Cache } from './cache/index.mjs';
|
|
2
2
|
export { default as createBaseRequest } from './request/index.mjs';
|
|
3
|
-
export {
|
|
3
|
+
export { createStoreGetter, createStoreGetterMemo } from './store/createGetter/index.mjs';
|
|
4
|
+
export { default as createStateStore } from './store/createStateStore/index.mjs';
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 创建 store getter
|
|
5
|
+
* @param store store实例
|
|
6
|
+
* @param getters getter函数
|
|
7
|
+
* @param getterNameMaps 将 getter 函数和 getter 名称一一映射
|
|
8
|
+
* @returns getter object
|
|
9
|
+
*/
|
|
10
|
+
var createStoreGetter = function (store, getters, getterNameMaps) {
|
|
11
|
+
var gettersObj = {};
|
|
12
|
+
Object.keys(getters).forEach(function (key) {
|
|
13
|
+
Object.defineProperty(gettersObj, getterNameMaps[key], {
|
|
14
|
+
get: function () { return getters[key](store.get()); }
|
|
15
|
+
});
|
|
16
|
+
});
|
|
17
|
+
return gettersObj;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
*
|
|
21
|
+
* @param store store实例
|
|
22
|
+
* @param getters getter函数
|
|
23
|
+
* @param getterNameMaps 将 getter 函数和 getter 名称一一映射
|
|
24
|
+
* @returns getter memo hook
|
|
25
|
+
*/
|
|
26
|
+
var createStoreGetterMemo = function (store, getters, getterNameMaps) {
|
|
27
|
+
// 创建缓存数据 hook
|
|
28
|
+
return function () {
|
|
29
|
+
var storeData = store.use()[0];
|
|
30
|
+
var reducedData = useMemo(function () {
|
|
31
|
+
return Object.keys(getters).reduce(function (acc, key) {
|
|
32
|
+
var mappedKey = getterNameMaps[key];
|
|
33
|
+
var getterValue = getters[key](storeData);
|
|
34
|
+
acc[mappedKey] = getterValue;
|
|
35
|
+
return acc;
|
|
36
|
+
}, {});
|
|
37
|
+
}, [storeData]);
|
|
38
|
+
return reducedData;
|
|
39
|
+
};
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
export { createStoreGetter, createStoreGetterMemo };
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export { default as Cache, ICache, ICacheOptions, StorageMap, StorageType } from './cache/index.js';
|
|
2
2
|
export { ErrorHandlerReturnType, Options, RequestOptions, default as createBaseRequest } from './request/index.js';
|
|
3
|
-
export {
|
|
3
|
+
export { createStoreGetter, createStoreGetterMemo } from './store/createGetter/index.js';
|
|
4
|
+
export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from './store/createStateStore/index.js';
|
|
4
5
|
export { default as RequestError, RequestErrorType } from './request/error.js';
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import createStateStore from '../createStateStore/index.js';
|
|
2
|
+
|
|
3
|
+
type StoreGetter<S = any> = {
|
|
4
|
+
[K in string]: (store: S) => any;
|
|
5
|
+
};
|
|
6
|
+
type GetterNameMap<G extends StoreGetter<any>> = {
|
|
7
|
+
[K in keyof G]: string;
|
|
8
|
+
};
|
|
9
|
+
type ReducedData<G extends StoreGetter<any>, M extends GetterNameMap<G>> = {
|
|
10
|
+
[K in keyof M as M[K]]: G[K extends keyof G ? K : never] extends (store: any) => infer R ? R : never;
|
|
11
|
+
};
|
|
12
|
+
/**
|
|
13
|
+
* 创建 store getter
|
|
14
|
+
* @param store store实例
|
|
15
|
+
* @param getters getter函数
|
|
16
|
+
* @param getterNameMaps 将 getter 函数和 getter 名称一一映射
|
|
17
|
+
* @returns getter object
|
|
18
|
+
*/
|
|
19
|
+
declare const createStoreGetter: <S, G extends StoreGetter<S>, M extends GetterNameMap<G>>(store: ReturnType<typeof createStateStore<S>>, getters: G, getterNameMaps: M) => ReducedData<G, M>;
|
|
20
|
+
/**
|
|
21
|
+
*
|
|
22
|
+
* @param store store实例
|
|
23
|
+
* @param getters getter函数
|
|
24
|
+
* @param getterNameMaps 将 getter 函数和 getter 名称一一映射
|
|
25
|
+
* @returns getter memo hook
|
|
26
|
+
*/
|
|
27
|
+
declare const createStoreGetterMemo: <S, G extends StoreGetter<S>, M extends GetterNameMap<G>>(store: ReturnType<typeof createStateStore<S>>, getters: G, getterNameMaps: M) => () => ReducedData<G, M>;
|
|
28
|
+
|
|
29
|
+
export { createStoreGetter, createStoreGetterMemo };
|
|
30
|
+
export type { GetterNameMap, ReducedData, StoreGetter };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rxtutils",
|
|
3
|
-
"version": "1.1.1",
|
|
3
|
+
"version": "1.1.2-beta.1",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/cjs/index.cjs",
|
|
6
6
|
"module": "dist/es/index.mjs",
|
|
@@ -16,6 +16,7 @@
|
|
|
16
16
|
"@rollup/plugin-typescript": "^12.1.2",
|
|
17
17
|
"@types/lodash": "^4.17.17",
|
|
18
18
|
"@types/node": "^22.15.29",
|
|
19
|
+
"@types/react": "^19.1.12",
|
|
19
20
|
"rollup": "^4.41.1",
|
|
20
21
|
"rollup-plugin-dts": "^6.2.1",
|
|
21
22
|
"typescript": "^5.8.3",
|
|
@@ -25,9 +26,9 @@
|
|
|
25
26
|
"react": "^19.1.0"
|
|
26
27
|
},
|
|
27
28
|
"dependencies": {
|
|
28
|
-
"tslib": "^2.8.1",
|
|
29
29
|
"axios": "^1.9.0",
|
|
30
30
|
"lodash": "^4.17.21",
|
|
31
|
-
"moment": "^2.30.1"
|
|
31
|
+
"moment": "^2.30.1",
|
|
32
|
+
"tslib": "^2.8.1"
|
|
32
33
|
}
|
|
33
|
-
}
|
|
34
|
+
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|