Plugins Recap
Here is a brief recap of how to integrate plugins with FrogBot, to learn more head back to the plugin overview page.How to install a plugin
To install any plugin, simply add it to your FrogBot Config in the plugins array.Initialization
The initialization process goes in the following order:- Incoming config is validated
- Plugins execute, by
order(see Order, slug, and options) - Default options are integrated
- Sanitization cleans and validates data
- Final config gets initialized
Set up a development app
Create a regular FrogBot app to exercise your plugin during development:src/frogbot.config.ts:
http://localhost:3000.
Testing
A good test suite verifies the behavior your plugin adds and confirms that existing configuration is preserved. Use the test framework supported by your package, run the plugin inside a test FrogBot app, and cover both enabled and disabled options when your plugin provides them.Building a Plugin
Now that we have our environment setup and dev project ready to go - it’s time to build the plugin!Spread syntax
Spread syntax (or the spread operator) is a feature in JavaScript that uses the dot notation (…) to spread elements from arrays, strings, or objects into various contexts. We are going to use spread syntax to allow us to add data to existing arrays without losing the existing data. It is crucial to spread the existing data correctly, else this can cause adverse behavior and conflicts with FrogBot Config and other plugins. Let’s say you want to build a plugin that adds a new collection:config.collections to ensure that we don’t lose the existing collections. Then you can add any additional collections, just as you would in a regular FrogBot Config.
This same logic is applied to other array and object-like properties such as admin and hooks:
Extending functions
Function properties cannot use spread syntax. The way to extend them is to execute the existing function if it exists and then run your additional functionality. Here is an example extending theonInit property:
Order, slug, and options
definePlugin builds a plugin factory with a slug, an optional order, and typed options. Its callback receives the incoming config, the options the factory was called with, and plugins, the app’s other plugins keyed by slug:
order, lowest first. The default is 0, and plugins with the same order keep their plugins array order. Use 10 when your plugin needs collections or fields another plugin adds, like the API keys collection above, so it runs after that plugin wherever the app lists it. When a plugin with a slug throws, the error names it: [frogbot] plugin "plugin-key-notes" failed: ….
Each plugin’s options are the object its factory was called with. To give other plugins typed access to them, register your slug by augmenting RegisteredPlugins:
definePlugin returns, and pass the resolved object as options. The API keys plugin does this, so its collectionSlug option is always set.
Types
If your plugin has options, you should define and provide types for these options in a separate file which gets exported from the mainindex.ts.