Skip to content

Commit 8ebfb46

Browse files
Merge pull request #1702 from jmcdo29/docs/custom-proxy
docs(microservice): add documentation for new custom client proxy
2 parents 0ec3038 + 8e4283e commit 8ebfb46

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

content/microservices/custom-transport.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,36 @@ And that's what you should see in the console:
215215
connect
216216
event to dispatch: { pattern: 'event', data: 'Hello world!' }
217217
```
218+
219+
#### Message serialization
220+
221+
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`.
222+
223+
```typescript
224+
@@filename(error-handling.proxy)
225+
import { ClientTcp, RpcException } from '@nestjs/microservices';
226+
227+
class ErrorHandlingProxy extends ClientTCP {
228+
serializeError(err: Error) {
229+
return new RpcException(err);
230+
}
231+
}
232+
```
233+
234+
and then use it in the `ClientsModule` like so:
235+
236+
```typescript
237+
@@filename(app.module)
238+
@Module({
239+
imports: [
240+
ClientsModule.register({
241+
name: 'CustomProxy',
242+
customClass: ErrorHandlingProxy,
243+
}),
244+
]
245+
})
246+
export class AppModule
247+
```
248+
249+
> 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`.
250+
>

0 commit comments

Comments
 (0)