Gathering detailed insights and metrics for fastify-plugin
Gathering detailed insights and metrics for fastify-plugin
Gathering detailed insights and metrics for fastify-plugin
Gathering detailed insights and metrics for fastify-plugin
npm install fastify-plugin
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
202 Stars
272 Commits
43 Forks
15 Watching
3 Branches
47 Contributors
Updated on 27 Nov 2024
JavaScript (73.17%)
TypeScript (26.83%)
Cumulative downloads
Total Downloads
Last day
3.8%
492,169
Compared to previous day
Last week
5.9%
2,609,439
Compared to previous week
Last month
21.4%
9,929,805
Compared to previous month
Last year
54%
94,755,784
Compared to previous year
fastify-plugin
is a plugin helper for Fastify.
When you build plugins for Fastify and you want them to be accessible in the same context where you require them, you have two ways:
skip-override
hidden propertyNote: the v4.x series of this module covers Fastify v4 Note: the v2.x & v3.x series of this module covers Fastify v3. For Fastify v2 support, refer to the v1.x series.
1npm i fastify-plugin
fastify-plugin
can do three things for you:
skip-override
hidden propertyExample using a callback:
1const fp = require('fastify-plugin') 2 3module.exports = fp(function (fastify, opts, done) { 4 // your plugin code 5 done() 6})
Example using an async function:
1const fp = require('fastify-plugin') 2 3// A callback function param is not required for async functions 4module.exports = fp(async function (fastify, opts) { 5 // Wait for an async function to fulfill promise before proceeding 6 await exampleAsyncFunction() 7})
In addition, if you use this module when creating new plugins, you can declare the dependencies, the name, and the expected Fastify version that your plugin needs.
If you need to set a bare-minimum version of Fastify for your plugin, just add the semver range that you need:
1const fp = require('fastify-plugin') 2 3module.exports = fp(function (fastify, opts, done) { 4 // your plugin code 5 done() 6}, { fastify: '5.x' })
If you need to check the Fastify version only, you can pass just the version string.
You can check here how to define a semver
range.
Fastify uses this option to validate the dependency graph, allowing it to ensure that no name collisions occur and making it possible to perform dependency checks.
1const fp = require('fastify-plugin') 2 3function plugin (fastify, opts, done) { 4 // your plugin code 5 done() 6} 7 8module.exports = fp(plugin, { 9 fastify: '5.x', 10 name: 'your-plugin-name' 11})
You can also check if the plugins
and decorators
that your plugin intend to use are present in the dependency graph.
Note: This is the point where registering
name
of the plugins become important, because you can referenceplugin
dependencies by their name.
1const fp = require('fastify-plugin') 2 3function plugin (fastify, opts, done) { 4 // your plugin code 5 done() 6} 7 8module.exports = fp(plugin, { 9 fastify: '5.x', 10 decorators: { 11 fastify: ['plugin1', 'plugin2'], 12 reply: ['compress'] 13 }, 14 dependencies: ['plugin1-name', 'plugin2-name'] 15})
By default, fastify-plugin
breaks the encapsulation but you can optionally keep the plugin encapsulated.
This allows you to set the plugin's name and validate its dependencies without making the plugin accessible.
1const fp = require('fastify-plugin') 2 3function plugin (fastify, opts, done) { 4 // the decorator is not accessible outside this plugin 5 fastify.decorate('util', function() {}) 6 done() 7} 8 9module.exports = fp(plugin, { 10 name: 'my-encapsulated-plugin', 11 fastify: '5.x', 12 decorators: { 13 fastify: ['plugin1', 'plugin2'], 14 reply: ['compress'] 15 }, 16 dependencies: ['plugin1-name', 'plugin2-name'], 17 encapsulate: true 18})
fastify-plugin
adds a .default
and [name]
property to the passed in function.
The type definition would have to be updated to leverage this.
It is common for developers to inline their plugin with fastify-plugin such as:
1fp((fastify, opts, done) => { done() }) 2fp(async (fastify, opts) => { return })
TypeScript can sometimes infer the types of the arguments for these functions. Plugins in Fastify are recommended to be typed using either FastifyPluginCallback
or FastifyPluginAsync
. These two definitions only differ in two ways:
done
(the callback part)FastifyPluginCallback
or FastifyPluginAsync
At this time, TypeScript inference is not smart enough to differentiate by definition argument length alone.
Thus, if you are a TypeScript developer please use on the following patterns instead:
1// Callback 2 3// Assign type directly 4const pluginCallback: FastifyPluginCallback = (fastify, options, done) => { } 5fp(pluginCallback) 6 7// or define your own function declaration that satisfies the existing definitions 8const pluginCallbackWithTypes = (fastify: FastifyInstance, options: FastifyPluginOptions, done: (error?: FastifyError) => void): void => { } 9fp(pluginCallbackWithTypes) 10// or inline 11fp((fastify: FastifyInstance, options: FastifyPluginOptions, done: (error?: FastifyError) => void): void => { }) 12 13// Async 14 15// Assign type directly 16const pluginAsync: FastifyPluginAsync = async (fastify, options) => { } 17fp(pluginAsync) 18 19// or define your own function declaration that satisfies the existing definitions 20const pluginAsyncWithTypes = async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => { } 21fp(pluginAsyncWithTypes) 22// or inline 23fp(async (fastify: FastifyInstance, options: FastifyPluginOptions): Promise<void> => { })
This project is kindly sponsored by:
Licensed under MIT.
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
security policy file detected
Details
Reason
SAST tool is not run on all commits -- score normalized to 7
Details
Reason
Found 15/22 approved changesets -- score normalized to 6
Reason
7 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 6
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More