Skip to content

Commit

Permalink
Revert "Revert "composedPlugin should have its own prefix context (#368
Browse files Browse the repository at this point in the history
…)""

This reverts commit 65c3dd1.
  • Loading branch information
jean-michelet authored Jun 17, 2024
1 parent bcca903 commit 5918421
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 3 deletions.
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,16 +81,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)
})
})

0 comments on commit 5918421

Please sign in to comment.