rxtutils 1.1.2-beta.10 → 1.1.2-beta.11
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/cjs/_utils/defaultEquals.cjs +21 -0
- package/cjs/cache/index.cjs +28 -15
- package/cjs/packages/cache/index.d.ts +141 -0
- package/cjs/packages/index.d.ts +7 -0
- package/cjs/packages/request/index.d.ts +140 -0
- package/cjs/packages/store/index.d.ts +2 -0
- package/cjs/packages/validator/index.d.ts +2 -0
- package/cjs/request/index.cjs +1 -1
- package/cjs/store/index.cjs +10 -0
- package/cjs/validator/index.cjs +19 -0
- package/es/_utils/defaultEquals.mjs +19 -0
- package/es/cache/index.mjs +28 -15
- package/es/packages/cache/index.d.ts +141 -0
- package/es/packages/index.d.ts +7 -0
- package/es/packages/request/index.d.ts +140 -0
- package/es/packages/store/index.d.ts +2 -0
- package/es/packages/validator/index.d.ts +2 -0
- package/es/request/index.mjs +1 -1
- package/es/store/index.mjs +2 -0
- package/es/validator/index.mjs +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 默认的相等性比较函数
|
|
5
|
+
* 通过将两个值序列化为 JSON 字符串来比较它们是否相等
|
|
6
|
+
*
|
|
7
|
+
* @template Param 比较值的类型,默认为 any
|
|
8
|
+
* @param prev 前一个值
|
|
9
|
+
* @param next 后一个值
|
|
10
|
+
* @returns {boolean} 如果两个值相等则返回 true,否则返回 false
|
|
11
|
+
*
|
|
12
|
+
* @remarks
|
|
13
|
+
* - 这个函数通过 JSON.stringify 进行比较,适用于大多数简单的数据结构
|
|
14
|
+
* - 不适用于包含函数、undefined、Symbol 等无法序列化的值
|
|
15
|
+
* - 对于循环引用的对象会抛出错误
|
|
16
|
+
*/
|
|
17
|
+
function defaultEquals(prev, next) {
|
|
18
|
+
return JSON.stringify(prev) === JSON.stringify(next);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
module.exports = defaultEquals;
|
package/cjs/cache/index.cjs
CHANGED
|
@@ -5,7 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var tslib = require('tslib');
|
|
6
6
|
var moment = require('moment');
|
|
7
7
|
var indexDB = require('./indexDB.cjs');
|
|
8
|
-
var defaultEquals = require('../defaultEquals.cjs');
|
|
8
|
+
var defaultEquals = require('../_utils/defaultEquals.cjs');
|
|
9
9
|
|
|
10
10
|
/** 存储类型映射表 */
|
|
11
11
|
var StorageMap = {
|
|
@@ -18,8 +18,16 @@ var StorageMap = {
|
|
|
18
18
|
* @template Data 缓存数据类型
|
|
19
19
|
*/
|
|
20
20
|
var Cache = /** @class */ (function () {
|
|
21
|
+
/**
|
|
22
|
+
* 构造函数
|
|
23
|
+
* @param cacheType 存储类型
|
|
24
|
+
* @param cacheKey 缓存键名
|
|
25
|
+
* @param cacheTime 缓存时间(秒)
|
|
26
|
+
* @param indexDBName IndexedDB 数据库名称,默认值为 '__apiCacheDatabase__'
|
|
27
|
+
* @param cacheKeyEquals 缓存键比较函数,默认使用 defaultEquals
|
|
28
|
+
*/
|
|
21
29
|
function Cache(cacheType, cacheKey, cacheTime, indexDBName, cacheKeyEquals) {
|
|
22
|
-
if (indexDBName === void 0) { indexDBName =
|
|
30
|
+
if (indexDBName === void 0) { indexDBName = "__apiCacheDatabase__"; }
|
|
23
31
|
if (cacheKeyEquals === void 0) { cacheKeyEquals = defaultEquals; }
|
|
24
32
|
/** 内存中的缓存数组 */
|
|
25
33
|
this.cache = [];
|
|
@@ -30,10 +38,10 @@ var Cache = /** @class */ (function () {
|
|
|
30
38
|
indexDBName: indexDBName,
|
|
31
39
|
cacheKeyEquals: cacheKeyEquals,
|
|
32
40
|
};
|
|
33
|
-
if (cacheType ===
|
|
34
|
-
this.storage = new indexDB.IndexedDBStorage(indexDBName,
|
|
41
|
+
if (cacheType === "indexedDB") {
|
|
42
|
+
this.storage = new indexDB.IndexedDBStorage(indexDBName, "cacheStore");
|
|
35
43
|
}
|
|
36
|
-
else if (typeof cacheType ===
|
|
44
|
+
else if (typeof cacheType === "string") {
|
|
37
45
|
this.storage = StorageMap[cacheType];
|
|
38
46
|
}
|
|
39
47
|
this._init();
|
|
@@ -55,15 +63,15 @@ var Cache = /** @class */ (function () {
|
|
|
55
63
|
_d = (_c = JSON).parse;
|
|
56
64
|
return [4 /*yield*/, this.storage.getItem(cacheKey)];
|
|
57
65
|
case 1:
|
|
58
|
-
_b.cache = _d.apply(_c, [(_e.sent()) ||
|
|
66
|
+
_b.cache = _d.apply(_c, [(_e.sent()) || "[]"]);
|
|
59
67
|
return [3 /*break*/, 3];
|
|
60
68
|
case 2:
|
|
61
69
|
if (this.storage instanceof Storage) {
|
|
62
70
|
this.storage = StorageMap[cacheType];
|
|
63
71
|
if (this.storage) {
|
|
64
|
-
if (typeof cacheKey ===
|
|
72
|
+
if (typeof cacheKey === "string") {
|
|
65
73
|
try {
|
|
66
|
-
this.cache = JSON.parse(this.storage.getItem(cacheKey) ||
|
|
74
|
+
this.cache = JSON.parse(this.storage.getItem(cacheKey) || "[]");
|
|
67
75
|
}
|
|
68
76
|
catch (e) {
|
|
69
77
|
this.cache = [];
|
|
@@ -80,7 +88,8 @@ var Cache = /** @class */ (function () {
|
|
|
80
88
|
}
|
|
81
89
|
});
|
|
82
90
|
});
|
|
83
|
-
};
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
84
93
|
* 过滤掉已过期的缓存项
|
|
85
94
|
* 通过比较当前时间和过期时间,移除过期的缓存项
|
|
86
95
|
* @private
|
|
@@ -90,18 +99,20 @@ var Cache = /** @class */ (function () {
|
|
|
90
99
|
return moment(item.expireTime).isAfter(moment());
|
|
91
100
|
});
|
|
92
101
|
this.cache = newCache;
|
|
93
|
-
};
|
|
102
|
+
};
|
|
103
|
+
/**
|
|
94
104
|
* 将当前缓存数据保存到存储中
|
|
95
105
|
* 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
|
|
96
106
|
* @private
|
|
97
107
|
*/
|
|
98
108
|
Cache.prototype._saveToStorage = function () {
|
|
99
109
|
if (this.storage) {
|
|
100
|
-
if (typeof this.cacheOptions.cacheKey ===
|
|
110
|
+
if (typeof this.cacheOptions.cacheKey === "string") {
|
|
101
111
|
this.storage.setItem(this.cacheOptions.cacheKey, JSON.stringify(this.cache));
|
|
102
112
|
}
|
|
103
113
|
}
|
|
104
|
-
};
|
|
114
|
+
};
|
|
115
|
+
/**
|
|
105
116
|
* 设置缓存数据
|
|
106
117
|
* @param params 缓存的参数
|
|
107
118
|
* @param data 要缓存的数据
|
|
@@ -118,10 +129,11 @@ var Cache = /** @class */ (function () {
|
|
|
118
129
|
this.cache.push({
|
|
119
130
|
params: params,
|
|
120
131
|
data: data,
|
|
121
|
-
expireTime: moment().add(cacheTime,
|
|
132
|
+
expireTime: moment().add(cacheTime, "seconds").toJSON(),
|
|
122
133
|
});
|
|
123
134
|
this._saveToStorage();
|
|
124
|
-
};
|
|
135
|
+
};
|
|
136
|
+
/**
|
|
125
137
|
* 获取缓存数据
|
|
126
138
|
* @param params 查询参数
|
|
127
139
|
* @returns 如果找到有效的缓存数据则返回数据,否则返回 null
|
|
@@ -142,7 +154,8 @@ var Cache = /** @class */ (function () {
|
|
|
142
154
|
}
|
|
143
155
|
}
|
|
144
156
|
return null;
|
|
145
|
-
};
|
|
157
|
+
};
|
|
158
|
+
/**
|
|
146
159
|
* 清空所有缓存数据
|
|
147
160
|
* 清空内存中的缓存数组并同步到存储中
|
|
148
161
|
*/
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { IndexedDBStorage } from '../../cache/indexDB.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 缓存存储类型
|
|
5
|
+
* - sessionStorage: 会话存储,浏览器关闭后清除
|
|
6
|
+
* - localStorage: 本地存储,永久保存
|
|
7
|
+
* - indexedDB: IndexedDB 数据库存储
|
|
8
|
+
*/
|
|
9
|
+
type StorageType = "sessionStorage" | "localStorage" | "indexedDB";
|
|
10
|
+
/**
|
|
11
|
+
* 缓存项接口定义
|
|
12
|
+
* 定义了单个缓存项的数据结构
|
|
13
|
+
*
|
|
14
|
+
* @template Param 缓存参数类型
|
|
15
|
+
* @template Data 缓存数据类型
|
|
16
|
+
*/
|
|
17
|
+
interface ICache<Param, Data> {
|
|
18
|
+
/**
|
|
19
|
+
* 缓存的参数
|
|
20
|
+
* 用于标识和查找缓存项
|
|
21
|
+
*/
|
|
22
|
+
params: Param;
|
|
23
|
+
/**
|
|
24
|
+
* 缓存的数据
|
|
25
|
+
* 实际存储的内容
|
|
26
|
+
*/
|
|
27
|
+
data: Data;
|
|
28
|
+
/**
|
|
29
|
+
* 过期时间
|
|
30
|
+
* - ISO 8601 格式的字符串
|
|
31
|
+
* - 由 moment().add(cacheTime, 'seconds').toJSON() 生成
|
|
32
|
+
* - 示例:'2025-06-12T10:30:00.000Z'
|
|
33
|
+
*/
|
|
34
|
+
expireTime: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 缓存选项接口
|
|
38
|
+
* @template Param 缓存参数类型
|
|
39
|
+
*/
|
|
40
|
+
interface ICacheOptions<Param> {
|
|
41
|
+
/**
|
|
42
|
+
* 存储类型
|
|
43
|
+
* - 'sessionStorage': 会话存储,浏览器关闭后清除
|
|
44
|
+
* - 'localStorage': 本地存储,永久保存
|
|
45
|
+
* - 'indexedDB': IndexedDB 数据库存储
|
|
46
|
+
* - undefined: 仅在内存中缓存(默认值)
|
|
47
|
+
*/
|
|
48
|
+
storageType?: StorageType;
|
|
49
|
+
/**
|
|
50
|
+
* 缓存键名
|
|
51
|
+
* - 当使用 localStorage/sessionStorage 时必须提供
|
|
52
|
+
* - 用于在存储中标识不同的缓存数据
|
|
53
|
+
* @default undefined 不使用持久化存储
|
|
54
|
+
*/
|
|
55
|
+
cacheKey?: string;
|
|
56
|
+
/**
|
|
57
|
+
* 缓存时间(秒)
|
|
58
|
+
* - 超过这个时间的缓存项会被自动清除
|
|
59
|
+
* @default 60 一分钟
|
|
60
|
+
*/
|
|
61
|
+
cacheTime?: number;
|
|
62
|
+
/**
|
|
63
|
+
* 缓存键比较函数
|
|
64
|
+
* - 用于判断两个缓存参数是否相等
|
|
65
|
+
* - 相等则认为是同一个缓存项
|
|
66
|
+
* @param prev 前一个参数
|
|
67
|
+
* @param next 后一个参数
|
|
68
|
+
* @returns 是否相等
|
|
69
|
+
* @default defaultEquals 使用 JSON.stringify 进行比较
|
|
70
|
+
*/
|
|
71
|
+
cacheKeyEquals: (prev: Param, next: Param) => boolean;
|
|
72
|
+
/**
|
|
73
|
+
* IndexedDB 数据库名称
|
|
74
|
+
* - 仅在 storageType 为 'indexedDB' 时使用
|
|
75
|
+
* @default '__apiCacheDatabase__'
|
|
76
|
+
*/
|
|
77
|
+
indexDBName?: string;
|
|
78
|
+
}
|
|
79
|
+
/** 存储类型映射表 */
|
|
80
|
+
declare const StorageMap: Record<StorageType | string, Storage>;
|
|
81
|
+
/**
|
|
82
|
+
* 缓存类
|
|
83
|
+
* @template Param 缓存参数类型
|
|
84
|
+
* @template Data 缓存数据类型
|
|
85
|
+
*/
|
|
86
|
+
declare class Cache<Param, Data> {
|
|
87
|
+
/** 内存中的缓存数组 */
|
|
88
|
+
cache: ICache<Param, Data>[];
|
|
89
|
+
/** 缓存选项 */
|
|
90
|
+
private cacheOptions;
|
|
91
|
+
/** 存储实例 */
|
|
92
|
+
storage?: Storage | IndexedDBStorage;
|
|
93
|
+
/**
|
|
94
|
+
* 构造函数
|
|
95
|
+
* @param cacheType 存储类型
|
|
96
|
+
* @param cacheKey 缓存键名
|
|
97
|
+
* @param cacheTime 缓存时间(秒)
|
|
98
|
+
* @param indexDBName IndexedDB 数据库名称,默认值为 '__apiCacheDatabase__'
|
|
99
|
+
* @param cacheKeyEquals 缓存键比较函数,默认使用 defaultEquals
|
|
100
|
+
*/
|
|
101
|
+
constructor(cacheType?: StorageType, cacheKey?: string, cacheTime?: number, indexDBName?: string, cacheKeyEquals?: (prev: Param, next: Param) => boolean);
|
|
102
|
+
/**
|
|
103
|
+
* 初始化缓存
|
|
104
|
+
* 从存储中加载已保存的缓存数据,并进行解析和过期处理
|
|
105
|
+
* @private
|
|
106
|
+
*/
|
|
107
|
+
private _init;
|
|
108
|
+
/**
|
|
109
|
+
* 过滤掉已过期的缓存项
|
|
110
|
+
* 通过比较当前时间和过期时间,移除过期的缓存项
|
|
111
|
+
* @private
|
|
112
|
+
*/
|
|
113
|
+
private _filterExpired;
|
|
114
|
+
/**
|
|
115
|
+
* 将当前缓存数据保存到存储中
|
|
116
|
+
* 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
|
|
117
|
+
* @private
|
|
118
|
+
*/
|
|
119
|
+
private _saveToStorage;
|
|
120
|
+
/**
|
|
121
|
+
* 设置缓存数据
|
|
122
|
+
* @param params 缓存的参数
|
|
123
|
+
* @param data 要缓存的数据
|
|
124
|
+
* @param cacheOptions 可选的缓存配置,可以覆盖默认的缓存时间
|
|
125
|
+
*/
|
|
126
|
+
setCache(params: Param, data: Data, cacheOptions?: Omit<ICacheOptions<Param>, "storageType" | "cacheKey" | "cacheKeyEquals">): void;
|
|
127
|
+
/**
|
|
128
|
+
* 获取缓存数据
|
|
129
|
+
* @param params 查询参数
|
|
130
|
+
* @returns 如果找到有效的缓存数据则返回数据,否则返回 null
|
|
131
|
+
*/
|
|
132
|
+
getCache(params: Param): Data;
|
|
133
|
+
/**
|
|
134
|
+
* 清空所有缓存数据
|
|
135
|
+
* 清空内存中的缓存数组并同步到存储中
|
|
136
|
+
*/
|
|
137
|
+
clear(): void;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export { StorageMap, Cache as default };
|
|
141
|
+
export type { ICache, ICacheOptions, StorageType };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { default as Cache, ICache, ICacheOptions, StorageMap, StorageType } from './cache/index.js';
|
|
2
|
+
export { ErrorHandlerReturnType, Options, RequestOptions, default as createBaseRequest } from './request/index.js';
|
|
3
|
+
export { createStoreGetter, createStoreGetterMemo } from '../store/createGetter/index.js';
|
|
4
|
+
export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from '../store/createStateStore/index.js';
|
|
5
|
+
export { BaseValidator } from '../validator/validator.js';
|
|
6
|
+
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from '../validator/decorators.js';
|
|
7
|
+
export { default as RequestError, RequestErrorType } from '../request/error.js';
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { AxiosResponse, Method, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { StorageType } from '../cache/index.js';
|
|
3
|
+
export { default as RequestError, RequestErrorType } from '../../request/error.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 错误处理器返回类型
|
|
7
|
+
* @template D 响应数据类型
|
|
8
|
+
*/
|
|
9
|
+
type ErrorHandlerReturnType<D> = {
|
|
10
|
+
/** 替换响应数据 */
|
|
11
|
+
replaceResData?: D;
|
|
12
|
+
/**
|
|
13
|
+
* 是否抛出错误
|
|
14
|
+
* - true: 强制抛出错误
|
|
15
|
+
* - false: 不抛出错误
|
|
16
|
+
* - 'default': 使用默认错误处理逻辑
|
|
17
|
+
*/
|
|
18
|
+
throwError?: boolean | "default";
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* 请求配置选项接口
|
|
22
|
+
* @template Params 请求参数类型
|
|
23
|
+
* @template Data 响应数据类型
|
|
24
|
+
*/
|
|
25
|
+
interface Options<Params = any, Data = any> {
|
|
26
|
+
/** 请求基础URL,默认为空字符串 */
|
|
27
|
+
baseURL?: string;
|
|
28
|
+
/**
|
|
29
|
+
* 是否抛出错误
|
|
30
|
+
* @default true
|
|
31
|
+
*/
|
|
32
|
+
throwError?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* 默认的消息展示函数
|
|
35
|
+
* @default window.alert
|
|
36
|
+
*/
|
|
37
|
+
defaultMessageShower?: (message: string) => void;
|
|
38
|
+
/**
|
|
39
|
+
* 是否启用缓存功能
|
|
40
|
+
* @default false
|
|
41
|
+
*/
|
|
42
|
+
enableCache?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* 缓存键比较函数
|
|
45
|
+
* @default defaultEquals 使用 JSON.stringify 进行比较
|
|
46
|
+
*/
|
|
47
|
+
cacheKeyEquals?: (prev: Params, next: Params) => boolean;
|
|
48
|
+
/**
|
|
49
|
+
* 是否将响应数据存入缓存
|
|
50
|
+
* @default false
|
|
51
|
+
*/
|
|
52
|
+
cacheData?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* 缓存时间(秒)
|
|
55
|
+
* @default 60
|
|
56
|
+
*/
|
|
57
|
+
cacheTime?: number;
|
|
58
|
+
/**
|
|
59
|
+
* 缓存数据的存储类型
|
|
60
|
+
* - localStorage: 使用浏览器本地存储,数据永久保存
|
|
61
|
+
* - sessionStorage: 使用会话存储,关闭浏览器后清除
|
|
62
|
+
* - indexedDB: 使用 IndexedDB 数据库存储
|
|
63
|
+
* - 不填则仅在内存中缓存,页面刷新后清除
|
|
64
|
+
*/
|
|
65
|
+
cacheDataInStorage?: StorageType;
|
|
66
|
+
/**
|
|
67
|
+
* 缓存数据的键名
|
|
68
|
+
* @default `${method}:${baseURL}${url}` 默认使用请求方法、基础URL和请求路径组合
|
|
69
|
+
*/
|
|
70
|
+
cacheDataKey?: string;
|
|
71
|
+
/**
|
|
72
|
+
* IndexedDB 数据库名称
|
|
73
|
+
* @default '__apiCacheDatabase__'
|
|
74
|
+
*/
|
|
75
|
+
indexDBName?: string;
|
|
76
|
+
/**
|
|
77
|
+
* 错误码在响应数据中的路径
|
|
78
|
+
* @default 'code'
|
|
79
|
+
*/
|
|
80
|
+
errorCodePath?: string;
|
|
81
|
+
/**
|
|
82
|
+
* 错误码映射表
|
|
83
|
+
* 可以配置错误码对应的错误信息或处理函数
|
|
84
|
+
* @default {} 空对象,使用默认处理函数
|
|
85
|
+
*/
|
|
86
|
+
errorCodeMap?: Record<string, string | ((code: string, data: Data, res: AxiosResponse<Data>, requestParam: RequestOptions<Params>) => ErrorHandlerReturnType<Data> | void)>;
|
|
87
|
+
/**
|
|
88
|
+
* 默认错误码处理函数
|
|
89
|
+
* 当错误码不在 errorCodeMap 中时调用
|
|
90
|
+
*/
|
|
91
|
+
defaultErrorCodeHandler?: (code: string, data: Data, res: AxiosResponse<Data>) => Promise<ErrorHandlerReturnType<Data> | void>;
|
|
92
|
+
/**
|
|
93
|
+
* 成功状态的错误码列表
|
|
94
|
+
* @default ['0', '200']
|
|
95
|
+
*/
|
|
96
|
+
successCodes?: string[];
|
|
97
|
+
/**
|
|
98
|
+
* HTTP 错误码映射表
|
|
99
|
+
* 可以配置 HTTP 状态码对应的错误信息或处理函数
|
|
100
|
+
* @default {} 空对象,使用默认处理函数
|
|
101
|
+
*/
|
|
102
|
+
httpErrorCodeMap?: Record<string, string | ((code: number, res: AxiosResponse<Data>, requestParam: RequestOptions<Params>) => Promise<ErrorHandlerReturnType<Data> | void>)>;
|
|
103
|
+
/**
|
|
104
|
+
* 默认 HTTP 错误码处理函数
|
|
105
|
+
* 当 HTTP 状态码不在 httpErrorCodeMap 中时调用
|
|
106
|
+
*/
|
|
107
|
+
defaultHttpErrorCodeHandler?: (code: number, error: any) => Promise<ErrorHandlerReturnType<Data> | void>;
|
|
108
|
+
/**
|
|
109
|
+
* 其他错误处理函数
|
|
110
|
+
* 处理非 HTTP 错误和非业务错误码的错误
|
|
111
|
+
*/
|
|
112
|
+
otherErrorHandler?: (error: any) => Promise<ErrorHandlerReturnType<Data> | void>;
|
|
113
|
+
axiosOptions?: Omit<AxiosRequestConfig<Params>, "method" | "url" | "params" | "data">;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* 请求参数接口
|
|
117
|
+
* @template Param 请求参数类型
|
|
118
|
+
*/
|
|
119
|
+
interface RequestOptions<Param> {
|
|
120
|
+
/** HTTP 请求方法 */
|
|
121
|
+
method: Method;
|
|
122
|
+
/** 请求URL */
|
|
123
|
+
url: string;
|
|
124
|
+
/** POST/PUT 等请求的数据 */
|
|
125
|
+
data?: Param;
|
|
126
|
+
/** URL 查询参数 */
|
|
127
|
+
params?: Param;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 创建基础请求实例
|
|
131
|
+
* @param baseOptions 基础配置选项
|
|
132
|
+
* @returns 请求创建函数
|
|
133
|
+
*/
|
|
134
|
+
declare function createBaseRequest(baseOptions?: Options): <Param, Data extends Record<any, any>>(requestOptions: RequestOptions<Param>, createOptions?: Omit<Options<Param, Data>, "baseURL">) => {
|
|
135
|
+
(requestParam?: Omit<RequestOptions<Param>, "url" | "method">, options?: Omit<Options<Param, Data>, "baseURL" | "cacheDataKey" | "cacheDataInStorage" | "cacheKeyEquals">): Promise<Data>;
|
|
136
|
+
clearCache(): void;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export { createBaseRequest as default };
|
|
140
|
+
export type { ErrorHandlerReturnType, Options, RequestOptions };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { GetterNameMap, ReducedData, StoreGetter, createStoreGetter, createStoreGetterMemo } from '../../store/createGetter/index.js';
|
|
2
|
+
export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from '../../store/createStateStore/index.js';
|
package/cjs/request/index.cjs
CHANGED
|
@@ -4,7 +4,7 @@ var tslib = require('tslib');
|
|
|
4
4
|
var axios = require('axios');
|
|
5
5
|
var lodash = require('lodash');
|
|
6
6
|
var defaultHandlers = require('./defaultHandlers.cjs');
|
|
7
|
-
var defaultEquals = require('../defaultEquals.cjs');
|
|
7
|
+
var defaultEquals = require('../_utils/defaultEquals.cjs');
|
|
8
8
|
var index = require('../cache/index.cjs');
|
|
9
9
|
var error = require('./error.cjs');
|
|
10
10
|
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var index = require('./createGetter/index.cjs');
|
|
4
|
+
var index$1 = require('./createStateStore/index.cjs');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
exports.createStoreGetter = index.createStoreGetter;
|
|
9
|
+
exports.createStoreGetterMemo = index.createStoreGetterMemo;
|
|
10
|
+
exports.createStateStore = index$1.default;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var validator = require('./validator.cjs');
|
|
4
|
+
var decorators = require('./decorators.cjs');
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
exports.BaseValidator = validator.BaseValidator;
|
|
9
|
+
exports.VArray = decorators.VArray;
|
|
10
|
+
exports.VBoolean = decorators.VBoolean;
|
|
11
|
+
exports.VEmail = decorators.VEmail;
|
|
12
|
+
exports.VMax = decorators.VMax;
|
|
13
|
+
exports.VMaxLength = decorators.VMaxLength;
|
|
14
|
+
exports.VMin = decorators.VMin;
|
|
15
|
+
exports.VMinLength = decorators.VMinLength;
|
|
16
|
+
exports.VNumber = decorators.VNumber;
|
|
17
|
+
exports.VPattern = decorators.VPattern;
|
|
18
|
+
exports.VRequired = decorators.VRequired;
|
|
19
|
+
exports.VString = decorators.VString;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 默认的相等性比较函数
|
|
3
|
+
* 通过将两个值序列化为 JSON 字符串来比较它们是否相等
|
|
4
|
+
*
|
|
5
|
+
* @template Param 比较值的类型,默认为 any
|
|
6
|
+
* @param prev 前一个值
|
|
7
|
+
* @param next 后一个值
|
|
8
|
+
* @returns {boolean} 如果两个值相等则返回 true,否则返回 false
|
|
9
|
+
*
|
|
10
|
+
* @remarks
|
|
11
|
+
* - 这个函数通过 JSON.stringify 进行比较,适用于大多数简单的数据结构
|
|
12
|
+
* - 不适用于包含函数、undefined、Symbol 等无法序列化的值
|
|
13
|
+
* - 对于循环引用的对象会抛出错误
|
|
14
|
+
*/
|
|
15
|
+
function defaultEquals(prev, next) {
|
|
16
|
+
return JSON.stringify(prev) === JSON.stringify(next);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export { defaultEquals as default };
|
package/es/cache/index.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { __awaiter, __generator, __assign } from 'tslib';
|
|
2
2
|
import moment from 'moment';
|
|
3
3
|
import { IndexedDBStorage } from './indexDB.mjs';
|
|
4
|
-
import defaultEquals from '../defaultEquals.mjs';
|
|
4
|
+
import defaultEquals from '../_utils/defaultEquals.mjs';
|
|
5
5
|
|
|
6
6
|
/** 存储类型映射表 */
|
|
7
7
|
var StorageMap = {
|
|
@@ -14,8 +14,16 @@ var StorageMap = {
|
|
|
14
14
|
* @template Data 缓存数据类型
|
|
15
15
|
*/
|
|
16
16
|
var Cache = /** @class */ (function () {
|
|
17
|
+
/**
|
|
18
|
+
* 构造函数
|
|
19
|
+
* @param cacheType 存储类型
|
|
20
|
+
* @param cacheKey 缓存键名
|
|
21
|
+
* @param cacheTime 缓存时间(秒)
|
|
22
|
+
* @param indexDBName IndexedDB 数据库名称,默认值为 '__apiCacheDatabase__'
|
|
23
|
+
* @param cacheKeyEquals 缓存键比较函数,默认使用 defaultEquals
|
|
24
|
+
*/
|
|
17
25
|
function Cache(cacheType, cacheKey, cacheTime, indexDBName, cacheKeyEquals) {
|
|
18
|
-
if (indexDBName === void 0) { indexDBName =
|
|
26
|
+
if (indexDBName === void 0) { indexDBName = "__apiCacheDatabase__"; }
|
|
19
27
|
if (cacheKeyEquals === void 0) { cacheKeyEquals = defaultEquals; }
|
|
20
28
|
/** 内存中的缓存数组 */
|
|
21
29
|
this.cache = [];
|
|
@@ -26,10 +34,10 @@ var Cache = /** @class */ (function () {
|
|
|
26
34
|
indexDBName: indexDBName,
|
|
27
35
|
cacheKeyEquals: cacheKeyEquals,
|
|
28
36
|
};
|
|
29
|
-
if (cacheType ===
|
|
30
|
-
this.storage = new IndexedDBStorage(indexDBName,
|
|
37
|
+
if (cacheType === "indexedDB") {
|
|
38
|
+
this.storage = new IndexedDBStorage(indexDBName, "cacheStore");
|
|
31
39
|
}
|
|
32
|
-
else if (typeof cacheType ===
|
|
40
|
+
else if (typeof cacheType === "string") {
|
|
33
41
|
this.storage = StorageMap[cacheType];
|
|
34
42
|
}
|
|
35
43
|
this._init();
|
|
@@ -51,15 +59,15 @@ var Cache = /** @class */ (function () {
|
|
|
51
59
|
_d = (_c = JSON).parse;
|
|
52
60
|
return [4 /*yield*/, this.storage.getItem(cacheKey)];
|
|
53
61
|
case 1:
|
|
54
|
-
_b.cache = _d.apply(_c, [(_e.sent()) ||
|
|
62
|
+
_b.cache = _d.apply(_c, [(_e.sent()) || "[]"]);
|
|
55
63
|
return [3 /*break*/, 3];
|
|
56
64
|
case 2:
|
|
57
65
|
if (this.storage instanceof Storage) {
|
|
58
66
|
this.storage = StorageMap[cacheType];
|
|
59
67
|
if (this.storage) {
|
|
60
|
-
if (typeof cacheKey ===
|
|
68
|
+
if (typeof cacheKey === "string") {
|
|
61
69
|
try {
|
|
62
|
-
this.cache = JSON.parse(this.storage.getItem(cacheKey) ||
|
|
70
|
+
this.cache = JSON.parse(this.storage.getItem(cacheKey) || "[]");
|
|
63
71
|
}
|
|
64
72
|
catch (e) {
|
|
65
73
|
this.cache = [];
|
|
@@ -76,7 +84,8 @@ var Cache = /** @class */ (function () {
|
|
|
76
84
|
}
|
|
77
85
|
});
|
|
78
86
|
});
|
|
79
|
-
};
|
|
87
|
+
};
|
|
88
|
+
/**
|
|
80
89
|
* 过滤掉已过期的缓存项
|
|
81
90
|
* 通过比较当前时间和过期时间,移除过期的缓存项
|
|
82
91
|
* @private
|
|
@@ -86,18 +95,20 @@ var Cache = /** @class */ (function () {
|
|
|
86
95
|
return moment(item.expireTime).isAfter(moment());
|
|
87
96
|
});
|
|
88
97
|
this.cache = newCache;
|
|
89
|
-
};
|
|
98
|
+
};
|
|
99
|
+
/**
|
|
90
100
|
* 将当前缓存数据保存到存储中
|
|
91
101
|
* 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
|
|
92
102
|
* @private
|
|
93
103
|
*/
|
|
94
104
|
Cache.prototype._saveToStorage = function () {
|
|
95
105
|
if (this.storage) {
|
|
96
|
-
if (typeof this.cacheOptions.cacheKey ===
|
|
106
|
+
if (typeof this.cacheOptions.cacheKey === "string") {
|
|
97
107
|
this.storage.setItem(this.cacheOptions.cacheKey, JSON.stringify(this.cache));
|
|
98
108
|
}
|
|
99
109
|
}
|
|
100
|
-
};
|
|
110
|
+
};
|
|
111
|
+
/**
|
|
101
112
|
* 设置缓存数据
|
|
102
113
|
* @param params 缓存的参数
|
|
103
114
|
* @param data 要缓存的数据
|
|
@@ -114,10 +125,11 @@ var Cache = /** @class */ (function () {
|
|
|
114
125
|
this.cache.push({
|
|
115
126
|
params: params,
|
|
116
127
|
data: data,
|
|
117
|
-
expireTime: moment().add(cacheTime,
|
|
128
|
+
expireTime: moment().add(cacheTime, "seconds").toJSON(),
|
|
118
129
|
});
|
|
119
130
|
this._saveToStorage();
|
|
120
|
-
};
|
|
131
|
+
};
|
|
132
|
+
/**
|
|
121
133
|
* 获取缓存数据
|
|
122
134
|
* @param params 查询参数
|
|
123
135
|
* @returns 如果找到有效的缓存数据则返回数据,否则返回 null
|
|
@@ -138,7 +150,8 @@ var Cache = /** @class */ (function () {
|
|
|
138
150
|
}
|
|
139
151
|
}
|
|
140
152
|
return null;
|
|
141
|
-
};
|
|
153
|
+
};
|
|
154
|
+
/**
|
|
142
155
|
* 清空所有缓存数据
|
|
143
156
|
* 清空内存中的缓存数组并同步到存储中
|
|
144
157
|
*/
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { IndexedDBStorage } from '../../cache/indexDB.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 缓存存储类型
|
|
5
|
+
* - sessionStorage: 会话存储,浏览器关闭后清除
|
|
6
|
+
* - localStorage: 本地存储,永久保存
|
|
7
|
+
* - indexedDB: IndexedDB 数据库存储
|
|
8
|
+
*/
|
|
9
|
+
type StorageType = "sessionStorage" | "localStorage" | "indexedDB";
|
|
10
|
+
/**
|
|
11
|
+
* 缓存项接口定义
|
|
12
|
+
* 定义了单个缓存项的数据结构
|
|
13
|
+
*
|
|
14
|
+
* @template Param 缓存参数类型
|
|
15
|
+
* @template Data 缓存数据类型
|
|
16
|
+
*/
|
|
17
|
+
interface ICache<Param, Data> {
|
|
18
|
+
/**
|
|
19
|
+
* 缓存的参数
|
|
20
|
+
* 用于标识和查找缓存项
|
|
21
|
+
*/
|
|
22
|
+
params: Param;
|
|
23
|
+
/**
|
|
24
|
+
* 缓存的数据
|
|
25
|
+
* 实际存储的内容
|
|
26
|
+
*/
|
|
27
|
+
data: Data;
|
|
28
|
+
/**
|
|
29
|
+
* 过期时间
|
|
30
|
+
* - ISO 8601 格式的字符串
|
|
31
|
+
* - 由 moment().add(cacheTime, 'seconds').toJSON() 生成
|
|
32
|
+
* - 示例:'2025-06-12T10:30:00.000Z'
|
|
33
|
+
*/
|
|
34
|
+
expireTime: string;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* 缓存选项接口
|
|
38
|
+
* @template Param 缓存参数类型
|
|
39
|
+
*/
|
|
40
|
+
interface ICacheOptions<Param> {
|
|
41
|
+
/**
|
|
42
|
+
* 存储类型
|
|
43
|
+
* - 'sessionStorage': 会话存储,浏览器关闭后清除
|
|
44
|
+
* - 'localStorage': 本地存储,永久保存
|
|
45
|
+
* - 'indexedDB': IndexedDB 数据库存储
|
|
46
|
+
* - undefined: 仅在内存中缓存(默认值)
|
|
47
|
+
*/
|
|
48
|
+
storageType?: StorageType;
|
|
49
|
+
/**
|
|
50
|
+
* 缓存键名
|
|
51
|
+
* - 当使用 localStorage/sessionStorage 时必须提供
|
|
52
|
+
* - 用于在存储中标识不同的缓存数据
|
|
53
|
+
* @default undefined 不使用持久化存储
|
|
54
|
+
*/
|
|
55
|
+
cacheKey?: string;
|
|
56
|
+
/**
|
|
57
|
+
* 缓存时间(秒)
|
|
58
|
+
* - 超过这个时间的缓存项会被自动清除
|
|
59
|
+
* @default 60 一分钟
|
|
60
|
+
*/
|
|
61
|
+
cacheTime?: number;
|
|
62
|
+
/**
|
|
63
|
+
* 缓存键比较函数
|
|
64
|
+
* - 用于判断两个缓存参数是否相等
|
|
65
|
+
* - 相等则认为是同一个缓存项
|
|
66
|
+
* @param prev 前一个参数
|
|
67
|
+
* @param next 后一个参数
|
|
68
|
+
* @returns 是否相等
|
|
69
|
+
* @default defaultEquals 使用 JSON.stringify 进行比较
|
|
70
|
+
*/
|
|
71
|
+
cacheKeyEquals: (prev: Param, next: Param) => boolean;
|
|
72
|
+
/**
|
|
73
|
+
* IndexedDB 数据库名称
|
|
74
|
+
* - 仅在 storageType 为 'indexedDB' 时使用
|
|
75
|
+
* @default '__apiCacheDatabase__'
|
|
76
|
+
*/
|
|
77
|
+
indexDBName?: string;
|
|
78
|
+
}
|
|
79
|
+
/** 存储类型映射表 */
|
|
80
|
+
declare const StorageMap: Record<StorageType | string, Storage>;
|
|
81
|
+
/**
|
|
82
|
+
* 缓存类
|
|
83
|
+
* @template Param 缓存参数类型
|
|
84
|
+
* @template Data 缓存数据类型
|
|
85
|
+
*/
|
|
86
|
+
declare class Cache<Param, Data> {
|
|
87
|
+
/** 内存中的缓存数组 */
|
|
88
|
+
cache: ICache<Param, Data>[];
|
|
89
|
+
/** 缓存选项 */
|
|
90
|
+
private cacheOptions;
|
|
91
|
+
/** 存储实例 */
|
|
92
|
+
storage?: Storage | IndexedDBStorage;
|
|
93
|
+
/**
|
|
94
|
+
* 构造函数
|
|
95
|
+
* @param cacheType 存储类型
|
|
96
|
+
* @param cacheKey 缓存键名
|
|
97
|
+
* @param cacheTime 缓存时间(秒)
|
|
98
|
+
* @param indexDBName IndexedDB 数据库名称,默认值为 '__apiCacheDatabase__'
|
|
99
|
+
* @param cacheKeyEquals 缓存键比较函数,默认使用 defaultEquals
|
|
100
|
+
*/
|
|
101
|
+
constructor(cacheType?: StorageType, cacheKey?: string, cacheTime?: number, indexDBName?: string, cacheKeyEquals?: (prev: Param, next: Param) => boolean);
|
|
102
|
+
/**
|
|
103
|
+
* 初始化缓存
|
|
104
|
+
* 从存储中加载已保存的缓存数据,并进行解析和过期处理
|
|
105
|
+
* @private
|
|
106
|
+
*/
|
|
107
|
+
private _init;
|
|
108
|
+
/**
|
|
109
|
+
* 过滤掉已过期的缓存项
|
|
110
|
+
* 通过比较当前时间和过期时间,移除过期的缓存项
|
|
111
|
+
* @private
|
|
112
|
+
*/
|
|
113
|
+
private _filterExpired;
|
|
114
|
+
/**
|
|
115
|
+
* 将当前缓存数据保存到存储中
|
|
116
|
+
* 如果设置了缓存键名且存储实例存在,则将缓存数据序列化后保存
|
|
117
|
+
* @private
|
|
118
|
+
*/
|
|
119
|
+
private _saveToStorage;
|
|
120
|
+
/**
|
|
121
|
+
* 设置缓存数据
|
|
122
|
+
* @param params 缓存的参数
|
|
123
|
+
* @param data 要缓存的数据
|
|
124
|
+
* @param cacheOptions 可选的缓存配置,可以覆盖默认的缓存时间
|
|
125
|
+
*/
|
|
126
|
+
setCache(params: Param, data: Data, cacheOptions?: Omit<ICacheOptions<Param>, "storageType" | "cacheKey" | "cacheKeyEquals">): void;
|
|
127
|
+
/**
|
|
128
|
+
* 获取缓存数据
|
|
129
|
+
* @param params 查询参数
|
|
130
|
+
* @returns 如果找到有效的缓存数据则返回数据,否则返回 null
|
|
131
|
+
*/
|
|
132
|
+
getCache(params: Param): Data;
|
|
133
|
+
/**
|
|
134
|
+
* 清空所有缓存数据
|
|
135
|
+
* 清空内存中的缓存数组并同步到存储中
|
|
136
|
+
*/
|
|
137
|
+
clear(): void;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
export { StorageMap, Cache as default };
|
|
141
|
+
export type { ICache, ICacheOptions, StorageType };
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { default as Cache, ICache, ICacheOptions, StorageMap, StorageType } from './cache/index.js';
|
|
2
|
+
export { ErrorHandlerReturnType, Options, RequestOptions, default as createBaseRequest } from './request/index.js';
|
|
3
|
+
export { createStoreGetter, createStoreGetterMemo } from '../store/createGetter/index.js';
|
|
4
|
+
export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from '../store/createStateStore/index.js';
|
|
5
|
+
export { BaseValidator } from '../validator/validator.js';
|
|
6
|
+
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from '../validator/decorators.js';
|
|
7
|
+
export { default as RequestError, RequestErrorType } from '../request/error.js';
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { AxiosResponse, Method, AxiosRequestConfig } from 'axios';
|
|
2
|
+
import { StorageType } from '../cache/index.js';
|
|
3
|
+
export { default as RequestError, RequestErrorType } from '../../request/error.js';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 错误处理器返回类型
|
|
7
|
+
* @template D 响应数据类型
|
|
8
|
+
*/
|
|
9
|
+
type ErrorHandlerReturnType<D> = {
|
|
10
|
+
/** 替换响应数据 */
|
|
11
|
+
replaceResData?: D;
|
|
12
|
+
/**
|
|
13
|
+
* 是否抛出错误
|
|
14
|
+
* - true: 强制抛出错误
|
|
15
|
+
* - false: 不抛出错误
|
|
16
|
+
* - 'default': 使用默认错误处理逻辑
|
|
17
|
+
*/
|
|
18
|
+
throwError?: boolean | "default";
|
|
19
|
+
};
|
|
20
|
+
/**
|
|
21
|
+
* 请求配置选项接口
|
|
22
|
+
* @template Params 请求参数类型
|
|
23
|
+
* @template Data 响应数据类型
|
|
24
|
+
*/
|
|
25
|
+
interface Options<Params = any, Data = any> {
|
|
26
|
+
/** 请求基础URL,默认为空字符串 */
|
|
27
|
+
baseURL?: string;
|
|
28
|
+
/**
|
|
29
|
+
* 是否抛出错误
|
|
30
|
+
* @default true
|
|
31
|
+
*/
|
|
32
|
+
throwError?: boolean;
|
|
33
|
+
/**
|
|
34
|
+
* 默认的消息展示函数
|
|
35
|
+
* @default window.alert
|
|
36
|
+
*/
|
|
37
|
+
defaultMessageShower?: (message: string) => void;
|
|
38
|
+
/**
|
|
39
|
+
* 是否启用缓存功能
|
|
40
|
+
* @default false
|
|
41
|
+
*/
|
|
42
|
+
enableCache?: boolean;
|
|
43
|
+
/**
|
|
44
|
+
* 缓存键比较函数
|
|
45
|
+
* @default defaultEquals 使用 JSON.stringify 进行比较
|
|
46
|
+
*/
|
|
47
|
+
cacheKeyEquals?: (prev: Params, next: Params) => boolean;
|
|
48
|
+
/**
|
|
49
|
+
* 是否将响应数据存入缓存
|
|
50
|
+
* @default false
|
|
51
|
+
*/
|
|
52
|
+
cacheData?: boolean;
|
|
53
|
+
/**
|
|
54
|
+
* 缓存时间(秒)
|
|
55
|
+
* @default 60
|
|
56
|
+
*/
|
|
57
|
+
cacheTime?: number;
|
|
58
|
+
/**
|
|
59
|
+
* 缓存数据的存储类型
|
|
60
|
+
* - localStorage: 使用浏览器本地存储,数据永久保存
|
|
61
|
+
* - sessionStorage: 使用会话存储,关闭浏览器后清除
|
|
62
|
+
* - indexedDB: 使用 IndexedDB 数据库存储
|
|
63
|
+
* - 不填则仅在内存中缓存,页面刷新后清除
|
|
64
|
+
*/
|
|
65
|
+
cacheDataInStorage?: StorageType;
|
|
66
|
+
/**
|
|
67
|
+
* 缓存数据的键名
|
|
68
|
+
* @default `${method}:${baseURL}${url}` 默认使用请求方法、基础URL和请求路径组合
|
|
69
|
+
*/
|
|
70
|
+
cacheDataKey?: string;
|
|
71
|
+
/**
|
|
72
|
+
* IndexedDB 数据库名称
|
|
73
|
+
* @default '__apiCacheDatabase__'
|
|
74
|
+
*/
|
|
75
|
+
indexDBName?: string;
|
|
76
|
+
/**
|
|
77
|
+
* 错误码在响应数据中的路径
|
|
78
|
+
* @default 'code'
|
|
79
|
+
*/
|
|
80
|
+
errorCodePath?: string;
|
|
81
|
+
/**
|
|
82
|
+
* 错误码映射表
|
|
83
|
+
* 可以配置错误码对应的错误信息或处理函数
|
|
84
|
+
* @default {} 空对象,使用默认处理函数
|
|
85
|
+
*/
|
|
86
|
+
errorCodeMap?: Record<string, string | ((code: string, data: Data, res: AxiosResponse<Data>, requestParam: RequestOptions<Params>) => ErrorHandlerReturnType<Data> | void)>;
|
|
87
|
+
/**
|
|
88
|
+
* 默认错误码处理函数
|
|
89
|
+
* 当错误码不在 errorCodeMap 中时调用
|
|
90
|
+
*/
|
|
91
|
+
defaultErrorCodeHandler?: (code: string, data: Data, res: AxiosResponse<Data>) => Promise<ErrorHandlerReturnType<Data> | void>;
|
|
92
|
+
/**
|
|
93
|
+
* 成功状态的错误码列表
|
|
94
|
+
* @default ['0', '200']
|
|
95
|
+
*/
|
|
96
|
+
successCodes?: string[];
|
|
97
|
+
/**
|
|
98
|
+
* HTTP 错误码映射表
|
|
99
|
+
* 可以配置 HTTP 状态码对应的错误信息或处理函数
|
|
100
|
+
* @default {} 空对象,使用默认处理函数
|
|
101
|
+
*/
|
|
102
|
+
httpErrorCodeMap?: Record<string, string | ((code: number, res: AxiosResponse<Data>, requestParam: RequestOptions<Params>) => Promise<ErrorHandlerReturnType<Data> | void>)>;
|
|
103
|
+
/**
|
|
104
|
+
* 默认 HTTP 错误码处理函数
|
|
105
|
+
* 当 HTTP 状态码不在 httpErrorCodeMap 中时调用
|
|
106
|
+
*/
|
|
107
|
+
defaultHttpErrorCodeHandler?: (code: number, error: any) => Promise<ErrorHandlerReturnType<Data> | void>;
|
|
108
|
+
/**
|
|
109
|
+
* 其他错误处理函数
|
|
110
|
+
* 处理非 HTTP 错误和非业务错误码的错误
|
|
111
|
+
*/
|
|
112
|
+
otherErrorHandler?: (error: any) => Promise<ErrorHandlerReturnType<Data> | void>;
|
|
113
|
+
axiosOptions?: Omit<AxiosRequestConfig<Params>, "method" | "url" | "params" | "data">;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* 请求参数接口
|
|
117
|
+
* @template Param 请求参数类型
|
|
118
|
+
*/
|
|
119
|
+
interface RequestOptions<Param> {
|
|
120
|
+
/** HTTP 请求方法 */
|
|
121
|
+
method: Method;
|
|
122
|
+
/** 请求URL */
|
|
123
|
+
url: string;
|
|
124
|
+
/** POST/PUT 等请求的数据 */
|
|
125
|
+
data?: Param;
|
|
126
|
+
/** URL 查询参数 */
|
|
127
|
+
params?: Param;
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* 创建基础请求实例
|
|
131
|
+
* @param baseOptions 基础配置选项
|
|
132
|
+
* @returns 请求创建函数
|
|
133
|
+
*/
|
|
134
|
+
declare function createBaseRequest(baseOptions?: Options): <Param, Data extends Record<any, any>>(requestOptions: RequestOptions<Param>, createOptions?: Omit<Options<Param, Data>, "baseURL">) => {
|
|
135
|
+
(requestParam?: Omit<RequestOptions<Param>, "url" | "method">, options?: Omit<Options<Param, Data>, "baseURL" | "cacheDataKey" | "cacheDataInStorage" | "cacheKeyEquals">): Promise<Data>;
|
|
136
|
+
clearCache(): void;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export { createBaseRequest as default };
|
|
140
|
+
export type { ErrorHandlerReturnType, Options, RequestOptions };
|
|
@@ -0,0 +1,2 @@
|
|
|
1
|
+
export { GetterNameMap, ReducedData, StoreGetter, createStoreGetter, createStoreGetterMemo } from '../../store/createGetter/index.js';
|
|
2
|
+
export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from '../../store/createStateStore/index.js';
|
package/es/request/index.mjs
CHANGED
|
@@ -2,7 +2,7 @@ import { __assign, __awaiter, __generator } from 'tslib';
|
|
|
2
2
|
import axios from 'axios';
|
|
3
3
|
import { at } from 'lodash';
|
|
4
4
|
import { _defaultErrorCodeHandler, _defaultHttpErrorCodeHandler, _defaultOtherErrorCodeHandler } from './defaultHandlers.mjs';
|
|
5
|
-
import defaultEquals from '../defaultEquals.mjs';
|
|
5
|
+
import defaultEquals from '../_utils/defaultEquals.mjs';
|
|
6
6
|
import Cache from '../cache/index.mjs';
|
|
7
7
|
import RequestError from './error.mjs';
|
|
8
8
|
|