react-toolkits 2.31.0 → 2.31.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/CHANGELOG.md +6 -0
- package/lib/index.d.ts +35 -89
- package/lib/index.js +3 -3
- package/lib/index.js.map +1 -1
- package/package.json +12 -3
package/CHANGELOG.md
CHANGED
package/lib/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ButtonProps, SelectProps, FormInstance, DrawerProps, Button, FormProps, ModalProps } from 'antd';
|
|
2
2
|
import { FC, PropsWithChildren, Key, ReactNode, ReactElement, Ref, DetailedHTMLProps, ImgHTMLAttributes, ComponentProps } from 'react';
|
|
3
|
-
import { Options, KyInstance } from 'ky';
|
|
3
|
+
import { Hooks, Options, KyInstance } from 'ky';
|
|
4
4
|
import { ParagraphProps } from 'antd/es/typography/Paragraph';
|
|
5
5
|
import * as react_jsx_runtime from 'react/jsx-runtime';
|
|
6
6
|
import { useLocation, PathPattern } from 'react-router';
|
|
@@ -12,8 +12,6 @@ import { StateCreator } from 'zustand';
|
|
|
12
12
|
import * as _tanstack_react_query from '@tanstack/react-query';
|
|
13
13
|
import { StateStorage } from 'zustand/middleware';
|
|
14
14
|
|
|
15
|
-
/** 成功响应状态码字段类型 */
|
|
16
|
-
type StatusField = 'code' | 'status' | 'errno';
|
|
17
15
|
/** 响应类型 */
|
|
18
16
|
type ResponseType = 'json' | 'blob' | 'text' | 'arrayBuffer' | 'formData';
|
|
19
17
|
/** 通用请求选项接口 */
|
|
@@ -22,38 +20,6 @@ interface RequestOptions extends Options {
|
|
|
22
20
|
/** 是否使用全局模式(覆盖 context 中的 isGlobalMode) */
|
|
23
21
|
isGlobalMode?: boolean;
|
|
24
22
|
}
|
|
25
|
-
/** 业务状态码字段配置 */
|
|
26
|
-
interface BusinessStatusCodeConfig {
|
|
27
|
-
/** 成功状态码列表,默认为 [0, 1, 200] */
|
|
28
|
-
successCodes?: readonly number[];
|
|
29
|
-
/** 需要检查的业务状态码字段,默认为 ['code', 'status', 'errno'] */
|
|
30
|
-
statusFields?: readonly StatusField[];
|
|
31
|
-
}
|
|
32
|
-
/** 错误消息提取配置 */
|
|
33
|
-
interface ErrorMessageConfig {
|
|
34
|
-
/** 错误消息字段列表(按优先级),默认为 ['msg', 'message', 'error'] */
|
|
35
|
-
errorFields?: readonly string[];
|
|
36
|
-
/** 自定义错误消息提取函数,优先级高于 errorFields */
|
|
37
|
-
extractErrorMessage?: (data: unknown) => string;
|
|
38
|
-
}
|
|
39
|
-
/** HTTP 状态码错误处理回调 */
|
|
40
|
-
interface HttpErrorHandlers {
|
|
41
|
-
/** 401 未授权处理 */
|
|
42
|
-
onUnauthorized?: (data?: unknown) => void;
|
|
43
|
-
/** 403 禁止访问处理 */
|
|
44
|
-
onForbidden?: (data?: unknown) => void;
|
|
45
|
-
/** 412 未注册处理 */
|
|
46
|
-
onUnregistered?: (data?: unknown) => void;
|
|
47
|
-
/** 其他 HTTP 错误处理 */
|
|
48
|
-
onError?: (status: number, data?: unknown, errorMessage?: string) => void;
|
|
49
|
-
/** 网络错误处理 */
|
|
50
|
-
onNetworkError?: (error: Error) => void;
|
|
51
|
-
}
|
|
52
|
-
/** 响应验证配置 */
|
|
53
|
-
interface ResponseValidationConfig {
|
|
54
|
-
/** 自定义响应成功判断函数,优先级高于业务状态码配置 */
|
|
55
|
-
isSuccess?: (data: unknown, status: number) => boolean;
|
|
56
|
-
}
|
|
57
23
|
/** React 上下文信息接口 */
|
|
58
24
|
interface KyClientContext {
|
|
59
25
|
token?: string;
|
|
@@ -65,13 +31,11 @@ interface KyClientContext {
|
|
|
65
31
|
notifyError?: (title: string, description: string) => void;
|
|
66
32
|
}
|
|
67
33
|
/** KyClient 构造函数配置 */
|
|
68
|
-
interface KyClientOptions
|
|
34
|
+
interface KyClientOptions {
|
|
69
35
|
/** 基础 URL */
|
|
70
36
|
baseURL?: string;
|
|
71
|
-
/**
|
|
72
|
-
|
|
73
|
-
/** React 上下文信息 */
|
|
74
|
-
context?: KyClientContext;
|
|
37
|
+
/** 自定义 hooks(拦截器) */
|
|
38
|
+
hooks?: Partial<Hooks>;
|
|
75
39
|
}
|
|
76
40
|
/** HTTP客户端方法接口 */
|
|
77
41
|
interface KyMethods {
|
|
@@ -87,19 +51,42 @@ interface KyMethods {
|
|
|
87
51
|
}) => Promise<T>;
|
|
88
52
|
instance: KyInstance;
|
|
89
53
|
}
|
|
54
|
+
/** useKyClient 配置选项 */
|
|
55
|
+
interface UseKyClientOptions {
|
|
56
|
+
/** 状态码字段配置:字段名 -> 成功码列表 */
|
|
57
|
+
statusFields?: Record<string, readonly number[]>;
|
|
58
|
+
/** 自定义响应成功判断函数 */
|
|
59
|
+
isSuccess?: (data: unknown, status: number) => boolean;
|
|
60
|
+
/** 错误消息字段列表(按优先级) */
|
|
61
|
+
errorFields?: readonly string[];
|
|
62
|
+
/** 自定义错误消息提取函数 */
|
|
63
|
+
extractErrorMessage?: (data: unknown) => string;
|
|
64
|
+
/** 401 未授权处理 */
|
|
65
|
+
onUnauthorized?: (data?: unknown) => void;
|
|
66
|
+
/** 403 禁止访问处理 */
|
|
67
|
+
onForbidden?: (data?: unknown) => void;
|
|
68
|
+
/** 412 未注册处理 */
|
|
69
|
+
onUnregistered?: (data?: unknown) => void;
|
|
70
|
+
/** 其他 HTTP 错误处理 */
|
|
71
|
+
onError?: (status: number, data?: unknown, errorMessage?: string) => void;
|
|
72
|
+
/** 网络错误处理 */
|
|
73
|
+
onNetworkError?: (error: Error) => void;
|
|
74
|
+
/** 是否禁用默认的错误提示 */
|
|
75
|
+
disableDefaultErrorNotification?: boolean;
|
|
76
|
+
/** 自定义 hooks(拦截器) */
|
|
77
|
+
hooks?: Partial<Hooks>;
|
|
78
|
+
}
|
|
79
|
+
/** 导出类型(向后兼容) */
|
|
80
|
+
type BusinessStatusCodeConfig = Pick<UseKyClientOptions, 'statusFields'>;
|
|
81
|
+
type ErrorMessageConfig = Pick<UseKyClientOptions, 'errorFields' | 'extractErrorMessage'>;
|
|
82
|
+
type HttpErrorHandlers = Pick<UseKyClientOptions, 'onUnauthorized' | 'onForbidden' | 'onUnregistered' | 'onError' | 'onNetworkError'>;
|
|
83
|
+
type ResponseValidationConfig = Pick<UseKyClientOptions, 'isSuccess'>;
|
|
90
84
|
/**
|
|
91
85
|
* Ky HTTP 客户端类
|
|
92
|
-
* 封装了基于 ky 的 HTTP 请求逻辑,包括认证、错误处理、响应验证等
|
|
93
86
|
*/
|
|
94
87
|
declare class KyClient implements KyMethods {
|
|
95
88
|
private readonly _instance;
|
|
96
|
-
private readonly businessConfig;
|
|
97
|
-
private readonly errorConfig;
|
|
98
|
-
private readonly errorHandlers;
|
|
99
|
-
private context?;
|
|
100
|
-
private readonly disableDefaultErrorNotification;
|
|
101
89
|
constructor(options?: KyClientOptions);
|
|
102
|
-
/** 获取 ky 实例(用于高级用法) */
|
|
103
90
|
get instance(): KyInstance;
|
|
104
91
|
get: <T = unknown>(url: string, options?: RequestOptions) => Promise<T>;
|
|
105
92
|
post: <T = unknown>(url: string, data?: unknown, options?: RequestOptions) => Promise<T>;
|
|
@@ -111,53 +98,12 @@ declare class KyClient implements KyMethods {
|
|
|
111
98
|
request: <T = unknown>(requestOptions: RequestOptions & {
|
|
112
99
|
url: string;
|
|
113
100
|
}) => Promise<T>;
|
|
114
|
-
/**
|
|
115
|
-
* 通用请求处理函数
|
|
116
|
-
*/
|
|
117
101
|
private makeRequest;
|
|
118
|
-
/**
|
|
119
|
-
* 创建响应处理函数
|
|
120
|
-
*/
|
|
121
|
-
private createResponseHandler;
|
|
122
|
-
/**
|
|
123
|
-
* 请求前处理:设置认证和 appId headers
|
|
124
|
-
*/
|
|
125
|
-
private handleBeforeRequest;
|
|
126
|
-
/**
|
|
127
|
-
* 错误前处理:处理 HTTP 状态码错误
|
|
128
|
-
*/
|
|
129
|
-
private handleBeforeError;
|
|
130
|
-
/**
|
|
131
|
-
* 处理 HTTP 状态码错误
|
|
132
|
-
*/
|
|
133
|
-
private handleHttpStatusError;
|
|
134
|
-
/**
|
|
135
|
-
* 默认处理未授权错误
|
|
136
|
-
*/
|
|
137
|
-
private defaultHandleUnauthorized;
|
|
138
|
-
/**
|
|
139
|
-
* 默认处理未注册错误
|
|
140
|
-
*/
|
|
141
|
-
private defaultHandleUnregistered;
|
|
142
|
-
/**
|
|
143
|
-
* 默认处理禁止访问错误
|
|
144
|
-
*/
|
|
145
|
-
private defaultHandleForbidden;
|
|
146
102
|
}
|
|
147
103
|
/**
|
|
148
104
|
* 在组件中使用 KyClient 的 Hook
|
|
149
|
-
* 创建新的 KyClient 实例,并从 ToolkitsProvider 获取 context
|
|
150
|
-
* 这样可以确保每个组件使用正确的 context(特别是 isGlobalMode)
|
|
151
|
-
*
|
|
152
|
-
* @example
|
|
153
|
-
* import { useKyClient } from '@/libs'
|
|
154
|
-
*
|
|
155
|
-
* function MyComponent() {
|
|
156
|
-
* const { get, post } = useKyClient()
|
|
157
|
-
* // 使用 get, post 等方法
|
|
158
|
-
* }
|
|
159
105
|
*/
|
|
160
|
-
declare function useKyClient(): KyClient;
|
|
106
|
+
declare function useKyClient(options?: UseKyClientOptions): KyClient;
|
|
161
107
|
|
|
162
108
|
interface AuthButtonProps extends ButtonProps {
|
|
163
109
|
/** 权限码,支持单个或多个 */
|