|
|
@@ -3,14 +3,7 @@ import { useWindowSize } from '@vueuse/core'
|
|
|
import { useTableColumns } from './useTableColumns'
|
|
|
import type { ColumnOption } from '@/types/component'
|
|
|
import { TableCache, CacheInvalidationStrategy, type ApiResponse } from '../utils/table/tableCache'
|
|
|
-import {
|
|
|
- type TableError,
|
|
|
- defaultResponseAdapter,
|
|
|
- extractTableData,
|
|
|
- updatePaginationFromResponse,
|
|
|
- createSmartDebounce,
|
|
|
- createErrorHandler
|
|
|
-} from '../utils/table/tableUtils'
|
|
|
+import { type TableError, defaultResponseAdapter, extractTableData, updatePaginationFromResponse, createSmartDebounce, createErrorHandler } from '../utils/table/tableUtils'
|
|
|
|
|
|
// 类型推导工具类型
|
|
|
type InferApiParams<T> = T extends (params: infer P) => any ? P : never
|
|
|
@@ -88,9 +81,7 @@ export interface UseTableConfig<
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-export function useTable<TApiFn extends (params: any) => Promise<any>>(
|
|
|
- config: UseTableConfig<TApiFn>
|
|
|
-) {
|
|
|
+export function useTable<TApiFn extends (params: any) => Promise<any>>(config: UseTableConfig<TApiFn>) {
|
|
|
return useTableImpl(config)
|
|
|
}
|
|
|
|
|
|
@@ -105,27 +96,13 @@ export function useTable<TApiFn extends (params: any) => Promise<any>>(
|
|
|
* - 错误处理
|
|
|
* - 列配置管理
|
|
|
*/
|
|
|
-function useTableImpl<TApiFn extends (params: any) => Promise<any>>(
|
|
|
- config: UseTableConfig<TApiFn>
|
|
|
-) {
|
|
|
+function useTableImpl<TApiFn extends (params: any) => Promise<any>>(config: UseTableConfig<TApiFn>) {
|
|
|
type TRecord = InferRecordType<InferApiResponse<TApiFn>>
|
|
|
type TParams = InferApiParams<TApiFn>
|
|
|
const {
|
|
|
- core: {
|
|
|
- apiFn,
|
|
|
- apiParams = {} as Partial<TParams>,
|
|
|
- excludeParams = [],
|
|
|
- immediate = true,
|
|
|
- columnsFactory,
|
|
|
- paginationKey = { current: 'pageNum', size: 'pageSize' }
|
|
|
- },
|
|
|
+ core: { apiFn, apiParams = {} as Partial<TParams>, excludeParams = [], immediate = true, columnsFactory, paginationKey = { current: 'pageNum', size: 'pageSize' } },
|
|
|
transform: { dataTransformer, responseAdapter = defaultResponseAdapter } = {},
|
|
|
- performance: {
|
|
|
- enableCache = false,
|
|
|
- cacheTime = 5 * 60 * 1000,
|
|
|
- debounceTime = 300,
|
|
|
- maxCacheSize = 50
|
|
|
- } = {},
|
|
|
+ performance: { enableCache = false, cacheTime = 5 * 60 * 1000, debounceTime = 300, maxCacheSize = 50 } = {},
|
|
|
hooks: { onSuccess, onError, onCacheHit, resetFormCallback } = {},
|
|
|
debug: { enableLog = false } = {}
|
|
|
} = config
|
|
|
@@ -250,10 +227,7 @@ function useTableImpl<TApiFn extends (params: any) => Promise<any>>(
|
|
|
}
|
|
|
|
|
|
// 获取数据的核心方法
|
|
|
- const fetchData = async (
|
|
|
- params?: Partial<TParams>,
|
|
|
- useCache = enableCache
|
|
|
- ): Promise<ApiResponse<TRecord>> => {
|
|
|
+ const fetchData = async (params?: Partial<TParams>, useCache = enableCache): Promise<ApiResponse<TRecord>> => {
|
|
|
// 取消上一个请求
|
|
|
if (abortController) {
|
|
|
abortController.abort()
|
|
|
@@ -267,15 +241,7 @@ function useTableImpl<TApiFn extends (params: any) => Promise<any>>(
|
|
|
error.value = null
|
|
|
|
|
|
try {
|
|
|
- let requestParams = Object.assign(
|
|
|
- {},
|
|
|
- searchParams,
|
|
|
- {
|
|
|
- [pageKey]: pagination.current,
|
|
|
- [sizeKey]: pagination.size
|
|
|
- },
|
|
|
- params || {}
|
|
|
- ) as TParams
|
|
|
+ let requestParams = Object.assign({}, searchParams, { [pageKey]: pagination.current, [sizeKey]: pagination.size }, params || {}) as TParams
|
|
|
|
|
|
// 剔除不需要的参数
|
|
|
if (excludeParams.length > 0) {
|