rxtutils 1.1.4-beta.9 → 1.1.6

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.
Files changed (64) hide show
  1. package/README.md +117 -49
  2. package/cjs/_utils/defaultEquals.cjs +4 -19
  3. package/cjs/cache/index.cjs +130 -163
  4. package/cjs/cache/indexDB.cjs +86 -99
  5. package/cjs/downloadBlob.cjs +14 -0
  6. package/cjs/hooks/index.cjs +3 -6
  7. package/cjs/hooks/useCombineControlValue.cjs +33 -41
  8. package/cjs/index.cjs +16 -17
  9. package/cjs/request/defaultHandlers.cjs +5 -27
  10. package/cjs/request/error.cjs +18 -30
  11. package/cjs/request/index.cjs +194 -174
  12. package/cjs/store/createGetter/index.cjs +26 -40
  13. package/cjs/store/createStateStore/index.cjs +47 -90
  14. package/cjs/store/index.cjs +4 -7
  15. package/cjs/validator/decorators.cjs +71 -228
  16. package/cjs/validator/index.cjs +4 -7
  17. package/cjs/validator/validator.cjs +101 -177
  18. package/es/_utils/defaultEquals.mjs +4 -17
  19. package/es/cache/index.d.ts +9 -13
  20. package/es/cache/index.mjs +132 -160
  21. package/es/cache/indexDB.d.ts +1 -3
  22. package/es/cache/indexDB.mjs +87 -98
  23. package/es/downloadBlob.d.ts +8 -0
  24. package/es/downloadBlob.mjs +14 -0
  25. package/es/hooks/index.d.ts +1 -1
  26. package/es/hooks/index.mjs +4 -1
  27. package/es/hooks/useCombineControlValue.d.ts +5 -8
  28. package/es/hooks/useCombineControlValue.mjs +34 -40
  29. package/es/index.d.ts +28 -8
  30. package/es/index.mjs +29 -7
  31. package/es/request/defaultHandlers.d.ts +24 -0
  32. package/es/request/defaultHandlers.mjs +8 -26
  33. package/es/request/error.d.ts +3 -6
  34. package/es/request/error.mjs +18 -28
  35. package/es/request/index.d.ts +32 -20
  36. package/es/request/index.mjs +194 -172
  37. package/es/store/createGetter/index.d.ts +6 -10
  38. package/es/store/createGetter/index.mjs +28 -39
  39. package/es/store/createStateStore/index.d.ts +9 -9
  40. package/es/store/createStateStore/index.mjs +49 -87
  41. package/es/store/index.d.ts +4 -2
  42. package/es/store/index.mjs +7 -2
  43. package/es/validator/decorators.d.ts +12 -21
  44. package/es/validator/decorators.mjs +81 -226
  45. package/es/validator/index.d.ts +2 -2
  46. package/es/validator/index.mjs +16 -2
  47. package/es/validator/validator.d.ts +5 -6
  48. package/es/validator/validator.mjs +102 -176
  49. package/package.json +85 -15
  50. package/cjs/_utils/deepAssign.cjs +0 -25
  51. package/cjs/cache/index.d.ts +0 -141
  52. package/cjs/cache/indexDB.d.ts +0 -52
  53. package/cjs/hooks/index.d.ts +0 -1
  54. package/cjs/hooks/useCombineControlValue.d.ts +0 -21
  55. package/cjs/index.d.ts +0 -8
  56. package/cjs/request/error.d.ts +0 -31
  57. package/cjs/request/index.d.ts +0 -147
  58. package/cjs/store/createGetter/index.d.ts +0 -30
  59. package/cjs/store/createStateStore/index.d.ts +0 -42
  60. package/cjs/store/index.d.ts +0 -2
  61. package/cjs/validator/decorators.d.ts +0 -159
  62. package/cjs/validator/index.d.ts +0 -2
  63. package/cjs/validator/validator.d.ts +0 -84
  64. package/es/_utils/deepAssign.mjs +0 -23
@@ -1,41 +1,35 @@
1
- import { useState, useCallback, useMemo } from 'react';
2
-
3
- /**
4
- * @param param props 组件属性
5
- * @param param valueKey 组件值的key,默认value, undefined 被认为是有值,只有当 key 不存在时,转换为非受控模式
6
- * @param param defaultValue 默认值,当 value 和 defaultValue 同时存在时,以 value 为默认值
7
- * @param param onChange 值改变时的回调
8
- * @returns value: 组件应该采用的值,onChange:值改变时的回调,处理非受控值与向父组件传递值的逻辑
9
- */
10
- function useCombineControlValue(_a, resolveFn) {
11
- var props = _a.props, _b = _a.valueKey, valueKey = _b === void 0 ? 'value' : _b, defaultValue = _a.defaultValue, onChange = _a.onChange;
12
- var _c = props, _d = valueKey, value = _c[_d];
13
- var hasValue = Object.prototype.hasOwnProperty.call(props, valueKey);
14
- var _e = useState(value !== null && value !== void 0 ? value : defaultValue), internalValue = _e[0], setInternalValue = _e[1];
15
- var handleChange = useCallback(function () {
16
- var params = [];
17
- for (var _i = 0; _i < arguments.length; _i++) {
18
- params[_i] = arguments[_i];
19
- }
20
- var realNextVal;
21
- if (typeof resolveFn === 'function') {
22
- realNextVal = resolveFn.apply(void 0, params);
23
- }
24
- else {
25
- realNextVal = params[0];
26
- }
27
- setInternalValue(realNextVal);
28
- onChange === null || onChange === void 0 ? void 0 : onChange.apply(void 0, params);
29
- }, [onChange, resolveFn]);
30
- var finalValue = useMemo(function () {
31
- if (hasValue)
32
- return value;
33
- return internalValue;
34
- }, [hasValue, internalValue, value]);
35
- return {
36
- value: finalValue,
37
- onChange: handleChange
38
- };
1
+ import { useState, useCallback, useMemo } from "react";
2
+ function useCombineControlValue({
3
+ props,
4
+ valueKey = "value",
5
+ defaultValue,
6
+ onChange
7
+ }, resolveFn) {
8
+ const { [valueKey]: value } = props;
9
+ const hasValue = Object.prototype.hasOwnProperty.call(props, valueKey);
10
+ const [internalValue, setInternalValue] = useState(value ?? defaultValue);
11
+ const handleChange = useCallback(
12
+ (...params) => {
13
+ let realNextVal;
14
+ if (typeof resolveFn === "function") {
15
+ realNextVal = resolveFn(...params);
16
+ } else {
17
+ realNextVal = params[0];
18
+ }
19
+ setInternalValue(realNextVal);
20
+ onChange?.(...params);
21
+ },
22
+ [onChange, resolveFn]
23
+ );
24
+ const finalValue = useMemo(() => {
25
+ if (hasValue) return value;
26
+ return internalValue;
27
+ }, [hasValue, internalValue, value]);
28
+ return {
29
+ value: finalValue,
30
+ onChange: handleChange
31
+ };
39
32
  }
40
-
41
- export { useCombineControlValue };
33
+ export {
34
+ useCombineControlValue
35
+ };
package/es/index.d.ts CHANGED
@@ -1,8 +1,28 @@
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 { useCombineControlValue } from './hooks/useCombineControlValue.js';
8
- export { default as RequestError, RequestErrorType } from './request/error.js';
1
+ /**
2
+ * 导出主要功能模块
3
+ */
4
+ /** 缓存管理模块 */
5
+ export { default as Cache } from './cache';
6
+ /** HTTP 请求创建模块 */
7
+ export { default as createBaseRequest } from './request';
8
+ /** 状态管理模块 */
9
+ export { createStateStore } from './store';
10
+ export { createStoreGetter, createStoreGetterMemo } from './store';
11
+ export { VRequired, VString, VNumber, VEmail, VMinLength, VArray, VBoolean, VPattern, VMaxLength, VMax, VMin, BaseValidator, } from './validator';
12
+ /**
13
+ * 缓存模块类型导出
14
+ * 包含缓存接口、配置选项、存储映射和存储类型
15
+ */
16
+ export type { ICache, ICacheOptions, StorageMap, StorageType } from './cache';
17
+ /**
18
+ * 请求模块类型导出
19
+ * 包含错误处理、请求选项和错误类型
20
+ */
21
+ export type { ErrorHandlerReturnType, Options, RequestOptions, RequestError, RequestErrorType, } from './request';
22
+ /**
23
+ * 状态管理模块类型导出
24
+ * 包含状态初始化、设置和更新的类型定义
25
+ */
26
+ export type { IHookStateInitialSetter, IHookStateInitAction, IHookStateSetter, IHookStateSetAction, IHookStateResolvable, } from './store';
27
+ export { useCombineControlValue } from './hooks';
28
+ export { downloadBlob } from './downloadBlob';
package/es/index.mjs CHANGED
@@ -1,7 +1,29 @@
1
- export { default as Cache } from './cache/index.mjs';
2
- export { default as createBaseRequest } from './request/index.mjs';
3
- export { createStoreGetter, createStoreGetterMemo } from './store/createGetter/index.mjs';
4
- export { default as createStateStore } from './store/createStateStore/index.mjs';
5
- export { BaseValidator } from './validator/validator.mjs';
6
- export { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from './validator/decorators.mjs';
7
- export { useCombineControlValue } from './hooks/useCombineControlValue.mjs';
1
+ import { default as default2 } from "./cache/index.mjs";
2
+ import { default as default3 } from "./request/index.mjs";
3
+ import { createStoreGetter, createStoreGetterMemo } from "./store/createGetter/index.mjs";
4
+ import { default as default4 } from "./store/createStateStore/index.mjs";
5
+ import { BaseValidator } from "./validator/validator.mjs";
6
+ import { VArray, VBoolean, VEmail, VMax, VMaxLength, VMin, VMinLength, VNumber, VPattern, VRequired, VString } from "./validator/decorators.mjs";
7
+ import { useCombineControlValue } from "./hooks/useCombineControlValue.mjs";
8
+ import { downloadBlob } from "./downloadBlob.mjs";
9
+ export {
10
+ BaseValidator,
11
+ default2 as Cache,
12
+ VArray,
13
+ VBoolean,
14
+ VEmail,
15
+ VMax,
16
+ VMaxLength,
17
+ VMin,
18
+ VMinLength,
19
+ VNumber,
20
+ VPattern,
21
+ VRequired,
22
+ VString,
23
+ default3 as createBaseRequest,
24
+ default4 as createStateStore,
25
+ createStoreGetter,
26
+ createStoreGetterMemo,
27
+ downloadBlob,
28
+ useCombineControlValue
29
+ };
@@ -0,0 +1,24 @@
1
+ /**
2
+ * 默认业务错误码处理函数
3
+ * 用于处理业务错误码的默认行为
4
+ *
5
+ * @param defaultMessageShower 消息展示函数
6
+ * @param code 业务错误码
7
+ */
8
+ export declare function _defaultErrorCodeHandler(defaultMessageShower: (message: string) => void, code: string): void;
9
+ /**
10
+ * 默认 HTTP 错误码处理函数
11
+ * 用于处理 HTTP 状态码错误的默认行为
12
+ *
13
+ * @param defaultMessageShower 消息展示函数
14
+ * @param code HTTP 状态码
15
+ */
16
+ export declare function _defaultHttpErrorCodeHandler(defaultMessageShower: (message: string) => void, code: number): void;
17
+ /**
18
+ * 默认其他错误处理函数
19
+ * 用于处理未知错误或非标准错误的默认行为
20
+ *
21
+ * @param defaultMessageShower 消息展示函数
22
+ * @param error 错误对象
23
+ */
24
+ export declare function _defaultOtherErrorCodeHandler(defaultMessageShower: (message: string) => void, error: any): void;
@@ -1,32 +1,14 @@
1
- /**
2
- * 默认业务错误码处理函数
3
- * 用于处理业务错误码的默认行为
4
- *
5
- * @param defaultMessageShower 消息展示函数
6
- * @param code 业务错误码
7
- */
8
1
  function _defaultErrorCodeHandler(defaultMessageShower, code) {
9
- defaultMessageShower("\u8BF7\u6C42\u51FA\u9519\uFF0C\u9519\u8BEF\u7801\uFF1A".concat(code, "\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5"));
2
+ defaultMessageShower(`请求出错,错误码:${code},请稍后再试`);
10
3
  }
11
- /**
12
- * 默认 HTTP 错误码处理函数
13
- * 用于处理 HTTP 状态码错误的默认行为
14
- *
15
- * @param defaultMessageShower 消息展示函数
16
- * @param code HTTP 状态码
17
- */
18
4
  function _defaultHttpErrorCodeHandler(defaultMessageShower, code) {
19
- defaultMessageShower("\u670D\u52A1\u7AEF\u8BF7\u6C42\u51FA\u9519\uFF0CHttp\u9519\u8BEF\u7801\uFF1A".concat(String(code), "\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5"));
5
+ defaultMessageShower(`服务端请求出错,Http错误码:${String(code)},请稍后再试`);
20
6
  }
21
- /**
22
- * 默认其他错误处理函数
23
- * 用于处理未知错误或非标准错误的默认行为
24
- *
25
- * @param defaultMessageShower 消息展示函数
26
- * @param error 错误对象
27
- */
28
7
  function _defaultOtherErrorCodeHandler(defaultMessageShower, error) {
29
- defaultMessageShower("\u672A\u77E5\u8BF7\u6C42\u51FA\u9519\uFF0C\u8BF7\u7A0D\u540E\u518D\u8BD5");
8
+ defaultMessageShower(`未知请求出错,请稍后再试`);
30
9
  }
31
-
32
- export { _defaultErrorCodeHandler, _defaultHttpErrorCodeHandler, _defaultOtherErrorCodeHandler };
10
+ export {
11
+ _defaultErrorCodeHandler,
12
+ _defaultHttpErrorCodeHandler,
13
+ _defaultOtherErrorCodeHandler
14
+ };
@@ -3,7 +3,7 @@
3
3
  * - server: 服务端业务错误
4
4
  * - http: HTTP 网络错误
5
5
  */
6
- type RequestErrorType = 'server' | 'http';
6
+ export type RequestErrorType = 'server' | 'http';
7
7
  /**
8
8
  * 请求错误类
9
9
  * 用于统一处理请求过程中的各种错误
@@ -11,7 +11,7 @@ type RequestErrorType = 'server' | 'http';
11
11
  * @template Data 错误数据类型
12
12
  * @extends Error
13
13
  */
14
- declare class RequestError<Data = any> extends Error {
14
+ export default class RequestError<Data = any> extends Error {
15
15
  /** 错误码 */
16
16
  code: string;
17
17
  /** 错误类型 */
@@ -24,8 +24,5 @@ declare class RequestError<Data = any> extends Error {
24
24
  * @param type 错误类型
25
25
  * @param data 错误相关的数据
26
26
  */
27
- constructor(message: string, type: RequestErrorType, data?: Data);
27
+ constructor(message: string, type: RequestErrorType, data?: Data, code?: string);
28
28
  }
29
-
30
- export { RequestError as default };
31
- export type { RequestErrorType };
@@ -1,28 +1,18 @@
1
- import { __extends } from 'tslib';
2
-
3
- /**
4
- * 请求错误类
5
- * 用于统一处理请求过程中的各种错误
6
- *
7
- * @template Data 错误数据类型
8
- * @extends Error
9
- */
10
- var RequestError = /** @class */ (function (_super) {
11
- __extends(RequestError, _super);
12
- /**
13
- * 构造函数
14
- * @param message 错误消息
15
- * @param type 错误类型
16
- * @param data 错误相关的数据
17
- */
18
- function RequestError(message, type, data) {
19
- var _this = _super.call(this, message) || this;
20
- _this.name = 'RequestError';
21
- _this.type = type;
22
- _this.data = data;
23
- return _this;
24
- }
25
- return RequestError;
26
- }(Error));
27
-
28
- export { RequestError as default };
1
+ class RequestError extends Error {
2
+ /**
3
+ * 构造函数
4
+ * @param message 错误消息
5
+ * @param type 错误类型
6
+ * @param data 错误相关的数据
7
+ */
8
+ constructor(message, type, data, code = "") {
9
+ super(message);
10
+ this.name = "RequestError";
11
+ this.type = type;
12
+ this.data = data;
13
+ this.code = code;
14
+ }
15
+ }
16
+ export {
17
+ RequestError as default
18
+ };
@@ -1,12 +1,11 @@
1
- import { AxiosResponse, Method, AxiosRequestConfig } from 'axios';
2
- import { StorageType } from '../cache/index.js';
3
- export { default as RequestError, RequestErrorType } from './error.js';
4
-
1
+ import { AxiosResponse, AxiosRequestConfig, Method } from 'axios';
2
+ import { StorageType } from '../cache';
3
+ import { default as RequestError, RequestErrorType } from './error';
5
4
  /**
6
5
  * 错误处理器返回类型
7
6
  * @template D 响应数据类型
8
7
  */
9
- type ErrorHandlerReturnType<D> = {
8
+ export type ErrorHandlerReturnType<D> = {
10
9
  /** 替换响应数据 */
11
10
  replaceResData?: D;
12
11
  /**
@@ -15,14 +14,14 @@ type ErrorHandlerReturnType<D> = {
15
14
  * - false: 不抛出错误
16
15
  * - 'default': 使用默认错误处理逻辑
17
16
  */
18
- throwError?: boolean | "default";
17
+ throwError?: boolean | 'default';
19
18
  };
20
19
  /**
21
20
  * 请求配置选项接口
22
21
  * @template Params 请求参数类型
23
22
  * @template Data 响应数据类型
24
23
  */
25
- interface Options<Params = any, Data = any> {
24
+ export interface Options<Params = any, Data = any> {
26
25
  /** 请求基础URL,默认为空字符串 */
27
26
  baseURL?: string;
28
27
  /**
@@ -83,12 +82,12 @@ interface Options<Params = any, Data = any> {
83
82
  * 可以配置错误码对应的错误信息或处理函数
84
83
  * @default {} 空对象,使用默认处理函数
85
84
  */
86
- errorCodeMap?: Record<string, string | ((code: string, data: Data, res: AxiosResponse<Data>, requestParam: RequestOptions<Params>) => ErrorHandlerReturnType<Data> | void)>;
85
+ errorCodeMap?: Record<string, string | ((code: string, data: Data, res: AxiosResponse<Data>, requestParam: RequestOptions<Params>) => ErrorHandlerReturnType<Data> | void | Promise<ErrorHandlerReturnType<Data> | void>)>;
87
86
  /**
88
87
  * 默认错误码处理函数
89
88
  * 当错误码不在 errorCodeMap 中时调用
90
89
  */
91
- defaultErrorCodeHandler?: (code: string, data: Data, res: AxiosResponse<Data>) => Promise<ErrorHandlerReturnType<Data> | void>;
90
+ defaultErrorCodeHandler?: (code: string, data: Data, res: AxiosResponse<Data>) => ErrorHandlerReturnType<Data> | void | Promise<ErrorHandlerReturnType<Data> | void>;
92
91
  /**
93
92
  * 成功状态的错误码列表
94
93
  * @default ['0', '200']
@@ -99,31 +98,46 @@ interface Options<Params = any, Data = any> {
99
98
  * 可以配置 HTTP 状态码对应的错误信息或处理函数
100
99
  * @default {} 空对象,使用默认处理函数
101
100
  */
102
- httpErrorCodeMap?: Record<string, string | ((code: number, res: AxiosResponse<Data>, requestParam: RequestOptions<Params>) => Promise<ErrorHandlerReturnType<Data> | void>)>;
101
+ httpErrorCodeMap?: Record<string, string | ((code: number, res: AxiosResponse<Data>, requestParam: RequestOptions<Params>) => ErrorHandlerReturnType<Data> | void | Promise<ErrorHandlerReturnType<Data> | void>)>;
103
102
  /**
104
103
  * 默认 HTTP 错误码处理函数
105
104
  * 当 HTTP 状态码不在 httpErrorCodeMap 中时调用
106
105
  */
107
- defaultHttpErrorCodeHandler?: (code: number, error: any) => Promise<ErrorHandlerReturnType<Data> | void>;
106
+ defaultHttpErrorCodeHandler?: (code: number, error: any) => ErrorHandlerReturnType<Data> | void | Promise<ErrorHandlerReturnType<Data> | void>;
108
107
  /**
109
108
  * 其他错误处理函数
110
109
  * 处理非 HTTP 错误和非业务错误码的错误
111
110
  */
112
- otherErrorHandler?: (error: any) => Promise<ErrorHandlerReturnType<Data> | void>;
113
- axiosOptions?: Omit<AxiosRequestConfig<Params>, "method" | "url" | "params" | "data">;
111
+ otherErrorHandler?: (error: any) => ErrorHandlerReturnType<Data> | void | Promise<ErrorHandlerReturnType<Data> | void>;
112
+ axiosOptions?: Omit<AxiosRequestConfig<Params>, 'method' | 'url' | 'params' | 'data'>;
114
113
  requestMiddlewares?: ((options: Options<Params, Data>, requestOptions: RequestOptions<Params>) => Promise<{
115
- axiosOptions?: Options<Params, Data>["axiosOptions"];
114
+ axiosOptions?: Options<Params, Data>['axiosOptions'];
116
115
  requestOptions?: RequestOptions<Params>;
117
116
  }> | {
118
- axiosOptions: Options<Params, Data>["axiosOptions"];
117
+ axiosOptions: Options<Params, Data>['axiosOptions'];
119
118
  requestOptions?: RequestOptions<Params>;
120
119
  })[];
120
+ retryTimes?: number;
121
+ /**
122
+ * 请求参数或数据转换函数
123
+ * 可以在发送请求前对参数或数据进行处理
124
+ * @param paramsOrData 请求参数或数据
125
+ * @returns 处理后的参数或数据
126
+ */
127
+ requestParamsOrDataTransfer?: (paramsOrData: Params) => any;
128
+ /**
129
+ * 响应数据转换函数
130
+ * 可以在接收响应数据后对数据进行处理
131
+ * @param data 响应数据
132
+ * @returns 处理后的响应数据
133
+ */
134
+ responseTransfer?: (data: any) => Data;
121
135
  }
122
136
  /**
123
137
  * 请求参数接口
124
138
  * @template Param 请求参数类型
125
139
  */
126
- interface RequestOptions<Param> {
140
+ export interface RequestOptions<Param> {
127
141
  /** HTTP 请求方法 */
128
142
  method: Method;
129
143
  /** 请求URL */
@@ -138,10 +152,8 @@ interface RequestOptions<Param> {
138
152
  * @param baseOptions 基础配置选项
139
153
  * @returns 请求创建函数
140
154
  */
141
- declare function createBaseRequest(baseOptions?: Options): <Param, Data extends Record<any, any>>(requestOptions: RequestOptions<Param>, createOptions?: Omit<Options<Param, Data>, "baseURL">) => {
155
+ export default function createBaseRequest(baseOptions?: Options): <Param, Data extends Record<any, any>>(requestOptions: RequestOptions<Param>, createOptions?: Omit<Options<Param, Data>, "baseURL">) => {
142
156
  (requestParam?: Omit<RequestOptions<Param>, "url" | "method">, options?: Omit<Options<Param, Data>, "baseURL" | "cacheDataKey" | "cacheDataInStorage" | "cacheKeyEquals">): Promise<Data>;
143
157
  clearCache(): void;
144
158
  };
145
-
146
- export { createBaseRequest as default };
147
- export type { ErrorHandlerReturnType, Options, RequestOptions };
159
+ export { type RequestError, type RequestErrorType };