@@ -53,29 +53,39 @@ export function Exclude(options?: ExcludeOptions) {
53
53
}
54
54
55
55
/**
56
- * Return the object with the exposed properties only .
56
+ * Transform the object from class to plain object and return only with the exposed properties.
57
57
*/
58
- export function TransformMethod ( params ?: ClassTransformOptions , method ?: "classToPlain" | "classToClass" ) : Function {
58
+ export function TransformClassToPlain ( params ?: ClassTransformOptions ) : Function {
59
59
60
60
return function ( target : Function , propertyKey : string , descriptor : PropertyDescriptor ) {
61
61
const classTransformer : ClassTransformer = new ClassTransformer ( ) ;
62
+ const MethodTransformer : Function = classTransformer . classToPlain ;
62
63
const originalMethod = descriptor . value ;
63
64
64
65
descriptor . value = function ( ...args : any [ ] ) {
65
66
const result : any = originalMethod . apply ( this , args ) ;
66
- const isPromise = ! ! result && ( typeof result === "object" || typeof result === "function" ) && typeof result . then === "function" ;
67
+ const isPromise = ! ! result && ( typeof result === "object" || typeof result === "function" ) && typeof result . then === "function" ;
67
68
68
- let transformer : Function ;
69
+ return isPromise ? result . then ( ( data : any ) => MethodTransformer ( data , params ) ) : MethodTransformer ( result , params ) ;
70
+ } ;
71
+ } ;
72
+ }
73
+
74
+ /**
75
+ * Return the class instance only with the exposed properties.
76
+ */
77
+ export function TransformClassToClass ( params ?: ClassTransformOptions ) : Function {
69
78
70
- switch ( method ) {
71
- case "classToClass" :
72
- transformer = classTransformer . classToClass ;
73
- break ;
74
- case "classToPlain" : default :
75
- transformer = classTransformer . classToPlain ;
76
- }
79
+ return function ( target : Function , propertyKey : string , descriptor : PropertyDescriptor ) {
80
+ const classTransformer : ClassTransformer = new ClassTransformer ( ) ;
81
+ const MethodTransformer : Function = classTransformer . classToClass ;
82
+ const originalMethod = descriptor . value ;
83
+
84
+ descriptor . value = function ( ...args : any [ ] ) {
85
+ const result : any = originalMethod . apply ( this , args ) ;
86
+ const isPromise = ! ! result && ( typeof result === "object" || typeof result === "function" ) && typeof result . then === "function" ;
77
87
78
- return isPromise ? result . then ( ( data : any ) => transformer ( data , params ) ) : transformer ( result , params ) ;
88
+ return isPromise ? result . then ( ( data : any ) => MethodTransformer ( data , params ) ) : MethodTransformer ( result , params ) ;
79
89
} ;
80
90
} ;
81
91
}
0 commit comments