import React from 'react'; import { B as BaseUser } from '../auth-Bkx--ikc.js'; /** * UmiJS App Runtime 配置工厂 * * 抽象三个项目中 app.tsx 的通用模式: * - getInitialState: 获取用户信息 + 认证状态 * - layout: 头像菜单、页面切换守卫、错误边界 * - rootContainer: 全局 Provider 包装 * * 将三个项目中 app.tsx 的重复模式抽象为统一的运行时初始化工厂。 */ /** * 应用用户信息 — 基于 BaseUser 扩展 * * 将 `BaseUser` 所有字段设为可选,以适配用户信息未完全加载的场景。 */ interface AppUser extends Partial { } /** * InitialState 结构 */ interface AppInitialState> { /** 当前用户信息 */ currentUser?: U; /** 正在加载用户信息 */ loading?: boolean; /** 额外的初始状态 (系统配置、组织信息等) */ extra?: E; /** 重新获取用户信息 */ fetchUserInfo: () => Promise; } /** * 头像菜单项 */ interface AvatarMenuItem { key: string; label: string; icon?: React.ReactNode; danger?: boolean; onClick?: () => void; } /** * 应用运行时配置选项 */ interface AppRuntimeOptions> { /** * 认证配置 */ auth: { /** 获取当前用户信息的 API */ fetchUserInfo: () => Promise; /** 登录页路径, 默认 '/login' */ loginPath?: string; /** 不需要认证的页面路径列表 (默认含 loginPath) */ guestPaths?: string[]; /** 检测是否已登录 (如检查 token) */ isAuthenticated?: () => boolean; /** 获取失败重试次数, 默认 0 */ retryCount?: number; /** 重试延迟 (ms), 默认 1000 */ retryDelay?: number; }; /** * 布局配置 */ layout?: { /** 应用标题 */ title?: string | ((initialState?: AppInitialState) => string); /** 应用 Logo URL 或组件 */ logo?: string | React.ReactNode; /** 头像菜单项 */ avatarMenuItems?: AvatarMenuItem[]; /** 显示用户名, 默认 true */ showAvatarName?: boolean; /** 页脚 */ footer?: React.ReactNode | (() => React.ReactNode); /** 页面切换回调 (可用于路由守卫) */ onPageChange?: (initialState: AppInitialState | undefined, location: { pathname: string; }, navigate: (path: string) => void) => void; /** 自定义 childrenRender 包装 (如水印) */ childrenRender?: (children: React.ReactNode, initialState?: AppInitialState) => React.ReactNode; /** 额外的 ProLayout 配置 */ layoutProps?: Record; }; /** * 额外初始数据获取 (与 fetchUserInfo 并行执行) */ fetchExtra?: () => Promise; /** * 导航函数 (用于路由跳转) * * SDK 不假设路由模式,由消费方注入。 * 如不提供,onPageChange 中的 navigate 回调将无操作。 * * @example * ```ts * import { history } from '@umijs/max'; * navigate: (path) => history.push(path), * ``` */ navigate?: (path: string) => void; /** * 初始化完成回调 */ onReady?: (state: AppInitialState) => void; /** * 初始化失败回调 */ onError?: (error: Error) => void; } /** * 创建 UmiJS App Runtime 配置 * * 返回 `getInitialState` 和 `layout` 函数,直接导出供 UmiJS 使用。 * * @example * ```ts * // app.tsx * import { createAppRuntime } from '@aspect/shared/config'; * * const appRuntime = createAppRuntime({ * auth: { * fetchUserInfo: () => getCurrentUser().then(res => * isApiSuccess(res) ? res.data : undefined * ), * loginPath: '/login', * guestPaths: ['/login', '/register', '/agreement'], * }, * layout: { * title: 'My App', * showAvatarName: true, * avatarMenuItems: [ * { key: 'settings', label: '个人设置', onClick: () => history.push('/settings') }, * { key: 'logout', label: '退出登录', danger: true, onClick: handleLogout }, * ], * onPageChange: (state, location, navigate) => { * if (!state?.currentUser && location.pathname !== '/login') { * navigate('/login'); * } * }, * }, * }); * * export const getInitialState = appRuntime.getInitialState; * export const layout = appRuntime.layout; * ``` */ declare function createAppRuntime>(options: AppRuntimeOptions): { getInitialState: () => Promise>; layout: (params: { initialState?: AppInitialState; location?: { pathname: string; }; [key: string]: unknown; }) => { title: string | undefined; logo: React.ReactNode; avatarProps: Record; avatarMenuItems: AvatarMenuItem[] | undefined; onPageChange: (() => void) | undefined; footerRender: (() => React.ReactNode) | undefined; childrenRender: ((children: React.ReactNode) => React.ReactNode) | undefined; }; /** 工具方法: 判断是否为免认证路径 */ isGuestPath: (pathname: string) => boolean; }; /** * 创建默认的页面切换守卫 * * 未登录用户在访问需要认证的页面时,自动跳转到登录页。 * * @example * ```ts * const appRuntime = createAppRuntime({ * // ... * layout: { * onPageChange: createPageChangeGuard({ * loginPath: '/login', * guestPaths: ['/login', '/register'], * }), * }, * }); * ``` */ declare function createPageChangeGuard>(options?: { loginPath?: string; guestPaths?: string[]; }): (initialState: AppInitialState | undefined, location: { pathname: string; }, navigate: (path: string) => void) => void; /** * UmiJS 通用配置工厂 */ /** * UmiJS 配置返回类型 * * 列出工厂函数可能输出的已知字段,同时允许 UmiJS 插件扩展的未知字段。 */ interface UmiBaseConfig { routes: Record[]; hash: boolean; history: { type: 'hash' | 'browser'; }; publicPath: string; model: Record; initialState: Record; access: Record; request: Record; mako: Record; fastRefresh: boolean; esbuildMinifyIIFE: boolean; ignoreMomentLocale: boolean; moment2dayjs: { preset: string; plugins: string[]; }; antd: Record; locale?: false | { default: string; antd: boolean; baseNavigator: boolean; }; layout?: Record; title?: string; alias?: Record; define?: Record; proxy?: Record; codeSplitting?: { jsStrategy: string; }; headScripts?: Record[]; favicons?: string[]; [key: string]: unknown; } /** * UmiJS 基础配置选项 */ interface UmiBaseConfigOptions { /** 路由配置 */ routes: Record[]; /** 国际化配置,传 false 关闭 */ locale?: false | { default: string; antd?: boolean; baseNavigator?: boolean; }; /** 公共路径,默认 '/' */ publicPath?: string; /** 路径别名 */ alias?: Record; /** 环境变量定义 */ define?: Record; /** 代理配置 */ proxy?: Record; /** 应用标题 */ title?: string; /** 是否使用 ProLayout,默认 false */ useProLayout?: boolean; /** ProLayout 配置 */ proLayoutSettings?: Record; /** 是否使用 hash 路由,默认 true */ hashRouting?: boolean; /** 是否启用代码分割,默认 false */ codeSplitting?: boolean; /** head 中的额外脚本 */ headScripts?: Record[]; /** 网站图标 */ favicons?: string[]; /** antd 主题配置 */ antdTheme?: Record; } /** * 创建 UmiJS 基础配置 * * 输出统一的: * - hash routing * - model + initialState + access 插件 * - mako 构建 * - fastRefresh 热更新 * - moment2dayjs * - antd CSS 变量模式 * - esbuildMinifyIIFE * * @param options - 配置选项 * @returns UmiJS 配置对象 * * @example * ```ts * // config/config.ts * import { createUmiBaseConfig } from '@aspect/shared/config'; * import routes from './routes'; * * export default createUmiBaseConfig({ * routes, * locale: { default: 'zh-CN', antd: true }, * title: 'My App', * define: { * 'process.env.API_BASE_URL': 'https://api.example.com', * }, * }); * ``` */ declare function createUmiBaseConfig(options: UmiBaseConfigOptions): UmiBaseConfig; export { type AppInitialState, type AppRuntimeOptions, type AppUser, type AvatarMenuItem, type UmiBaseConfig, type UmiBaseConfigOptions, createAppRuntime, createPageChangeGuard, createUmiBaseConfig };