Standardized way of exposing and using Obsidian plugin APIs.
APIs are published on the global window.PluginApi object, so they can be used by other plugins as well as anywhere else in Obsidian, e.g. in Templater user scripts or the developer console.
npm install --save-dev @vanakat/plugin-apiThis module exposes 2 functions:
registerAPI(name, api, plugin)– registers plugin API in global variable (window.PluginApi); the API is automatically unregistered when the plugin unloadspluginApi(name)– returns plugin API based on its name; if no API is registered under that name, shows a notice asking the user to install the plugin
Exposing an API from your plugin:
import { Plugin } from 'obsidian';
import { registerAPI } from '@vanakat/plugin-api';
export class MyPlugin extends Plugin {
async onload() {
registerAPI('MyPlugin', new MyPluginApi(this), this);
}
}Consuming an API from another plugin:
import { pluginApi } from '@vanakat/plugin-api';
const api = pluginApi('MyPlugin');Following plugins already use @vanakat/plugin-api:
- Zotero Bridge as an API provider
- Zotero Link as an API consumer
MIT