diff --git a/index.js b/index.js index 47cfee7e..5309e27b 100644 --- a/index.js +++ b/index.js @@ -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) => { @@ -90,7 +85,7 @@ const fastifyAutoload = async function autoload (fastify, options) { } fastify.register(composedPlugin, { - prefix: rootPrefix + replaceRouteParamPattern(prefix) + prefix: options.options?.prefix ?? replaceRouteParamPattern(prefix) }) } } diff --git a/test/issues/326/test.js b/test/issues/326/test.js index ba8ab23a..58f4f99f 100644 --- a/test/issues/326/test.js +++ b/test/issues/326/test.js @@ -62,7 +62,7 @@ app.ready(function (err) { }) app.inject({ - url: '/custom-prefix/child' + url: '/custom-prefix' }, function (err, res) { t.error(err) @@ -70,7 +70,7 @@ app.ready(function (err) { }) app.inject({ - url: '/custom-prefix/child/not-exists' + url: '/custom-prefix/not-exists' }, function (err, res) { t.error(err) t.equal(res.headers.from, 'routes-b') diff --git a/test/issues/376/routes/entity/.autohooks.js b/test/issues/376/routes/entity/.autohooks.js deleted file mode 100644 index 951e9583..00000000 --- a/test/issues/376/routes/entity/.autohooks.js +++ /dev/null @@ -1,8 +0,0 @@ -'use strict' - -module.exports = async function (app, opts) { - app.addHook('onRequest', async (req, reply) => { - req.hooked = req.hooked || [] - req.hooked.push('root') - }) -} diff --git a/test/issues/376/routes/entity/index.js b/test/issues/376/routes/entity/index.js deleted file mode 100644 index 0b14399b..00000000 --- a/test/issues/376/routes/entity/index.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict' - -module.exports = async function (app, opts, next) { - app.get('/', async function (req, reply) { - reply.status(200).send({ path: req.url, hooked: req.hooked }) - }) -} diff --git a/test/issues/376/routes/index.js b/test/issues/376/routes/index.js deleted file mode 100644 index 51a699b4..00000000 --- a/test/issues/376/routes/index.js +++ /dev/null @@ -1,7 +0,0 @@ -'use strict' - -module.exports = async function (app, opts, next) { - app.get('/', async function (req, reply) { - reply.status(200).send({ path: req.url }) - }) -} diff --git a/test/issues/376/test.js b/test/issues/376/test.js deleted file mode 100644 index d1f42b2d..00000000 --- a/test/issues/376/test.js +++ /dev/null @@ -1,58 +0,0 @@ -'use strict' - -const t = require('tap') -const path = require('node:path') -const Fastify = require('fastify') -const autoLoad = require('../../../') - -t.plan(13) - -const app = Fastify() - -app.register(autoLoad, { - dir: path.join(__dirname, 'routes'), - autoHooks: true, - options: { prefix: '/api' } -}) - -app.register(autoLoad, { - dir: path.join(__dirname, 'routes'), - autoHooks: true, - options: { prefix: '/prefix/' } -}) - -app.ready(function (err) { - t.error(err) - - app.inject({ - url: '/api' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(res.json(), { path: '/api' }) - }) - - app.inject({ - url: '/api/entity' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(res.json(), { path: '/api/entity', hooked: ['root'] }) - }) - - app.inject({ - url: '/prefix' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(res.json(), { path: '/prefix' }) - }) - - app.inject({ - url: '/prefix/entity' - }, function (err, res) { - t.error(err) - t.equal(res.statusCode, 200) - t.same(res.json(), { path: '/prefix/entity', hooked: ['root'] }) - }) -})