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: revert composing autohooks #382

Merged
merged 4 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 3 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ const fastifyAutoload = async function autoload (fastify, options) {
const pluginArray = [].concat.apply([], Object.values(pluginTree).map(o => o.plugins))
const hookArray = [].concat.apply([], Object.values(pluginTree).map(o => o.hooks))

let rootPrefix = options.options?.prefix ?? ''
// when prefix is provided /prefix/ format
// it is not good for concat, since most folder prefix start with /
if (rootPrefix[rootPrefix.length - 1] === '/') rootPrefix = rootPrefix.slice(0, -1)

await Promise.all(pluginArray.map(({ file, type, prefix }) => {
return loadPlugin({ file, type, directoryPrefix: prefix, options: opts, log: fastify.log })
.then((plugin) => {
Expand Down Expand Up @@ -86,24 +81,16 @@ const fastifyAutoload = async function autoload (fastify, options) {
// encapsulate hooks at plugin level
app.register(hookPlugin)
}
registerAllPlugins(app, pluginFiles, true)
registerAllPlugins(app, pluginFiles)
}

fastify.register(composedPlugin, {
prefix: rootPrefix + replaceRouteParamPattern(prefix)
})
fastify.register(composedPlugin)
}
}

function registerAllPlugins (app, pluginFiles, composed = false) {
function registerAllPlugins (app, pluginFiles) {
for (const pluginFile of pluginFiles) {
// find plugins for this prefix, based on filename stored in registerPlugins()
const plugin = metas.find((i) => i.filename === pluginFile.file)

if (composed) {
plugin.options.prefix = undefined
}

// register plugins at fastify level
if (plugin) registerPlugin(app, plugin, pluginsMeta)
}
Expand Down
19 changes: 0 additions & 19 deletions test/issues/326/autohooks/index.js

This file was deleted.

7 changes: 0 additions & 7 deletions test/issues/326/autohooks/routes-a/child/.autohooks.js

This file was deleted.

7 changes: 0 additions & 7 deletions test/issues/326/autohooks/routes-a/child/routes.js

This file was deleted.

7 changes: 0 additions & 7 deletions test/issues/326/autohooks/routes-a/sibling/.autohooks.js

This file was deleted.

7 changes: 0 additions & 7 deletions test/issues/326/autohooks/routes-a/sibling/routes.js

This file was deleted.

7 changes: 0 additions & 7 deletions test/issues/326/autohooks/routes-b/child/.autohooks.js

This file was deleted.

7 changes: 0 additions & 7 deletions test/issues/326/autohooks/routes-b/child/routes.js

This file was deleted.

80 changes: 0 additions & 80 deletions test/issues/326/test.js

This file was deleted.

3 changes: 2 additions & 1 deletion test/issues/374/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ const app = Fastify()
app.register(autoLoad, {
dir: path.join(__dirname, 'routes'),
autoHooks: true,
cascadeHooks: true
cascadeHooks: true,
routeParams: true
})

app.ready(function (err) {
Expand Down
Loading