Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ function registerNode (node, fastify) {
}

function findCommonHooksPrefix (node) {
const prefixes = Object.values(node.pluginsMeta)
.map((meta) => meta?.options?.prefix)
const prefixes = node.plugins
.map((plugin) => node.pluginsMeta[plugin.file]?.options?.prefix)
.filter((prefix) => typeof prefix === 'string' && prefix.length > 0)

if (prefixes.length === 0) {
Expand Down
32 changes: 32 additions & 0 deletions test/commonjs/autohooks-basic-mini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'use strict'

const { after, before, describe, it } = require('node:test')
const assert = require('node:assert')
const Fastify = require('fastify')

describe('Node test suite for autohooks-basic', function () {
const app = Fastify()
before(async function () {
app.register(require('./autohooks/basic-mini'))
app.decorateRequest('hooked', '')
await app.ready()
})

after(async function () {
await app.close()
})

it('should respond correctly to /', async function () {
const res = await app.inject({ url: '/' })

assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(JSON.parse(res.payload), { hooked: ['root'] })
})

it('should respond correctly to /child', async function () {
const res = await app.inject({ url: '/child' })

assert.strictEqual(res.statusCode, 200)
assert.deepStrictEqual(JSON.parse(res.payload), { hooked: '' })
})
})
15 changes: 15 additions & 0 deletions test/commonjs/autohooks/basic-mini.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use strict'

const path = require('node:path')
const autoLoad = require('../../../')

module.exports = function (fastify, opts, next) {
fastify.log.error(__dirname)

fastify.register(autoLoad, {
dir: path.join(__dirname, 'routes-mini'),
autoHooks: true
})

next()
}
6 changes: 6 additions & 0 deletions test/commonjs/autohooks/routes-mini/.autohooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
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/commonjs/autohooks/routes-mini/child/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (app, opts) {
app.get('/', async function (req, reply) {
reply.status(200).send({ hooked: req.hooked })
})
}
7 changes: 7 additions & 0 deletions test/commonjs/autohooks/routes-mini/routes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
'use strict'

module.exports = async function (app, opts) {
app.get('/', async function (req, reply) {
reply.status(200).send({ hooked: req.hooked })
})
}
Loading