Skip to content

Commit

Permalink
fix: return the FastifyReply so async works as expected (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
wkillerud authored Sep 23, 2024
1 parent bce0ca7 commit 2d919ab
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 7 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ app.register(FastifyLayout, layout);
app.get(layout.pathname(), async (request, reply) => {
const incoming = reply.app.podium;
const result = await podlet.fetch(incoming);
reply.podiumSend(result.content);
await reply;
return reply.podiumSend(result.content);
});

const start = async () => {
Expand Down
11 changes: 9 additions & 2 deletions lib/layout-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,15 @@ export default fp(
*/
function podiumSend(payload, ...args) {
this.type('text/html; charset=utf-8'); // "this" here is the fastify 'Reply' object
// @ts-ignore We type this for our consumers with fixup.sh
this.send(layout.render(this.app.podium, payload, ...args));
return this.send(
layout.render(
// @ts-ignore We type this for our consumers with fixup.sh
this.app.podium,
// @ts-ignore
payload,
...args,
),
);
},
);

Expand Down
2 changes: 1 addition & 1 deletion tests/layout-plugin.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Server {

this.app.get(layout.pathname(), async (req, reply) => {
const result = await podlet.fetch(reply.app.podium);
reply.podiumSend(result);
return reply.podiumSend(result);
});
}

Expand Down
3 changes: 1 addition & 2 deletions types/podium.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,9 @@ declare module 'fastify' {
*
* @param markup The HTML contents of the document
* @param args Parameters sent to the template function
* @returns {void} Sends the FastifyReply
*
* @see https://podium-lib.io/docs/api/layout#respodiumsendfragment
*/
podiumSend: (markup: unknown, ...args: unknown[]) => void;
podiumSend(markup: unknown, ...args: unknown[]): FastifyReply;
}
}

0 comments on commit 2d919ab

Please sign in to comment.