Skip to content

Commit 524a192

Browse files
committed
fix(transformMethod): fixing code-review and changing decorator naming
1 parent 9268225 commit 524a192

File tree

3 files changed

+241
-50
lines changed

3 files changed

+241
-50
lines changed

src/decorators.ts

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {ExposeMetadata} from "./metadata/ExposeMetadata";
55
import {ExposeOptions, ExcludeOptions, TypeOptions, TransformOptions} from "./metadata/ExposeExcludeOptions";
66
import {ExcludeMetadata} from "./metadata/ExcludeMetadata";
77
import {TransformMetadata} from "./metadata/TransformMetadata";
8+
import {ClassTransformOptions} from "./ClassTransformOptions";
89

910
/**
1011
* Defines a custom logic for value transformation.
@@ -54,17 +55,26 @@ export function Exclude(options?: ExcludeOptions) {
5455
/**
5556
* Return the object with the exposed properties only.
5657
*/
57-
export function JsonView(params?: {}, method?: string): Function {
58+
export function TransformMethod(params?: ClassTransformOptions, method?: "classToPlain"|"classToClass"): Function {
5859

5960
return function (target: Function, propertyKey: string, descriptor: PropertyDescriptor) {
60-
const classTransformer: any = new ClassTransformer();
61+
const classTransformer: ClassTransformer = new ClassTransformer();
6162
const originalMethod = descriptor.value;
62-
63+
6364
descriptor.value = function(...args: any[]) {
64-
let transformer: Function = typeof method === "string" && method ? classTransformer[method] : classTransformer.classToPlain;
65-
let result: any = originalMethod.apply(this, args);
66-
67-
let isPromise = !!result && (typeof result === "object" || typeof result === "function") && typeof result.then === "function";
65+
const result: any = originalMethod.apply(this, args);
66+
const isPromise = !!result && (typeof result === "object" || typeof result === "function") && typeof result.then === "function";
67+
68+
let transformer: Function;
69+
70+
switch (method) {
71+
case "classToClass":
72+
transformer = classTransformer.classToClass;
73+
break;
74+
case "classToPlain":
75+
default:
76+
transformer = classTransformer.classToPlain;
77+
}
6878

6979
return isPromise ? result.then((data: any) => transformer(data, params)) : transformer(result, params);
7080
};

test/functional/basic-functionality.spec.ts

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {
77
classToClass, classToClassFromExist
88
} from "../../src/index";
99
import {defaultMetadataStorage} from "../../src/storage";
10-
import {Exclude, Expose, Type, JsonView} from "../../src/decorators";
10+
import {Exclude, Expose, Type} from "../../src/decorators";
1111
import {expect} from "chai";
1212

1313
describe("basic functionality", () => {
@@ -1680,47 +1680,5 @@ describe("basic functionality", () => {
16801680
classToClassFromExistUser.should.be.eql([fromExistUserLike1, fromExistUserLike2]);
16811681

16821682
});
1683-
1684-
it("should expose properties with json view", () => {
1685-
defaultMetadataStorage.clear();
1686-
1687-
@Exclude()
1688-
class User {
1689-
1690-
id: number;
1691-
1692-
@Expose()
1693-
firstName: string;
1694-
1695-
@Expose()
1696-
lastName: string;
1697-
1698-
password: string;
1699-
}
1700-
1701-
const user = new User();
1702-
user.firstName = "Umed";
1703-
user.lastName = "Khudoiberdiev";
1704-
user.password = "imnosuperman";
1705-
1706-
const plainUser = {
1707-
firstName: "Umed",
1708-
lastName: "Khudoiberdiev"
1709-
};
1710-
1711-
class UserController {
1712-
1713-
@JsonView()
1714-
getUser() {
1715-
return user;
1716-
}
1717-
}
1718-
1719-
const controller = new UserController();
1720-
1721-
let result = controller.getUser();
1722-
expect(result.password).to.be.undefined;
1723-
expect(result).to.be.eql(plainUser);
1724-
});
17251683

17261684
});
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
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

Comments
 (0)