Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: process incoming request and params later #278

Merged
merged 1 commit into from
Sep 23, 2024
Merged
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
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