eipc-api

1.0.1 • Public • Published

eipc-api

This module is designed to allow for easily designing modular/namespaced API's and exposing them to the browser process through electron's IPC functionality.

Installation

npm install eipc-api

Usage

lib/api.js - api declaration:

const IpcApi = require('eipc-api');

const api = {
    ns1: {
        method1: function() { /* do something */ },
        method2: function() { /* do something */ },
    }
};

module.exports = new IpcApi(api);

electron.js - main electron file, runs in main process

const { app, BrowserWindow, ipcMain } = require('electron');
const api = require('.lib/api.js');

...

app.whenReady().then(() => {
    api.registerHandlers(ipcMain);
});

...

preload.js - electron preload file, exposes to renderer process

const { contextBridge, ipcRenderer } = require('electron');
const api = require('./lib/api.js');

...
const invoker = api.getInvoker(ipcRenderer)
contextBridge.exposeInMainWorld('api', invoker);

client.js - javascript running in the browser renderer process

...

const returnValue = window.api.ns1.method1(/* params */);

...

IpcApi

Constructor

IpcApi(api [, config])

  • api - an object containing, methods, properties, and namespaced properties to parse and register.
  • config - object containing config options for the api
    • includeBaseMethods - boolean - if true any "base methods" (methods defined on the root api object, and not in a namespace) will be included in the generated api object. Base methods will be registered and have an invoker handler created for them just like namespaced methods.
    • includeBaseProperties - array - a list containing the names of base properties (any non-function and non-namespace properties defined on the root api object) to include in the generated api. Base properties will be included in the generated invoker, but wil not have any IPC functionality. You can use this to pass static data from the main to the render process.

Instance Methods

registerHandlers(ipcMain) - register handlers in the main process using the provided ipcMain objects handle function. This sets up the main process to handle IPC requests from the renderer process

  • ipcMain - the ipcMain object provided by electron

getInvoker(ipcRenderer) - generates and returns an api invoker object that includes namespaced methods and optionally base properties/methods, if configured to. The generated invoker will have the same structure as the originally passed api object, but any methods will automatically handle the calling of ipcRenderer.invoke(), passing params, and returning any return value that might exist.

Test

npm test

Real World Example

img-keeper

I designed this package for use in my project github.com/slimnate/img-keeper, check it out. Relevant files:

  • src/lib/api.js
  • public/electron.js
  • public/preload.js

Package Sidebar

Install

npm i eipc-api

Weekly Downloads

4

Version

1.0.1

License

ISC

Unpacked Size

30.2 kB

Total Files

8

Last publish

Collaborators

  • slimnate