Plugins
A plugin extends an app with a reusable feature: routes, global middlewares, response filters, context keys and lifecycle hooks. The packages of Dotpress are plugins (authPlugin(), openapiPlugin(), mailerPlugin()…), and your app can have its own.
Using a plugin
Plugins are given to createApp():
They are set up in order, after the plugins they depend on.
Writing a plugin
A plugin is an object declared with definePlugin(): a unique name, and a setup() function that receives the plugin API. A function that takes options and returns the plugin keeps it configurable:
Handlers of the app then use ctx.flags, typed:
The plugin API
Registration methods are only available during setup(), which can be async: they throw once it has returned.
Hooks
See Logging and error hooks for examples.
Dependencies between plugins
A plugin that needs another one declares it with dependsOn: it is set up after it, whatever the order of plugins. A missing or circular dependency makes createApp() throw.
The other properties of a plugin object are visible to other plugins through api.getPlugin(name): a plugin can expose a service to the plugins that depend on it.
Plugin or middleware?
Plugins are objects declared with definePlugin(), no longer functions. addGroup() is renamed addRouteGroup(), and useBeforeRoutes() / useAfterRoutes() are removed: plugins do not depend on a server, native middlewares go to the adapter options. Plugins gain dependencies, lifecycle hooks and context keys.