@loopback/metadata 4.0.0-alpha.6 → 4.0.0
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.
- package/LICENSE +1 -1
- package/README.md +273 -150
- package/dist/decorator-factory.d.ts +297 -0
- package/{dist6/src → dist}/decorator-factory.js +197 -90
- package/dist/decorator-factory.js.map +1 -0
- package/dist/index.d.ts +22 -1
- package/dist/index.js +24 -7
- package/dist/index.js.map +1 -0
- package/dist/inspector.d.ts +120 -0
- package/dist/inspector.js +186 -0
- package/dist/inspector.js.map +1 -0
- package/dist/{src/reflect.d.ts → reflect.d.ts} +16 -15
- package/dist/{src/reflect.js → reflect.js} +7 -8
- package/dist/reflect.js.map +1 -0
- package/dist/types.d.ts +70 -0
- package/dist/types.js +32 -0
- package/dist/types.js.map +1 -0
- package/package.json +43 -44
- package/src/decorator-factory.ts +289 -140
- package/src/index.ts +22 -2
- package/src/inspector.ts +118 -126
- package/src/reflect.ts +32 -21
- package/src/types.ts +94 -0
- package/CHANGELOG.md +0 -75
- package/api-docs/apple-touch-icon-114x114-precomposed.png +0 -0
- package/api-docs/apple-touch-icon-144x144-precomposed.png +0 -0
- package/api-docs/apple-touch-icon-57x57-precomposed.png +0 -0
- package/api-docs/apple-touch-icon-72x72-precomposed.png +0 -0
- package/api-docs/apple-touch-icon-precomposed.png +0 -0
- package/api-docs/apple-touch-icon.png +0 -0
- package/api-docs/css/bootstrap.min.css +0 -9
- package/api-docs/css/code-themes/arta.css +0 -158
- package/api-docs/css/code-themes/ascetic.css +0 -50
- package/api-docs/css/code-themes/brown_paper.css +0 -104
- package/api-docs/css/code-themes/brown_papersq.png +0 -0
- package/api-docs/css/code-themes/dark.css +0 -103
- package/api-docs/css/code-themes/default.css +0 -135
- package/api-docs/css/code-themes/far.css +0 -111
- package/api-docs/css/code-themes/github.css +0 -127
- package/api-docs/css/code-themes/googlecode.css +0 -144
- package/api-docs/css/code-themes/idea.css +0 -121
- package/api-docs/css/code-themes/ir_black.css +0 -104
- package/api-docs/css/code-themes/magula.css +0 -121
- package/api-docs/css/code-themes/monokai.css +0 -114
- package/api-docs/css/code-themes/pojoaque.css +0 -104
- package/api-docs/css/code-themes/pojoaque.jpg +0 -0
- package/api-docs/css/code-themes/rainbow.css +0 -114
- package/api-docs/css/code-themes/school_book.css +0 -111
- package/api-docs/css/code-themes/school_book.png +0 -0
- package/api-docs/css/code-themes/sl-theme.css +0 -45
- package/api-docs/css/code-themes/solarized_dark.css +0 -88
- package/api-docs/css/code-themes/solarized_light.css +0 -88
- package/api-docs/css/code-themes/sunburst.css +0 -158
- package/api-docs/css/code-themes/tomorrow-night-blue.css +0 -52
- package/api-docs/css/code-themes/tomorrow-night-bright.css +0 -51
- package/api-docs/css/code-themes/tomorrow-night-eighties.css +0 -51
- package/api-docs/css/code-themes/tomorrow-night.css +0 -52
- package/api-docs/css/code-themes/tomorrow.css +0 -49
- package/api-docs/css/code-themes/vs.css +0 -86
- package/api-docs/css/code-themes/xcode.css +0 -154
- package/api-docs/css/code-themes/zenburn.css +0 -115
- package/api-docs/css/main.css +0 -139
- package/api-docs/favicon.ico +0 -0
- package/api-docs/fonts/0ihfXUL2emPh0ROJezvraLO3LdcAZYWl9Si6vvxL-qU.woff +0 -0
- package/api-docs/fonts/OsJ2DjdpjqFRVUSto6IffLO3LdcAZYWl9Si6vvxL-qU.woff +0 -0
- package/api-docs/fonts/_aijTyevf54tkVDLy-dlnLO3LdcAZYWl9Si6vvxL-qU.woff +0 -0
- package/api-docs/index.html +0 -4594
- package/api-docs/js/main.js +0 -19
- package/api-docs/js/vendor/bootstrap.min.js +0 -6
- package/api-docs/js/vendor/jquery-1.10.1.min.js +0 -6
- package/api-docs/js/vendor/jquery.scrollTo-1.4.3.1.js +0 -218
- package/api-docs/js/vendor/modernizr-2.6.2-respond-1.1.0.min.js +0 -11
- package/dist/src/decorator-factory.d.ts +0 -252
- package/dist/src/decorator-factory.js +0 -468
- package/dist/src/decorator-factory.js.map +0 -1
- package/dist/src/index.d.ts +0 -3
- package/dist/src/index.js +0 -13
- package/dist/src/index.js.map +0 -1
- package/dist/src/inspector.d.ts +0 -150
- package/dist/src/inspector.js +0 -163
- package/dist/src/inspector.js.map +0 -1
- package/dist/src/reflect.js.map +0 -1
- package/dist6/index.d.ts +0 -1
- package/dist6/index.js +0 -13
- package/dist6/src/decorator-factory.d.ts +0 -252
- package/dist6/src/decorator-factory.js.map +0 -1
- package/dist6/src/index.d.ts +0 -3
- package/dist6/src/index.js +0 -13
- package/dist6/src/index.js.map +0 -1
- package/dist6/src/inspector.d.ts +0 -150
- package/dist6/src/inspector.js +0 -163
- package/dist6/src/inspector.js.map +0 -1
- package/dist6/src/reflect.d.ts +0 -38
- package/dist6/src/reflect.js +0 -143
- package/dist6/src/reflect.js.map +0 -1
- package/index.d.ts +0 -6
- package/index.js +0 -7
package/src/index.ts
CHANGED
|
@@ -1,8 +1,28 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2017. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. 2017,2020. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/metadata
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
/**
|
|
7
|
+
* A package with utilities to help developers implement
|
|
8
|
+
* {@link https://www.typescriptlang.org/docs/handbook/decorators.html |TypeScript decorators},
|
|
9
|
+
* define/merge metadata, and inspect metadata.
|
|
10
|
+
*
|
|
11
|
+
* @remarks
|
|
12
|
+
* Utilities:
|
|
13
|
+
*
|
|
14
|
+
* - Reflector: Wrapper of
|
|
15
|
+
* {@link https://github.com/rbuckton/reflect-metadata | reflect-metadata}.
|
|
16
|
+
* - Decorator factories: A set of factories for class/method/property/parameter
|
|
17
|
+
* decorators to apply metadata to a given class and its static or instance
|
|
18
|
+
* members.
|
|
19
|
+
* - MetadataInspector: High level APIs to inspect a class and/or its members to
|
|
20
|
+
* get metadata applied by decorators.
|
|
21
|
+
*
|
|
22
|
+
* @packageDocumentation
|
|
23
|
+
*/
|
|
24
|
+
|
|
7
25
|
export * from './decorator-factory';
|
|
8
26
|
export * from './inspector';
|
|
27
|
+
export * from './reflect';
|
|
28
|
+
export * from './types';
|
package/src/inspector.ts
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2017,
|
|
1
|
+
// Copyright IBM Corp. 2017,2019. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/metadata
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
|
+
import debugModule from 'debug';
|
|
6
|
+
import {DecoratorFactory} from './decorator-factory';
|
|
7
|
+
import {NamespacedReflect, Reflector} from './reflect';
|
|
8
|
+
import {
|
|
9
|
+
DecoratorType,
|
|
10
|
+
DesignTimeMethodMetadata,
|
|
11
|
+
MetadataKey,
|
|
12
|
+
MetadataMap,
|
|
13
|
+
} from './types';
|
|
5
14
|
|
|
6
|
-
|
|
7
|
-
import {MetadataMap} from './decorator-factory';
|
|
15
|
+
const debug = debugModule('loopback:metadata:inspector');
|
|
8
16
|
|
|
9
17
|
/**
|
|
10
18
|
* TypeScript reflector without a namespace. The TypeScript compiler can be
|
|
@@ -14,44 +22,6 @@ import {MetadataMap} from './decorator-factory';
|
|
|
14
22
|
*/
|
|
15
23
|
const TSReflector = new NamespacedReflect();
|
|
16
24
|
|
|
17
|
-
/**
|
|
18
|
-
* Design time metadata for a method.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* ```ts
|
|
22
|
-
* class MyController
|
|
23
|
-
* {
|
|
24
|
-
* myMethod(x: string, y: number, z: MyClass): boolean {
|
|
25
|
-
* // ...
|
|
26
|
-
* return true;
|
|
27
|
-
* }
|
|
28
|
-
* }
|
|
29
|
-
* ```
|
|
30
|
-
*
|
|
31
|
-
* The `myMethod` above has design-time metadata as follows:
|
|
32
|
-
* ```ts
|
|
33
|
-
* {
|
|
34
|
-
* type: Function,
|
|
35
|
-
* parameterTypes: [String, Number, MyClass],
|
|
36
|
-
* returnType: Boolean
|
|
37
|
-
* }
|
|
38
|
-
* ```
|
|
39
|
-
*/
|
|
40
|
-
export interface DesignTimeMethodMetadata {
|
|
41
|
-
/**
|
|
42
|
-
* Type of the method itself. It is `Function`
|
|
43
|
-
*/
|
|
44
|
-
type: Function;
|
|
45
|
-
/**
|
|
46
|
-
* An array of parameter types
|
|
47
|
-
*/
|
|
48
|
-
parameterTypes: Function[];
|
|
49
|
-
/**
|
|
50
|
-
* Return type
|
|
51
|
-
*/
|
|
52
|
-
returnType: Function;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
25
|
/**
|
|
56
26
|
* Options for inspection
|
|
57
27
|
*/
|
|
@@ -82,185 +52,188 @@ export class MetadataInspector {
|
|
|
82
52
|
|
|
83
53
|
/**
|
|
84
54
|
* Get the metadata associated with the given key for a given class
|
|
85
|
-
* @param key Metadata key
|
|
86
|
-
* @param target Class that contains the metadata
|
|
87
|
-
* @param options Options for inspection
|
|
55
|
+
* @param key - Metadata key
|
|
56
|
+
* @param target - Class that contains the metadata
|
|
57
|
+
* @param options - Options for inspection
|
|
88
58
|
*/
|
|
89
59
|
static getClassMetadata<T>(
|
|
90
|
-
key:
|
|
60
|
+
key: MetadataKey<T, ClassDecorator>,
|
|
91
61
|
target: Function,
|
|
92
62
|
options?: InspectionOptions,
|
|
93
63
|
): T | undefined {
|
|
94
|
-
return options
|
|
95
|
-
? Reflector.getOwnMetadata(key, target)
|
|
96
|
-
: Reflector.getMetadata(key, target);
|
|
64
|
+
return options?.ownMetadataOnly
|
|
65
|
+
? Reflector.getOwnMetadata(key.toString(), target)
|
|
66
|
+
: Reflector.getMetadata(key.toString(), target);
|
|
97
67
|
}
|
|
98
68
|
|
|
99
69
|
/**
|
|
100
70
|
* Define metadata for the given target
|
|
101
|
-
* @param key Metadata key
|
|
102
|
-
* @param value Metadata value
|
|
103
|
-
* @param target Target for the metadata
|
|
104
|
-
* @param member Optional property or method name
|
|
71
|
+
* @param key - Metadata key
|
|
72
|
+
* @param value - Metadata value
|
|
73
|
+
* @param target - Target for the metadata
|
|
74
|
+
* @param member - Optional property or method name
|
|
105
75
|
*/
|
|
106
76
|
static defineMetadata<T>(
|
|
107
|
-
key:
|
|
77
|
+
key: MetadataKey<T, DecoratorType>,
|
|
108
78
|
value: T,
|
|
109
79
|
target: Object,
|
|
110
|
-
member?: string
|
|
80
|
+
member?: string,
|
|
111
81
|
) {
|
|
112
|
-
Reflector.defineMetadata(key, value, target, member);
|
|
82
|
+
Reflector.defineMetadata(key.toString(), value, target, member);
|
|
113
83
|
}
|
|
114
84
|
|
|
115
85
|
/**
|
|
116
86
|
* Get the metadata associated with the given key for all methods of the
|
|
117
87
|
* target class or prototype
|
|
118
|
-
* @param key Metadata key
|
|
119
|
-
* @param target Class for static methods or prototype for instance methods
|
|
120
|
-
* @param options Options for inspection
|
|
88
|
+
* @param key - Metadata key
|
|
89
|
+
* @param target - Class for static methods or prototype for instance methods
|
|
90
|
+
* @param options - Options for inspection
|
|
121
91
|
*/
|
|
122
92
|
static getAllMethodMetadata<T>(
|
|
123
|
-
key:
|
|
93
|
+
key: MetadataKey<T, MethodDecorator>,
|
|
124
94
|
target: Object,
|
|
125
95
|
options?: InspectionOptions,
|
|
126
96
|
): MetadataMap<T> | undefined {
|
|
127
|
-
return options
|
|
128
|
-
? Reflector.getOwnMetadata(key, target)
|
|
129
|
-
: Reflector.getMetadata(key, target);
|
|
97
|
+
return options?.ownMetadataOnly
|
|
98
|
+
? Reflector.getOwnMetadata(key.toString(), target)
|
|
99
|
+
: Reflector.getMetadata(key.toString(), target);
|
|
130
100
|
}
|
|
131
101
|
|
|
132
102
|
/**
|
|
133
103
|
* Get the metadata associated with the given key for a given method of the
|
|
134
104
|
* target class or prototype
|
|
135
|
-
* @param key Metadata key
|
|
136
|
-
* @param target Class for static methods or prototype for instance methods
|
|
137
|
-
* @param methodName Method name. If not present, default to '' to use
|
|
105
|
+
* @param key - Metadata key
|
|
106
|
+
* @param target - Class for static methods or prototype for instance methods
|
|
107
|
+
* @param methodName - Method name. If not present, default to '' to use
|
|
138
108
|
* the constructor
|
|
139
|
-
* @param options Options for inspection
|
|
109
|
+
* @param options - Options for inspection
|
|
140
110
|
*/
|
|
141
111
|
static getMethodMetadata<T>(
|
|
142
|
-
key:
|
|
112
|
+
key: MetadataKey<T, MethodDecorator>,
|
|
143
113
|
target: Object,
|
|
144
|
-
methodName?: string
|
|
114
|
+
methodName?: string,
|
|
145
115
|
options?: InspectionOptions,
|
|
146
116
|
): T | undefined {
|
|
147
|
-
methodName = methodName
|
|
148
|
-
const meta: MetadataMap<T> =
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
return meta && meta[methodName];
|
|
117
|
+
methodName = methodName ?? '';
|
|
118
|
+
const meta: MetadataMap<T> = options?.ownMetadataOnly
|
|
119
|
+
? Reflector.getOwnMetadata(key.toString(), target)
|
|
120
|
+
: Reflector.getMetadata(key.toString(), target);
|
|
121
|
+
return meta?.[methodName];
|
|
153
122
|
}
|
|
154
123
|
|
|
155
124
|
/**
|
|
156
125
|
* Get the metadata associated with the given key for all properties of the
|
|
157
126
|
* target class or prototype
|
|
158
|
-
* @param key Metadata key
|
|
159
|
-
* @param target Class for static methods or prototype for instance methods
|
|
160
|
-
* @param options Options for inspection
|
|
127
|
+
* @param key - Metadata key
|
|
128
|
+
* @param target - Class for static methods or prototype for instance methods
|
|
129
|
+
* @param options - Options for inspection
|
|
161
130
|
*/
|
|
162
131
|
static getAllPropertyMetadata<T>(
|
|
163
|
-
key:
|
|
132
|
+
key: MetadataKey<T, PropertyDecorator>,
|
|
164
133
|
target: Object,
|
|
165
134
|
options?: InspectionOptions,
|
|
166
135
|
): MetadataMap<T> | undefined {
|
|
167
|
-
return options
|
|
168
|
-
? Reflector.getOwnMetadata(key, target)
|
|
169
|
-
: Reflector.getMetadata(key, target);
|
|
136
|
+
return options?.ownMetadataOnly
|
|
137
|
+
? Reflector.getOwnMetadata(key.toString(), target)
|
|
138
|
+
: Reflector.getMetadata(key.toString(), target);
|
|
170
139
|
}
|
|
171
140
|
|
|
172
141
|
/**
|
|
173
142
|
* Get the metadata associated with the given key for a given property of the
|
|
174
143
|
* target class or prototype
|
|
175
|
-
* @param key Metadata key
|
|
176
|
-
* @param target Class for static properties or prototype for instance
|
|
144
|
+
* @param key - Metadata key
|
|
145
|
+
* @param target - Class for static properties or prototype for instance
|
|
177
146
|
* properties
|
|
178
|
-
* @param propertyName Property name
|
|
179
|
-
* @param options Options for inspection
|
|
147
|
+
* @param propertyName - Property name
|
|
148
|
+
* @param options - Options for inspection
|
|
180
149
|
*/
|
|
181
150
|
static getPropertyMetadata<T>(
|
|
182
|
-
key:
|
|
151
|
+
key: MetadataKey<T, PropertyDecorator>,
|
|
183
152
|
target: Object,
|
|
184
|
-
propertyName: string
|
|
153
|
+
propertyName: string,
|
|
185
154
|
options?: InspectionOptions,
|
|
186
155
|
): T | undefined {
|
|
187
|
-
const meta: MetadataMap<T> =
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
return meta && meta[propertyName];
|
|
156
|
+
const meta: MetadataMap<T> = options?.ownMetadataOnly
|
|
157
|
+
? Reflector.getOwnMetadata(key.toString(), target)
|
|
158
|
+
: Reflector.getMetadata(key.toString(), target);
|
|
159
|
+
return meta?.[propertyName];
|
|
192
160
|
}
|
|
193
161
|
|
|
194
162
|
/**
|
|
195
163
|
* Get the metadata associated with the given key for all parameters of a
|
|
196
164
|
* given method
|
|
197
|
-
* @param key Metadata key
|
|
198
|
-
* @param target Class for static methods or prototype for instance methods
|
|
199
|
-
* @param methodName Method name. If not present, default to '' to use
|
|
165
|
+
* @param key - Metadata key
|
|
166
|
+
* @param target - Class for static methods or prototype for instance methods
|
|
167
|
+
* @param methodName - Method name. If not present, default to '' to use
|
|
200
168
|
* the constructor
|
|
201
|
-
* @param options Options for inspection
|
|
169
|
+
* @param options - Options for inspection
|
|
202
170
|
*/
|
|
203
171
|
static getAllParameterMetadata<T>(
|
|
204
|
-
key:
|
|
172
|
+
key: MetadataKey<T, ParameterDecorator>,
|
|
205
173
|
target: Object,
|
|
206
|
-
methodName?: string
|
|
174
|
+
methodName?: string,
|
|
207
175
|
options?: InspectionOptions,
|
|
208
176
|
): T[] | undefined {
|
|
209
|
-
methodName = methodName
|
|
210
|
-
const meta: MetadataMap<T[]> =
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
return meta && meta[methodName];
|
|
177
|
+
methodName = methodName ?? '';
|
|
178
|
+
const meta: MetadataMap<T[]> = options?.ownMetadataOnly
|
|
179
|
+
? Reflector.getOwnMetadata(key.toString(), target)
|
|
180
|
+
: Reflector.getMetadata(key.toString(), target);
|
|
181
|
+
return meta?.[methodName];
|
|
215
182
|
}
|
|
216
183
|
|
|
217
184
|
/**
|
|
218
185
|
* Get the metadata associated with the given key for a parameter of a given
|
|
219
186
|
* method by index
|
|
220
|
-
* @param key Metadata key
|
|
221
|
-
* @param target Class for static methods or prototype for instance methods
|
|
222
|
-
* @param methodName Method name. If not present, default to '' to use
|
|
187
|
+
* @param key - Metadata key
|
|
188
|
+
* @param target - Class for static methods or prototype for instance methods
|
|
189
|
+
* @param methodName - Method name. If not present, default to '' to use
|
|
223
190
|
* the constructor
|
|
224
|
-
* @param index Index of the parameter, starting with 0
|
|
225
|
-
* @param options Options for inspection
|
|
191
|
+
* @param index - Index of the parameter, starting with 0
|
|
192
|
+
* @param options - Options for inspection
|
|
226
193
|
*/
|
|
227
194
|
static getParameterMetadata<T>(
|
|
228
|
-
key:
|
|
195
|
+
key: MetadataKey<T, ParameterDecorator>,
|
|
229
196
|
target: Object,
|
|
230
|
-
methodName: string
|
|
197
|
+
methodName: string,
|
|
231
198
|
index: number,
|
|
232
199
|
options?: InspectionOptions,
|
|
233
200
|
): T | undefined {
|
|
234
201
|
methodName = methodName || '';
|
|
235
|
-
const meta: MetadataMap<T[]> =
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
return params && params[index];
|
|
202
|
+
const meta: MetadataMap<T[]> = options?.ownMetadataOnly
|
|
203
|
+
? Reflector.getOwnMetadata(key.toString(), target)
|
|
204
|
+
: Reflector.getMetadata(key.toString(), target);
|
|
205
|
+
const params = meta?.[methodName];
|
|
206
|
+
return params?.[index];
|
|
241
207
|
}
|
|
242
208
|
|
|
243
209
|
/**
|
|
244
210
|
* Get TypeScript design time type for a property
|
|
245
|
-
* @param target Class or prototype
|
|
246
|
-
* @param propertyName Property name
|
|
211
|
+
* @param target - Class or prototype
|
|
212
|
+
* @param propertyName - Property name
|
|
213
|
+
* @returns Design time metadata. The return value is `undefined` when:
|
|
214
|
+
* - The property has type `undefined`, `null`
|
|
215
|
+
* - The TypeScript project has not enabled the compiler option `emitDecoratorMetadata`.
|
|
216
|
+
* - The code is written in vanilla JavaScript.
|
|
247
217
|
*/
|
|
248
218
|
static getDesignTypeForProperty(
|
|
249
219
|
target: Object,
|
|
250
|
-
propertyName: string
|
|
251
|
-
): Function {
|
|
220
|
+
propertyName: string,
|
|
221
|
+
): Function | undefined {
|
|
252
222
|
return TSReflector.getMetadata('design:type', target, propertyName);
|
|
253
223
|
}
|
|
254
224
|
|
|
255
225
|
/**
|
|
256
|
-
* Get TypeScript design time type for a method
|
|
257
|
-
* @param target Class or prototype
|
|
258
|
-
* @param methodName Method name
|
|
226
|
+
* Get TypeScript design time type for a method.
|
|
227
|
+
* @param target - Class or prototype
|
|
228
|
+
* @param methodName - Method name
|
|
229
|
+
* @returns Design time metadata. The return value is `undefined`
|
|
230
|
+
* in projects that do not enable `emitDecoratorMetadata`
|
|
231
|
+
* in TypeScript compiler options or are written in vanilla JavaScript.
|
|
259
232
|
*/
|
|
260
233
|
static getDesignTypeForMethod(
|
|
261
234
|
target: Object,
|
|
262
|
-
methodName: string
|
|
263
|
-
): DesignTimeMethodMetadata {
|
|
235
|
+
methodName: string,
|
|
236
|
+
): DesignTimeMethodMetadata | undefined {
|
|
264
237
|
const type = TSReflector.getMetadata('design:type', target, methodName);
|
|
265
238
|
const parameterTypes = TSReflector.getMetadata(
|
|
266
239
|
'design:paramtypes',
|
|
@@ -272,6 +245,25 @@ export class MetadataInspector {
|
|
|
272
245
|
target,
|
|
273
246
|
methodName,
|
|
274
247
|
);
|
|
248
|
+
|
|
249
|
+
if (
|
|
250
|
+
type === undefined &&
|
|
251
|
+
parameterTypes === undefined &&
|
|
252
|
+
returnType === undefined
|
|
253
|
+
) {
|
|
254
|
+
/* istanbul ignore next */
|
|
255
|
+
if (debug.enabled) {
|
|
256
|
+
const targetName = DecoratorFactory.getTargetName(target, methodName);
|
|
257
|
+
debug(
|
|
258
|
+
'No design-time type metadata found while inspecting %s. ' +
|
|
259
|
+
'Did you forget to enable TypeScript compiler option `emitDecoratorMetadata`?',
|
|
260
|
+
targetName,
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
return undefined;
|
|
265
|
+
}
|
|
266
|
+
|
|
275
267
|
return {
|
|
276
268
|
type,
|
|
277
269
|
parameterTypes,
|
package/src/reflect.ts
CHANGED
|
@@ -1,18 +1,18 @@
|
|
|
1
|
-
// Copyright IBM Corp. 2017. All Rights Reserved.
|
|
1
|
+
// Copyright IBM Corp. 2017,2019. All Rights Reserved.
|
|
2
2
|
// Node module: @loopback/metadata
|
|
3
3
|
// This file is licensed under the MIT License.
|
|
4
4
|
// License text available at https://opensource.org/licenses/MIT
|
|
5
5
|
|
|
6
6
|
import 'reflect-metadata';
|
|
7
7
|
|
|
8
|
-
/*
|
|
8
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
9
9
|
|
|
10
10
|
/*
|
|
11
11
|
* namespaced wrapper to handle reflect api
|
|
12
12
|
*/
|
|
13
13
|
export class NamespacedReflect {
|
|
14
14
|
/**
|
|
15
|
-
* @param namespace
|
|
15
|
+
* @param namespace - Namespace to bind this reflect context
|
|
16
16
|
*/
|
|
17
17
|
constructor(private namespace?: string) {}
|
|
18
18
|
|
|
@@ -28,7 +28,7 @@ export class NamespacedReflect {
|
|
|
28
28
|
metadataKey: string,
|
|
29
29
|
metadataValue: any,
|
|
30
30
|
target: Object,
|
|
31
|
-
propertyKey?: string
|
|
31
|
+
propertyKey?: string,
|
|
32
32
|
) {
|
|
33
33
|
metadataKey = this.getMetadataKey(metadataKey);
|
|
34
34
|
if (propertyKey) {
|
|
@@ -41,11 +41,7 @@ export class NamespacedReflect {
|
|
|
41
41
|
/**
|
|
42
42
|
* lookup metadata from a target object and its prototype chain
|
|
43
43
|
*/
|
|
44
|
-
getMetadata(
|
|
45
|
-
metadataKey: string,
|
|
46
|
-
target: Object,
|
|
47
|
-
propertyKey?: string | symbol,
|
|
48
|
-
): any {
|
|
44
|
+
getMetadata(metadataKey: string, target: Object, propertyKey?: string): any {
|
|
49
45
|
metadataKey = this.getMetadataKey(metadataKey);
|
|
50
46
|
if (propertyKey) {
|
|
51
47
|
return Reflect.getMetadata(metadataKey, target, propertyKey);
|
|
@@ -59,7 +55,7 @@ export class NamespacedReflect {
|
|
|
59
55
|
getOwnMetadata(
|
|
60
56
|
metadataKey: string,
|
|
61
57
|
target: Object,
|
|
62
|
-
propertyKey?: string
|
|
58
|
+
propertyKey?: string,
|
|
63
59
|
): any {
|
|
64
60
|
metadataKey = this.getMetadataKey(metadataKey);
|
|
65
61
|
if (propertyKey) {
|
|
@@ -70,14 +66,14 @@ export class NamespacedReflect {
|
|
|
70
66
|
|
|
71
67
|
/**
|
|
72
68
|
* Check if the target has corresponding metadata
|
|
73
|
-
* @param metadataKey Key
|
|
74
|
-
* @param target Target
|
|
75
|
-
* @param propertyKey Optional property key
|
|
69
|
+
* @param metadataKey - Key
|
|
70
|
+
* @param target - Target
|
|
71
|
+
* @param propertyKey - Optional property key
|
|
76
72
|
*/
|
|
77
73
|
hasMetadata(
|
|
78
74
|
metadataKey: string,
|
|
79
75
|
target: Object,
|
|
80
|
-
propertyKey?: string
|
|
76
|
+
propertyKey?: string,
|
|
81
77
|
): boolean {
|
|
82
78
|
metadataKey = this.getMetadataKey(metadataKey);
|
|
83
79
|
if (propertyKey) {
|
|
@@ -89,7 +85,7 @@ export class NamespacedReflect {
|
|
|
89
85
|
hasOwnMetadata(
|
|
90
86
|
metadataKey: string,
|
|
91
87
|
target: Object,
|
|
92
|
-
propertyKey?: string
|
|
88
|
+
propertyKey?: string,
|
|
93
89
|
): boolean {
|
|
94
90
|
metadataKey = this.getMetadataKey(metadataKey);
|
|
95
91
|
if (propertyKey) {
|
|
@@ -101,7 +97,7 @@ export class NamespacedReflect {
|
|
|
101
97
|
deleteMetadata(
|
|
102
98
|
metadataKey: string,
|
|
103
99
|
target: Object,
|
|
104
|
-
propertyKey?: string
|
|
100
|
+
propertyKey?: string,
|
|
105
101
|
): boolean {
|
|
106
102
|
metadataKey = this.getMetadataKey(metadataKey);
|
|
107
103
|
if (propertyKey) {
|
|
@@ -110,7 +106,7 @@ export class NamespacedReflect {
|
|
|
110
106
|
return Reflect.deleteMetadata(metadataKey, target);
|
|
111
107
|
}
|
|
112
108
|
|
|
113
|
-
getMetadataKeys(target: Object, propertyKey?: string
|
|
109
|
+
getMetadataKeys(target: Object, propertyKey?: string): string[] {
|
|
114
110
|
let keys: string[];
|
|
115
111
|
if (propertyKey) {
|
|
116
112
|
keys = Reflect.getMetadataKeys(target, propertyKey);
|
|
@@ -131,7 +127,7 @@ export class NamespacedReflect {
|
|
|
131
127
|
return metaKeys;
|
|
132
128
|
}
|
|
133
129
|
|
|
134
|
-
getOwnMetadataKeys(target: Object, propertyKey?: string
|
|
130
|
+
getOwnMetadataKeys(target: Object, propertyKey?: string): string[] {
|
|
135
131
|
let keys: string[];
|
|
136
132
|
if (propertyKey) {
|
|
137
133
|
keys = Reflect.getOwnMetadataKeys(target, propertyKey);
|
|
@@ -152,6 +148,18 @@ export class NamespacedReflect {
|
|
|
152
148
|
return metaKeys;
|
|
153
149
|
}
|
|
154
150
|
|
|
151
|
+
decorate(
|
|
152
|
+
decorators: (PropertyDecorator | MethodDecorator)[],
|
|
153
|
+
target: Object,
|
|
154
|
+
targetKey?: string | symbol,
|
|
155
|
+
descriptor?: PropertyDescriptor,
|
|
156
|
+
): PropertyDescriptor | Function;
|
|
157
|
+
|
|
158
|
+
decorate(
|
|
159
|
+
decorators: ClassDecorator[],
|
|
160
|
+
target: Object,
|
|
161
|
+
): PropertyDescriptor | Function;
|
|
162
|
+
|
|
155
163
|
decorate(
|
|
156
164
|
decorators: (PropertyDecorator | MethodDecorator)[] | ClassDecorator[],
|
|
157
165
|
target: Object,
|
|
@@ -159,13 +167,17 @@ export class NamespacedReflect {
|
|
|
159
167
|
descriptor?: PropertyDescriptor,
|
|
160
168
|
): PropertyDescriptor | Function {
|
|
161
169
|
if (targetKey) {
|
|
162
|
-
return Reflect.decorate(
|
|
170
|
+
return Reflect.decorate(
|
|
171
|
+
<(PropertyDecorator | MethodDecorator)[]>decorators,
|
|
172
|
+
target,
|
|
173
|
+
targetKey,
|
|
174
|
+
descriptor,
|
|
175
|
+
);
|
|
163
176
|
} else {
|
|
164
177
|
return Reflect.decorate(<ClassDecorator[]>decorators, <Function>target);
|
|
165
178
|
}
|
|
166
179
|
}
|
|
167
180
|
|
|
168
|
-
/* tslint:disable-next-line:no-any */
|
|
169
181
|
metadata(
|
|
170
182
|
metadataKey: string,
|
|
171
183
|
metadataValue: any,
|
|
@@ -178,5 +190,4 @@ export class NamespacedReflect {
|
|
|
178
190
|
}
|
|
179
191
|
}
|
|
180
192
|
|
|
181
|
-
/* tslint:disable-next-line:variable-name */
|
|
182
193
|
export const Reflector = new NamespacedReflect('loopback');
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
// Copyright IBM Corp. 2018,2019. All Rights Reserved.
|
|
2
|
+
// Node module: @loopback/metadata
|
|
3
|
+
// This file is licensed under the MIT License.
|
|
4
|
+
// License text available at https://opensource.org/licenses/MIT
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Decorator function types
|
|
8
|
+
*/
|
|
9
|
+
export type DecoratorType =
|
|
10
|
+
| ClassDecorator
|
|
11
|
+
| PropertyDecorator
|
|
12
|
+
| MethodDecorator
|
|
13
|
+
| ParameterDecorator;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A strongly-typed metadata accessor via reflection
|
|
17
|
+
* @typeParam T - Type of the metadata value
|
|
18
|
+
* @typeParam D - Type of the decorator
|
|
19
|
+
*/
|
|
20
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
21
|
+
export class MetadataAccessor<T, D extends DecoratorType = DecoratorType> {
|
|
22
|
+
private constructor(public readonly key: string) {}
|
|
23
|
+
|
|
24
|
+
toString() {
|
|
25
|
+
return this.key;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Create a strongly-typed metadata accessor
|
|
30
|
+
* @param key - The metadata key
|
|
31
|
+
* @typeParam V - Type of the metadata value
|
|
32
|
+
* @typeParam DT - Type of the decorator
|
|
33
|
+
*/
|
|
34
|
+
static create<V, DT extends DecoratorType>(key: string) {
|
|
35
|
+
return new MetadataAccessor<V, DT>(key);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Key for metadata access via reflection
|
|
41
|
+
* @typeParam T - Type of the metadata value
|
|
42
|
+
* @typeParam D - Type of the decorator
|
|
43
|
+
*/
|
|
44
|
+
export type MetadataKey<T, D extends DecoratorType> =
|
|
45
|
+
| MetadataAccessor<T, D>
|
|
46
|
+
| string;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* An object mapping keys to corresponding metadata
|
|
50
|
+
*/
|
|
51
|
+
export interface MetadataMap<T> {
|
|
52
|
+
[propertyOrMethodName: string]: T;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Design time metadata for a method.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* ```ts
|
|
60
|
+
* class MyController
|
|
61
|
+
* {
|
|
62
|
+
* myMethod(x: string, y: number, z: MyClass): boolean {
|
|
63
|
+
* // ...
|
|
64
|
+
* return true;
|
|
65
|
+
* }
|
|
66
|
+
* }
|
|
67
|
+
* ```
|
|
68
|
+
*
|
|
69
|
+
* The `myMethod` above has design-time metadata as follows:
|
|
70
|
+
* ```ts
|
|
71
|
+
* {
|
|
72
|
+
* type: Function,
|
|
73
|
+
* parameterTypes: [String, Number, MyClass],
|
|
74
|
+
* returnType: Boolean
|
|
75
|
+
* }
|
|
76
|
+
* ```
|
|
77
|
+
*/
|
|
78
|
+
export interface DesignTimeMethodMetadata {
|
|
79
|
+
/**
|
|
80
|
+
* Type of the method itself. It is `Function` for methods, `undefined` for the constructor.
|
|
81
|
+
*/
|
|
82
|
+
type: Function | undefined;
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* An array of parameter types.
|
|
86
|
+
*/
|
|
87
|
+
|
|
88
|
+
parameterTypes: Function[];
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Return type, may be `undefined` (e.g. for constructors).
|
|
92
|
+
*/
|
|
93
|
+
returnType: Function | undefined;
|
|
94
|
+
}
|