import { Ref } from 'vue'; import { AxiosRequestConfig } from 'axios'; /** 数据源类型 */ type DataSourceType = 'static' | 'api'; /** 加载状态 */ type LoadingStatus = 'idle' | 'loading' | 'success' | 'error'; /** 单个数据源配置 */ interface DataSourceConfig { /** 数据源唯一标识 */ key: string; /** 数据源类型 */ type: DataSourceType; /** 静态数据(type 为 static 时使用) */ data?: T; /** API 请求配置(type 为 api 时使用) */ api?: { url: string; method?: 'GET' | 'POST' | 'PUT' | 'DELETE'; params?: Record; data?: Record; headers?: Record; /** 响应数据字段路径,如 '/artists' 或 'data.items' */ fieldPath?: string; /** 响应数据转换函数 */ transform?: (response: any) => T; }; } /** 数据源状态 */ interface DataSourceState { data: T | null; status: LoadingStatus; error: Error | null; lastUpdated: Date | null; } /** 融合数据转换函数参数 */ type MergeDataMap = Record; /** DataSource 构造配置 */ interface DataSourceOptions { /** 是否立即加载 */ autoLoad?: boolean; /** 请求失败重试次数 */ retryCount?: number; /** 重试延迟(毫秒) */ retryDelay?: number; /** 全局请求拦截器 */ onRequest?: (config: AxiosRequestConfig) => AxiosRequestConfig; /** 全局响应拦截器 */ onResponse?: (response: any) => any; /** 全局错误处理 */ onError?: (error: Error, key: string) => void; /** 融合数据的转换函数,参数为 { key: data } 形式的对象,配置后自动融合所有数据源 */ mergeTransformer?: (dataMap: MergeDataMap) => any[]; } /** 数据更新监听器 */ type DataUpdateListener = (key: string, data: T, state: DataSourceState) => void; declare class DataSource { /** 数据源配置映射 */ private _configMap; /** 数据源状态映射 */ private _stateMap; /** 配置选项 */ private _options; /** 数据更新监听器列表 */ private _listeners; /** 响应式状态(用于 Vue 响应式更新) */ reactiveStates: Record; /** 全局加载状态 */ isLoading: Ref; /** 全局错误状态 */ hasError: Ref; /** 调试模式 */ debugMode: boolean; constructor(sources: any, options?: DataSourceOptions); /** * 添加数据源 */ addSource(config: DataSourceConfig): void; /** * 移除数据源 */ removeSource(key: string): boolean; /** * 加载单个数据源 */ private loadSingle; /** * 从对象中按路径获取值 * 支持两种路径格式: * - 点分隔:'data.users' * - 斜杠分隔:'/data/users' 或 'data/users' */ private pickPath; /** * 带重试的请求 */ private fetchWithRetry; /** * 延迟函数 */ private delay; /** * 更新状态 */ private updateState; /** * 更新全局状态 */ private updateGlobalStatus; /** * 通知所有监听器 */ private notifyListeners; private normalizeMethod; private isOldDataSource; /** * 清理配置,将旧版格式(path + resPath)转换为新版 DataSourceConfig * - 支持新版 fieldPath * - 兼容旧版 resPath,将其转换为 fieldPath */ private cleanConfig; /** * 加载数据(支持加载全部或指定的数据源) * 返回融合后的数组数据 */ loadData(keys?: string | string[]): Promise; /** * 刷新数据(参数变更时重新请求,或手动更新数据) * @param key 数据源标识 * @param paramsOrData 请求参数或直接更新的数据 * @param manual 是否手动更新本地数据(true: 直接更新本地数据,false: 从数据源加载) */ refreshData(key: string, paramsOrData?: Record | T, manual?: boolean): Promise; /** * 刷新所有数据源 * 返回融合后的数组数据 */ refreshAll(params?: Record>): Promise; /** * 获取数据 */ getData(key: string): T | null; /** * 获取所有数据 */ getAllData(): Record; /** * 获取状态 */ getState(key: string): DataSourceState | undefined; /** * 获取所有状态 */ getAllStates(): Record; /** * 获取加载状态 */ getLoadingStatus(key: string): LoadingStatus; /** * 检查是否正在加载 */ isSourceLoading(key: string): boolean; /** * 添加数据更新监听器 */ onDataUpdate(listener: DataUpdateListener): () => void; /** * 融合所有数据源数据 * @param transformer 可选的转换函数(不传则使用 options 中配置的 mergeTransformer) * @returns 融合后的数组数据 */ mergeData(transformer?: (dataMap: MergeDataMap) => T[]): T[]; /** * 清除所有数据 */ clear(): void; /** * 销毁实例 */ destroy(): void; /** * 旧版本兼容 */ reload(key?: any, params?: Record): Promise; } export { DataSource, type DataSourceConfig, type DataSourceState, type DataSourceOptions, type DataSourceType, type LoadingStatus, type DataUpdateListener, }; export { useDataSource, type UseDataSourceOptions } from './useDataSource';