refactor: split preload api by domain

This commit is contained in:
Misaka
2026-03-21 10:49:13 +08:00
parent 26c05f3726
commit ed65312fff
13 changed files with 284 additions and 242 deletions

28
src/preload/lib/ipc.ts Normal file
View File

@@ -0,0 +1,28 @@
import { ipcRenderer } from 'electron'
import type { IpcResult } from '../../main/types/ipc.types'
export function invokeIpc<T = unknown>(channel: string, ...args: unknown[]): Promise<IpcResult<T>> {
return ipcRenderer.invoke(channel, ...args).then((result: unknown) => {
if (result && typeof result === 'object' && 'success' in (result as Record<string, unknown>)) {
const typed = result as Record<string, unknown>
const hasIpcShape = 'data' in typed || 'code' in typed || 'error' in typed
if (hasIpcShape) {
return typed as unknown as IpcResult<T>
}
if (typed.success === true) {
return { success: true, data: result as T }
}
return {
success: false,
error: typeof typed.error === 'string' ? typed.error : 'IPC operation failed'
}
}
return { success: true, data: result as T }
})
}
export { ipcRenderer }