Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jean-michelet/fastify-autoload in…
Browse files Browse the repository at this point in the history
…to v5-2
  • Loading branch information
jean-michelet committed May 15, 2024
2 parents 96f4db8 + 7026f13 commit 8139c73
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 2 deletions.
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@fastify/autoload",
"version": "5.8.1",
"version": "5.8.2",
"description": "Require all plugins in a directory",
"main": "index.js",
"type": "commonjs",
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 8139c73

Please sign in to comment.