Plugin API
definePlugin(), the API given to setup(), and the hooks. For a guide, see Plugins.
definePlugin()
Extra properties are allowed, and readable by other plugins with api.getPlugin(). By convention, a plugin is exported as a function that takes its options: cachePlugin({ cache }).
The plugins of Dotpress are named auth, cache, events, mailer and openapi: dependsOn: ['auth'].
PluginAPI
The registration methods work during setup() only: called later, they throw.
Hooks
onInit and onReady run inside createApp(): a failure makes it reject. The hooks of several plugins run in setup order; for wrapRequest, the first plugin is the outermost.
The response event
Order in a request
- CORS preflight requests are answered.
- No route:
404or405, thenonResponse. - The request ID and the request logger are created.
wrapRequest, thenonRequest.- The middlewares: plugins,
createApp(), groups, route. - Request validation, the handler, response validation.
- The response filters:
createApp(), plugins, router. - CORS headers, then
onResponse.
See The request pipeline.
Context keys
api.decorateContext(key, factory): the factory receives the context, and runs on first access, at most once per request. Two plugins cannot provide the same key, and the keys of the core (req,res,requestId,logger,raw,publicPath) are reserved.declareContextKey(key, pluginName): at the top level of the module, next to the augmentation. Without the plugin,ctx.tenantthrows"ctx.tenant" is used, but the "tenantPlugin" plugin is not installed in createApp().
Plugin or middleware?
A middleware is enough for logic attached to some routes. Write a plugin when the feature needs routes of its own, a context key, a hook on every response, or code at startup and shutdown. See Plugin or middleware?.