method to check if plugin version is matching the Fastify version
matthyk opened this issue Β· comments
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the feature has not already been requested
π Feature Proposal
If you register a plugin that does not match the Fastify version, an error is thrown. It would be helpful to have a function like 'checkPluginVersion' that follows the same logic to check if the given plugin version matches the Fastify version.
Motivation
No response
Example
import fp from 'fastify-plugin'
import Fastify from 'fastify'
const plugin = fp(async app => {}, {
fastify: '4.x'
})
const app = Fastify()
if (app.checkPluginVersion(plugin)) {
console.log('plugin version does match' )
} else {
console.log('plugin version does not match' )
}
I'm kind of over the fence, it seems like a good fit for a plugin, but also do not have a strong argument for not add it to core; what use cases do you have in mind?
My use case is as follows: I have a CLI that receives a file path/several file paths to plugins from the user. This CLI then registers the given plugins in a self-created Fastify instance. Now it would be handy to be able to check whether the version of the Fastify plugins matches the Fastify version. Currently this is possible via a register, where you can see if an error is thrown. But I would prefer to have a method to easily check this without having to catch an error.
It kind of sounds pretty well for a plugin, fastify
already exposes the version
from the instance that be consumed by a meta-plugin that decorates app with the API you propose to validate plugins.
e.g.
const metaPlugin = require('<the_name_of_the_plugin>');
const app = fastify().register(metaPlugin, { throw: true, /* ... other opts */ });
app.verifyPlugin(plugin) //throws or returns false.