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

fix: should ensure can access all decorators from app built with helper.build #742

Merged
merged 7 commits into from
Jul 25, 2024
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,8 @@ const { test } = require('tap')
test('test my application', async t => {
const argv = ['app.js']
const app = await build(argv, {
extraParam: 'foo'
extraParam: 'foo',
skipOverride: true // If you want your application to be registered with fastify-plugin
})
t.teardown(() => app.close())

Expand Down
1 change: 1 addition & 0 deletions helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
enumerable: false,
writable: false
})

return runFastify(args, additionalOptions, serverOptions)
},
listen: runFastify
Expand Down
6 changes: 5 additions & 1 deletion start.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const {
showHelpForCommand,
isKubernetes
} = require('./util')
const fp = require('fastify-plugin')

let Fastify = null

Expand Down Expand Up @@ -170,7 +171,10 @@ async function runFastify (args, additionalOptions, serverOptions) {
}

const appConfig = Object.assign({}, opts.options ? file.options : {}, opts.pluginOptions, additionalOptions)
await fastify.register(file.default || file, appConfig)

const appFn = file.default || file
const appPlugin = appConfig.skipOverride ? fp(appFn) : appFn
await fastify.register(appPlugin, appConfig)
Comment on lines +175 to +177
Copy link
Contributor Author

@jean-michelet jean-michelet Jul 24, 2024

Choose a reason for hiding this comment

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

I am not sure it's possible to reset the fp wrapping without breaking the existing. We can allow a skipOverride option here. On the other hand, I don't know if it's a good idea to pass it in additionalOptions, if a user is using a skipOverride option that does something different in the app, it could be an unexpected side effect, right? Altough seems very unlikely.

I can avoid this with a destructuration:

const { skipOverride, ...config } = appConfig
await fastify.register(appPlugin, config)

We can also pass a fourth argument to runFastify, but it seems a bit overkill.

Any suggestion?


const closeListeners = closeWithGrace({ delay: opts.closeGraceDelay }, async function ({ signal, err, manual }) {
if (err) {
Expand Down
11 changes: 11 additions & 0 deletions test/helper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,14 @@ test('should merge the CLI and FILE configs', async t => {
t.same(lines.length, 1)
t.same(lines[0].foo, '***')
})

test('should ensure can access all decorators', async t => {
const argv = ['./examples/plugin.js']
const app = await helper.build(argv, { skipOverride: true })
t.teardown(() => app.close())
t.ok(app.test)

const app2 = await helper.listen(argv, { skipOverride: true })
t.teardown(() => app2.close())
t.ok(app2.test)
})