From 65c3dd1f1d90aa6bb1deb57f631fcbe4c860d54f Mon Sep 17 00:00:00 2001 From: KaKa Date: Mon, 17 Jun 2024 12:15:51 +0800 Subject: [PATCH] Revert "composedPlugin should have its own prefix context (#368)" This reverts commit c1685f4b7a971c29096dad7ed6f753d5e3040a40. --- index.js | 12 +-- test/issues/326/autohooks/index.js | 19 ----- .../autohooks/routes-a/child/.autohooks.js | 7 -- .../326/autohooks/routes-a/child/routes.js | 7 -- .../autohooks/routes-a/sibling/.autohooks.js | 7 -- .../326/autohooks/routes-a/sibling/routes.js | 7 -- .../autohooks/routes-b/child/.autohooks.js | 7 -- .../326/autohooks/routes-b/child/routes.js | 7 -- test/issues/326/test.js | 80 ------------------- 9 files changed, 3 insertions(+), 150 deletions(-) delete mode 100644 test/issues/326/autohooks/index.js delete mode 100644 test/issues/326/autohooks/routes-a/child/.autohooks.js delete mode 100644 test/issues/326/autohooks/routes-a/child/routes.js delete mode 100644 test/issues/326/autohooks/routes-a/sibling/.autohooks.js delete mode 100644 test/issues/326/autohooks/routes-a/sibling/routes.js delete mode 100644 test/issues/326/autohooks/routes-b/child/.autohooks.js delete mode 100644 test/issues/326/autohooks/routes-b/child/routes.js delete mode 100644 test/issues/326/test.js diff --git a/index.js b/index.js index 5f9dd358..0714843b 100644 --- a/index.js +++ b/index.js @@ -81,22 +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: options.options?.prefix ?? 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) } diff --git a/test/issues/326/autohooks/index.js b/test/issues/326/autohooks/index.js deleted file mode 100644 index 93d93f27..00000000 --- a/test/issues/326/autohooks/index.js +++ /dev/null @@ -1,19 +0,0 @@ -'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 deleted file mode 100644 index 37e7bed8..00000000 --- a/test/issues/326/autohooks/routes-a/child/.autohooks.js +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index 8d16accd..00000000 --- a/test/issues/326/autohooks/routes-a/child/routes.js +++ /dev/null @@ -1,7 +0,0 @@ -'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 deleted file mode 100644 index 3d63b457..00000000 --- a/test/issues/326/autohooks/routes-a/sibling/.autohooks.js +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index 8d16accd..00000000 --- a/test/issues/326/autohooks/routes-a/sibling/routes.js +++ /dev/null @@ -1,7 +0,0 @@ -'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 deleted file mode 100644 index a002c29b..00000000 --- a/test/issues/326/autohooks/routes-b/child/.autohooks.js +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index 8d16accd..00000000 --- a/test/issues/326/autohooks/routes-b/child/routes.js +++ /dev/null @@ -1,7 +0,0 @@ -'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 deleted file mode 100644 index 58f4f99f..00000000 --- a/test/issues/326/test.js +++ /dev/null @@ -1,80 +0,0 @@ -'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) - }) -})