Skip to content

Commit 5787df9

Browse files
committed
docs: rename section to better display functionalty
1 parent adc8816 commit 5787df9

File tree

1 file changed

+33
-30
lines changed

1 file changed

+33
-30
lines changed

content/microservices/basics.md

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -280,36 +280,6 @@ 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-
313283
#### Sending messages
314284

315285
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.
@@ -401,3 +371,36 @@ this.client
401371
> info **Hint** The `timeout` operator is imported from the `rxjs/operators` package.
402372
403373
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+
>

0 commit comments

Comments
 (0)