Plugin encapsulation #2735
-
Hello - thanks for the amazing library! Like many, I've been heads-down using Express for years, but this has made writing Nodejs apps exciting again :) Quick question regarding scope - I've noticed that when writing plugins (routes), something like this: const plugin = (fastify, opts) => {
fastify.post("/", async function (request, reply) {
const data = await getSomeData()
return someSpecialCleaner(data)
})
}
function someSpecialCleaner(data) {
// do stuff
} Does work in development, but does not work in production. In production, I have to bring the function into the scope of the plugin itself? Why is that? I'm guessing it has something to do with the encapsulation, but I was just curious on the discrepancy between the two environments. Also, I know I could throw that logic into a hook, or decorate it but it's a one-off so I figured just keeping it as a standalone made most sense. Any direction pointing would be greatly appreciated. Thanks again! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 7 replies
-
Did you read https://github.com/fastify/fastify/blob/master/docs/Encapsulation.md already? Not sure if it answers all of your questions, but it might help. |
Beta Was this translation helpful? Give feedback.
-
Fastify's encapsulation does not deal with any environment variables. The code you have provided is registering an asynchronous plugin without a callback. So it's rather surprising that it ever works. I imagine what you really want is: const plugin = (fastify, opts, done) => {
fastify.post("/", async function (request, reply) {
const data = await getSomeData()
return someSpecialCleaner(data)
})
done()
}
function someSpecialCleaner(data) {
// do stuff
} |
Beta Was this translation helpful? Give feedback.
-
Thanks both @kibertoad and @jsumners . The response time was amazing. The verdict is that this has nothing to do with encapsulation, and everything to do with user error. I'll be careful out there with my novice Typescript transpilation skills. Thanks again for all the hard work - this framework is the future, baby 🎸 ❗ |
Beta Was this translation helpful? Give feedback.
-
You are welcome! Glad you are using fastify. |
Beta Was this translation helpful? Give feedback.
Fastify's encapsulation does not deal with any environment variables. The code you have provided is registering an asynchronous plugin without a callback. So it's rather surprising that it ever works. I imagine what you really want is: