You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/techniques/serialization.md
+31-31Lines changed: 31 additions & 31 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -60,37 +60,6 @@ When this endpoint is requested, the client receives the following response:
60
60
61
61
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.
62
62
63
-
#### Transform plain objects
64
-
65
-
You can enforce transformations at the controller level by using the `@SerializeOptions({ type: <CLASS> })` decorator. This ensures that all responses are transformed into instances of the specified class, applying any decorators from class-validator or class-transformer, even when plain objects are returned. This approach leads to cleaner code without the need to repeatedly instantiate the class or call `plainToInstance`.
66
-
67
-
In the example below, despite returning plain JavaScript objects in both conditional branches, they will be automatically converted into `UserEntity` instances, with the relevant decorators applied:
68
-
69
-
```typescript
70
-
@UseInterceptors(ClassSerializerInterceptor)
71
-
@SerializeOptions({ type: UserEntity })
72
-
@Get()
73
-
findOne(@Query() { id }: { id: number }): UserEntity {
74
-
if (id===1) {
75
-
return {
76
-
id: 1,
77
-
firstName: 'John',
78
-
lastName: 'Doe',
79
-
password: 'password',
80
-
};
81
-
}
82
-
83
-
return {
84
-
id: 2,
85
-
firstName: 'Kamil',
86
-
lastName: 'Mysliwiec',
87
-
password: 'password2',
88
-
};
89
-
}
90
-
```
91
-
92
-
> info **Hint** By specifying the expected return type for the controller, you can leverage TypeScript's type-checking capabilities to ensure that the returned plain object adheres to the shape of the DTO or entity. The `plainToInstance` function doesn't provide this level of type hinting, which can lead to potential bugs if the plain object doesn't match the expected DTO or entity structure.
93
-
94
63
#### Expose properties
95
64
96
65
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.
@@ -129,6 +98,37 @@ findOne(): UserEntity {
129
98
130
99
Options passed via `@SerializeOptions()` are passed as the second argument of the underlying `instanceToPlain()` function. In this example, we are automatically excluding all properties that begin with the `_` prefix.
131
100
101
+
#### Transform plain objects
102
+
103
+
You can enforce transformations at the controller level by using the `@SerializeOptions` decorator. This ensures that all responses are transformed into instances of the specified class, applying any decorators from class-validator or class-transformer, even when plain objects are returned. This approach leads to cleaner code without the need to repeatedly instantiate the class or call `plainToInstance`.
104
+
105
+
In the example below, despite returning plain JavaScript objects in both conditional branches, they will be automatically converted into `UserEntity` instances, with the relevant decorators applied:
106
+
107
+
```typescript
108
+
@UseInterceptors(ClassSerializerInterceptor)
109
+
@SerializeOptions({ type: UserEntity })
110
+
@Get()
111
+
findOne(@Query() { id }: { id: number }): UserEntity {
112
+
if (id===1) {
113
+
return {
114
+
id: 1,
115
+
firstName: 'John',
116
+
lastName: 'Doe',
117
+
password: 'password',
118
+
};
119
+
}
120
+
121
+
return {
122
+
id: 2,
123
+
firstName: 'Kamil',
124
+
lastName: 'Mysliwiec',
125
+
password: 'password2',
126
+
};
127
+
}
128
+
```
129
+
130
+
> info **Hint** By specifying the expected return type for the controller, you can leverage TypeScript's type-checking capabilities to ensure that the returned plain object adheres to the shape of the DTO or entity. The `plainToInstance` function doesn't provide this level of type hinting, which can lead to potential bugs if the plain object doesn't match the expected DTO or entity structure.
131
+
132
132
#### Example
133
133
134
134
A working example is available [here](https://github.com/nestjs/nest/tree/master/sample/21-serializer).
0 commit comments