Volt v0.1 · TypeScript APIs on Bun · Get started
Docs/Architecture & DI/Writing Plugins

Writing Plugins

Register shared services and clean them up explicitly.

Complete plugin example

index.ts
import { Volt, type VoltPlugin } from 'voltts';

const greetingPlugin: VoltPlugin = {
  name: 'example-greeting',
  version: '1.0.0',
  setup(app) {
    app.decorate('greeting', { message: 'Hello from a plugin' });
  },
  cleanup() {
    console.log('Plugin cleanup');
  },
};
const app = new Volt();
app.plugin(greetingPlugin);
app.get('/greeting', (c) => c.json(c.store.greeting));
if (globalThis._importMeta_.main) app.listen(3000);
export default app;

Plugin contract

A VoltPlugin has a unique name, optional version, setup(app), and optional cleanup(app). Register with app.plugin(plugin) or app.use(plugin). Duplicate names throw an error. app.decorate(name, value) stores a shared service on app.store and the application object; handlers read it through c.store.

Initialization and cleanup

The current plugin() implementation invokes setup without awaiting its Promise. Connect asynchronous clients before registering the plugin, then pass the ready client into setup. await app.stop() invokes each cleanup callback. Do not put request-specific identity in application decorations because c.store is shared across requests.