Skip to content

Commit 194aced

Browse files
committed
fix: parsing not working on fastify-plugin
1 parent a1e2c4a commit 194aced

File tree

4 files changed

+51
-8
lines changed

4 files changed

+51
-8
lines changed

src/__tests__/common.test.ts

+19-2
Original file line numberDiff line numberDiff line change
@@ -75,17 +75,34 @@ describe('common', () => {
7575
message,
7676
handler,
7777
parser: data => parser(data),
78-
context: { hello: 'there' },
7978
log: {} as pino.Logger,
8079
});
8180

8281
expect(handler).toHaveBeenCalledWith({
8382
message,
8483
data: '',
85-
context: { hello: 'there' },
8684
log: {} as pino.Logger,
8785
});
8886

8987
expect(parser).toHaveBeenCalledWith({});
9088
});
89+
90+
it('should throw when parser fails', async () => {
91+
const message = createPubSubdata({}, true);
92+
const handler = jest.fn();
93+
const parser = jest.fn(_ => {
94+
throw new Error('error');
95+
});
96+
97+
const result = () => {
98+
return handlePubSubMessage({
99+
message,
100+
handler,
101+
parser: data => parser(data),
102+
log: {} as pino.Logger,
103+
});
104+
};
105+
106+
expect(result).rejects.toThrowErrorMatchingInlineSnapshot(`"error"`);
107+
});
91108
});

src/__tests__/fastify-plugin.test.ts

+26
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import Fastify, { FastifyInstance } from 'fastify';
22
import { pubSubFastifyPlugin } from '../methods/fastify-plugin';
3+
import { PubSubConfig } from '../types';
34
import { createPubSubRequest } from './fixtures';
45

56
describe('fastify-plugin', () => {
@@ -94,6 +95,31 @@ describe('fastify-plugin', () => {
9495
);
9596
});
9697

98+
it('should throw when parsing fails', async () => {
99+
const handler = jest.fn(() => ({ hello: 'world' }));
100+
const parser = jest.fn(() => {
101+
throw new Error('parsing fail');
102+
});
103+
const payload = createPubSubRequest('forward me');
104+
105+
app.register(pubSubFastifyPlugin, { handler, parser } as PubSubConfig<
106+
any,
107+
any
108+
>);
109+
110+
const res = await app.inject({
111+
method: 'POST',
112+
url: '/',
113+
payload,
114+
});
115+
116+
expect(parser).toHaveBeenCalled();
117+
118+
expect(res.payload).toMatchInlineSnapshot(
119+
`"{\\"statusCode\\":500,\\"error\\":\\"Internal Server Error\\",\\"message\\":\\"parsing fail\\"}"`,
120+
);
121+
});
122+
97123
it('should not throw if request is missing attributes', async () => {
98124
const handler = jest.fn();
99125
const payload = {

src/methods/fastify-plugin.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const pubSubFastifyPluginFn = async <Data, Context>(
77
fastify: FastifyInstance,
88
options: PubSubConfig<Data, Context>,
99
): Promise<void> => {
10-
const { path = '/', handler, parseJson, onError } = options;
10+
const { path = '/', handler, parseJson, onError, parser } = options;
1111
fastify.post<{
1212
Body: PubSubRequestType;
1313
}>(
@@ -20,22 +20,22 @@ const pubSubFastifyPluginFn = async <Data, Context>(
2020
},
2121
async (req, reply) => {
2222
try {
23-
const res = await handlePubSubMessage({
23+
const res = await handlePubSubMessage<Data, Context>({
24+
parser,
2425
message: req.body.message,
2526
handler,
2627
parseJson,
2728
// context: req,
2829
log: req.log,
2930
});
3031

31-
reply.code(res?.statusCode || 204);
32+
return reply.code(res?.statusCode || 204).send();
3233
} catch (error) {
3334
if (onError) {
3435
await onError(error);
35-
reply.code(204);
36+
return reply.code(204).send();
3637
} else throw error;
3738
}
38-
reply.send();
3939
},
4040
);
4141
};

src/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export interface HandlePubSubMessageArgs<Data, Context> {
6363
message: PubSubMessageType;
6464
handler: PubSubHandler<Data, Context>;
6565
parseJson?: boolean;
66-
parser?: (data: unknown) => Data;
66+
parser?: (data: unknown) => Data | Promise<Data>;
6767
context?: Context;
6868
log?: pino.Logger | FastifyBaseLogger;
6969
}

0 commit comments

Comments
 (0)