@tsed/core 8.0.1 → 8.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/package.json +5 -4
  2. package/readme.md +5 -5
  3. package/src/decorators/storeFn.ts +20 -0
  4. package/src/decorators/storeMerge.ts +8 -0
  5. package/src/decorators/storeSet.ts +8 -0
  6. package/src/domain/AnyToPromise.spec.ts +158 -0
  7. package/src/domain/AnyToPromise.ts +181 -0
  8. package/src/domain/DecoratorTypes.ts +10 -0
  9. package/src/domain/Env.ts +15 -0
  10. package/src/domain/Metadata.spec.ts +142 -0
  11. package/src/domain/Metadata.ts +523 -0
  12. package/src/domain/Store.spec.ts +164 -0
  13. package/src/domain/Store.ts +175 -0
  14. package/src/domain/Type.ts +20 -0
  15. package/src/errors/UnsupportedDecoratorType.spec.ts +81 -0
  16. package/src/errors/UnsupportedDecoratorType.ts +25 -0
  17. package/src/index.ts +75 -0
  18. package/src/interfaces/AnyDecorator.ts +1 -0
  19. package/src/interfaces/DecoratorParameters.ts +8 -0
  20. package/src/interfaces/MetadataTypes.ts +6 -0
  21. package/src/interfaces/Relation.ts +1 -0
  22. package/src/interfaces/ValueOf.ts +1 -0
  23. package/src/utils/catchError.spec.ts +17 -0
  24. package/src/utils/catchError.ts +15 -0
  25. package/src/utils/decorators/decorateMethodsOf.spec.ts +83 -0
  26. package/src/utils/decorators/decorateMethodsOf.ts +31 -0
  27. package/src/utils/decorators/decoratorArgs.spec.ts +15 -0
  28. package/src/utils/decorators/decoratorArgs.ts +6 -0
  29. package/src/utils/decorators/decoratorTypeOf.spec.ts +92 -0
  30. package/src/utils/decorators/decoratorTypeOf.ts +24 -0
  31. package/src/utils/decorators/inheritedDescriptorOf.ts +13 -0
  32. package/src/utils/decorators/useDecorators.spec.ts +61 -0
  33. package/src/utils/decorators/useDecorators.ts +21 -0
  34. package/src/utils/decorators/useMethodDecorator.spec.ts +45 -0
  35. package/src/utils/decorators/useMethodDecorators.ts +12 -0
  36. package/src/utils/getRandomId.spec.ts +7 -0
  37. package/src/utils/getRandomId.ts +3 -0
  38. package/src/utils/http/getHostInfoFromPort.spec.ts +25 -0
  39. package/src/utils/http/getHostInfoFromPort.ts +20 -0
  40. package/src/utils/objects/ObjectUtils.spec.ts +322 -0
  41. package/src/utils/objects/ancestorOf.ts +7 -0
  42. package/src/utils/objects/ancestorsOf.ts +21 -0
  43. package/src/utils/objects/classOf.ts +18 -0
  44. package/src/utils/objects/cleanObject.spec.ts +22 -0
  45. package/src/utils/objects/cleanObject.ts +20 -0
  46. package/src/utils/objects/constructorOf.ts +17 -0
  47. package/src/utils/objects/createInstance.ts +5 -0
  48. package/src/utils/objects/deepClone.spec.ts +130 -0
  49. package/src/utils/objects/deepClone.ts +85 -0
  50. package/src/utils/objects/deepMerge.spec.ts +196 -0
  51. package/src/utils/objects/deepMerge.ts +84 -0
  52. package/src/utils/objects/descriptorOf.spec.ts +16 -0
  53. package/src/utils/objects/descriptorOf.ts +13 -0
  54. package/src/utils/objects/getClassOrSymbol.ts +6 -0
  55. package/src/utils/objects/getConstructorArgNames.spec.ts +11 -0
  56. package/src/utils/objects/getConstructorArgNames.ts +10 -0
  57. package/src/utils/objects/getValue.spec.ts +62 -0
  58. package/src/utils/objects/getValue.ts +87 -0
  59. package/src/utils/objects/hasJsonMethod.spec.ts +11 -0
  60. package/src/utils/objects/hasJsonMethod.ts +3 -0
  61. package/src/utils/objects/isArray.ts +18 -0
  62. package/src/utils/objects/isArrowFn.ts +5 -0
  63. package/src/utils/objects/isBoolean.ts +12 -0
  64. package/src/utils/objects/isBuffer.spec.ts +14 -0
  65. package/src/utils/objects/isBuffer.ts +19 -0
  66. package/src/utils/objects/isClass.spec.ts +15 -0
  67. package/src/utils/objects/isClass.ts +28 -0
  68. package/src/utils/objects/isCollection.ts +20 -0
  69. package/src/utils/objects/isDate.ts +8 -0
  70. package/src/utils/objects/isEmpty.spec.ts +10 -0
  71. package/src/utils/objects/isEmpty.ts +10 -0
  72. package/src/utils/objects/isEnumerable.ts +21 -0
  73. package/src/utils/objects/isFunction.ts +3 -0
  74. package/src/utils/objects/isInheritedFrom.ts +26 -0
  75. package/src/utils/objects/isMomentObject.spec.ts +16 -0
  76. package/src/utils/objects/isMomentObject.ts +3 -0
  77. package/src/utils/objects/isMongooseObject.spec.ts +22 -0
  78. package/src/utils/objects/isMongooseObject.ts +9 -0
  79. package/src/utils/objects/isNil.spec.ts +10 -0
  80. package/src/utils/objects/isNil.ts +3 -0
  81. package/src/utils/objects/isNumber.ts +12 -0
  82. package/src/utils/objects/isObject.ts +3 -0
  83. package/src/utils/objects/isObservable.ts +3 -0
  84. package/src/utils/objects/isPlainObject.spec.ts +10 -0
  85. package/src/utils/objects/isPlainObject.ts +14 -0
  86. package/src/utils/objects/isPrimitive.spec.ts +17 -0
  87. package/src/utils/objects/isPrimitive.ts +26 -0
  88. package/src/utils/objects/isPromise.ts +13 -0
  89. package/src/utils/objects/isProtectedKey.spec.ts +14 -0
  90. package/src/utils/objects/isProtectedKey.ts +7 -0
  91. package/src/utils/objects/isRegExp.ts +3 -0
  92. package/src/utils/objects/isSerializable.spec.ts +14 -0
  93. package/src/utils/objects/isSerializable.ts +9 -0
  94. package/src/utils/objects/isStream.ts +3 -0
  95. package/src/utils/objects/isString.ts +11 -0
  96. package/src/utils/objects/isSymbol.spec.ts +11 -0
  97. package/src/utils/objects/isSymbol.ts +11 -0
  98. package/src/utils/objects/methodsOf.ts +25 -0
  99. package/src/utils/objects/nameOf.ts +27 -0
  100. package/src/utils/objects/objectKeys.spec.ts +10 -0
  101. package/src/utils/objects/objectKeys.ts +5 -0
  102. package/src/utils/objects/primitiveOf.ts +19 -0
  103. package/src/utils/objects/prototypeOf.ts +10 -0
  104. package/src/utils/objects/setValue.spec.ts +60 -0
  105. package/src/utils/objects/setValue.ts +35 -0
  106. package/src/utils/objects/toMap.spec.ts +211 -0
  107. package/src/utils/objects/toMap.ts +44 -0
  108. package/src/utils/objects/toStringConstructor.ts +9 -0
  109. package/src/utils/objects/toStringConstrutor.spec.ts +18 -0
  110. package/src/utils/objects/uniq.spec.ts +8 -0
  111. package/src/utils/objects/uniq.ts +16 -0
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@tsed/core",
3
3
  "description": "Core module for Ts.ED Framework",
4
4
  "type": "module",
5
- "version": "8.0.1",
5
+ "version": "8.0.3",
6
6
  "source": "./src/index.ts",
7
7
  "main": "./lib/esm/index.js",
8
8
  "module": "./lib/esm/index.js",
@@ -10,6 +10,7 @@
10
10
  "browser": "./lib/browser/core.umd.min.js",
11
11
  "exports": {
12
12
  ".": {
13
+ "@tsed/source": "./src/index.ts",
13
14
  "types": "./lib/types/index.d.ts",
14
15
  "browser": "./lib/browser/core.umd.min.js",
15
16
  "import": "./lib/esm/index.js",
@@ -34,10 +35,10 @@
34
35
  "tslib": "2.7.0"
35
36
  },
36
37
  "devDependencies": {
37
- "@tsed/barrels": "8.0.1",
38
+ "@tsed/barrels": "8.0.3",
38
39
  "@tsed/monorepo-utils": "2.3.9",
39
- "@tsed/typescript": "8.0.1",
40
- "@tsed/vitest": "8.0.1",
40
+ "@tsed/typescript": "8.0.3",
41
+ "@tsed/vitest": "8.0.3",
41
42
  "eslint": "9.12.0",
42
43
  "typescript": "5.4.5",
43
44
  "vite": "^5.4.8",
package/readme.md CHANGED
@@ -1,5 +1,5 @@
1
1
  <p style="text-align: center" align="center">
2
- <a href="https://tsed.io" target="_blank"><img src="https://tsed.io/tsed-og.png" width="200" alt="Ts.ED logo"/></a>
2
+ <a href="https://tsed.dev" target="_blank"><img src="https://tsed.dev/tsed-og.png" width="200" alt="Ts.ED logo"/></a>
3
3
  </p>
4
4
 
5
5
  <div align="center">
@@ -16,9 +16,9 @@
16
16
  </div>
17
17
 
18
18
  <div align="center">
19
- <a href="https://tsed.io/">Website</a>
19
+ <a href="https://tsed.dev/">Website</a>
20
20
  <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
21
- <a href="https://tsed.io/getting-started/">Getting started</a>
21
+ <a href="https://tsed.dev/getting-started/">Getting started</a>
22
22
  <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
23
23
  <a href="https://api.tsed.io/rest/slack/tsedio/tsed">Slack</a>
24
24
  <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
@@ -27,7 +27,7 @@
27
27
 
28
28
  <hr />
29
29
 
30
- A package of Ts.ED framework. See website: https://tsed.io/
30
+ A package of Ts.ED framework. See website: https://tsed.dev/
31
31
 
32
32
  # Installation
33
33
 
@@ -37,7 +37,7 @@ npm install --save @tsed/core
37
37
 
38
38
  ## Contributors
39
39
 
40
- Please read [contributing guidelines here](https://tsed.io/contributing.html).
40
+ Please read [contributing guidelines here](https://tsed.dev/contributing.html).
41
41
 
42
42
  <a href="https://github.com/tsedio/tsed/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>
43
43
 
@@ -0,0 +1,20 @@
1
+ import {Store} from "../domain/Store.js";
2
+ import {DecoratorParameters} from "../interfaces/DecoratorParameters.js";
3
+
4
+ /**
5
+ * Create a store correctly configured from the parameters given by the decorator.
6
+ * The `fn` can return a decorator that will be initialized with the parameters (target, propertyKey, descriptor).
7
+ * @param {(store: Store, parameters: DecoratorParameters) => void} fn
8
+ * @returns {Function}
9
+ */
10
+ export function StoreFn(fn: (store: Store, parameters: DecoratorParameters) => void): Function {
11
+ return (...parameters: any[]): any => {
12
+ const store = Store.from(...parameters);
13
+ const result: any = fn(store, parameters as DecoratorParameters);
14
+ if (typeof result === "function") {
15
+ result(...parameters);
16
+ }
17
+
18
+ return parameters[2];
19
+ };
20
+ }
@@ -0,0 +1,8 @@
1
+ import {Store} from "../domain/Store.js";
2
+ import {StoreFn} from "./storeFn.js";
3
+
4
+ export function StoreMerge(key: any, value: any): Function {
5
+ return StoreFn((store: Store) => {
6
+ store.merge(key, value);
7
+ });
8
+ }
@@ -0,0 +1,8 @@
1
+ import {Store} from "../domain/Store.js";
2
+ import {StoreFn} from "./storeFn.js";
3
+
4
+ export function StoreSet(key: any, value: any): Function {
5
+ return StoreFn((store: Store) => {
6
+ store.set(key, value);
7
+ });
8
+ }
@@ -0,0 +1,158 @@
1
+ import {createReadStream} from "fs";
2
+ import {of} from "rxjs";
3
+
4
+ import {catchAsyncError} from "../utils/catchError.js";
5
+ import {isStream} from "../utils/objects/isStream.js";
6
+ import {AnyToPromise, AnyToPromiseStatus} from "./AnyToPromise.js";
7
+
8
+ const rootDir = import.meta.dirname; // automatically replaced by import.meta.dirname on build
9
+
10
+ describe("AnyToPromise", () => {
11
+ it("should handle sync value", async () => {
12
+ const resolver = new AnyToPromise();
13
+
14
+ const result = await resolver.call(() => {
15
+ return "test";
16
+ });
17
+
18
+ resolver.cancel();
19
+ resolver.resolve();
20
+ resolver.next();
21
+ resolver.reject(new Error(""));
22
+
23
+ await resolver.call(() => {
24
+ return "test";
25
+ });
26
+
27
+ expect(result).toEqual({state: "RESOLVED", data: "test", type: "DATA"});
28
+ });
29
+
30
+ it("should handle async value", async () => {
31
+ const resolver = new AnyToPromise();
32
+
33
+ const result = await resolver.call(() => {
34
+ return Promise.resolve("test");
35
+ });
36
+
37
+ expect(result).toEqual({state: "RESOLVED", data: "test", type: "DATA"});
38
+ });
39
+ it("should handle canceled async value", async () => {
40
+ const resolver = new AnyToPromise();
41
+
42
+ const result = await resolver.call(() => {
43
+ return Promise.resolve(AnyToPromiseStatus.CANCELED);
44
+ });
45
+
46
+ expect(result).toEqual({state: "CANCELED"});
47
+ });
48
+ it("should handle response with data", async () => {
49
+ const resolver = new AnyToPromise();
50
+
51
+ const result = await resolver.call(() => {
52
+ return Promise.resolve({
53
+ data: "data",
54
+ headers: {
55
+ "Content-Type": "type"
56
+ },
57
+ status: 200,
58
+ statusText: "OK"
59
+ });
60
+ });
61
+
62
+ expect(result).toEqual({
63
+ state: "RESOLVED",
64
+ data: "data",
65
+ headers: {
66
+ "Content-Type": "type"
67
+ },
68
+ status: 200,
69
+ type: "DATA"
70
+ });
71
+ });
72
+ it("should handle async undefined value", async () => {
73
+ const resolver = new AnyToPromise();
74
+
75
+ const result = await resolver.call(() => {
76
+ return Promise.resolve(undefined);
77
+ });
78
+
79
+ expect(result).toEqual({state: "RESOLVED", data: undefined, type: "DATA"});
80
+ });
81
+ it("should handle observable", async () => {
82
+ const resolver = new AnyToPromise();
83
+
84
+ const result = await resolver.call(() => {
85
+ return of("test");
86
+ });
87
+
88
+ expect(result).toEqual({state: "RESOLVED", data: "test", type: "DATA"});
89
+ });
90
+ it("should handle buffer", async () => {
91
+ const resolver = new AnyToPromise();
92
+
93
+ const result = await resolver.call(() => {
94
+ return Buffer.from("test");
95
+ });
96
+
97
+ expect(Buffer.isBuffer(result.data)).toBe(true);
98
+ expect(result.type).toBe("BUFFER");
99
+ });
100
+ it("should handle stream", async () => {
101
+ const resolver = new AnyToPromise();
102
+
103
+ const result = await resolver.call(() => {
104
+ return createReadStream(rootDir + "/__mock__/response.txt");
105
+ });
106
+
107
+ expect(result.type).toBe("STREAM");
108
+ expect(isStream(result.data)).toBe(true);
109
+ });
110
+
111
+ it("should catch error", async () => {
112
+ const resolver = new AnyToPromise();
113
+
114
+ const result = await resolver.call(() => {
115
+ return Promise.resolve("test");
116
+ });
117
+
118
+ expect(result).toEqual({state: "RESOLVED", data: "test", type: "DATA"});
119
+ });
120
+
121
+ it("should handle async error", async () => {
122
+ const resolver = new AnyToPromise();
123
+
124
+ const error = await catchAsyncError(() => {
125
+ return resolver.call(() => {
126
+ return Promise.reject(new Error("test"));
127
+ });
128
+ });
129
+
130
+ expect(error?.message).toEqual("test");
131
+ });
132
+
133
+ it("should handle sync error", async () => {
134
+ const resolver = new AnyToPromise();
135
+
136
+ const error = await catchAsyncError(() => {
137
+ return resolver.call(() => {
138
+ throw new Error("test");
139
+ });
140
+ });
141
+
142
+ expect(error?.message).toEqual("test");
143
+ });
144
+
145
+ it("should handle next callback with error", async () => {
146
+ const resolver = new AnyToPromise();
147
+ const {next} = resolver;
148
+
149
+ const error = await catchAsyncError(() => {
150
+ return resolver.call(() => {
151
+ next(new Error("test"));
152
+ return Promise.resolve();
153
+ });
154
+ });
155
+
156
+ expect(error?.message).toEqual("test");
157
+ });
158
+ });
@@ -0,0 +1,181 @@
1
+ import {isObject} from "../utils/objects/isObject.js";
2
+ import {isObservable} from "../utils/objects/isObservable.js";
3
+ import {isPromise} from "../utils/objects/isPromise.js";
4
+ import {isStream} from "../utils/objects/isStream.js";
5
+
6
+ /**
7
+ * @ignore
8
+ */
9
+ function isResponse(obj: any) {
10
+ return isObject(obj) && "data" in obj && "headers" in obj && "status" in obj && "statusText" in obj;
11
+ }
12
+
13
+ /**
14
+ * @ignore
15
+ */
16
+ export enum AnyToPromiseStatus {
17
+ PENDING = "PENDING",
18
+ CANCELED = "CANCELED",
19
+ RESOLVED = "RESOLVED",
20
+ REJECTED = "REJECTED"
21
+ }
22
+
23
+ export enum AnyToPromiseResponseTypes {
24
+ STREAM = "STREAM",
25
+ BUFFER = "BUFFER",
26
+ DATA = "DATA"
27
+ }
28
+
29
+ export interface AnyPromiseResult<T = any> {
30
+ state: AnyToPromiseStatus;
31
+ type: AnyToPromiseResponseTypes;
32
+ status?: number;
33
+ headers?: Record<string, any>;
34
+ data: T;
35
+ }
36
+
37
+ export class AnyToPromise<T = any> {
38
+ public status = AnyToPromiseStatus.PENDING;
39
+ public args: any[];
40
+
41
+ #resolves: any;
42
+ #rejects: any;
43
+ #hasNextFunction = false;
44
+
45
+ readonly #promise: Promise<AnyPromiseResult<T>>;
46
+
47
+ constructor({hasNextFunction = false}: {hasNextFunction?: boolean} = {}) {
48
+ this.#hasNextFunction = hasNextFunction;
49
+
50
+ this.#promise = new Promise((resolve: any, reject: any) => {
51
+ this.#resolves = resolve;
52
+ this.#rejects = reject;
53
+ });
54
+ }
55
+
56
+ isDone(): boolean {
57
+ return this.status !== AnyToPromiseStatus.PENDING;
58
+ }
59
+
60
+ get next() {
61
+ this.#hasNextFunction = true;
62
+
63
+ return (error?: any) => {
64
+ if (this.isDone()) {
65
+ return;
66
+ }
67
+
68
+ return error ? this.reject(error) : this.resolve({type: AnyToPromiseResponseTypes.DATA});
69
+ };
70
+ }
71
+
72
+ /**
73
+ *
74
+ */
75
+ call(cb: Function): Promise<AnyPromiseResult<T>> {
76
+ if (this.isDone()) {
77
+ return Promise.resolve(this as any);
78
+ }
79
+
80
+ try {
81
+ this.handle(cb());
82
+ } catch (er) {
83
+ this.reject(er);
84
+ }
85
+
86
+ return this.#promise;
87
+ }
88
+
89
+ reject(er: any) {
90
+ if (this.isDone()) {
91
+ return;
92
+ }
93
+
94
+ this.done(AnyToPromiseStatus.REJECTED);
95
+
96
+ this.#rejects(er);
97
+ }
98
+
99
+ resolve(response: any = {}) {
100
+ if (this.isDone()) {
101
+ return;
102
+ }
103
+
104
+ this.done(AnyToPromiseStatus.RESOLVED);
105
+
106
+ this.#resolves({...response, state: AnyToPromiseStatus.RESOLVED});
107
+ }
108
+
109
+ destroy() {}
110
+
111
+ cancel() {
112
+ if (this.isDone()) {
113
+ return;
114
+ }
115
+
116
+ this.done(AnyToPromiseStatus.CANCELED);
117
+
118
+ return this.#resolves({state: AnyToPromiseStatus.CANCELED});
119
+ }
120
+
121
+ done(status: AnyToPromiseStatus) {
122
+ this.destroy();
123
+ this.status = status;
124
+ }
125
+
126
+ handle(process: any, additionalProps = {}): any {
127
+ if (this.isDone()) {
128
+ return;
129
+ }
130
+
131
+ if (process) {
132
+ if (this.isCanceledResponse(process)) {
133
+ // ABANDON
134
+ return this.cancel();
135
+ }
136
+
137
+ if (isObservable(process)) {
138
+ process = process.toPromise();
139
+ }
140
+
141
+ if (isResponse(process)) {
142
+ return this.handle(process.data, {
143
+ ...additionalProps,
144
+ status: process.status,
145
+ headers: process.headers
146
+ });
147
+ }
148
+
149
+ if (isStream(process)) {
150
+ return this.resolve({...additionalProps, type: AnyToPromiseResponseTypes.STREAM, data: process});
151
+ }
152
+ if (isStream(process) || Buffer.isBuffer(process)) {
153
+ return this.resolve({...additionalProps, type: AnyToPromiseResponseTypes.BUFFER, data: process});
154
+ }
155
+
156
+ if (isPromise(process)) {
157
+ return process
158
+ .then((result: any) => this.handle(result, additionalProps))
159
+ .catch((error: any) => {
160
+ if (error.response && isResponse(error.response)) {
161
+ return this.handle(error.response);
162
+ }
163
+ return this.reject(error);
164
+ });
165
+ }
166
+ }
167
+
168
+ if (!this.#hasNextFunction) {
169
+ // no next function and empty response
170
+ return this.resolve({
171
+ ...additionalProps,
172
+ data: process,
173
+ type: AnyToPromiseResponseTypes.DATA
174
+ });
175
+ }
176
+ }
177
+
178
+ protected isCanceledResponse(process: any) {
179
+ return process === AnyToPromiseStatus.CANCELED;
180
+ }
181
+ }
@@ -0,0 +1,10 @@
1
+ export enum DecoratorTypes {
2
+ PARAM = "parameter",
3
+ PARAM_CTOR = "parameter.constructor",
4
+ PARAM_STC = "parameter.static",
5
+ PROP = "property",
6
+ PROP_STC = "property.static",
7
+ METHOD = "method",
8
+ METHOD_STC = "method.static",
9
+ CLASS = "class"
10
+ }
@@ -0,0 +1,15 @@
1
+ /**
2
+ *
3
+ */
4
+ export enum Env {
5
+ PROD = "production",
6
+ DEV = "development",
7
+ TEST = "test"
8
+ }
9
+
10
+ /**
11
+ * Fallback
12
+ * @type {Env}
13
+ */
14
+ // tslint:disable-next-line: variable-name
15
+ export const EnvTypes = Env;
@@ -0,0 +1,142 @@
1
+ import {Metadata} from "./Metadata.js";
2
+
3
+ function logger(target: any, method?: any, descriptor?: any) {
4
+ return descriptor;
5
+ }
6
+
7
+ @logger
8
+ class Test {
9
+ @logger
10
+ attribut: string = "";
11
+
12
+ // tslint:disable-next-line: no-unused-variable
13
+ constructor(private type?: string) {}
14
+
15
+ static methodStatic() {}
16
+
17
+ @logger
18
+ method(type: string): boolean {
19
+ return true;
20
+ }
21
+ }
22
+
23
+ class Test2 {
24
+ attribut: any;
25
+
26
+ constructor() {}
27
+
28
+ static methodStatic() {}
29
+
30
+ method() {}
31
+ }
32
+
33
+ describe("Metadata", () => {
34
+ describe("has", () => {
35
+ it("should return false (String)", () => {
36
+ expect(Metadata.has("testunknow", String)).toBe(false);
37
+ });
38
+
39
+ it("should return false (bad target)", () => {
40
+ expect(Metadata.has("testunknow", undefined)).toBe(false);
41
+ });
42
+ });
43
+
44
+ describe("set", () => {
45
+ it("should set meta on a class", () => {
46
+ expect(Metadata.set("metadatakey1", "test1", Test)).toBeUndefined();
47
+ expect(Metadata.has("metadatakey1", Test)).toBe(true);
48
+ });
49
+
50
+ it("should set meta on instance", () => {
51
+ expect(Metadata.set("metadatakey2", "test2", new Test())).toBeUndefined();
52
+ expect(Metadata.has("metadatakey2", Test)).toBe(true);
53
+ });
54
+
55
+ it("should set meta on a method", () => {
56
+ expect(Metadata.set("metadatakey3", "test1", Test, "method")).toBeUndefined();
57
+ expect(Metadata.has("metadatakey3", Test, "method")).toBe(true);
58
+ });
59
+ });
60
+
61
+ describe("get", () => {
62
+ it("should get meta on a class", () => {
63
+ expect(Metadata.get("metadatakey1", Test)).toBe("test1");
64
+ });
65
+
66
+ it("should get meta on a method", () => {
67
+ expect(Metadata.get("metadatakey3", Test, "method")).toBe("test1");
68
+ });
69
+ });
70
+
71
+ describe("getOwn", () => {
72
+ it("should get meta on a class", () => {
73
+ expect(Metadata.getOwn("metadatakey1", Test)).toBe("test1");
74
+ });
75
+
76
+ it("should get meta on a method", () => {
77
+ expect(Metadata.getOwn("metadatakey3", Test, "method")).toBe("test1");
78
+ });
79
+ });
80
+
81
+ describe("delete", () => {
82
+ it("should remove meta on a class", () => {
83
+ expect(Metadata.delete("metadatakey1", Test)).toBe(true);
84
+ });
85
+ });
86
+
87
+ describe("getType", () => {
88
+ it("should return attribut type", () => {
89
+ expect(Metadata.getType(Test.prototype, "attribut")).toBe(String);
90
+ });
91
+ });
92
+
93
+ describe("getOwnType", () => {
94
+ it("should return attribut type", () => {
95
+ expect(Metadata.getOwnType(Test.prototype, "attribut")).toBe(String);
96
+ });
97
+ });
98
+
99
+ describe("getParamTypes", () => {
100
+ it("should return types on constructor", () => {
101
+ expect(Metadata.getParamTypes(Test)).toBeInstanceOf(Array);
102
+ expect(Metadata.getParamTypes(Test)[0]).toBe(String);
103
+ });
104
+
105
+ it("should return types on method", () => {
106
+ expect(Metadata.getParamTypes(Test.prototype, "method")).toBeInstanceOf(Array);
107
+ expect(Metadata.getParamTypes(Test.prototype, "method")[0]).toBe(String);
108
+ });
109
+ });
110
+
111
+ describe("getOwnParamTypes", () => {
112
+ it("should return types on constructor", () => {
113
+ expect(Metadata.getOwnParamTypes(Test)).toBeInstanceOf(Array);
114
+ expect(Metadata.getOwnParamTypes(Test)[0]).toBe(String);
115
+ });
116
+
117
+ it("should return types on method", () => {
118
+ expect(Metadata.getOwnParamTypes(Test.prototype, "method")).toBeInstanceOf(Array);
119
+ expect(Metadata.getOwnParamTypes(Test.prototype, "method")[0]).toBe(String);
120
+ });
121
+ });
122
+
123
+ describe("list", () => {
124
+ it("should return unique provide from property key", () => {
125
+ Metadata.set("controller", "test", Test);
126
+ Metadata.set("controller", "test2", Test2);
127
+ Metadata.set("controller", "test", Test);
128
+
129
+ const result = Metadata.getTargetsFromPropertyKey("controller");
130
+
131
+ expect(result).toBeInstanceOf(Array);
132
+ // expect(result.length).to.equal(2);
133
+ expect(result.indexOf(Test) > -1).toBe(true);
134
+ expect(result.indexOf(Test2) > -1).toBe(true);
135
+
136
+ const result2 = Metadata.getTargetsFromPropertyKey("controller2");
137
+
138
+ expect(result2).toBeInstanceOf(Array);
139
+ expect(result2.length).toBe(0);
140
+ });
141
+ });
142
+ });