Skip to content

Documentation update for avvio PR 82#1498

Merged
mcollina merged 15 commits intofastify:masterfrom
alex-ppg:documentation-update
Mar 8, 2019
Merged

Documentation update for avvio PR 82#1498
mcollina merged 15 commits intofastify:masterfrom
alex-ppg:documentation-update

Conversation

@alex-ppg
Copy link
Copy Markdown
Contributor

@alex-ppg alex-ppg commented Mar 1, 2019

Hey everyone,

You can find some updated documentation to reflect the changes made to the plugin registration API for the options parameter. It refers to the avvio PR82. I believe it is clear enough but contributions are more than welcome!

Checklist

  • run npm run test and npm run benchmark
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message and code follows Code of conduct

Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Comment thread docs/Plugins-Guide.md Outdated
Comment thread docs/Plugins-Guide.md Outdated
Comment thread docs/Plugins-Guide.md Outdated
@alex-ppg
Copy link
Copy Markdown
Contributor Author

alex-ppg commented Mar 1, 2019

I integrated your comments, let me know your thoughts.

P.S. Plugins.md was updated as well in the initial commit, should I expand more / is it not clear enough?

Comment thread docs/Plugins.md Outdated
Comment thread docs/Plugins.md Outdated
Comment thread docs/Plugins.md Outdated
Copy link
Copy Markdown
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM

Comment thread test/plugin.test.js
@alex-ppg
Copy link
Copy Markdown
Contributor Author

alex-ppg commented Mar 5, 2019

An interesting and potentially unwanted behaviour of the avvio update has arose.

The fastify instance is passed by reference to the options argument of register and is the same reference as the one passed in to the plugin's function.

This means that it is possible to declare routes on the options argument, use decorate and have the changes reflected to the outer scope if the plugin's function is wrapped with fastify-plugin or even use decorate without fastify-plugin to inject variables that will be locally available to the plugin's function in the fastify instance.

This is showcased in the test suite but a quick example would be:

fastify.register((instance, options, next) => {
  console.log(instance.plugin_opts === 'hello') // Evaluates to true 
  instance.decorate('local', () => 'world')
}, parent => {
  parent.decorate('plugin_opts', 'hello')
  parent.get('/', (request, reply) => {
    reply.send({ hello: parent.local() }) // Will send { "hello": "world" }
  })
})

fastify.after(() => {
  console.log(fastify.plugin_opts === undefined) // Evaluates to true
  console.log(fastify.local === undefined) // Evaluates to true
})

Not sure if this is behaviour we want to remove as it is possible to accidentally declare an externally available plugin if the plugin's function is wrapped by fastify-plugin. Example:

fastify.register(fp((instance, options, next) => {
  instance.decorate('my_plugin', () => 'world')
}), parent => {
  parent.decorate('accidental_plugin', () => 'hello')
})

fastify.after(() => {
  console.log(fastify.my_plugin() === 'world') // Evaluates to true, as it should
  console.log(fastify.accidental_plugin() === 'hello') // Evaluates to true, potentially unwanted behaviour
})

The unwanted behaviour I believe could be removed by calling override once again within the avvio plugin and passing in an overridden instance. Looking forward to your thoughts.

@mcollina
Copy link
Copy Markdown
Member

mcollina commented Mar 5, 2019

I don't see problems with it.

cc @fastify/fastify what do you think?

@delvedor
Copy link
Copy Markdown
Member

delvedor commented Mar 5, 2019

I don't see problems with it.
cc @fastify/fastify what do you think?

No problem for me either.

@delvedor delvedor added documentation Improvements or additions to documentation test Issue or pr related to our testing infrastructure. labels Mar 5, 2019
@github-actions
Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Feb 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

documentation Improvements or additions to documentation test Issue or pr related to our testing infrastructure.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants