Skip to content
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

Merged
merged 11 commits into from Nov 18, 2022
Merged

feat: core functionality added #1

merged 11 commits into from Nov 18, 2022

Conversation

Ceres6
Copy link
Contributor

@Ceres6 Ceres6 commented Nov 16, 2022

Core functionality added with an onRoute hook to add constraints to all routes declared after registration on the same or descendent scope

resolves fastify/fastify#3615

@Ceres6 Ceres6 marked this pull request as ready for review November 16, 2022 15:28
@Ceres6 Ceres6 self-assigned this Nov 16, 2022
@Ceres6 Ceres6 marked this pull request as draft November 17, 2022 08:05
Copy link
Member

@simoneb simoneb left a 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

@@ -2,3 +2,4 @@
. "$(dirname -- "$0")/_/husky.sh"

npx lint-staged
npm test
Copy link
Member

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 Show resolved Hide resolved
README.md Outdated

## Usage

Register fastifyConstraints as a Fastify plugin.
Copy link
Member

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
Copy link
Member

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}
Copy link
Member

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)

Copy link
Contributor Author

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 || {})
Copy link
Member

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

import fastifyConstraints from '../index.js'

test('it should add constraints to all routes', async t => {
t.plan(2)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is unnecessary, remove

fastify.get('/', () => {})
fastify.post('/some-route', () => {})

t.equal(
Copy link
Member

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

await fastify.register((instance, opts, done) => {
fastify.get('/', () => {})

done()
Copy link
Member

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

Copy link
Contributor

@climba03003 climba03003 left a 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 }
  }
})

@Ceres6
Copy link
Contributor Author

Ceres6 commented Nov 17, 2022

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

@Ceres6 Ceres6 marked this pull request as ready for review November 17, 2022 09:57
index.js Outdated Show resolved Hide resolved
index.js Outdated
return
}

instance.addHook('onRoute', function hook(routeOptions) {
Copy link
Contributor

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.

Copy link
Contributor Author

@Ceres6 Ceres6 Nov 17, 2022

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.

Copy link
Member

@simoneb simoneb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 🚀

@@ -0,0 +1,15 @@
const Fastify = require('fastify')
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support constraints when registering plugins
3 participants