Skip to content

Commit 544ac5f

Browse files
Merge branch 'docs/add-serialize-options-transform-docs' of https://github.com/austinwoon/docs.nestjs.com into austinwoon-docs/add-serialize-options-transform-docs
2 parents 3b1ddaa + 4801072 commit 544ac5f

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

content/techniques/serialization.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ findOne(): UserEntity {
4444
}
4545
```
4646

47-
> **Warning** Note that we must return an instance of the class. If you return a plain JavaScript object, for example, `{{ '{' }} user: new UserEntity() {{ '}' }}`, the object won't be properly serialized.
47+
> **Warning** Note that we must return an instance of the class. If you return a plain JavaScript object, for example, `{{ '{' }} user: new UserEntity() {{ '}' }}`, the object won't be properly serialized.
4848
4949
> info **Hint** The `ClassSerializerInterceptor` is imported from `@nestjs/common`.
5050
@@ -60,6 +60,38 @@ When this endpoint is requested, the client receives the following response:
6060

6161
Note that the interceptor can be applied application-wide (as covered [here](https://docs.nestjs.com/interceptors#binding-interceptors)). The combination of the interceptor and the entity class declaration ensures that **any** method that returns a `UserEntity` will be sure to remove the `password` property. This gives you a measure of centralized enforcement of this business rule.
6262

63+
64+
#### Using the SerializeOptions to transform plain objects to the class instance.
65+
66+
You could enforce transformations at the controller level by stating `@SerializeOptions({{ '{' }} type {{ ':' }} <CLASS> {{ '}' }})` to transform all responses to the instance. Doing so will ensure that decorators on the class will always be applied, even if plain objects are returned. This allows for terser code, without the added verbosity of instantiating the class or calling `plainToInstance` repeatedly.
67+
68+
In this example, even though a plain js object was returned in both conditional statements, they will all be converted into `UserEntity` with annotated class-validator or class-transformer decorators applied.
69+
70+
```typescript
71+
@UseInterceptors(ClassSerializerInterceptor)
72+
@Get()
73+
@SerializeOptions({ type : UserEntity })
74+
findOne(@Query() { id }: { id: number }): UserEntity {
75+
if (id === 1) {
76+
return {
77+
id: 1,
78+
firstName: 'Kamil',
79+
lastName: 'Mysliwiec',
80+
password: 'password',
81+
};
82+
}
83+
84+
return {
85+
id: 2,
86+
firstName: 'Kamil2',
87+
lastName: 'Mysliwiec2',
88+
password: 'password2',
89+
};
90+
}
91+
```
92+
93+
> info **Hint** By stating the expecting controller return type, we can take advantage of Typescript to check if the returned plain object conforms to the DTO/entity shape. `plainToInstance`' does not hint if the plain object conforms to the the DTO/entity class instance you are transforming it into (somewhat equivalent to typecasting). This could lead to bugs in your application.
94+
6395
#### Expose properties
6496

6597
You can use the `@Expose()` decorator to provide alias names for properties, or to execute a function to calculate a property value (analogous to **getter** functions), as shown below.

0 commit comments

Comments
 (0)