@funduck/connectrpc-fastify 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/README.md +154 -0
  2. package/package.json +2 -2
package/README.md ADDED
@@ -0,0 +1,154 @@
1
+ # Connectrpc Fastify Wrapper
2
+
3
+ ## Description
4
+
5
+ This is a wrapper for [Connectrpc](https://github.com/connectrpc/connect-es) using the [Fastify](https://github.com/fastify/fastify) server.
6
+
7
+ If you are comfortable with HTTP/1 only and want a compact, ready-to-use setup, this repository is for you.
8
+
9
+ It simplifies the binding of controllers, middlewares, and guards.
10
+
11
+ I use it as a basis for integration into Nestjs, which will be implemented [here](https://github.com/funduck/connectrpc-fastify-nestjs).
12
+
13
+
14
+ ## Features
15
+
16
+ This library allows you to:
17
+ * Use only HTTP/1 transport
18
+ * Perform RPC with simple request and response messages
19
+ * Perform RPC with streaming responses
20
+ * Perform RPC with streaming requests
21
+ * Use middlewares
22
+ * Use global guards
23
+
24
+ *Bidirectional streaming RPC is currently out of scope because it requires HTTP/2, which is unstable on public networks. In practice, HTTP/1 provides more consistent performance.*
25
+
26
+ ## How To Use
27
+ You can check out `test` directory for a complete example of server and client.
28
+
29
+ ### Controllers
30
+ Controller must implement the service interface and register itself with `ConnectRPC.registerController` in the constructor.
31
+ ```TS
32
+ export class ElizaController implements Service<typeof ElizaService> {
33
+ constructor() {
34
+ ConnectRPC.registerController(this, ElizaService);
35
+ }
36
+
37
+ async say(
38
+ request: SayRequest,
39
+ ) {
40
+ return {
41
+ sentence: `You said: ${request.sentence}`,
42
+ };
43
+ }
44
+ }
45
+ ```
46
+
47
+ Create Fastify server, initialize controller and register ConnectRPC plugin.
48
+ ```TS
49
+ const fastify = Fastify({
50
+ logger: true,
51
+ });
52
+
53
+ new ElizaController();
54
+
55
+ await ConnectRPC.registerFastifyPlugin(fastify);
56
+
57
+ try {
58
+ await fastify.listen({ port: 3000 });
59
+ } catch (err) {
60
+ fastify.log.error(err);
61
+ process.exit(1);
62
+ }
63
+ ```
64
+
65
+ ### Middlewares
66
+ You can use middlewares for pre-processing requests.
67
+
68
+ Middleware must implement `Middleware` interface and register itself with `ConnectRPC.registerMiddleware` in the constructor.
69
+ ```TS
70
+ export class TestMiddleware1 implements Middleware {
71
+ constructor() {
72
+ ConnectRPC.registerMiddleware(this);
73
+ }
74
+
75
+ use(req: FastifyRequest['raw'], res: FastifyReply['raw'], next: () => void) {
76
+ next();
77
+ }
78
+ }
79
+ ```
80
+
81
+ Then create an instance of the middleware before registering the ConnectRPC plugin.
82
+ ```TS
83
+ const fastify = Fastify({
84
+ logger: true,
85
+ });
86
+
87
+ new ElizaController();
88
+
89
+ new TestMiddleware1();
90
+
91
+ await ConnectRPC.registerFastifyPlugin(fastify);
92
+
93
+ ConnectRPC.initMiddlewares(fastify, [
94
+ middlewareConfig(TestMiddleware1), // Global middleware for all services and methods
95
+ //middlewareConfig(TestMiddleware1, ElizaService), // Middleware for all ElizaService methods
96
+ //middlewareConfig(TestMiddleware1, ElizaService, ['say']), // Middleware for ElizaService's say method only
97
+ ]);
98
+
99
+ try {
100
+ await fastify.listen({ port: 3000 });
101
+ } catch (err) {
102
+ fastify.log.error(err);
103
+ process.exit(1);
104
+ }
105
+ ```
106
+
107
+ ### Guards
108
+ Guards are used to restrict access to certain services or methods.
109
+
110
+ Guard must implement `Guard` interface and register itself with `ConnectRPC.registerGuard` in the constructor.
111
+ ```TS
112
+ export class TestGuard1 implements Guard {
113
+ constructor() {
114
+ ConnectRPC.registerGuard(this);
115
+ }
116
+
117
+ canActivate(context: ExecutionContext): boolean {
118
+ return true;
119
+ }
120
+ }
121
+ ```
122
+
123
+ Then create an instance of the guard before registering the ConnectRPC plugin and initialize it similarly to middlewares.
124
+ ```TS
125
+ const fastify = Fastify({
126
+ logger: true,
127
+ });
128
+
129
+ new ElizaController();
130
+
131
+ new TestMiddleware1();
132
+
133
+ new TestGuard1();
134
+
135
+ await ConnectRPC.registerFastifyPlugin(fastify);
136
+
137
+ ConnectRPC.initMiddlewares(fastify, [
138
+ middlewareConfig(TestMiddleware1), // Global middleware for all services and methods
139
+ // middlewareConfig(TestMiddleware1, ElizaService), // Middleware for all ElizaService methods
140
+ // middlewareConfig(TestMiddleware1, ElizaService, ['say']), // Middleware for ElizaService's say method only
141
+ ]);
142
+
143
+ ConnectRPC.initGuards(fastify);
144
+
145
+ try {
146
+ await fastify.listen({ port: 3000 });
147
+ } catch (err) {
148
+ fastify.log.error(err);
149
+ process.exit(1);
150
+ }
151
+ ```
152
+
153
+ ## Feedback
154
+ Please use [Discussions](https://github.com/funduck/connectrpc-fastify/discussions) or email me.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@funduck/connectrpc-fastify",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "author": "Oleg Milekhin <qlfunduck@gmail.com>",
5
5
  "description": "Wrapper for official @connectrpc/connect and fastify. Simplifies configuration, type safe binding to controller, simplifies use of middlewares and guards.",
6
6
  "main": "dist/src/index.js",
@@ -14,7 +14,7 @@
14
14
  "@bufbuild/protobuf": "^2.10.2",
15
15
  "@connectrpc/connect": "^2.1.1",
16
16
  "@connectrpc/connect-fastify": "^2.1.1",
17
- "fastify": "^5.7.1",
17
+ "fastify": "^5.6.2",
18
18
  "type-fest": "^5.4.1"
19
19
  },
20
20
  "devDependencies": {