Plugins
A plugin packages setup that an application would otherwise do by hand: providers, interceptors, error mappers, extra routes and shutdown work. @navios/openapi, @navios/otel and @navios/adapter-xml are plugins. There is one plugin shape.
Defining a plugin
This plugin counts requests per route and status, and serves the counts at /metrics. The registry and the interceptor are ordinary services:
The plugin wires them into an application:
definePlugin returns a function. Calling it with options gives a plugin instance:
A plugin whose setup declares no options is called without arguments: app.usePlugin(csvPlugin()). One whose options are all optional may be as well. usePlugin throws once init() has been called.
The plugin definition
Plugins are set up in dependency order, whatever order they were registered in. A dependency that is not registered, or a cycle between plugins, fails init() with a message naming the plugin.
The plugin API
setup runs after the application's transports, and so the HTTP adapter, have been created and before modules are loaded. provide and decorate apply at once, everything else at its point of init(), so a plugin does not choose a stage. All of them belong into setup and throw afterwards; api.transport() and api.container work at any time.
Providers are described in Providers, interceptors in Guards and interceptors and error mappers in Errors.
The HTTP transport
Everything in the table above works in any application, with or without HTTP. What belongs to HTTP is reached through the transport:
An interceptor that reads the request, such as its headers or path, is registered with http.intercept. One registered with api.intercept also runs for the handlers of other transports and receives their contexts.
Contributing routes
A plugin adds routes by adding a module. The module, its controller and its endpoint declaration are written like any other; a plugin usually has its own builder() because it does not know the application's.
Routes added this way get the application's global prefix, pass through application-level interceptors and appear in app.routes().
api.onBootstrap is where a plugin looks at the finished application. Its route list includes routes added by other plugins, because it is built after every setup has run:
Using the native server
http.adapter is typed as the adapter-neutral HttpAdapter, whose server is unknown. A plugin written for one adapter narrows it:
Registering a Fastify plugin works the same way: await server.register(somePlugin) in an async setup. On Bun the native server does not exist until listen(), so http.adapter.server throws during setup.
Response encoders
The server knows how to write six kinds of result: json, text, blob, stream, sse and empty. An encoder converts a result of another kind into one of those, which lets a package add a response format without touching the adapters.
A handler produces a custom kind with result(kind, body), usually behind a helper the package exports:
The endpoint is declared with response: 'text', 'stream' or 'blob', and the handler returns the helper's value:
- An encoder runs inside the pipeline: after the handler's value has become a result, and before interceptors exit. It receives the request context, and an error it throws is mapped to a response like any other.
result()also accepts{ status, headers }as a third argument.- There is one encoder per kind; registering a second one for the same kind replaces the first.
- Encoders apply to route results only, not to the not-found response.
@navios/adapter-xml is built this way; see XML responses.