Skip to content

Commit

Permalink
Revert "Revert "fix: autohooks prefix concatenation (#378)""
Browse files Browse the repository at this point in the history
This reverts commit 1833842.
  • Loading branch information
jean-michelet authored Jun 17, 2024
1 parent 9db495a commit 86179c7
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 3 deletions.
7 changes: 6 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ 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) => {
Expand Down Expand Up @@ -85,7 +90,7 @@ const fastifyAutoload = async function autoload (fastify, options) {
}

fastify.register(composedPlugin, {
prefix: options.options?.prefix ?? replaceRouteParamPattern(prefix)
prefix: rootPrefix + replaceRouteParamPattern(prefix)
})
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/issues/326/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ app.ready(function (err) {
})

app.inject({
url: '/custom-prefix'
url: '/custom-prefix/child'
}, function (err, res) {
t.error(err)

t.equal(res.statusCode, 200)
})

app.inject({
url: '/custom-prefix/not-exists'
url: '/custom-prefix/child/not-exists'
}, function (err, res) {
t.error(err)
t.equal(res.headers.from, 'routes-b')
Expand Down
8 changes: 8 additions & 0 deletions test/issues/376/routes/entity/.autohooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict'

module.exports = async function (app, opts) {
app.addHook('onRequest', async (req, reply) => {
req.hooked = req.hooked || []
req.hooked.push('root')
})
}
7 changes: 7 additions & 0 deletions test/issues/376/routes/entity/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'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 })
})
}
7 changes: 7 additions & 0 deletions test/issues/376/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (app, opts, next) {
app.get('/', async function (req, reply) {
reply.status(200).send({ path: req.url })
})
}
58 changes: 58 additions & 0 deletions test/issues/376/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
'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'] })
})
})

0 comments on commit 86179c7

Please sign in to comment.