Skip to content

Commit 85f2036

Browse files
committed
docs: move custom message serialization to custom transport page
1 parent 5787df9 commit 85f2036

File tree

2 files changed

+33
-33
lines changed

2 files changed

+33
-33
lines changed

content/microservices/basics.md

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -371,36 +371,3 @@ this.client
371371
> info **Hint** The `timeout` operator is imported from the `rxjs/operators` package.
372372
373373
After 5 seconds, if the microservice isn't responding, it will throw an error.
374-
375-
#### Custom Message Serialization
376-
377-
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`.
378-
379-
```typescript
380-
@@filename(error-handling.proxy)
381-
import { ClientTcp, RpcException } from '@nestjs/microservices';
382-
383-
class ErrorHandlingProxy extends ClientTCP {
384-
serializeError(err: Error) {
385-
return new RpcException(err);
386-
}
387-
}
388-
```
389-
390-
and then use it in the `ClientsModule` like so
391-
392-
```typescript
393-
@@filename(app.module)
394-
@Module({
395-
imports: [
396-
ClientsModule.register({
397-
name: 'CustomProxy',
398-
customClass: ErrorHandlingProxy,
399-
}),
400-
]
401-
})
402-
export class AppModule
403-
```
404-
405-
> 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`.
406-
>

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)