rxtutils 1.1.2-beta.15 → 1.1.2-beta.16
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/{packages/cache → cache}/index.d.ts +1 -1
- package/cjs/{packages/index.d.ts → index.d.ts} +5 -5
- package/cjs/{packages/request → request}/index.d.ts +1 -1
- package/cjs/{packages/store → store}/index.d.ts +2 -2
- package/{es/validator/index.mjs → cjs/validator/index.d.ts} +2 -2
- package/es/cache/index.d.ts +141 -0
- package/es/index.d.ts +7 -0
- package/es/request/index.d.ts +140 -0
- package/es/store/index.d.ts +2 -0
- package/es/validator/index.d.ts +2 -2
- package/package.json +1 -1
- package/cjs/packages/validator/index.d.ts +0 -2
- package/cjs/store/index.cjs +0 -10
- package/cjs/validator/index.cjs +0 -19
- package/es/store/index.mjs +0 -2
|
@@ -1,7 +1,7 @@
|
|
|
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 { createStoreGetter, createStoreGetterMemo } from '
|
|
4
|
-
export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from '
|
|
5
|
-
export { BaseValidator } from '
|
|
6
|
-
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from '
|
|
7
|
-
export { default as RequestError, RequestErrorType } from '
|
|
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';
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AxiosResponse, Method, AxiosRequestConfig } from 'axios';
|
|
2
2
|
import { StorageType } from '../cache/index.js';
|
|
3
|
-
export { default as RequestError, RequestErrorType } from '
|
|
3
|
+
export { default as RequestError, RequestErrorType } from './error.js';
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* 错误处理器返回类型
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { GetterNameMap, ReducedData, StoreGetter, createStoreGetter, createStoreGetterMemo } from '
|
|
2
|
-
export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from '
|
|
1
|
+
export { GetterNameMap, ReducedData, StoreGetter, createStoreGetter, createStoreGetterMemo } from './createGetter/index.js';
|
|
2
|
+
export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from './createStateStore/index.js';
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { BaseValidator } from './validator.
|
|
2
|
-
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from './decorators.
|
|
1
|
+
export { BaseValidator } from './validator.js';
|
|
2
|
+
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from './decorators.js';
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
import { IndexedDBStorage } from './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 };
|
package/es/index.d.ts
ADDED
|
@@ -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 './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 './createGetter/index.js';
|
|
2
|
+
export { IHookStateInitAction, IHookStateInitialSetter, IHookStateResolvable, IHookStateSetAction, IHookStateSetter, default as createStateStore } from './createStateStore/index.js';
|
package/es/validator/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { BaseValidator } from '
|
|
2
|
-
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from '
|
|
1
|
+
export { BaseValidator } from './validator.js';
|
|
2
|
+
export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from './decorators.js';
|
package/package.json
CHANGED
package/cjs/store/index.cjs
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
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;
|
package/cjs/validator/index.cjs
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
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;
|
package/es/store/index.mjs
DELETED