Skip to content

Commit

Permalink
fix: autohooks prefix (#375)
Browse files Browse the repository at this point in the history
  • Loading branch information
climba03003 committed May 6, 2024
1 parent 07f6ece commit 64e6bf2
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 1 deletion.
4 changes: 3 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ const fastifyAutoload = async function autoload (fastify, options) {
registerAllPlugins(app, pluginFiles, true)
}

fastify.register(composedPlugin, { prefix: options.options?.prefix ?? prefix })
fastify.register(composedPlugin, {
prefix: options.options?.prefix ?? replaceRouteParamPattern(prefix)
})
}
}

Expand Down
8 changes: 8 additions & 0 deletions test/issues/374/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/374/routes/entity/_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, params: req.params })
})
}
7 changes: 7 additions & 0 deletions test/issues/374/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/374/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 })
})
}
44 changes: 44 additions & 0 deletions test/issues/374/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
'use strict'

const t = require('tap')
const path = require('node:path')
const Fastify = require('fastify')
const autoLoad = require('../../../')

t.plan(10)

const app = Fastify()

app.register(autoLoad, {
dir: path.join(__dirname, 'routes'),
autoHooks: true,
cascadeHooks: true
})

app.ready(function (err) {
t.error(err)

app.inject({
url: '/'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.same(res.json(), { path: '/' })
})

app.inject({
url: '/entity'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.same(res.json(), { path: '/entity', hooked: ['root'] })
})

app.inject({
url: '/entity/1'
}, function (err, res) {
t.error(err)
t.equal(res.statusCode, 200)
t.same(res.json(), { path: '/entity/1', hooked: ['root'], params: { entity: 1 } })
})
})

0 comments on commit 64e6bf2

Please sign in to comment.