electron-typescript-ipc
TypeScript icon, indicating that this package has built-in type declarations

3.0.0 • Public • Published

electron-typescript-ipc

Library for type-safe use of contextBridge in Electron.

Motivation

When we implement ipc using contextBridge in TypeScript, we can't know if the method exists (unsafe). This library is aimed at making it type-safe to use.

Install

Install with npm (or you can use your favorite tool).

npm i electron-typescript-ipc

Example

We have a template repository for using this module.
Please see here: https://github.com/JichouP/electron-typescript-ipc-example.

preload/preload.ts

import {
  contextBridge,
  createIpcRenderer,
  GetApiType,
} from 'electron-typescript-ipc';
import { Api } from 'path/to/api.ts';

const ipcRenderer = createIpcRenderer<Api>();

export type Api = GetApiType<
  {
    getDataFromStore: (str: string) => Promise<string>;
  },
  {
    showAlert: (text: string, num: number) => Promise<void>;
  }
>;

const api: Api = {
  invoke: {
    getDataFromStore: async (key: string) => {
      return await ipcRenderer.invoke('getDataFromStore', key);
    },
  },
  on: {
    showAlert: listener => {
      ipcRenderer.on('showAlert', listener);
    },
  },
};

contextBridge.exposeInMainWorld('myAPI', api);

declare global {
  interface Window {
    myAPI: Api;
  }
}

lib/main.ts

const createWindow = (): void => {
  const mainWindow = ...

  mainWindow.on('ready-to-show', () => {
    ipcMain.removeHandler<Api>('getDataFromStore'); // This is essential in case you are called multiple times
    ipcMain.handle<Api>('getDataFromStore', async (_event, key) => {
      return await store.get(key);
    });
    setInterval(ipcMain.send<Api>(mainWindow, 'showAlert', 'Hi'), 10000)
  })
}

renderer/app.ts

window.myApi.invoke.getDataFromStore('someKey').then(console.log);
window.myApi.on.showAlert((_event, text) => {
  alert(text);
});

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 3.0.0
    106
    • latest

Version History

Package Sidebar

Install

npm i electron-typescript-ipc

Weekly Downloads

114

Version

3.0.0

License

MIT

Unpacked Size

28.3 kB

Total Files

12

Last publish

Collaborators

  • jichoup