Overview
Elysia’s plugin system allows you to create modular, reusable functionality that can be shared across applications. Plugins are simply Elysia instances that can be mounted onto other instances using the.use() method.
Basic plugin usage
Create a plugin by instantiating a new Elysia instance and adding routes or functionality:Plugin with configuration
Plugins can accept configuration parameters by wrapping them in a function:Plugin deduplication
Elysia automatically deduplicates plugins using thename and seed properties to prevent duplicate registration:
The
seed parameter is useful for cache busting when you update a plugin. Change the seed value to force Elysia to re-register the plugin.Scoped plugins
Plugins can be scoped to apply their hooks and state only within a specific context:Lifecycle inheritance
Plugins inherit lifecycle hooks from parent instances and can add their own:Async plugins
Plugins can be loaded asynchronously using dynamic imports:Prefix plugins
Add a prefix to all routes in a plugin:Real-world example
Here’s a complete authentication plugin example:Plugin composition
Plugins can compose other plugins to create complex functionality:Best practices
Use meaningful names
Use meaningful names
Always provide a
name for your plugins to enable deduplication and easier debugging.Version with seed
Version with seed
Use the
seed parameter to version your plugins and control cache invalidation.Keep plugins focused
Keep plugins focused
Each plugin should have a single responsibility. This makes them more reusable and easier to test.
Document configuration
Document configuration
Clearly document any configuration options your plugin accepts using TypeScript interfaces.