Exploring the options parameter
This section is a deep look at the optional options
parameter and how we can develop reusable plugins. A plugin declaration function isn’t anything more than a factory function that, instead of returning some new entity as factory functions usually do, adds behaviors to a Fastify instance. If we look at it like this, we can think about the options
parameter as our constructor’s arguments.
Firstly, let’s recall the plugin declaration function signature:
async function myPlugin(fastify, [options])
How can we pass our custom arguments to the options
parameter? It turns out that the register method has a second parameter too. So, the object we use as the argument will be passed by Fastify as Plugin’s options
parameter:
app.register(myPlugin, { first: 'option'})
Now, inside the myPlugin
function, we can access this value simply using options.first
.
It is worth mentioning that Fastify reserves three specific...