Skip to content

Commit 078e872

Browse files
authored
fix: process incoming request and params later (#278)
1 parent 92e379f commit 078e872

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

lib/layout-plugin.js

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,36 @@ export default fp(
77
*/
88
(fastify, layout, done) => {
99
// Decorate reply with .app.podium we can write to throughout the request
10-
fastify.decorateReply('app', null);
10+
if (!fastify.hasReplyDecorator('app')) {
11+
fastify.decorateReply('app', null);
12+
}
13+
1114
fastify.addHook('onRequest', async (request, reply) => {
12-
// namespace
15+
// @ts-ignore We decorate this above
16+
reply.app = reply.app || {};
17+
1318
// @ts-ignore We type this for our consumers with fixup.sh
1419
reply.app = reply.app || {};
20+
1521
// used to pass additional values to HttpIncoming
1622
// @ts-ignore We type this for our consumers with fixup.sh
1723
reply.app.params = reply.app.params || {};
18-
// used to hold the HttpIncoming object
19-
// @ts-ignore We type this for our consumers with fixup.sh
20-
reply.app.podium = new HttpIncoming(
24+
});
25+
26+
// Run as late as we can (but before the route handler) so other plugins
27+
// can add to `reply.app.params` using either of the hooks in
28+
// the lifecycle before it https://fastify.dev/docs/latest/Reference/Hooks/#hooks
29+
fastify.addHook('preHandler', async (request, reply) => {
30+
const incoming = new HttpIncoming(
2131
request.raw,
2232
reply.raw,
2333
// @ts-ignore We type this for our consumers with fixup.sh
2434
reply.app.params,
2535
);
36+
37+
// used to hold the HttpIncoming object
2638
// @ts-ignore We type this for our consumers with fixup.sh
27-
reply.app.podium = await layout.process(reply.app.podium, {
39+
reply.app.podium = await layout.process(incoming, {
2840
proxy: false,
2941
});
3042
});

0 commit comments

Comments
 (0)