Skip to content

Commit

Permalink
fix: process incoming request and params later (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud authored Sep 23, 2024
1 parent 92e379f commit 078e872
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions lib/layout-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,36 @@ export default fp(
*/
(fastify, layout, done) => {
// Decorate reply with .app.podium we can write to throughout the request
fastify.decorateReply('app', null);
if (!fastify.hasReplyDecorator('app')) {
fastify.decorateReply('app', null);
}

fastify.addHook('onRequest', async (request, reply) => {
// namespace
// @ts-ignore We decorate this above
reply.app = reply.app || {};

// @ts-ignore We type this for our consumers with fixup.sh
reply.app = reply.app || {};

// used to pass additional values to HttpIncoming
// @ts-ignore We type this for our consumers with fixup.sh
reply.app.params = reply.app.params || {};
// used to hold the HttpIncoming object
// @ts-ignore We type this for our consumers with fixup.sh
reply.app.podium = new HttpIncoming(
});

// Run as late as we can (but before the route handler) so other plugins
// can add to `reply.app.params` using either of the hooks in
// the lifecycle before it https://fastify.dev/docs/latest/Reference/Hooks/#hooks
fastify.addHook('preHandler', async (request, reply) => {
const incoming = new HttpIncoming(
request.raw,
reply.raw,
// @ts-ignore We type this for our consumers with fixup.sh
reply.app.params,
);

// used to hold the HttpIncoming object
// @ts-ignore We type this for our consumers with fixup.sh
reply.app.podium = await layout.process(reply.app.podium, {
reply.app.podium = await layout.process(incoming, {
proxy: false,
});
});
Expand Down

0 comments on commit 078e872

Please sign in to comment.