exposeApiToGlobalWindow

A function that exposes the IPCs to the global window object from the preload script


Params

It takes a single object as a parameter with the following properties:


  • apiKey - the api key that will be used to access the IPC handlers from the global window object (optional)
  • exposeAll - a boolean that determines whether all the IPC methods will be exposed to the global window object (optional). When set to true, invoke, handle and remove methods will be exposed!
  • append - an object that take any properties and values that will be appended to the global window object (optional)
  • override - a method that will override all - except appended data - the initial entries of exposed IPCs methods (invoke, handle and remove) and let's you manage what and how it will be exposed manually (optional)

Default

apiKey: 'api'
exposeAll: false // only invokers are exposed
append: {}
override: null

Return

  • key - the api key that will be used to access the IPCs from the global window object
  • api - an object that contains all the exposed IPCs

Example

import { exposeApiToGlobalWindow } from 'shared/ipcs'

const { key, api } = exposeApiToGlobalWindow({
  override(ipcs) {
    return {
      removePong() {
        ipcs.remove.getPong()
      },
    }
  },

  append: {
    appName: 'Electron App',

    sayHello(data: 'World') {
      return `Hello ${data}`
    },
  } as const,
})

declare global {
  interface Window {
    [key]: typeof api
  }
}