Plugins are the way to extend Fastify and Fastify Decorators functionality.
Plugins API is exported under "fastify-decorators/plugins" entrypoint. It includes common symbols and functions.
Fastify decorators provides several hooks:
appInit
- executes after Fastify instance is created but before Fastify decorators starts initializationbeforeControllerCreation
- executes for each Controller/RequestHandler before instance createdafterControllerCreation
- executes for each Controller/RequestHandler after instance createdappReady
- executes when Fastify decorators done initializationappDestroy
- executes before Fastify instance destroyed
"Class loader" is a function used to instantiate Controller/RequestHandler/Service/etc. It accepts class itself and scope.
where:
- class itself could be Controller/RequestHandler or any other class type defined in the plugin (f.e. Service)
- scope is where this class were requested - FastifyInstance or FastifyRequest.
Registration of class loader SHOULD be done on appInit
stage by decorating FastifyInstance.
example:
import { CLASS_LOADER, createInitializationHook } from 'fastify-decorators/plugins';
createInitializationHook('appInit', (fastify) => {
fastify.decorate(CLASS_LOADER, (clazz: new () => any, _scope) => new clazz)
})
In addition to normal Lifecycle hooks, Fastify Decorators provides interfaces and symbols to interact with registered handlers and hooks.
This functionality can be used to implicitly decorate methods
Example: log arguments when any hook is called:
import { FastifyInstance } from 'fastify';
import { createInitializationHook, Registrable, hasHooks, HOOKS, IHook } from 'fastify-decorators/plugins';
createInitializationHook('beforeControllerCreation', (fastifyInstance: FastifyInstance, target: Registrable) => {
if (hasHooks(target)) {
for (const hook of target[HOOKS]) { // hook has type IHook
const _method = target[hook.handlerName];
target[hook.handlerName] = function logged(...args: any) {
console.log('Hook called with args: ', args)
return _method.apply(this, args)
}
}
}
})
Please refer to Fastify docs