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

composedPlugin should have its own prefix context #368

Merged
merged 10 commits into from
May 2, 2024
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
19 changes: 19 additions & 0 deletions test/issues/326/autohooks/index.js
Original file line number Diff line number Diff line change
@@ -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' }
})
}
7 changes: 7 additions & 0 deletions test/issues/326/autohooks/routes-a/child/.autohooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = async function (app) {
app.setNotFoundHandler((request, reply) => {
reply.code(404)
.header('from', 'routes-a/child')
.send()
});
}
7 changes: 7 additions & 0 deletions test/issues/326/autohooks/routes-a/child/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (app) {
app.get('/', async function (req, reply) {
reply.send()
})
}
7 changes: 7 additions & 0 deletions test/issues/326/autohooks/routes-a/sibling/.autohooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = async function (app) {
app.setNotFoundHandler((request, reply) => {
reply.code(404)
.header('from', 'routes-a/sibling')
.send()
});
}
7 changes: 7 additions & 0 deletions test/issues/326/autohooks/routes-a/sibling/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (app) {
app.get('/', async function (req, reply) {
reply.send()
})
}
7 changes: 7 additions & 0 deletions test/issues/326/autohooks/routes-b/child/.autohooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module.exports = async function (app) {
app.setNotFoundHandler((request, reply) => {
reply.code(404)
.header('from', 'routes-b')
.send()
});
}
7 changes: 7 additions & 0 deletions test/issues/326/autohooks/routes-b/child/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (app) {
app.get('/', async function (req, reply) {
reply.send()
})
}
80 changes: 80 additions & 0 deletions test/issues/326/test.js
Original file line number Diff line number Diff line change
@@ -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)
})
})