Skip to content

Commit adc8816

Browse files
committed
docs(microservice): add documentation for new custom client proxy
1 parent 15090cb commit adc8816

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

content/microservices/basics.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,36 @@ async onApplicationBootstrap() {
280280

281281
If the connection cannot be created, the `connect()` method will reject with the corresponding error object.
282282

283+
#### Extending the ClientProxy
284+
285+
If you need to add some custom logic around the serialization of responses on the client side, you can use a custom class that extends the `ClientProxy` class or one of its child classes. For modifying successful requests you can override the `serializeResponse` method, and for modifying any errors that go through this client you can override the `serializeError` method. To make use of this custom class, you can pass the class itself to the `ClientsModule.register()` method using the `customClass` property. Below is an example of a custom `ClientProxy` that serializes each error into an `RpcException`.
286+
287+
```ts
288+
@@filename(error-handling.proxy)
289+
import { ClientTcp, RpcException } from '@nestjs/microservices';
290+
291+
class ErrorHandlingProxy extends ClientTCP {
292+
serializeError(err) {
293+
return new RpcException(err);
294+
}
295+
}
296+
```
297+
298+
```ts
299+
@@filename(app.module)
300+
@Module({
301+
imports: [
302+
ClientsModule.register({
303+
name: 'CustomProxy',
304+
customClass: ErrorHandlingProxy,
305+
}),
306+
]
307+
})
308+
export class AppModule
309+
```
310+
311+
> info **hint** This is the class itself being passed to `customClass`, not an instance of the class. Nest will create the instance under the hood for you, and will pass any options given to the `options` property to the new `ClientProxy`.
312+
283313
#### Sending messages
284314

285315
The `ClientProxy` exposes a `send()` method. This method is intended to call the microservice and returns an `Observable` with its response. Thus, we can subscribe to the emitted values easily.

0 commit comments

Comments
 (0)