Skip to content

Commit c1685f4

Browse files
composedPlugin should have its own prefix context (#368)
1 parent 7e83838 commit c1685f4

File tree

9 files changed

+150
-3
lines changed

9 files changed

+150
-3
lines changed

index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,22 @@ const fastifyAutoload = async function autoload (fastify, options) {
9696
// encapsulate hooks at plugin level
9797
app.register(hookPlugin)
9898
}
99-
registerAllPlugins(app, pluginFiles)
99+
registerAllPlugins(app, pluginFiles, true)
100100
}
101-
fastify.register(composedPlugin)
101+
102+
fastify.register(composedPlugin, { prefix: options.options?.prefix ?? prefix })
102103
}
103104
}
104105

105-
function registerAllPlugins (app, pluginFiles) {
106+
function registerAllPlugins (app, pluginFiles, composed = false) {
106107
for (const pluginFile of pluginFiles) {
107108
// find plugins for this prefix, based on filename stored in registerPlugins()
108109
const plugin = metas.find((i) => i.filename === pluginFile.file)
110+
111+
if (composed) {
112+
plugin.options.prefix = undefined
113+
}
114+
109115
// register plugins at fastify level
110116
if (plugin) registerPlugin(app, plugin, pluginsMeta)
111117
}

test/issues/326/autohooks/index.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict'
2+
3+
const path = require('node:path')
4+
const autoLoad = require('../../../../')
5+
6+
module.exports = async function (fastify) {
7+
fastify.register(autoLoad, {
8+
dir: path.join(__dirname, '/routes-a'),
9+
autoHooks: true,
10+
cascadeHooks: true
11+
})
12+
13+
fastify.register(autoLoad, {
14+
dir: path.join(__dirname, '/routes-b'),
15+
autoHooks: true,
16+
cascadeHooks: true,
17+
options: { prefix: 'custom-prefix' }
18+
})
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = async function (app) {
2+
app.setNotFoundHandler((request, reply) => {
3+
reply.code(404)
4+
.header('from', 'routes-a/child')
5+
.send()
6+
});
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
module.exports = async function (app) {
4+
app.get('/', async function (req, reply) {
5+
reply.send()
6+
})
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = async function (app) {
2+
app.setNotFoundHandler((request, reply) => {
3+
reply.code(404)
4+
.header('from', 'routes-a/sibling')
5+
.send()
6+
});
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
module.exports = async function (app) {
4+
app.get('/', async function (req, reply) {
5+
reply.send()
6+
})
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = async function (app) {
2+
app.setNotFoundHandler((request, reply) => {
3+
reply.code(404)
4+
.header('from', 'routes-b')
5+
.send()
6+
});
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict'
2+
3+
module.exports = async function (app) {
4+
app.get('/', async function (req, reply) {
5+
reply.send()
6+
})
7+
}

test/issues/326/test.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
'use strict'
2+
3+
const t = require('tap')
4+
const Fastify = require('fastify')
5+
6+
t.plan(19)
7+
8+
const app = Fastify()
9+
10+
app.register(require('./autohooks'))
11+
12+
app.setNotFoundHandler((request, reply) => {
13+
reply.code(404)
14+
.header('from', 'root')
15+
.send()
16+
})
17+
18+
app.ready(function (err) {
19+
t.error(err)
20+
21+
app.inject({
22+
url: '/not-exists'
23+
}, function (err, res) {
24+
t.error(err)
25+
t.equal(res.headers.from, 'root')
26+
27+
t.equal(res.statusCode, 404)
28+
})
29+
30+
app.inject({
31+
url: '/child'
32+
}, function (err, res) {
33+
t.error(err)
34+
35+
t.equal(res.statusCode, 200)
36+
})
37+
38+
app.inject({
39+
url: '/child/not-exists'
40+
}, function (err, res) {
41+
t.error(err)
42+
t.equal(res.headers.from, 'routes-a/child')
43+
44+
t.equal(res.statusCode, 404)
45+
})
46+
47+
app.inject({
48+
url: '/sibling'
49+
}, function (err, res) {
50+
t.error(err)
51+
52+
t.equal(res.statusCode, 200)
53+
})
54+
55+
app.inject({
56+
url: '/sibling/not-exists'
57+
}, function (err, res) {
58+
t.error(err)
59+
t.equal(res.headers.from, 'routes-a/sibling')
60+
61+
t.equal(res.statusCode, 404)
62+
})
63+
64+
app.inject({
65+
url: '/custom-prefix'
66+
}, function (err, res) {
67+
t.error(err)
68+
69+
t.equal(res.statusCode, 200)
70+
})
71+
72+
app.inject({
73+
url: '/custom-prefix/not-exists'
74+
}, function (err, res) {
75+
t.error(err)
76+
t.equal(res.headers.from, 'routes-b')
77+
78+
t.equal(res.statusCode, 404)
79+
})
80+
})

0 commit comments

Comments
 (0)