/** * LUVIA · 云衍 SDK · TypeScript type declarations * * Drop into your TS/JS project as `luvia-sdk.d.ts` (or in `types/`) so the * IDE auto-completes the LUVIA global, the SDK, and all event types. * * Usage in your HTML/JS: * * * * Usage in TypeScript: * declare const LUVIA: import('luvia-sdk').LuviaGlobal; * LUVIA.heartbeat('user_saved', { doc_id: 42 }); */ // ---- LUVIA-injected data shapes ---- export interface LuviaEmbedHello { type: 'luvia:embed:hello'; app_id: string; ts: number; event?: string; detail?: Record | null; } export interface LuviaParams { type: 'luvia:params'; app_id: string; params: Record; } export interface LuviaFocus { type: 'luvia:focus'; app_id: string; } export interface LuviaAction { type: 'luvia:action'; app_id: string; name: string; payload?: Record; } export interface LuviaEvent { /** Cross-app event bus message. */ type: 'luvia:event'; source: string; event: string; payload: Record; ts: number; } export interface LuviaHeartbeat { type: 'luvia:heartbeat'; app_id: string; event: string; extra?: Record; ts: number; } export interface LuviaDebugConsole { type: 'luvia:debug:console'; app_id: string; level: 'log' | 'warn' | 'error'; msg: string; ts: number; } export interface LuviaNavigate { type: 'luvia:navigate'; app_id: string; params?: Record; } export type LuviaMessage = | LuviaEmbedHello | LuviaParams | LuviaFocus | LuviaAction | LuviaEvent | LuviaHeartbeat | LuviaDebugConsole; // ---- event subscriptions ---- export type LuviaEventName = | 'params' | 'focus' | 'action' | 'event' | 'heartbeat'; export type LuviaEventHandler = E extends 'params' ? (msg: LuviaParams) => void : E extends 'focus' ? (msg: LuviaFocus) => void : E extends 'action' ? (msg: LuviaAction) => void : E extends 'event' ? (msg: LuviaEvent) => void : E extends 'heartbeat'? (msg: LuviaHeartbeat) => void : never; export type LuviaUnsubscribe = () => void; // ---- public API ---- export interface LuviaGlobal { /** The app_id this SDK was loaded with (from data-luvia-app or window.LUVIA_APP_ID). */ appId: string | null; /** True if running inside a LUVIA iframe, false if standalone. */ inLUVIA: boolean; /** * Tell LUVIA your app is ready. LUVIA will stop showing the loading * spinner and mark the iframe as "🟢 已连接". */ ready(detail?: Record): void; /** * Send a business event to LUVIA for the audit log (visible in * admin → 入口审计). */ heartbeat(event: string, extra?: Record): void; /** * Request LUVIA to switch to (or open) another app. Returns false if * the app is unknown or running outside LUVIA. */ navigate(app_id: string, params?: Record): boolean; /** Alias for navigate() — clearer name in some call sites. */ switchApp(app_id: string, params?: Record): boolean; /** * Subscribe to events from LUVIA. Returns an unsubscribe function. * * @example * const off = LUVIA.on('params', e => console.log(e.params)); * // later: * off(); */ on(event: E, fn: LuviaEventHandler): LuviaUnsubscribe; } declare global { interface Window { LUVIA: LuviaGlobal; LUVIA_APP_ID?: string; } } // ---- outgoing message types (for type-safe call sites) ---- export type LuviaOutgoing = LuviaEmbedHello | LuviaHeartbeat | LuviaNavigate;