fastify-dynareg
This package is technically a plugin builder for Fastify. It exports a single function that accepts two arguments, a package name and a boolean indicating if that package is required, and returns a valid Fastify plugin. This library can be used to define both optional or parametric plugins.
dynareg(packageName[, isRequired])
packageName
<String>
The plugin's package name torequire()
.isRequired
<Boolean>
Defaults tofalse
.- Returns:
<FastifyPlugin>
Optional plugin presence
In the next example, this library is used to make a plugin (fastify-blipp
) optional. This behavior can be helpful for packages only used during development. If the plugin is not present, a nice debug log is written, and no errors are thrown.
const fastify = const dynareg = const app = // This plugin will try to register fastify-blipp if It's installedconst optionalBlipp = // You can still pass plugin options as usualapp app // TODO: Setup and start you application...
Parametric plugin presence
The plugin presence can also be parametric. Passing true
as second argument will require the plugin presence.
const fastify = const dynareg = const app = // Force fastify-blipp presence during developmentconst developmentBlipp = app app // TODO: Setup and start you application...
Fully parametric plugin
In the last example, both plugin's name and presence are parametric.
const fastify = const dynareg = const app = // Declare a dynamic production pluginconst myDynamicPlugin = app // TODO: Setup and start you application...
Example
const fastify = const dynareg = const app = // Force fastify-blipp to be registered when env is not productionapp app const start = async { try await app // Use conditional blipp if appblipp app applog catch err applog process }