Skip to content

Commit d0d5f1e

Browse files
committed
docs: separate into own section
1 parent abf5cb5 commit d0d5f1e

File tree

1 file changed

+32
-16
lines changed

1 file changed

+32
-16
lines changed

content/techniques/serialization.md

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,6 @@ findOne(): UserEntity {
4444

4545
> **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.
4646
47-
> info **Hint** You could enforce transformations at the controller level by stating `@SerializeOptions({{ '{' }} type {{ ':' }} UserEntity {{ '}' }})` to transform all responses to the instance of the dto without calling `plainToInstance`. Doing so will ensure that decorators on the `UserEntity` 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.
48-
49-
```typescript
50-
@UseInterceptors(ClassSerializerInterceptor)
51-
@Get()
52-
@SerializeOptions({ type : UserEntity })
53-
findOne(): UserEntity {
54-
return {
55-
id: 1,
56-
firstName: 'Kamil',
57-
lastName: 'Mysliwiec',
58-
password: 'password',
59-
};
60-
}
61-
```
62-
6347
> info **Hint** The `ClassSerializerInterceptor` is imported from `@nestjs/common`.
6448
6549
When this endpoint is requested, the client receives the following response:
@@ -74,6 +58,38 @@ When this endpoint is requested, the client receives the following response:
7458

7559
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.
7660

61+
62+
#### Using the SerializeOptions to transform plain objects to the class instance.
63+
64+
You could enforce transformations at the controller level by stating `@SerializeOptions({{ '{' }} type {{ ':' }} <CLASS_INSTANCE> {{ '}' }})` 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.
65+
66+
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.
67+
68+
```typescript
69+
@UseInterceptors(ClassSerializerInterceptor)
70+
@Get()
71+
@SerializeOptions({ type : UserEntity })
72+
findOne(@Query() { id }: { id: number }): UserEntity {
73+
if (id === 1) {
74+
return {
75+
id: 1,
76+
firstName: 'Kamil',
77+
lastName: 'Mysliwiec',
78+
password: 'password',
79+
};
80+
}
81+
82+
return {
83+
id: 2,
84+
firstName: 'Kamil2',
85+
lastName: 'Mysliwiec2',
86+
password: 'password2',
87+
};
88+
}
89+
```
90+
91+
> info **Hint** By stating the return type, we take advantage of Typescript to check if the plain js object conforms to the class shape.`plainToInstance`'s second argument does not check if the object has the same shape as the class instance (somewhat equivalent to typecasting), and could lead to bugs in your application.
92+
7793
#### Expose properties
7894

7995
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)