diff --git a/index.js b/index.js index 81260671..93fd8b34 100644 --- a/index.js +++ b/index.js @@ -96,16 +96,22 @@ const fastifyAutoload = async function autoload (fastify, options) { // encapsulate hooks at plugin level app.register(hookPlugin) } - registerAllPlugins(app, pluginFiles) + registerAllPlugins(app, pluginFiles, true) } - fastify.register(composedPlugin) + + fastify.register(composedPlugin, { prefix: options.options?.prefix ?? prefix }) } } - function registerAllPlugins (app, pluginFiles) { + function registerAllPlugins (app, pluginFiles, composed = false) { 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) } diff --git a/test/issues/326/autohooks/index.js b/test/issues/326/autohooks/index.js new file mode 100644 index 00000000..93d93f27 --- /dev/null +++ b/test/issues/326/autohooks/index.js @@ -0,0 +1,19 @@ +'use strict' + +const path = require('node:path') +const autoLoad = require('../../../../') + +module.exports = async function (fastify) { + fastify.register(autoLoad, { + dir: path.join(__dirname, '/routes-a'), + autoHooks: true, + cascadeHooks: true + }) + + fastify.register(autoLoad, { + dir: path.join(__dirname, '/routes-b'), + autoHooks: true, + cascadeHooks: true, + options: { prefix: 'custom-prefix' } + }) +} diff --git a/test/issues/326/autohooks/routes-a/child/.autohooks.js b/test/issues/326/autohooks/routes-a/child/.autohooks.js new file mode 100644 index 00000000..37e7bed8 --- /dev/null +++ b/test/issues/326/autohooks/routes-a/child/.autohooks.js @@ -0,0 +1,7 @@ +module.exports = async function (app) { + app.setNotFoundHandler((request, reply) => { + reply.code(404) + .header('from', 'routes-a/child') + .send() + }); +} diff --git a/test/issues/326/autohooks/routes-a/child/routes.js b/test/issues/326/autohooks/routes-a/child/routes.js new file mode 100644 index 00000000..8d16accd --- /dev/null +++ b/test/issues/326/autohooks/routes-a/child/routes.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = async function (app) { + app.get('/', async function (req, reply) { + reply.send() + }) +} diff --git a/test/issues/326/autohooks/routes-a/sibling/.autohooks.js b/test/issues/326/autohooks/routes-a/sibling/.autohooks.js new file mode 100644 index 00000000..3d63b457 --- /dev/null +++ b/test/issues/326/autohooks/routes-a/sibling/.autohooks.js @@ -0,0 +1,7 @@ +module.exports = async function (app) { + app.setNotFoundHandler((request, reply) => { + reply.code(404) + .header('from', 'routes-a/sibling') + .send() + }); +} diff --git a/test/issues/326/autohooks/routes-a/sibling/routes.js b/test/issues/326/autohooks/routes-a/sibling/routes.js new file mode 100644 index 00000000..8d16accd --- /dev/null +++ b/test/issues/326/autohooks/routes-a/sibling/routes.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = async function (app) { + app.get('/', async function (req, reply) { + reply.send() + }) +} diff --git a/test/issues/326/autohooks/routes-b/child/.autohooks.js b/test/issues/326/autohooks/routes-b/child/.autohooks.js new file mode 100644 index 00000000..a002c29b --- /dev/null +++ b/test/issues/326/autohooks/routes-b/child/.autohooks.js @@ -0,0 +1,7 @@ +module.exports = async function (app) { + app.setNotFoundHandler((request, reply) => { + reply.code(404) + .header('from', 'routes-b') + .send() + }); +} diff --git a/test/issues/326/autohooks/routes-b/child/routes.js b/test/issues/326/autohooks/routes-b/child/routes.js new file mode 100644 index 00000000..8d16accd --- /dev/null +++ b/test/issues/326/autohooks/routes-b/child/routes.js @@ -0,0 +1,7 @@ +'use strict' + +module.exports = async function (app) { + app.get('/', async function (req, reply) { + reply.send() + }) +} diff --git a/test/issues/326/test.js b/test/issues/326/test.js new file mode 100644 index 00000000..58f4f99f --- /dev/null +++ b/test/issues/326/test.js @@ -0,0 +1,80 @@ +'use strict' + +const t = require('tap') +const Fastify = require('fastify') + +t.plan(19) + +const app = Fastify() + +app.register(require('./autohooks')) + +app.setNotFoundHandler((request, reply) => { + reply.code(404) + .header('from', 'root') + .send() +}) + +app.ready(function (err) { + t.error(err) + + app.inject({ + url: '/not-exists' + }, function (err, res) { + t.error(err) + t.equal(res.headers.from, 'root') + + t.equal(res.statusCode, 404) + }) + + app.inject({ + url: '/child' + }, function (err, res) { + t.error(err) + + t.equal(res.statusCode, 200) + }) + + app.inject({ + url: '/child/not-exists' + }, function (err, res) { + t.error(err) + t.equal(res.headers.from, 'routes-a/child') + + t.equal(res.statusCode, 404) + }) + + app.inject({ + url: '/sibling' + }, function (err, res) { + t.error(err) + + t.equal(res.statusCode, 200) + }) + + app.inject({ + url: '/sibling/not-exists' + }, function (err, res) { + t.error(err) + t.equal(res.headers.from, 'routes-a/sibling') + + t.equal(res.statusCode, 404) + }) + + app.inject({ + url: '/custom-prefix' + }, function (err, res) { + t.error(err) + + t.equal(res.statusCode, 200) + }) + + app.inject({ + url: '/custom-prefix/not-exists' + }, function (err, res) { + t.error(err) + t.equal(res.headers.from, 'routes-b') + + t.equal(res.statusCode, 404) + }) +})