Skip to content

Commit 5d6219b

Browse files
committed
fix(decorators): change decorators names
1 parent ff53c95 commit 5d6219b

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/decorators.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,29 +53,39 @@ export function Exclude(options?: ExcludeOptions) {
5353
}
5454

5555
/**
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.
5757
*/
58-
export function TransformMethod(params?: ClassTransformOptions, method?: "classToPlain"|"classToClass"): Function {
58+
export function TransformClassToPlain(params?: ClassTransformOptions): Function {
5959

6060
return function (target: Function, propertyKey: string, descriptor: PropertyDescriptor) {
6161
const classTransformer: ClassTransformer = new ClassTransformer();
62+
const MethodTransformer: Function = classTransformer.classToPlain;
6263
const originalMethod = descriptor.value;
6364

6465
descriptor.value = function(...args: any[]) {
6566
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";
6768

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 {
6978

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";
7787

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);
7989
};
8090
};
8191
}

test/functional/transformer-method.spec.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import "reflect-metadata";
22
import {defaultMetadataStorage} from "../../src/storage";
3-
import {Exclude, Expose, TransformMethod} from "../../src/decorators";
3+
import {Exclude, Expose, TransformClassToPlain, TransformClassToClass} from "../../src/decorators";
44
import {expect} from "chai";
55

6-
describe("transformer method decorator", () => {
6+
describe("transformer methods decorator", () => {
77

88
it("should expose non configuration properties and return User instance class", () => {
99
defaultMetadataStorage.clear();
@@ -24,7 +24,7 @@ describe("transformer method decorator", () => {
2424

2525
class UserController {
2626

27-
@TransformMethod({}, "classToClass")
27+
@TransformClassToClass()
2828
getUser() {
2929
const user = new User();
3030
user.firstName = "Snir";
@@ -69,7 +69,7 @@ describe("transformer method decorator", () => {
6969

7070
class UserController {
7171

72-
@TransformMethod()
72+
@TransformClassToPlain()
7373
getUser() {
7474
const user = new User();
7575
user.firstName = "Snir";
@@ -115,7 +115,7 @@ describe("transformer method decorator", () => {
115115

116116
class UserController {
117117

118-
@TransformMethod({ groups: ["user.permissions"] })
118+
@TransformClassToPlain({ groups: ["user.permissions"] })
119119
getUserWithRoles() {
120120
const user = new User();
121121
user.firstName = "Snir";
@@ -167,7 +167,7 @@ describe("transformer method decorator", () => {
167167

168168
class UserController {
169169

170-
@TransformMethod({ version: 1 })
170+
@TransformClassToPlain({ version: 1 })
171171
getUserVersion1() {
172172
const user = new User();
173173
user.firstName = "Snir";
@@ -179,7 +179,7 @@ describe("transformer method decorator", () => {
179179
return user;
180180
}
181181

182-
@TransformMethod({ version: 2 })
182+
@TransformClassToPlain({ version: 2 })
183183
getUserVersion2() {
184184
const user = new User();
185185
user.firstName = "Snir";

0 commit comments

Comments
 (0)