1
+ import "reflect-metadata" ;
2
+ import { defaultMetadataStorage } from "../../src/storage" ;
3
+ import { Exclude , Expose , TransformMethod } from "../../src/decorators" ;
4
+ import { expect } from "chai" ;
5
+
6
+ describe ( "transformer method decorator" , ( ) => {
7
+
8
+ it ( "should expose non configuration properties and return User instance class" , ( ) => {
9
+ defaultMetadataStorage . clear ( ) ;
10
+
11
+ @Exclude ( )
12
+ class User {
13
+
14
+ id : number ;
15
+
16
+ @Expose ( )
17
+ firstName : string ;
18
+
19
+ @Expose ( )
20
+ lastName : string ;
21
+
22
+ password : string ;
23
+ }
24
+
25
+ class UserController {
26
+
27
+ @TransformMethod ( { } , "classToClass" )
28
+ getUser ( ) {
29
+ const user = new User ( ) ;
30
+ user . firstName = "Snir" ;
31
+ user . lastName = "Segal" ;
32
+ user . password = "imnosuperman" ;
33
+
34
+ return user ;
35
+ }
36
+ }
37
+
38
+ const controller = new UserController ( ) ;
39
+
40
+ const result = controller . getUser ( ) ;
41
+ expect ( result . password ) . to . be . undefined ;
42
+
43
+ const plainUser = {
44
+ firstName : "Snir" ,
45
+ lastName : "Segal"
46
+ } ;
47
+
48
+
49
+ expect ( result ) . to . be . eql ( plainUser ) ;
50
+ expect ( result ) . to . be . instanceof ( User ) ;
51
+ } ) ;
52
+
53
+ it ( "should expose non configuration properties" , ( ) => {
54
+ defaultMetadataStorage . clear ( ) ;
55
+
56
+ @Exclude ( )
57
+ class User {
58
+
59
+ id : number ;
60
+
61
+ @Expose ( )
62
+ firstName : string ;
63
+
64
+ @Expose ( )
65
+ lastName : string ;
66
+
67
+ password : string ;
68
+ }
69
+
70
+ class UserController {
71
+
72
+ @TransformMethod ( )
73
+ getUser ( ) {
74
+ const user = new User ( ) ;
75
+ user . firstName = "Snir" ;
76
+ user . lastName = "Segal" ;
77
+ user . password = "imnosuperman" ;
78
+
79
+ return user ;
80
+ }
81
+ }
82
+
83
+ const controller = new UserController ( ) ;
84
+
85
+ const result = controller . getUser ( ) ;
86
+ expect ( result . password ) . to . be . undefined ;
87
+
88
+ const plainUser = {
89
+ firstName : "Snir" ,
90
+ lastName : "Segal"
91
+ } ;
92
+
93
+ expect ( result ) . to . be . eql ( plainUser ) ;
94
+ } ) ;
95
+
96
+ it ( "should expose non configuration properties and properties with specific groups" , ( ) => {
97
+ defaultMetadataStorage . clear ( ) ;
98
+
99
+ @Exclude ( )
100
+ class User {
101
+
102
+ id : number ;
103
+
104
+ @Expose ( )
105
+ firstName : string ;
106
+
107
+ @Expose ( )
108
+ lastName : string ;
109
+
110
+ @Expose ( { groups : [ "user.permissions" ] } )
111
+ roles : string [ ] ;
112
+
113
+ password : string ;
114
+ }
115
+
116
+ class UserController {
117
+
118
+ @TransformMethod ( { groups : [ "user.permissions" ] } )
119
+ getUserWithRoles ( ) {
120
+ const user = new User ( ) ;
121
+ user . firstName = "Snir" ;
122
+ user . lastName = "Segal" ;
123
+ user . password = "imnosuperman" ;
124
+ user . roles = [ "USER" , "MANAGER" ] ;
125
+
126
+ return user ;
127
+ }
128
+
129
+ }
130
+
131
+ const controller = new UserController ( ) ;
132
+
133
+ const result = controller . getUserWithRoles ( ) ;
134
+ expect ( result . password ) . to . be . undefined ;
135
+
136
+ const plainUser = {
137
+ firstName : "Snir" ,
138
+ lastName : "Segal" ,
139
+ roles : [ "USER" , "MANAGER" ]
140
+ } ;
141
+
142
+ expect ( result ) . to . be . eql ( plainUser ) ;
143
+ } ) ;
144
+
145
+ it ( "should expose non configuration properties with specific version" , ( ) => {
146
+ defaultMetadataStorage . clear ( ) ;
147
+
148
+ @Exclude ( )
149
+ class User {
150
+
151
+ id : number ;
152
+
153
+ @Expose ( )
154
+ firstName : string ;
155
+
156
+ @Expose ( )
157
+ lastName : string ;
158
+
159
+ @Expose ( { groups : [ "user.permissions" ] } )
160
+ roles : string [ ] ;
161
+
162
+ @Expose ( { since : 2 } )
163
+ websiteUrl ?: string ;
164
+
165
+ password : string ;
166
+ }
167
+
168
+ class UserController {
169
+
170
+ @TransformMethod ( { version : 1 } )
171
+ getUserVersion1 ( ) {
172
+ const user = new User ( ) ;
173
+ user . firstName = "Snir" ;
174
+ user . lastName = "Segal" ;
175
+ user . password = "imnosuperman" ;
176
+ user . roles = [ "USER" , "MANAGER" ] ;
177
+ user . websiteUrl = "http://www.github.com" ;
178
+
179
+ return user ;
180
+ }
181
+
182
+ @TransformMethod ( { version : 2 } )
183
+ getUserVersion2 ( ) {
184
+ const user = new User ( ) ;
185
+ user . firstName = "Snir" ;
186
+ user . lastName = "Segal" ;
187
+ user . password = "imnosuperman" ;
188
+ user . roles = [ "USER" , "MANAGER" ] ;
189
+ user . websiteUrl = "http://www.github.com" ;
190
+
191
+ return user ;
192
+ }
193
+
194
+ }
195
+
196
+ const controller = new UserController ( ) ;
197
+
198
+ const resultV2 = controller . getUserVersion2 ( ) ;
199
+ expect ( resultV2 . password ) . to . be . undefined ;
200
+ expect ( resultV2 . roles ) . to . be . undefined ;
201
+
202
+ const plainUserV2 = {
203
+ firstName : "Snir" ,
204
+ lastName : "Segal" ,
205
+ websiteUrl : "http://www.github.com"
206
+ } ;
207
+
208
+ expect ( resultV2 ) . to . be . eql ( plainUserV2 ) ;
209
+
210
+ const resultV1 = controller . getUserVersion1 ( ) ;
211
+ expect ( resultV1 . password ) . to . be . undefined ;
212
+ expect ( resultV1 . roles ) . to . be . undefined ;
213
+ expect ( resultV1 . websiteUrl ) . to . be . undefined ;
214
+
215
+ const plainUserV1 = {
216
+ firstName : "Snir" ,
217
+ lastName : "Segal"
218
+ } ;
219
+
220
+ expect ( resultV1 ) . to . be . eql ( plainUserV1 ) ;
221
+ } ) ;
222
+
223
+ } ) ;
0 commit comments