@funduck/connectrpc-fastify 1.0.12 → 1.0.14
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.
- package/README.md +35 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -5,12 +5,10 @@ Code is not production ready.
|
|
|
5
5
|
|
|
6
6
|
## Description
|
|
7
7
|
|
|
8
|
-
This is a wrapper for [Connectrpc](https://github.com/connectrpc/connect-es) using the [Fastify](https://github.com/fastify/fastify) server.
|
|
9
|
-
|
|
8
|
+
This is a wrapper for [Connectrpc](https://github.com/connectrpc/connect-es) using the [Fastify](https://github.com/fastify/fastify) server.
|
|
10
9
|
If you are comfortable with HTTP/1 only and want a compact, ready-to-use setup, this repository is for you.
|
|
11
10
|
|
|
12
|
-
It simplifies the binding of controllers and middlewares.
|
|
13
|
-
|
|
11
|
+
It simplifies the binding of controllers and middlewares.
|
|
14
12
|
I use it as a basis for integration into Nestjs, which will be implemented [here](https://github.com/funduck/connectrpc-fastify-nestjs).
|
|
15
13
|
|
|
16
14
|
|
|
@@ -32,7 +30,7 @@ You can check out `test` directory for a complete example of server and client.
|
|
|
32
30
|
### Controllers
|
|
33
31
|
Controller must implement the service interface and register itself with `ConnectRPC.registerController` in the constructor.
|
|
34
32
|
|
|
35
|
-
Controller methods receive `HandlerContext` which provides access to request headers, context values, and other metadata.
|
|
33
|
+
Controller methods receive original Connectrpc's `HandlerContext` which provides access to request headers, context values, and other metadata.
|
|
36
34
|
|
|
37
35
|
```TS
|
|
38
36
|
import type { HandlerContext } from '@connectrpc/connect';
|
|
@@ -75,7 +73,7 @@ try {
|
|
|
75
73
|
```
|
|
76
74
|
|
|
77
75
|
### Middlewares
|
|
78
|
-
Middlewares run as Fastify hooks and have access to raw request/response objects.
|
|
76
|
+
Middlewares run as Fastify hooks and have access to raw request/response objects and context values which can be retrieved using `getCustomContextValues` helper.
|
|
79
77
|
|
|
80
78
|
Middleware must implement `Middleware` interface and register itself with `ConnectRPC.registerMiddleware` in the constructor.
|
|
81
79
|
```TS
|
|
@@ -97,7 +95,10 @@ export class AuthMiddleware implements Middleware {
|
|
|
97
95
|
}
|
|
98
96
|
```
|
|
99
97
|
|
|
100
|
-
Create an instance of the middleware before registering the ConnectRPC plugin.
|
|
98
|
+
Create an instance of the middleware before registering the ConnectRPC plugin.
|
|
99
|
+
And then initialize middlewares using `ConnectRPC.initMiddlewares` method.
|
|
100
|
+
You can configure middleware to run globally for all services and methods, for specific service only, or for specific method only.
|
|
101
|
+
For type safety use `middlewareConfig` helper.
|
|
101
102
|
```TS
|
|
102
103
|
const fastify = Fastify({ logger: true });
|
|
103
104
|
|
|
@@ -107,13 +108,37 @@ new AuthMiddleware();
|
|
|
107
108
|
await ConnectRPC.registerFastifyPlugin(fastify);
|
|
108
109
|
|
|
109
110
|
ConnectRPC.initMiddlewares(fastify, [
|
|
110
|
-
middlewareConfig(AuthMiddleware), // Global
|
|
111
|
-
// middlewareConfig(AuthMiddleware, ElizaService), //
|
|
112
|
-
// middlewareConfig(AuthMiddleware, ElizaService, ['say']), //
|
|
111
|
+
middlewareConfig(AuthMiddleware), // Global - for all services and methods
|
|
112
|
+
// middlewareConfig(AuthMiddleware, ElizaService), // for all ElizaService methods
|
|
113
|
+
// middlewareConfig(AuthMiddleware, ElizaService, ['say']), // for specific method only
|
|
113
114
|
]);
|
|
114
115
|
|
|
115
116
|
await fastify.listen({ port: 3000 });
|
|
116
117
|
```
|
|
117
118
|
|
|
119
|
+
## Using Context Values
|
|
120
|
+
This is original `ContextValues`, so you need to create context keys using `createContextKey` helper from Connectrpc.
|
|
121
|
+
```TS
|
|
122
|
+
import { createContextKey } from '@connectrpc/connect';
|
|
123
|
+
|
|
124
|
+
const authKey = createContextKey(''); // '' as default value
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
To use context values in middleware, retrieve them using `getCustomContextValues` helper.
|
|
128
|
+
```TS
|
|
129
|
+
import { getCustomContextValues } from '@funduck/connectrpc-fastify';
|
|
130
|
+
|
|
131
|
+
// In middleware "use" method
|
|
132
|
+
const authHeader = req.headers['authorization'];
|
|
133
|
+
const contextValues = getCustomContextValues(req);
|
|
134
|
+
contextValues.set(authKey, authHeader);
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
To use context values in controller, retrieve them from `HandlerContext`.
|
|
138
|
+
```TS
|
|
139
|
+
// In controller method
|
|
140
|
+
const authValue = context.values.get(authKey);
|
|
141
|
+
```
|
|
142
|
+
|
|
118
143
|
## Feedback
|
|
119
144
|
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.
|
|
3
|
+
"version": "1.0.14",
|
|
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/index.js",
|