Skip to content

Commit abf5cb5

Browse files
committed
docs: add serialize options docs
1 parent 05d6294 commit abf5cb5

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

content/techniques/serialization.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,23 @@ findOne(): UserEntity {
4242
}
4343
```
4444

45-
> **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.
45+
> **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.
46+
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+
```
4662

4763
> info **Hint** The `ClassSerializerInterceptor` is imported from `@nestjs/common`.
4864

0 commit comments

Comments
 (0)