Skip to content

Commit 2746766

Browse files
authored
fix: return fastifyreply so async/await works as expected (#287)
1 parent bcc0ce5 commit 2746766

File tree

4 files changed

+24
-32
lines changed

4 files changed

+24
-32
lines changed

README.md

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ please see the [Podium documentation].
1111
## Installation
1212

1313
```bash
14-
$ npm install @podium/fastify-podlet
14+
npm install @podium/fastify-podlet
1515
```
1616

1717
## Requirements
@@ -40,16 +40,13 @@ app.register(fastifyPodletPlugin, podlet);
4040

4141
app.get(podlet.content(), async (request, reply) => {
4242
if (reply.app.podium.context.locale === 'nb-NO') {
43-
reply.podiumSend('<h2>Hei verden</h2>');
44-
return;
43+
return reply.podiumSend('<h2>Hei verden</h2>');
4544
}
46-
reply.podiumSend('<h2>Hello world</h2>');
47-
await reply;
45+
return reply.podiumSend('<h2>Hello world</h2>');
4846
});
4947

5048
app.get(podlet.manifest(), async (request, reply) => {
51-
reply.send(podlet);
52-
await reply;
49+
return reply.send(podlet);
5350
});
5451

5552
const start = async () => {
@@ -85,11 +82,9 @@ accessible inside a request handelers.
8582
```js
8683
app.get(podlet.content(), async (request, reply) => {
8784
if (reply.app.podium.context.locale === 'nb-NO') {
88-
reply.podiumSend('<h2>Hei verden</h2>');
89-
return;
85+
return reply.podiumSend('<h2>Hei verden</h2>');
9086
}
91-
reply.podiumSend('<h2>Hello world</h2>');
92-
await reply;
87+
return reply.podiumSend('<h2>Hello world</h2>');
9388
});
9489
```
9590

lib/podlet-plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default fp(
3535
// Decorate response with .podiumSend() method
3636
fastify.decorateReply('podiumSend', function podiumSend(payload) {
3737
this.type('text/html; charset=utf-8'); // "this" here is the fastify 'Reply' object
38-
this.send(
38+
return this.send(
3939
podlet.render(
4040
// @ts-ignore We decorate this above
4141
this.app.podium,

test/podlet-plugin.js

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,35 +28,33 @@ class Server {
2828

2929
this.app.get(podlet.content(), async (req, reply) => {
3030
if (reply.app.podium.context.locale === 'nb-NO') {
31-
reply.podiumSend('nb-NO');
32-
return;
31+
return reply.podiumSend('nb-NO');
3332
}
3433
if (reply.app.podium.context.locale === 'en-NZ') {
35-
reply.podiumSend('en-NZ');
36-
return;
34+
return reply.podiumSend('en-NZ');
3735
}
38-
reply.podiumSend('en-US');
36+
return reply.podiumSend('en-US');
3937
});
4038

4139
this.app.get(podlet.fallback(), async (req, reply) => {
42-
reply.podiumSend('fallback');
40+
return reply.podiumSend('fallback');
4341
});
4442

4543
this.app.get(podlet.manifest(), async (req, reply) => {
46-
reply.send(podlet);
44+
return reply.send(podlet);
4745
});
4846

4947
// Dummy endpoints for proxying
5048
this.app.get('/public', async (req, reply) => {
51-
reply.send('GET proxy target');
49+
return reply.send('GET proxy target');
5250
});
5351

5452
this.app.post('/public', async (req, reply) => {
55-
reply.send('POST proxy target');
53+
return reply.send('POST proxy target');
5654
});
5755

5856
this.app.put('/public', async (req, reply) => {
59-
reply.send('PUT proxy target');
57+
return reply.send('PUT proxy target');
6058
});
6159

6260
// Proxy to the dummy endpoints

types/podium.d.ts

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
import { HttpIncoming } from '@podium/utils';
22

33
declare module 'fastify' {
4-
5-
6-
interface PodiumHttpIncomingParameters {
4+
interface PodiumHttpIncomingParameters {
75
[key: string]: unknown;
86
}
97

@@ -13,30 +11,31 @@ declare module 'fastify' {
1311
[key: string]: unknown;
1412
}
1513

16-
interface PodiumHttpIncomingViewParameters {
14+
interface PodiumHttpIncomingViewParameters {
1715
[key: string]: unknown;
1816
}
1917

2018
interface PodiumLocals {
21-
podium: HttpIncoming<PodiumHttpIncomingParameters, PodiumHttpIncomingContext, PodiumHttpIncomingViewParameters>;
19+
podium: HttpIncoming<
20+
PodiumHttpIncomingParameters,
21+
PodiumHttpIncomingContext,
22+
PodiumHttpIncomingViewParameters
23+
>;
2224
}
2325

2426
interface FastifyReply {
2527
app: PodiumLocals;
2628

2729
/**
28-
* Calls the send / write method on the `http.ServerResponse` object.
29-
*
3030
* When in development mode this method will wrap the provided fragment in a
3131
* default HTML document before dispatching. When not in development mode, this
3232
* method will just dispatch the fragment.
3333
*
3434
* @example
3535
* app.get(podlet.content(), async (req, reply) => {
36-
* reply.podiumSend('<h1>Hello World</h1>');
37-
* await reply;
36+
* return reply.podiumSend('<h1>Hello World</h1>');
3837
* });
3938
*/
40-
podiumSend(fragment: string, ...args: unknown[]): Response;
39+
podiumSend(fragment: string, ...args: unknown[]): FastifyReply;
4140
}
4241
}

0 commit comments

Comments
 (0)