Skip to content

Commit 25da846

Browse files
authored
refactor!: Remove createPubSubServer (#255)
1 parent 62da724 commit 25da846

18 files changed

+252
-386
lines changed

README.md

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
11
# PubSub HTTP Handler
22

3-
![Build & Deploy][build-badge] [![Total alerts][lgtm-badge]][lgtm-alerts]
4-
[![LGTM Grade][lgtm-grade]][lgtm-alerts]
5-
[![Maintainability][codeclimate-badge]][codeclimate]
6-
73
![PubSub HTTP Handler](.github/header.jpg)
84

95
PubSub HTTP Handler is a simple Typescript/Javascript package that solves
10-
serving an HTTP endpoint that can consume PubSub messages. It is intended to use
11-
with Google Cloud Functions or Google Cloud Run.
6+
serving an HTTP endpoint that can consume PubSub messages. To do this task we
7+
utilize the [Fastify][] framework or Google Cloud Functions.
128

13-
This package was built so that when creating microservices that subscribes to a
9+
This package was built so that when creating microservices that subscribe to a
1410
PubSub-topic, we don't have to implement a server or validate the request.
1511

16-
The package utilizes [Fastify][] to serve HTTP.
17-
1812
## Quickstart
1913

2014
```shell
2115
▶ yarn add pubsub-http-handler
2216
```
2317

24-
## Example
18+
## Example with Fastify Server
2519

2620
```typescript
27-
import { createPubSubServer, PubSubHandler } from 'pubsub-http-handler';
21+
import Fastify from 'fastify';
22+
import { pubSubFastifyPlugin, PubSubHandler } from 'pubsub-http-handler';
2823

2924
interface MyHandler {
3025
hello: string;
@@ -38,19 +33,34 @@ const server = async () => {
3833
// ...
3934
};
4035

41-
const { listen } = await createPubSubServer(handler);
42-
await listen();
36+
const app = Fastify().withTypeProvider<TypeBoxTypeProvider>();
37+
38+
app.register(pubSubFastifyPlugin, makePubSubConfig({ handler }));
39+
40+
await app.listen(8000);
4341
};
4442
```
4543

46-
We also support `fastify-plugin` and `cloud-functions`. See [more examples in
47-
the `examples/` folder][examples].
44+
## Example with Google Cloud Functions
45+
46+
```typescript
47+
import { pubSubCloudFunction, PubSubHandler } from 'pubsub-http-handler';
48+
49+
interface MyHandler {
50+
hello: string;
51+
world: string;
52+
}
53+
54+
export const helloWorld = createPubSubCloudFunctions<MyHandler>(
55+
({ data, log }) => {
56+
log.info(`Hello, ${data.name}`);
57+
},
58+
);
59+
```
4860

4961
Read more about [configuration here][configuration] or check out the [API
5062
documentation][docs]
5163

52-
[examples]: ./examples
53-
5464
## Options
5565

5666
- `onError` (function, default is undefined). Use to ensure that the function
@@ -76,19 +86,6 @@ We love contributions! 🙏 Bug reports and pull requests are welcome on [GitHub
7686

7787
[banner]: ./assets/banner.jpg
7888
[npm]: https://www.npmjs.com/package/pubsub-http-handler
79-
[build-badge]:
80-
https://img.shields.io/github/actions/workflow/status/cobraz/pubsub-http-handler/workflow.yml?branch=main&style=flat-square
81-
[codeclimate-badge]:
82-
https://img.shields.io/codeclimate/maintainability/cobraz/pubsub-http-handler?style=flat-square
83-
[codeclimate]:
84-
https://codeclimate.com/github/cobraz/pubsub-http-handler/maintainability
85-
[lgtm-badge]:
86-
https://img.shields.io/lgtm/alerts/g/cobraz/pubsub-http-handler.svg?logo=lgtm&logoWidth=18?style=flat-square
87-
[lgtm-alerts]: https://lgtm.com/projects/g/cobraz/pubsub-http-handler/alerts/
88-
[lgtm-grade]:
89-
https://img.shields.io/lgtm/grade/javascript/github/cobraz/pubsub-http-handler?style=flat-square
90-
[semantic-release-badge]:
91-
https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg?style=flat-square
9289
[fastify]: https://www.fastify.io/
9390
[configuration]: ./docs/interfaces/pubsubconfig.md
9491
[docs]: ./docs/

examples/server.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

migrate-from-v4-to-v5.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Migrate from v4 to v5
2+
3+
v5 is mainly a refactor of the codebase, to make it more maintainable and
4+
improve developer experience. The API is the same, but we have officially
5+
removed the `createPubSubServer`.
6+
7+
We also changed from using `@sinclair/typebox` to
8+
`@fastify/type-provider-typebox` which probably will not affect most users, this
9+
bumped `@sinclair/typebox` from `0.24` to `0.27`.
10+
11+
## Replacing `createPubSubServer` with Fastify instance
12+
13+
The reasoning behind removing `createPubSubServer` is that it was not really
14+
needed. It was a function that created a `fastify` instance, and then returned a
15+
`listen` function. The `fastify` instance can be created by the user with almost
16+
the same amount of code.
17+
18+
### Before
19+
20+
```typescript
21+
import { createPubSubServer, PubSubHandler } from 'pubsub-http-handler';
22+
23+
interface MyHandler {
24+
hello: string;
25+
world: string;
26+
}
27+
28+
const server = async () => {
29+
const handler: PubSubHandler<MyHandler> = ({ message, data }) => {
30+
// `message` contains attributes, data (as string), messageId
31+
// `data` contains a base64 decoded JSON serialized object (type is MyHandler in the example)
32+
// ...
33+
};
34+
35+
const { listen } = await createPubSubServer(handler);
36+
await listen();
37+
};
38+
```
39+
40+
### After
41+
42+
```typescript
43+
import Fastify from 'fastify';
44+
import { pubSubFastifyPlugin, PubSubHandler } from 'pubsub-http-handler';
45+
46+
interface MyHandler {
47+
hello: string;
48+
world: string;
49+
}
50+
51+
const server = async () => {
52+
const handler: PubSubHandler<MyHandler> = ({ message, data }) => {
53+
// `message` contains attributes, data (as string), messageId
54+
// `data` contains a base64 decoded JSON serialized object (type is MyHandler in the example)
55+
// ...
56+
};
57+
58+
const app = Fastify().withTypeProvider<TypeBoxTypeProvider>();
59+
60+
app.register(pubSubFastifyPlugin, makePubSubConfig({ handler }));
61+
62+
await app.listen(8000);
63+
};
64+
```

src/__tests__/cloud-functions.test.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { createPubSubCloudFunctions } from '../methods/cloud-functions';
1+
import { createPubSubCloudFunctions } from '../cloud-functions';
22
import { createPubSubRequest } from './fixtures';
33
import type * as express from 'express';
44

@@ -24,13 +24,18 @@ describe('cloud functions', () => {
2424
};
2525
const onError = jest.fn();
2626
const send = jest.fn();
27-
const fun = createPubSubCloudFunctions(handle, { onError });
27+
const fun = createPubSubCloudFunctions(handle, {
28+
onError,
29+
context: () => ({ hello: 'world' }),
30+
});
2831
await fun(
2932
{ body: payload } as any,
3033
{ status: () => ({ send }) } as unknown as any,
3134
);
3235

33-
expect(onError).toHaveBeenCalledWith(new Error('error'), payload);
36+
expect(onError).toHaveBeenCalledWith(new Error('error'), {
37+
hello: 'world',
38+
});
3439
});
3540

3641
it('should throw when onError is undefined', async () => {

src/__tests__/common.test.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { handlePubSubMessage } from '../common';
2-
import jsf from 'json-schema-faker';
3-
import { PubSubMessage, PubSubMessageType } from '../types';
2+
import { JSONSchemaFaker } from 'json-schema-faker';
3+
import { PubSubMessage, pubSubMessageSchema } from '../types';
44
import pino from 'pino';
55

6-
function createPubSubdata(input: unknown, stringify = true): PubSubMessageType {
7-
const message = jsf.generate(PubSubMessage) as PubSubMessageType;
6+
function createPubSubdata(input: unknown, stringify = true): PubSubMessage {
7+
const message = JSONSchemaFaker.generate(
8+
pubSubMessageSchema,
9+
) as PubSubMessage;
810
const data = Buffer.from(
911
stringify ? JSON.stringify(input) : (input as string),
1012
).toString('base64');

src/__tests__/fastify-plugin.test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import Fastify, { FastifyInstance } from 'fastify';
2-
import { pubSubFastifyPlugin } from '../methods/fastify-plugin';
3-
import { PubSubConfig } from '../types';
2+
import {
3+
PubSubHandlerFastifyConfig,
4+
pubSubFastifyPlugin,
5+
} from '../fastify-plugin';
46
import { createPubSubRequest } from './fixtures';
57

68
describe('fastify-plugin', () => {
@@ -75,7 +77,7 @@ describe('fastify-plugin', () => {
7577

7678
expect(onError).toHaveBeenCalledWith(
7779
new Error('error'),
78-
expect.objectContaining({ body: payload }),
80+
expect.any(Object),
7981
);
8082
});
8183

@@ -105,10 +107,10 @@ describe('fastify-plugin', () => {
105107
});
106108
const payload = createPubSubRequest('forward me');
107109

108-
app.register(pubSubFastifyPlugin, { handler, parser } as PubSubConfig<
109-
any,
110-
any
111-
>);
110+
app.register(pubSubFastifyPlugin, {
111+
handler,
112+
parser,
113+
} as PubSubHandlerFastifyConfig<any, any>);
112114

113115
const res = await app.inject({
114116
method: 'POST',

src/__tests__/fixtures.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { PubSubRequest, PubSubRequestType } from '../types';
2-
import jsf from 'json-schema-faker';
1+
import { PubSubRequest, pubSubRequestSchema } from '../types';
2+
import { JSONSchemaFaker } from 'json-schema-faker';
33

44
export function createPubSubRequest(
55
input: unknown,
66
stringify = true,
7-
): PubSubRequestType {
8-
const data = jsf.generate(PubSubRequest) as PubSubRequestType;
7+
): PubSubRequest {
8+
const data = JSONSchemaFaker.generate(pubSubRequestSchema) as PubSubRequest;
99
data.message.data = Buffer.from(
1010
stringify ? JSON.stringify(input) : (input as string),
1111
).toString('base64');

src/__tests__/index.test.ts

Lines changed: 0 additions & 90 deletions
This file was deleted.

src/__tests__/server-utils.test.ts

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)