/
inspector.ts
274 lines (257 loc) · 8.69 KB
/
inspector.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright IBM Corp. 2017,2019. All Rights Reserved.
// Node module: @loopback/metadata
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import debugModule from 'debug';
import {DecoratorFactory} from './decorator-factory';
import {NamespacedReflect, Reflector} from './reflect';
import {
DecoratorType,
DesignTimeMethodMetadata,
MetadataKey,
MetadataMap,
} from './types';
const debug = debugModule('loopback:metadata:inspector');
/**
* TypeScript reflector without a namespace. The TypeScript compiler can be
* configured to add design time metadata.
*
* See https://www.typescriptlang.org/docs/handbook/decorators.html
*/
const TSReflector = new NamespacedReflect();
/**
* Options for inspection
*/
export interface InspectionOptions {
/**
* Only inspect own metadata of a given target. The prototype chain will not
* be checked. The implementation uses `Reflect.getOwnMetadata()` if the flag
* is set to `true`. Otherwise, it uses `Reflect.getMetadata()`.
*
* The flag is `false` by default for `MetadataInspector`.
*/
ownMetadataOnly?: boolean;
}
/**
* Inspector for metadata applied by decorators
*/
export class MetadataInspector {
/**
* Expose Reflector, which is a wrapper of `Reflect` and it uses `loopback`
* as the namespace prefix for all metadata keys
*/
static readonly Reflector = Reflector;
/**
* Expose the reflector for TypeScript design-time metadata
*/
static readonly DesignTimeReflector = TSReflector;
/**
* Get the metadata associated with the given key for a given class
* @param key - Metadata key
* @param target - Class that contains the metadata
* @param options - Options for inspection
*/
static getClassMetadata<T>(
key: MetadataKey<T, ClassDecorator>,
target: Function,
options?: InspectionOptions,
): T | undefined {
return options?.ownMetadataOnly
? Reflector.getOwnMetadata(key.toString(), target)
: Reflector.getMetadata(key.toString(), target);
}
/**
* Define metadata for the given target
* @param key - Metadata key
* @param value - Metadata value
* @param target - Target for the metadata
* @param member - Optional property or method name
*/
static defineMetadata<T>(
key: MetadataKey<T, DecoratorType>,
value: T,
target: Object,
member?: string,
) {
Reflector.defineMetadata(key.toString(), value, target, member);
}
/**
* Get the metadata associated with the given key for all methods of the
* target class or prototype
* @param key - Metadata key
* @param target - Class for static methods or prototype for instance methods
* @param options - Options for inspection
*/
static getAllMethodMetadata<T>(
key: MetadataKey<T, MethodDecorator>,
target: Object,
options?: InspectionOptions,
): MetadataMap<T> | undefined {
return options?.ownMetadataOnly
? Reflector.getOwnMetadata(key.toString(), target)
: Reflector.getMetadata(key.toString(), target);
}
/**
* Get the metadata associated with the given key for a given method of the
* target class or prototype
* @param key - Metadata key
* @param target - Class for static methods or prototype for instance methods
* @param methodName - Method name. If not present, default to '' to use
* the constructor
* @param options - Options for inspection
*/
static getMethodMetadata<T>(
key: MetadataKey<T, MethodDecorator>,
target: Object,
methodName?: string,
options?: InspectionOptions,
): T | undefined {
methodName = methodName ?? '';
const meta: MetadataMap<T> = options?.ownMetadataOnly
? Reflector.getOwnMetadata(key.toString(), target)
: Reflector.getMetadata(key.toString(), target);
return meta?.[methodName];
}
/**
* Get the metadata associated with the given key for all properties of the
* target class or prototype
* @param key - Metadata key
* @param target - Class for static methods or prototype for instance methods
* @param options - Options for inspection
*/
static getAllPropertyMetadata<T>(
key: MetadataKey<T, PropertyDecorator>,
target: Object,
options?: InspectionOptions,
): MetadataMap<T> | undefined {
return options?.ownMetadataOnly
? Reflector.getOwnMetadata(key.toString(), target)
: Reflector.getMetadata(key.toString(), target);
}
/**
* Get the metadata associated with the given key for a given property of the
* target class or prototype
* @param key - Metadata key
* @param target - Class for static properties or prototype for instance
* properties
* @param propertyName - Property name
* @param options - Options for inspection
*/
static getPropertyMetadata<T>(
key: MetadataKey<T, PropertyDecorator>,
target: Object,
propertyName: string,
options?: InspectionOptions,
): T | undefined {
const meta: MetadataMap<T> = options?.ownMetadataOnly
? Reflector.getOwnMetadata(key.toString(), target)
: Reflector.getMetadata(key.toString(), target);
return meta?.[propertyName];
}
/**
* Get the metadata associated with the given key for all parameters of a
* given method
* @param key - Metadata key
* @param target - Class for static methods or prototype for instance methods
* @param methodName - Method name. If not present, default to '' to use
* the constructor
* @param options - Options for inspection
*/
static getAllParameterMetadata<T>(
key: MetadataKey<T, ParameterDecorator>,
target: Object,
methodName?: string,
options?: InspectionOptions,
): T[] | undefined {
methodName = methodName ?? '';
const meta: MetadataMap<T[]> = options?.ownMetadataOnly
? Reflector.getOwnMetadata(key.toString(), target)
: Reflector.getMetadata(key.toString(), target);
return meta?.[methodName];
}
/**
* Get the metadata associated with the given key for a parameter of a given
* method by index
* @param key - Metadata key
* @param target - Class for static methods or prototype for instance methods
* @param methodName - Method name. If not present, default to '' to use
* the constructor
* @param index - Index of the parameter, starting with 0
* @param options - Options for inspection
*/
static getParameterMetadata<T>(
key: MetadataKey<T, ParameterDecorator>,
target: Object,
methodName: string,
index: number,
options?: InspectionOptions,
): T | undefined {
methodName = methodName || '';
const meta: MetadataMap<T[]> = options?.ownMetadataOnly
? Reflector.getOwnMetadata(key.toString(), target)
: Reflector.getMetadata(key.toString(), target);
const params = meta?.[methodName];
return params?.[index];
}
/**
* Get TypeScript design time type for a property
* @param target - Class or prototype
* @param propertyName - Property name
* @returns Design time metadata. The return value is `undefined` when:
* - The property has type `undefined`, `null` or a complex type like
* `Partial<MyModel>`, `string | number`, `string[]`.
* - The TypeScript project has not enabled the compiler option `emitDecoratorMetadata`.
* - The code is written in vanilla JavaScript.
*/
static getDesignTypeForProperty(
target: Object,
propertyName: string,
): Function | undefined {
return TSReflector.getMetadata('design:type', target, propertyName);
}
/**
* Get TypeScript design time type for a method.
* @param target - Class or prototype
* @param methodName - Method name
* @returns Design time metadata. The return value is `undefined`
* in projects that do not enable `emitDecoratorMetadata`
* in TypeScript compiler options or are written in vanilla JavaScript.
*/
static getDesignTypeForMethod(
target: Object,
methodName: string,
): DesignTimeMethodMetadata | undefined {
const type = TSReflector.getMetadata('design:type', target, methodName);
const parameterTypes = TSReflector.getMetadata(
'design:paramtypes',
target,
methodName,
);
const returnType = TSReflector.getMetadata(
'design:returntype',
target,
methodName,
);
if (
type === undefined &&
parameterTypes === undefined &&
returnType === undefined
) {
/* istanbul ignore next */
if (debug.enabled) {
const targetName = DecoratorFactory.getTargetName(target, methodName);
debug(
'No design-time type metadata found while inspecting %s. ' +
'Did you forget to enable TypeScript compiler option `emitDecoratorMetadata`?',
targetName,
);
}
return undefined;
}
return {
type,
parameterTypes,
returnType,
};
}
}