react-toolkits 2.31.0 → 2.31.2
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 +12 -0
- package/lib/index.d.ts +35 -103
- package/lib/index.js +3 -3
- package/lib/index.js.map +1 -1
- package/package.json +12 -3
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# react-toolkits
|
|
2
2
|
|
|
3
|
+
## 2.31.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 71e5a70: refactor: replace useKyClient with useKy hook across components
|
|
8
|
+
|
|
9
|
+
## 2.31.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- bba1a46: refactor: enhance KyClient and useKyClient for improved error handling and configuration
|
|
14
|
+
|
|
3
15
|
## 2.31.0
|
|
4
16
|
|
|
5
17
|
### Minor Changes
|
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,56 +20,12 @@ 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
|
-
/** React 上下文信息接口 */
|
|
58
|
-
interface KyClientContext {
|
|
59
|
-
token?: string;
|
|
60
|
-
isGlobalMode?: boolean;
|
|
61
|
-
loginPath?: string;
|
|
62
|
-
apiBaseUrl?: string;
|
|
63
|
-
appId?: string | number;
|
|
64
|
-
clear?: () => void;
|
|
65
|
-
notifyError?: (title: string, description: string) => void;
|
|
66
|
-
}
|
|
67
23
|
/** KyClient 构造函数配置 */
|
|
68
|
-
interface KyClientOptions
|
|
24
|
+
interface KyClientOptions {
|
|
69
25
|
/** 基础 URL */
|
|
70
26
|
baseURL?: string;
|
|
71
|
-
/**
|
|
72
|
-
|
|
73
|
-
/** React 上下文信息 */
|
|
74
|
-
context?: KyClientContext;
|
|
27
|
+
/** 自定义 hooks(拦截器) */
|
|
28
|
+
hooks?: Partial<Hooks>;
|
|
75
29
|
}
|
|
76
30
|
/** HTTP客户端方法接口 */
|
|
77
31
|
interface KyMethods {
|
|
@@ -87,19 +41,42 @@ interface KyMethods {
|
|
|
87
41
|
}) => Promise<T>;
|
|
88
42
|
instance: KyInstance;
|
|
89
43
|
}
|
|
44
|
+
/** useKy 配置选项 */
|
|
45
|
+
interface UseKyOptions {
|
|
46
|
+
/** 状态码字段配置:字段名 -> 成功码列表 */
|
|
47
|
+
statusFields?: Record<string, readonly number[]>;
|
|
48
|
+
/** 自定义响应成功判断函数 */
|
|
49
|
+
isSuccess?: (data: unknown, status: number) => boolean;
|
|
50
|
+
/** 错误消息字段列表(按优先级) */
|
|
51
|
+
errorFields?: readonly string[];
|
|
52
|
+
/** 自定义错误消息提取函数 */
|
|
53
|
+
extractErrorMessage?: (data: unknown) => string;
|
|
54
|
+
/** 401 未授权处理 */
|
|
55
|
+
onUnauthorized?: (data?: unknown) => void;
|
|
56
|
+
/** 403 禁止访问处理 */
|
|
57
|
+
onForbidden?: (data?: unknown) => void;
|
|
58
|
+
/** 412 未注册处理 */
|
|
59
|
+
onUnregistered?: (data?: unknown) => void;
|
|
60
|
+
/** 其他 HTTP 错误处理 */
|
|
61
|
+
onError?: (status: number, data?: unknown, errorMessage?: string) => void;
|
|
62
|
+
/** 网络错误处理 */
|
|
63
|
+
onNetworkError?: (error: Error) => void;
|
|
64
|
+
/** 是否禁用默认的错误提示 */
|
|
65
|
+
disableDefaultErrorNotification?: boolean;
|
|
66
|
+
/** 自定义 hooks(拦截器) */
|
|
67
|
+
hooks?: Partial<Hooks>;
|
|
68
|
+
}
|
|
69
|
+
/** 导出类型(向后兼容) */
|
|
70
|
+
type BusinessStatusCodeConfig = Pick<UseKyOptions, 'statusFields'>;
|
|
71
|
+
type ErrorMessageConfig = Pick<UseKyOptions, 'errorFields' | 'extractErrorMessage'>;
|
|
72
|
+
type HttpErrorHandlers = Pick<UseKyOptions, 'onUnauthorized' | 'onForbidden' | 'onUnregistered' | 'onError' | 'onNetworkError'>;
|
|
73
|
+
type ResponseValidationConfig = Pick<UseKyOptions, 'isSuccess'>;
|
|
90
74
|
/**
|
|
91
75
|
* Ky HTTP 客户端类
|
|
92
|
-
* 封装了基于 ky 的 HTTP 请求逻辑,包括认证、错误处理、响应验证等
|
|
93
76
|
*/
|
|
94
77
|
declare class KyClient implements KyMethods {
|
|
95
78
|
private readonly _instance;
|
|
96
|
-
private readonly businessConfig;
|
|
97
|
-
private readonly errorConfig;
|
|
98
|
-
private readonly errorHandlers;
|
|
99
|
-
private context?;
|
|
100
|
-
private readonly disableDefaultErrorNotification;
|
|
101
79
|
constructor(options?: KyClientOptions);
|
|
102
|
-
/** 获取 ky 实例(用于高级用法) */
|
|
103
80
|
get instance(): KyInstance;
|
|
104
81
|
get: <T = unknown>(url: string, options?: RequestOptions) => Promise<T>;
|
|
105
82
|
post: <T = unknown>(url: string, data?: unknown, options?: RequestOptions) => Promise<T>;
|
|
@@ -111,53 +88,8 @@ declare class KyClient implements KyMethods {
|
|
|
111
88
|
request: <T = unknown>(requestOptions: RequestOptions & {
|
|
112
89
|
url: string;
|
|
113
90
|
}) => Promise<T>;
|
|
114
|
-
/**
|
|
115
|
-
* 通用请求处理函数
|
|
116
|
-
*/
|
|
117
91
|
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
92
|
}
|
|
147
|
-
/**
|
|
148
|
-
* 在组件中使用 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
|
-
*/
|
|
160
|
-
declare function useKyClient(): KyClient;
|
|
161
93
|
|
|
162
94
|
interface AuthButtonProps extends ButtonProps {
|
|
163
95
|
/** 权限码,支持单个或多个 */
|
|
@@ -1530,4 +1462,4 @@ declare function useAuth(code?: string | string[], config?: RequestOptions): {
|
|
|
1530
1462
|
declare function useMenuList(): _tanstack_react_query.UseQueryResult<MenuListItem[], Error>;
|
|
1531
1463
|
declare const useGames: () => _tanstack_react_query.UseQueryResult<Game[], Error>;
|
|
1532
1464
|
|
|
1533
|
-
export { APP_ID_HEADER, AuthButton, type AuthButtonProps, type BusinessStatusCodeConfig, DynamicTags, type DynamicTagsProps, type ErrorMessageConfig, ExpandableParagraph, type ExpandableParagraphProps, FRONTEND_ROUTE_PREFIX, FilterFormWrapper, type FilterFormWrapperProps, type Game, type GameSelectConfig, type GameSelectProps, type HeaderExtra, type HeaderExtraConfig, Highlight, type HighlightProps, type HttpErrorHandlers, InfiniteList, type InfiniteListDataAdapter, type InfiniteListPayload, type InfiniteListProps, type InfiniteListRef, type InfiniteListRequestConfig, type InfiniteListRequestConfigType, type JsonResponse, KeepAlive, type KeepAliveCacheItem, KeepAliveOutlet, type KeepAliveOutletProps, type KeepAliveProps, KeepAliveProvider, type KeepAliveProviderProps, KyClient, type
|
|
1465
|
+
export { APP_ID_HEADER, AuthButton, type AuthButtonProps, type BusinessStatusCodeConfig, DynamicTags, type DynamicTagsProps, type ErrorMessageConfig, ExpandableParagraph, type ExpandableParagraphProps, FRONTEND_ROUTE_PREFIX, FilterFormWrapper, type FilterFormWrapperProps, type Game, type GameSelectConfig, type GameSelectProps, type HeaderExtra, type HeaderExtraConfig, Highlight, type HighlightProps, type HttpErrorHandlers, InfiniteList, type InfiniteListDataAdapter, type InfiniteListPayload, type InfiniteListProps, type InfiniteListRef, type InfiniteListRequestConfig, type InfiniteListRequestConfigType, type JsonResponse, KeepAlive, type KeepAliveCacheItem, KeepAliveOutlet, type KeepAliveOutletProps, type KeepAliveProps, KeepAliveProvider, type KeepAliveProviderProps, KyClient, type KyClientOptions, Layout, type LayoutProps, Logo, type LogoProps, type MenuListItem, type NavItem, type NavMenuItemGroupType, type NavMenuItemType, type NavSubMenuType, type NavigationConfig, NotFound, OperationLogList, type PageParam, type Permission, PermissionMode, QueryList, QueryListAction, type QueryListPayload, type QueryListProps, type QueryListRef, type RecursivePartial, type RequestOptions, RequireAuth, type RequireAuthProps, RequireGame, type ResponseType, type ResponseValidationConfig, type RouteMatchRule, SSO_URL, SelectAll, type SelectAllProps, type ShowFormOptions$1 as ShowFormDrawerOptions, type ShowFormOptions as ShowFormModalOptions, SignIn, ToolkitsProvider, type ToolkitsProviderProps, type UseDrawerOperation, type UseDrawerProps, type UseFormDrawerProps, type UseFormDrawerReturn, type UseFormModalProps, type UseFormModalReturn, type UseKeepAliveReturn, type UseKyOptions, type UseModalOperation, type UseModalProps, UserDropdown, type VisibilityState, WILDCARD, createVisibilityStoreConfig, generateId, _default$1 as menuRoutes, mixedStorage, _default as permissionRoutes, useAuth, useDrawer, useDrawerStore, useFormDrawer, useFormModal, useGames, useInfiniteListStore, useKeepAlive, useKeepAliveContext, useMenuList, useModal, useModalStore, useQueryListStore, useToolkitsStore };
|