@robot-admin/request-core 0.1.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/README.md +426 -0
- package/dist/index.cjs +1130 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +644 -0
- package/dist/index.d.ts +644 -0
- package/dist/index.js +1107 -0
- package/dist/index.js.map +1 -0
- package/package.json +59 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,644 @@
|
|
|
1
|
+
import * as axios from 'axios';
|
|
2
|
+
import { InternalAxiosRequestConfig, AxiosResponse, AxiosRequestConfig, AxiosInstance } from 'axios';
|
|
3
|
+
import { App, Ref, ComputedRef } from 'vue';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 获取全局配置
|
|
7
|
+
*/
|
|
8
|
+
declare function getGlobalConfig(): Required<Pick<RequestCoreConfig, "successCodes" | "fieldAliases">>;
|
|
9
|
+
/**
|
|
10
|
+
* 拦截器配置
|
|
11
|
+
*
|
|
12
|
+
* @description
|
|
13
|
+
* 用于配置 axios 请求和响应拦截器,处理 token 注入、业务码判断等业务逻辑
|
|
14
|
+
*/
|
|
15
|
+
interface InterceptorConfig {
|
|
16
|
+
/**
|
|
17
|
+
* 请求拦截器
|
|
18
|
+
* @description 在请求发送前执行,常用于注入 token、修改请求头等
|
|
19
|
+
*/
|
|
20
|
+
request?: (config: InternalAxiosRequestConfig) => InternalAxiosRequestConfig | Promise<InternalAxiosRequestConfig>;
|
|
21
|
+
/**
|
|
22
|
+
* 请求错误拦截器
|
|
23
|
+
* @description 请求配置阶段发生错误时执行
|
|
24
|
+
*/
|
|
25
|
+
requestError?: (error: any) => any;
|
|
26
|
+
/**
|
|
27
|
+
* 响应拦截器
|
|
28
|
+
* @description 响应到达后执行,常用于统一处理业务码、数据格式等
|
|
29
|
+
*/
|
|
30
|
+
response?: (response: AxiosResponse) => AxiosResponse | Promise<AxiosResponse>;
|
|
31
|
+
/**
|
|
32
|
+
* 响应错误拦截器
|
|
33
|
+
* @description 响应阶段发生错误时执行,常用于处理 401、403、500 等错误
|
|
34
|
+
*/
|
|
35
|
+
responseError?: (error: any) => any;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 字段别名配置
|
|
39
|
+
*
|
|
40
|
+
* @description
|
|
41
|
+
* 用于自定义 API 响应的字段映射,适配不同的后端响应格式
|
|
42
|
+
*/
|
|
43
|
+
interface FieldAliases {
|
|
44
|
+
/**
|
|
45
|
+
* 数据层字段别名(用于提取响应中的 data 层)
|
|
46
|
+
* @default ['data', 'list', 'items', 'records']
|
|
47
|
+
* @example ['result', 'payload']
|
|
48
|
+
*/
|
|
49
|
+
data?: string[];
|
|
50
|
+
/**
|
|
51
|
+
* 列表字段别名(用于提取数据层中的列表数组)
|
|
52
|
+
* @default ['list', 'items', 'records', 'rows', 'data']
|
|
53
|
+
* @example ['employees', 'users', 'products']
|
|
54
|
+
*/
|
|
55
|
+
list?: string[];
|
|
56
|
+
/**
|
|
57
|
+
* 总数字段别名(用于提取数据总数)
|
|
58
|
+
* @default ['total', 'totalCount', 'count', 'totalElements']
|
|
59
|
+
* @example ['totalRecords', 'totalItems']
|
|
60
|
+
*/
|
|
61
|
+
total?: string[];
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Request Core 配置
|
|
65
|
+
*
|
|
66
|
+
* @description
|
|
67
|
+
* 用于初始化 Request Core 实例,配置 axios 和拦截器
|
|
68
|
+
*/
|
|
69
|
+
interface RequestCoreConfig {
|
|
70
|
+
/**
|
|
71
|
+
* Axios 基础配置
|
|
72
|
+
* @description 包括 baseURL、timeout、headers 等,参考 axios 官方文档
|
|
73
|
+
*/
|
|
74
|
+
request?: AxiosRequestConfig;
|
|
75
|
+
/**
|
|
76
|
+
* 拦截器配置
|
|
77
|
+
* @description 用于处理 token 注入、业务码判断、错误提示等业务逻辑
|
|
78
|
+
*/
|
|
79
|
+
interceptors?: InterceptorConfig;
|
|
80
|
+
/**
|
|
81
|
+
* 成功状态码配置
|
|
82
|
+
* @description 用于判断 API 响应的业务状态码是否成功
|
|
83
|
+
* @default [200, 0, '200', '0']
|
|
84
|
+
* @example [1, '1', 'success']
|
|
85
|
+
*/
|
|
86
|
+
successCodes?: Array<number | string>;
|
|
87
|
+
/**
|
|
88
|
+
* 字段别名配置
|
|
89
|
+
* @description 用于自定义 API 响应的字段映射,适配不同的后端响应格式
|
|
90
|
+
*/
|
|
91
|
+
fieldAliases?: FieldAliases;
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 创建 Request Core 实例
|
|
95
|
+
*
|
|
96
|
+
* @description
|
|
97
|
+
* 初始化 axios 实例并注册 7 个内置插件(cache、retry、dedupe、cancel、request、response、reLogin)
|
|
98
|
+
* 返回 Vue 插件对象和 axios 实例
|
|
99
|
+
*
|
|
100
|
+
* @param config Request Core 配置
|
|
101
|
+
* @returns Vue 插件对象(包含 install 方法)和 axios 实例
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```ts
|
|
105
|
+
* // main.ts
|
|
106
|
+
* import { createRequestCore, onReLoginSuccess } from '@robot-admin/request-core'
|
|
107
|
+
* import { useUserStore } from '@/stores/user'
|
|
108
|
+
*
|
|
109
|
+
* const requestCore = createRequestCore({
|
|
110
|
+
* request: {
|
|
111
|
+
* baseURL: import.meta.env.VITE_API_BASE,
|
|
112
|
+
* timeout: 10000,
|
|
113
|
+
* },
|
|
114
|
+
* interceptors: {
|
|
115
|
+
* // 请求拦截:注入 token
|
|
116
|
+
* request: (config) => {
|
|
117
|
+
* const token = localStorage.getItem('token')
|
|
118
|
+
* if (token) {
|
|
119
|
+
* config.headers.Authorization = `Bearer ${token}`
|
|
120
|
+
* }
|
|
121
|
+
* return config
|
|
122
|
+
* },
|
|
123
|
+
* // 响应拦截:处理业务码
|
|
124
|
+
* response: (response) => {
|
|
125
|
+
* const { code, message } = response.data
|
|
126
|
+
* if (code !== 200) {
|
|
127
|
+
* window.$message?.error(message || '请求失败')
|
|
128
|
+
* return Promise.reject(new Error(message))
|
|
129
|
+
* }
|
|
130
|
+
* return response
|
|
131
|
+
* },
|
|
132
|
+
* // 响应错误拦截:处理 401
|
|
133
|
+
* responseError: async (error) => {
|
|
134
|
+
* if (error.response?.status === 401) {
|
|
135
|
+
* // 触发重新登录逻辑
|
|
136
|
+
* const userStore = useUserStore()
|
|
137
|
+
* await userStore.reLogin()
|
|
138
|
+
* onReLoginSuccess() // 通知所有等待的请求继续
|
|
139
|
+
* return Promise.reject(error)
|
|
140
|
+
* }
|
|
141
|
+
* return Promise.reject(error)
|
|
142
|
+
* }
|
|
143
|
+
* }
|
|
144
|
+
* })
|
|
145
|
+
*
|
|
146
|
+
* app.use(requestCore)
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
declare function createRequestCore(config?: RequestCoreConfig): {
|
|
150
|
+
install(app: App): void;
|
|
151
|
+
axiosInstance: axios.AxiosInstance;
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* 请求去重配置
|
|
156
|
+
*/
|
|
157
|
+
interface DedupeConfig {
|
|
158
|
+
/** 是否启用去重(默认 true) */
|
|
159
|
+
enabled?: boolean;
|
|
160
|
+
/** 自定义请求 key 生成函数 */
|
|
161
|
+
keyGenerator?: (config: AxiosRequestConfig) => string;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* 请求缓存配置
|
|
165
|
+
*/
|
|
166
|
+
interface CacheConfig {
|
|
167
|
+
/** 是否启用缓存(默认 false,只对 GET 请求有效) */
|
|
168
|
+
enabled?: boolean;
|
|
169
|
+
/** 缓存时间(毫秒,默认 5 分钟) */
|
|
170
|
+
ttl?: number;
|
|
171
|
+
/** 是否强制刷新缓存 */
|
|
172
|
+
forceUpdate?: boolean;
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* 请求重试配置
|
|
176
|
+
*/
|
|
177
|
+
interface RetryConfig {
|
|
178
|
+
/** 是否启用重试(默认 false) */
|
|
179
|
+
enabled?: boolean;
|
|
180
|
+
/** 重试次数(默认 3) */
|
|
181
|
+
count?: number;
|
|
182
|
+
/** 重试延迟(毫秒,默认 1000) */
|
|
183
|
+
delay?: number;
|
|
184
|
+
/** 是否使用指数退避(默认 true) */
|
|
185
|
+
exponentialBackoff?: boolean;
|
|
186
|
+
/** 可重试的 HTTP 状态码 */
|
|
187
|
+
retryableStatusCodes?: number[];
|
|
188
|
+
}
|
|
189
|
+
/**
|
|
190
|
+
* 路由取消配置
|
|
191
|
+
*/
|
|
192
|
+
interface CancelConfig {
|
|
193
|
+
/** 是否启用路由切换时自动取消(默认 true) */
|
|
194
|
+
enabled?: boolean;
|
|
195
|
+
/** 白名单:不需要取消的请求 URL 模式 */
|
|
196
|
+
whitelist?: RegExp[];
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* 扩展的 Axios 请求配置
|
|
200
|
+
*/
|
|
201
|
+
interface EnhancedAxiosRequestConfig extends AxiosRequestConfig {
|
|
202
|
+
/** 请求去重配置 */
|
|
203
|
+
dedupe?: boolean | DedupeConfig;
|
|
204
|
+
/** 请求缓存配置 */
|
|
205
|
+
cache?: boolean | CacheConfig;
|
|
206
|
+
/** 请求重试配置 */
|
|
207
|
+
retry?: boolean | RetryConfig;
|
|
208
|
+
/** 路由取消配置 */
|
|
209
|
+
cancel?: boolean | CancelConfig;
|
|
210
|
+
/** 内部标记:当前重试次数 */
|
|
211
|
+
__retryCount?: number;
|
|
212
|
+
/** 内部标记:取消请求 ID */
|
|
213
|
+
__cancelId?: string;
|
|
214
|
+
/** 内部标记:去重请求键 */
|
|
215
|
+
__requestKey?: string;
|
|
216
|
+
/** 内部标记:是否来自缓存 */
|
|
217
|
+
__fromCache?: boolean;
|
|
218
|
+
/** 内部标记:是否被 cancel 插件管理 */
|
|
219
|
+
__managedByCancel?: boolean;
|
|
220
|
+
/** 内部标记:是否正在处理 401 错误 */
|
|
221
|
+
__handling401?: boolean;
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* 缓存项
|
|
225
|
+
*/
|
|
226
|
+
interface CacheItem<T = any> {
|
|
227
|
+
/** 缓存数据 */
|
|
228
|
+
data: T;
|
|
229
|
+
/** 过期时间戳 */
|
|
230
|
+
expireAt: number;
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* 请求 key 生成器参数
|
|
234
|
+
*/
|
|
235
|
+
interface RequestKeyParams {
|
|
236
|
+
method?: string;
|
|
237
|
+
url?: string;
|
|
238
|
+
params?: any;
|
|
239
|
+
data?: any;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* 取消所有待处理的请求
|
|
244
|
+
*
|
|
245
|
+
* 使用场景:
|
|
246
|
+
* - 用户登出时清理所有请求
|
|
247
|
+
* - 网络异常时重置请求状态
|
|
248
|
+
* - 调试和测试场景
|
|
249
|
+
*
|
|
250
|
+
* 安全机制:
|
|
251
|
+
* - 捕获每个取消操作的异常
|
|
252
|
+
* - 确保一个请求的失败不影响其他请求的取消
|
|
253
|
+
* - 完全清理 Map 中的所有引用
|
|
254
|
+
*/
|
|
255
|
+
declare function cancelAllPendingRequests(): void;
|
|
256
|
+
/**
|
|
257
|
+
* 获取待处理请求数量
|
|
258
|
+
*
|
|
259
|
+
* @returns 当前待处理的请求数量
|
|
260
|
+
*
|
|
261
|
+
* 监控用途:
|
|
262
|
+
* - 性能监控和分析
|
|
263
|
+
* - 调试请求状态
|
|
264
|
+
* - 检测潜在的请求泄漏
|
|
265
|
+
* - 评估去重效果
|
|
266
|
+
*/
|
|
267
|
+
declare function getPendingRequestCount(): number;
|
|
268
|
+
|
|
269
|
+
/**
|
|
270
|
+
* 清空所有缓存
|
|
271
|
+
*
|
|
272
|
+
* 用于:
|
|
273
|
+
* - 用户登出时清理敏感数据
|
|
274
|
+
* - 开发调试时重置缓存状态
|
|
275
|
+
*/
|
|
276
|
+
declare function clearAllCache(): void;
|
|
277
|
+
/**
|
|
278
|
+
* 删除指定缓存
|
|
279
|
+
*
|
|
280
|
+
* @param config 请求配置
|
|
281
|
+
* @returns 是否删除成功
|
|
282
|
+
*
|
|
283
|
+
* 用于:
|
|
284
|
+
* - 数据更新时清理特定缓存
|
|
285
|
+
* - 缓存失效时的精确清理
|
|
286
|
+
*/
|
|
287
|
+
declare function clearCache(config: InternalAxiosRequestConfig): boolean;
|
|
288
|
+
/**
|
|
289
|
+
* 清理过期缓存
|
|
290
|
+
*
|
|
291
|
+
* 自动清理机制:
|
|
292
|
+
* - 由 globalCache 内部实现
|
|
293
|
+
* - 基于 TTL 过期时间
|
|
294
|
+
* - 定期清理防止内存泄漏
|
|
295
|
+
*/
|
|
296
|
+
declare function cleanupExpiredCache(): void;
|
|
297
|
+
/**
|
|
298
|
+
* 获取缓存大小
|
|
299
|
+
*
|
|
300
|
+
* @returns 当前缓存条目数量
|
|
301
|
+
*
|
|
302
|
+
* 用于:
|
|
303
|
+
* - 监控缓存使用情况
|
|
304
|
+
* - 性能分析和调试
|
|
305
|
+
*/
|
|
306
|
+
declare function getCacheSize(): number;
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* 取消所有待处理的请求
|
|
310
|
+
*
|
|
311
|
+
* 使用场景:
|
|
312
|
+
* - 路由切换时取消当前页面的所有请求
|
|
313
|
+
* - 用户登出时清理所有请求
|
|
314
|
+
* - 网络异常时重置请求状态
|
|
315
|
+
*
|
|
316
|
+
* 安全机制:
|
|
317
|
+
* - 捕获每个取消操作的异常
|
|
318
|
+
* - 确保一个请求的失败不影响其他请求的取消
|
|
319
|
+
*/
|
|
320
|
+
declare function cancelAllRequests(): void;
|
|
321
|
+
/**
|
|
322
|
+
* 获取待取消请求数量
|
|
323
|
+
*
|
|
324
|
+
* @returns 当前待取消的请求数量
|
|
325
|
+
*
|
|
326
|
+
* 监控用途:
|
|
327
|
+
* - 性能监控和分析
|
|
328
|
+
* - 调试请求状态
|
|
329
|
+
* - 检测潜在的请求泄漏
|
|
330
|
+
*/
|
|
331
|
+
declare function getCancelableRequestCount(): number;
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* 创建 axios 实例
|
|
335
|
+
* @param config axios 配置
|
|
336
|
+
*/
|
|
337
|
+
declare function createAxiosInstance(config?: AxiosRequestConfig): AxiosInstance;
|
|
338
|
+
/**
|
|
339
|
+
* GET 请求
|
|
340
|
+
* @param url 请求地址
|
|
341
|
+
* @param config 请求配置(可选),支持插件配置
|
|
342
|
+
*/
|
|
343
|
+
declare const getData: <T = any>(url: string, config?: EnhancedAxiosRequestConfig) => Promise<T>;
|
|
344
|
+
/**
|
|
345
|
+
* POST 请求
|
|
346
|
+
* @param url 请求地址
|
|
347
|
+
* @param data 请求体数据(可选)
|
|
348
|
+
* @param config 请求配置(可选),支持插件配置
|
|
349
|
+
*/
|
|
350
|
+
declare const postData: <T = any>(url: string, data?: any, config?: EnhancedAxiosRequestConfig) => Promise<T>;
|
|
351
|
+
/**
|
|
352
|
+
* PUT 请求
|
|
353
|
+
* @param url 请求地址
|
|
354
|
+
* @param data 请求体数据(可选)
|
|
355
|
+
* @param config 请求配置(可选),支持插件配置
|
|
356
|
+
*/
|
|
357
|
+
declare const putData: <T = any>(url: string, data?: any, config?: EnhancedAxiosRequestConfig) => Promise<T>;
|
|
358
|
+
/**
|
|
359
|
+
* DELETE 请求
|
|
360
|
+
* @param url 请求地址
|
|
361
|
+
* @param config 请求配置(可选),支持插件配置
|
|
362
|
+
*/
|
|
363
|
+
declare const deleteData: <T = any>(url: string, config?: EnhancedAxiosRequestConfig) => Promise<T>;
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* 重新登录成功后的回调
|
|
367
|
+
* 调用此函数会 resolve 所有等待中的请求
|
|
368
|
+
*/
|
|
369
|
+
declare const onReLoginSuccess: () => void;
|
|
370
|
+
/**
|
|
371
|
+
* 重新登录取消后的回调
|
|
372
|
+
* 调用此函数会 reject 所有等待中的请求
|
|
373
|
+
*/
|
|
374
|
+
declare const onReLoginCancel: () => void;
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* 数据记录基础类型
|
|
378
|
+
*/
|
|
379
|
+
interface DataRecord {
|
|
380
|
+
[key: string]: any;
|
|
381
|
+
}
|
|
382
|
+
/**
|
|
383
|
+
* 表格列配置(兼容 Naive UI DataTable)
|
|
384
|
+
*/
|
|
385
|
+
interface TableColumn<T = any> {
|
|
386
|
+
/** 列标题 */
|
|
387
|
+
title?: string;
|
|
388
|
+
/** 数据字段 key */
|
|
389
|
+
key?: string;
|
|
390
|
+
/** 渲染函数 */
|
|
391
|
+
render?: (row: T, index: number) => any;
|
|
392
|
+
/** 列宽度 */
|
|
393
|
+
width?: number | string;
|
|
394
|
+
/** 最小宽度 */
|
|
395
|
+
minWidth?: number | string;
|
|
396
|
+
/** 最大宽度 */
|
|
397
|
+
maxWidth?: number | string;
|
|
398
|
+
/** 固定列 */
|
|
399
|
+
fixed?: "left" | "right";
|
|
400
|
+
/** 对齐方式 */
|
|
401
|
+
align?: "left" | "center" | "right";
|
|
402
|
+
/** 省略 */
|
|
403
|
+
ellipsis?: boolean | object;
|
|
404
|
+
/** 排序 */
|
|
405
|
+
sorter?: boolean | ((a: T, b: T) => number) | string;
|
|
406
|
+
/** 过滤 */
|
|
407
|
+
filter?: boolean | ((value: string, row: T) => boolean);
|
|
408
|
+
/** 其他属性 */
|
|
409
|
+
[key: string]: any;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* API 端点配置
|
|
413
|
+
*/
|
|
414
|
+
interface ApiEndpoints {
|
|
415
|
+
/** 列表查询接口 */
|
|
416
|
+
list: string;
|
|
417
|
+
/** 详情查询接口 */
|
|
418
|
+
get?: string;
|
|
419
|
+
/** 新增接口 */
|
|
420
|
+
create?: string;
|
|
421
|
+
/** 更新接口 */
|
|
422
|
+
update?: string;
|
|
423
|
+
/** 删除接口 */
|
|
424
|
+
remove?: string;
|
|
425
|
+
/** 批量删除接口 */
|
|
426
|
+
batchRemove?: string;
|
|
427
|
+
}
|
|
428
|
+
/**
|
|
429
|
+
* 操作上下文(传递给自定义操作处理函数)
|
|
430
|
+
*/
|
|
431
|
+
interface ActionContext<T> {
|
|
432
|
+
/** 表格数据数组 */
|
|
433
|
+
data: T[];
|
|
434
|
+
/** 当前行索引 */
|
|
435
|
+
index: number;
|
|
436
|
+
/** 分页信息 */
|
|
437
|
+
page: {
|
|
438
|
+
current: number;
|
|
439
|
+
size: number;
|
|
440
|
+
};
|
|
441
|
+
/** 分页是否启用 */
|
|
442
|
+
paginationEnabled: boolean;
|
|
443
|
+
/** 消息通知实例 */
|
|
444
|
+
message: any;
|
|
445
|
+
/** 对话框实例 */
|
|
446
|
+
dialog: any;
|
|
447
|
+
/** 刷新数据 */
|
|
448
|
+
refresh: () => Promise<void>;
|
|
449
|
+
}
|
|
450
|
+
/**
|
|
451
|
+
* 自定义操作按钮配置
|
|
452
|
+
*/
|
|
453
|
+
interface CustomAction<T> {
|
|
454
|
+
/** 操作唯一标识 */
|
|
455
|
+
key: string;
|
|
456
|
+
/** 按钮文本 */
|
|
457
|
+
label: string;
|
|
458
|
+
/** 图标名称 */
|
|
459
|
+
icon: string;
|
|
460
|
+
/** 按钮类型 */
|
|
461
|
+
type?: "default" | "primary" | "info" | "success" | "warning" | "error";
|
|
462
|
+
/** 操作处理函数 */
|
|
463
|
+
handler: (row: T, context: ActionContext<T>) => void | Promise<void>;
|
|
464
|
+
}
|
|
465
|
+
/**
|
|
466
|
+
* 详情项配置
|
|
467
|
+
*/
|
|
468
|
+
interface DetailItem {
|
|
469
|
+
/** 字段标签 */
|
|
470
|
+
label: string;
|
|
471
|
+
/** 数据字段名 */
|
|
472
|
+
key: string;
|
|
473
|
+
/** 显示类型 */
|
|
474
|
+
type?: string;
|
|
475
|
+
/** 占据列数 */
|
|
476
|
+
span?: number;
|
|
477
|
+
/** 值格式化函数 */
|
|
478
|
+
formatter?: (val: any) => string;
|
|
479
|
+
/** 标签类型 */
|
|
480
|
+
tagType?: string;
|
|
481
|
+
[key: string]: any;
|
|
482
|
+
}
|
|
483
|
+
/**
|
|
484
|
+
* 详情分组配置
|
|
485
|
+
*/
|
|
486
|
+
interface DetailSection {
|
|
487
|
+
/** 分组标题 */
|
|
488
|
+
title: string;
|
|
489
|
+
/** 列数 */
|
|
490
|
+
columns: number;
|
|
491
|
+
/** 字段列表 */
|
|
492
|
+
items: DetailItem[];
|
|
493
|
+
}
|
|
494
|
+
/**
|
|
495
|
+
* 详情配置
|
|
496
|
+
*/
|
|
497
|
+
interface DetailConfig {
|
|
498
|
+
/** 详情分组 */
|
|
499
|
+
sections: DetailSection[];
|
|
500
|
+
}
|
|
501
|
+
/**
|
|
502
|
+
* useTableCrud 配置选项
|
|
503
|
+
*/
|
|
504
|
+
interface UseTableCrudConfig<T extends DataRecord> {
|
|
505
|
+
/** API 端点配置 */
|
|
506
|
+
api: ApiEndpoints;
|
|
507
|
+
/** 表格列配置 */
|
|
508
|
+
columns: TableColumn<T>[];
|
|
509
|
+
/** 自定义操作按钮 */
|
|
510
|
+
customActions?: CustomAction<T>[];
|
|
511
|
+
/** 详情弹窗配置 */
|
|
512
|
+
detail?: DetailConfig;
|
|
513
|
+
/** ID 字段名,默认 'id' */
|
|
514
|
+
idKey?: keyof T;
|
|
515
|
+
/** 默认分页大小,默认 10 */
|
|
516
|
+
defaultPageSize?: number;
|
|
517
|
+
/** 是否默认启用分页,默认 true */
|
|
518
|
+
defaultPaginationEnabled?: boolean;
|
|
519
|
+
/** 创建新行的工厂函数 */
|
|
520
|
+
createNewRow?: () => T;
|
|
521
|
+
/** 列表数据提取函数(处理不同响应格式) */
|
|
522
|
+
extractListData?: (response: any) => {
|
|
523
|
+
items: T[];
|
|
524
|
+
total: number;
|
|
525
|
+
};
|
|
526
|
+
/** 是否自动加载数据,默认 true */
|
|
527
|
+
autoLoad?: boolean;
|
|
528
|
+
}
|
|
529
|
+
/**
|
|
530
|
+
* 详情弹窗状态
|
|
531
|
+
*/
|
|
532
|
+
interface DetailModal<T> {
|
|
533
|
+
/** 弹窗可见性 */
|
|
534
|
+
visible: Ref<boolean>;
|
|
535
|
+
/** 详情数据 */
|
|
536
|
+
data: Ref<T | null>;
|
|
537
|
+
/** 弹窗标题 */
|
|
538
|
+
title: Ref<string>;
|
|
539
|
+
/** 显示详情 */
|
|
540
|
+
show: (row: T) => void;
|
|
541
|
+
/** 关闭详情 */
|
|
542
|
+
close: () => void;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* useTableCrud 返回类型
|
|
546
|
+
*/
|
|
547
|
+
interface UseTableCrudReturn<T extends DataRecord> {
|
|
548
|
+
/** 表格数据 */
|
|
549
|
+
data: Ref<T[]>;
|
|
550
|
+
/** 加载状态 */
|
|
551
|
+
loading: Ref<boolean>;
|
|
552
|
+
/** 数据总数 */
|
|
553
|
+
total: Ref<number>;
|
|
554
|
+
/** 表格列配置 */
|
|
555
|
+
columns: ComputedRef<TableColumn<T>[]>;
|
|
556
|
+
/** 操作按钮配置 */
|
|
557
|
+
actions: ComputedRef<any>;
|
|
558
|
+
/** 表格引用 */
|
|
559
|
+
tableRef: Ref<any>;
|
|
560
|
+
/** 分页状态 */
|
|
561
|
+
page: {
|
|
562
|
+
current: number;
|
|
563
|
+
size: number;
|
|
564
|
+
};
|
|
565
|
+
/** 分页启用状态 */
|
|
566
|
+
paginationEnabled: Ref<boolean>;
|
|
567
|
+
/** 分页配置(供组件使用) */
|
|
568
|
+
pagination: ComputedRef<false | {
|
|
569
|
+
enabled: boolean;
|
|
570
|
+
page: number;
|
|
571
|
+
pageSize: number;
|
|
572
|
+
}>;
|
|
573
|
+
/** 刷新数据 */
|
|
574
|
+
refresh: () => Promise<void>;
|
|
575
|
+
/** 新增数据 */
|
|
576
|
+
create: (row: T) => Promise<void>;
|
|
577
|
+
/** 更新数据 */
|
|
578
|
+
save: (row: T) => Promise<void>;
|
|
579
|
+
/** 删除数据 */
|
|
580
|
+
remove: (row: T) => Promise<void>;
|
|
581
|
+
/** 批量删除数据 */
|
|
582
|
+
batchRemove: (rows: T[]) => Promise<void>;
|
|
583
|
+
/** 获取详情 */
|
|
584
|
+
getDetail: (row: T) => Promise<T | null>;
|
|
585
|
+
/** 处理取消编辑 */
|
|
586
|
+
handleCancel: () => Promise<void>;
|
|
587
|
+
/** 处理分页变化 */
|
|
588
|
+
handlePaginationChange: (pageNum: number, pageSize: number) => void;
|
|
589
|
+
/** 处理行删除(UI层) */
|
|
590
|
+
handleRowDelete: (row: T, index: number) => void;
|
|
591
|
+
/** 详情弹窗状态 */
|
|
592
|
+
detail: DetailModal<T>;
|
|
593
|
+
/** 详情配置 */
|
|
594
|
+
detailConfig?: DetailConfig;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* 表格 CRUD 组合式 API
|
|
599
|
+
*
|
|
600
|
+
* @description
|
|
601
|
+
* 配置驱动的表格 CRUD 解决方案,极简使用,功能完整
|
|
602
|
+
* 完全替代 usePageCrud,专注于表格场景
|
|
603
|
+
*
|
|
604
|
+
* @template T 数据行类型
|
|
605
|
+
* @param config 配置对象
|
|
606
|
+
* @returns 表格 CRUD 实例
|
|
607
|
+
*
|
|
608
|
+
* @example
|
|
609
|
+
* ```ts
|
|
610
|
+
* const table = useTableCrud<Employee>({
|
|
611
|
+
* api: {
|
|
612
|
+
* list: '/employees/list',
|
|
613
|
+
* get: '/employees/:id',
|
|
614
|
+
* update: '/employees/:id',
|
|
615
|
+
* remove: '/employees/:id',
|
|
616
|
+
* create: '/employees'
|
|
617
|
+
* },
|
|
618
|
+
* columns: [...],
|
|
619
|
+
* customActions: [
|
|
620
|
+
* {
|
|
621
|
+
* key: 'copy',
|
|
622
|
+
* label: '复制',
|
|
623
|
+
* icon: 'mdi:content-copy',
|
|
624
|
+
* handler: (row, ctx) => {
|
|
625
|
+
* const newRow = { ...row, id: Date.now() }
|
|
626
|
+
* ctx.data.unshift(newRow)
|
|
627
|
+
* ctx.message.success('复制成功')
|
|
628
|
+
* }
|
|
629
|
+
* }
|
|
630
|
+
* ]
|
|
631
|
+
* })
|
|
632
|
+
*
|
|
633
|
+
* // 组件中使用
|
|
634
|
+
* <c-table
|
|
635
|
+
* v-model:data="table.data.value"
|
|
636
|
+
* :columns="table.columns.value"
|
|
637
|
+
* :actions="table.actions.value"
|
|
638
|
+
* @save="table.save"
|
|
639
|
+
* />
|
|
640
|
+
* ```
|
|
641
|
+
*/
|
|
642
|
+
declare function useTableCrud<T extends DataRecord>(config: UseTableCrudConfig<T>): UseTableCrudReturn<T>;
|
|
643
|
+
|
|
644
|
+
export { type ActionContext, type ApiEndpoints, type CacheConfig, type CacheItem, type CancelConfig, type CustomAction, type DataRecord, type DedupeConfig, type DetailConfig, type DetailItem, type DetailModal, type DetailSection, type EnhancedAxiosRequestConfig, type FieldAliases, type InterceptorConfig, type RequestCoreConfig, type RequestKeyParams, type RetryConfig, type TableColumn, type UseTableCrudConfig, type UseTableCrudReturn, cancelAllPendingRequests, cancelAllRequests, cleanupExpiredCache, clearAllCache, clearCache, createAxiosInstance, createRequestCore, deleteData, getCacheSize, getCancelableRequestCount, getData, getGlobalConfig, getPendingRequestCount, onReLoginCancel, onReLoginSuccess, postData, putData, useTableCrud };
|