LingTropy/lingtropy-client/src/preload/index.ts

64 lines
2.1 KiB
TypeScript

import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron'
// Whitelist of valid channels used for IPC communication (Send message from Renderer to Main)
const mainAvailChannels: string[] = [
'msgRequestGetVersion',
'msgOpenExternalLink',
'msgOpenFile',
'store-set',
'store-get',
'fetch-models',
'getBaseUrl',
'call-openai',
'openai-partial-response',
'get-api-key',
'set-api-key',
'call-openai-title'
]
const rendererAvailChannels: string[] = ['openai-partial-response', 'get-api-key', 'set-api-key']
contextBridge.exposeInMainWorld('mainApi', {
send: (channel: string, ...data: any[]): void => {
if (mainAvailChannels.includes(channel)) {
ipcRenderer.send.apply(null, [channel, ...data])
if (process.env.NODE_ENV === 'development') {
console.log({ type: 'send', channel, request: data })
}
} else {
throw new Error(`Unknown ipc channel name: ${channel}`)
}
},
on: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void): void => {
if (rendererAvailChannels.includes(channel)) {
ipcRenderer.on(channel, listener)
} else {
throw new Error(`Unknown ipc channel name: ${channel}`)
}
},
once: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void): void => {
if (rendererAvailChannels.includes(channel)) {
ipcRenderer.once(channel, listener)
} else {
throw new Error(`Unknown ipc channel name: ${channel}`)
}
},
off: (channel: string, listener: (event: IpcRendererEvent, ...args: any[]) => void): void => {
if (rendererAvailChannels.includes(channel)) {
ipcRenderer.off(channel, listener)
} else {
throw new Error(`Unknown ipc channel name: ${channel}`)
}
},
invoke: async (channel: string, ...data: any[]): Promise<any> => {
if (mainAvailChannels.includes(channel)) {
const result = await ipcRenderer.invoke.apply(null, [channel, ...data])
if (process.env.NODE_ENV === 'development') {
console.log({ type: 'invoke', channel, request: data, result })
}
return result
}
throw new Error(`Unknown ipc channel name: ${channel}`)
}
})