New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: core functionality added #1
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Overall very good in the sense that the plugin functionality seems correct and complete 🚀
I've added a few comments around mostly minor things and style
.husky/pre-commit
Outdated
@@ -2,3 +2,4 @@ | |||
. "$(dirname -- "$0")/_/husky.sh" | |||
|
|||
npx lint-staged | |||
npm test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I prefer not to run tests on pre-commit, it slows down the development cycle
README.md
Outdated
|
||
## Usage | ||
|
||
Register fastifyConstraints as a Fastify plugin. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's name the plugin consistently, it's called fastify-constraints. Also, consider wrapping it in backticks to format it as code
index.d.ts
Outdated
|
||
export { fastifyConstraints, FastifyConstraintsOptions } | ||
|
||
export default fastifyConstraints |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing new line at end of file
index.d.ts
Outdated
@@ -0,0 +1,9 @@ | |||
type FastifyConstraintsOptions = { | |||
constraints: { [name: string]: any} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
formatting (missing space before closing brace)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll be removing the file as the new implementation won't take any options
index.js
Outdated
fastify.addHook('onRoute', function hook(routeOptions) { | ||
routeOptions.constraints = { | ||
...options.constraints, | ||
...(routeOptions?.constraints || {}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
|| {} is unnecessary. destructuring a falsy value is a noop
test/index.test.js
Outdated
import fastifyConstraints from '../index.js' | ||
|
||
test('it should add constraints to all routes', async t => { | ||
t.plan(2) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is unnecessary, remove
test/index.test.js
Outdated
fastify.get('/', () => {}) | ||
fastify.post('/some-route', () => {}) | ||
|
||
t.equal( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: instead of comparing with true, you can simply use t.ok
test/index.test.js
Outdated
await fastify.register((instance, opts, done) => { | ||
fastify.get('/', () => {}) | ||
|
||
done() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess there's no need for the callback based registration syntax. Use an async function instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be better to allow
fastify.register(function(fastify, option) {
fastify.get('/', () => {})
}, { constraints: { foo: 'bar' } })
The plugin would not take any option and create a property to store the constraints
.
const symbol = Symbol.for('kConstraints')
fastify.decorate(symbol, null)
fastify.addHook('onRegister', function(instance, options) {
if(options.constraints) {
instance[symbol] = { ...instance[symbol], ...options.constraints }
}
})
fastify.addHook('onRoute', function(routeOptions) {
if(this[symbol]) {
routeOptions.constraints = { ...this[symbol], ...routeOptions.constraints }
}
})
Hi @climba03003 Thanks for pointing it out. I was already working on making it that way. It should work that way now. Let me know if you have any other comments |
index.js
Outdated
return | ||
} | ||
|
||
instance.addHook('onRoute', function hook(routeOptions) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if it is good or not. It may add a lot of onRoute
function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see your point, we would have as many onRoute
hooks as plugins with constraints. I think your way would be more efficient. Let me change that.
Co-authored-by: KaKa <climba03003@gmail.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM 🚀
examples/imported-routes.cjs
Outdated
@@ -0,0 +1,15 @@ | |||
const Fastify = require('fastify') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's remove the cjs ones
Core functionality added with an
onRoute
hook to add constraints to all routes declared after registration on the same or descendent scoperesolves fastify/fastify#3615