Base

Base class for all plugins.

This is the class you must extend when creating a new plugin. It provides basic methods and properties to manage your plugin.

You need to wrap your plugin in a factory function with a single parameter pkgName, which will be called by the Bajo framework during boot process. The factory function must return your plugin class.

Don't forget to write package.json for your plugin that follows this specification. Please do not confuse this with your app's package.json specification, since this one is for your plugin only.

Example:

// index.js
async function factory (pkgName) {
  const { Base } = this.app.baseClass // get Base from app's baseClass repository
  const me = this // 'this' is Bajo instance. See Bajo boot process for more details
  class BajoCache extends Base {
    constructor () {
      super(pkgName, me.app)
      this.config = {} // your plugin's configuration object. If omitted, it will be set to an empty object
    }

    init = async () => {
      // your plugin's initialization code here
    }

    start = async () => {
      // your plugin's start code here
    }
  }
  return BajoCache
}

Constructor

new Base(pkgName, app)

Constructor.

Parameters:
NameTypeDescription
pkgNamestring

Package name (the one in package.json)

appObject

App instance reference. Useful to call app method inside a plugin

Members

dependencies :Array.<string>

Array of plugin dependencies. If your plugin depends on other plugins, you can specify their package names here e.g. ['bajo-config', 'bajo-cli'], NOT the plugin name/namespace

Type:
  • Array.<string>

pkg :object

Package information from package.json. It will be automatically loaded by the framework during plugin initialization

Type:
  • object

Methods

(async) dispose() → {Promise.<void>}

Dispose internal references.

Returns:
Type: 
Promise.<void>

(async) exit() → {Promise.<void>}

Upon app termination, this method will be called first. Mostly useful for system cleanup, delete temporary files, freeing resources etc.

Returns:
Type: 
Promise.<void>

(async) init() → {Promise.<void>}

Plugin initialization. This method will be called by the framework during boot process, after configuration is loaded.

Returns:
Type: 
Promise.<void>

(async) loadConfig() → {Promise.<void>}

Load plugin configuration. This method will be called by the framework during boot process.

Returns:
Type: 
Promise.<void>

(async) start() → {Promise.<void>}

Plugin start. This method will be called by the framework during boot process, after initialization. You still can modifiy your plugin's configuration here before it is deep frozen.

Returns:
Type: 
Promise.<void>

(async) stop() → {Promise.<void>}

Reserved for future use. This method will be called before plugin is stopped.

Returns:
Type: 
Promise.<void>

Type Definitions

package·json

To be recognized as a valid Bajo plugin, your package must be an ES6 module and have a package.json file with the additional bajo property and at least the type property set to plugin.

Other than type, you might need to specify these additional properties:

  • type: string, must be set to plugin
  • alias: string, the alias name of your plugin. Must be unique among all plugins. If it is missing, it will be generated by Bajo as camel-cased version of your plugin namespace.
  • dependencies: array, if your plugin requires one or more other plugins to work, you need to put their package names in this array
  • appletSupport: boolean, if your plugin doesn't support applet mode, set this to false to skip init() and start() of your plugin

Example:

{
  "name": "my-plugin",
  "version": "1.0.0",
  "description": "My Bajo plugin",
  "type": "module",
  "main": "index.js",
  "bajo": {
    "type": "plugin",
    "alias": "myplugin",
    "dependencies": ["bajo-config", "bajo-cli"]
  }
}
...

Note: The dot symbol in package.json has been replaced with · symbol because of JSDoc theme limitation

Warning: Do not confuse with your app's package.json specification, since this one is for plugin while the other is for app.