/
reflect.ts
182 lines (167 loc) · 5.01 KB
/
reflect.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
// Copyright IBM Corp. 2017. All Rights Reserved.
// Node module: @loopback/context
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import 'reflect-metadata';
/* tslint:disable:no-any */
/*
* namespaced wrapper to handle reflect api
*/
export class NamespacedReflect {
/**
* @param namespace : namespace to bind this reflect context
*/
constructor(private namespace?: string) {}
private getMetadataKey(metadataKey: string): string {
// prefix namespace, if provided, to the metadata key
return this.namespace ? this.namespace + ':' + metadataKey : metadataKey;
}
/**
* define metadata for a target class or it's property/method
*/
defineMetadata(
metadataKey: string,
metadataValue: any,
target: Object,
propertyKey?: string | symbol,
) {
metadataKey = this.getMetadataKey(metadataKey);
if (propertyKey) {
Reflect.defineMetadata(metadataKey, metadataValue, target, propertyKey);
} else {
Reflect.defineMetadata(metadataKey, metadataValue, target);
}
}
/**
* lookup metadata from a target object and its prototype chain
*/
getMetadata(
metadataKey: string,
target: Object,
propertyKey?: string | symbol,
): any {
metadataKey = this.getMetadataKey(metadataKey);
if (propertyKey) {
return Reflect.getMetadata(metadataKey, target, propertyKey);
}
return Reflect.getMetadata(metadataKey, target);
}
/**
* get own metadata for a target object or it's property/method
*/
getOwnMetadata(
metadataKey: string,
target: Object,
propertyKey?: string | symbol,
): any {
metadataKey = this.getMetadataKey(metadataKey);
if (propertyKey) {
return Reflect.getOwnMetadata(metadataKey, target, propertyKey);
}
return Reflect.getOwnMetadata(metadataKey, target);
}
/**
* Check if the target has corresponding metadata
* @param metadataKey Key
* @param target Target
* @param propertyKey Optional property key
*/
hasMetadata(
metadataKey: string,
target: Object,
propertyKey?: string | symbol,
): boolean {
metadataKey = this.getMetadataKey(metadataKey);
if (propertyKey) {
return Reflect.hasMetadata(metadataKey, target, propertyKey);
}
return Reflect.hasMetadata(metadataKey, target);
}
hasOwnMetadata(
metadataKey: string,
target: Object,
propertyKey?: string | symbol,
): boolean {
metadataKey = this.getMetadataKey(metadataKey);
if (propertyKey) {
return Reflect.hasOwnMetadata(metadataKey, target, propertyKey);
}
return Reflect.hasOwnMetadata(metadataKey, target);
}
deleteMetadata(
metadataKey: string,
target: Object,
propertyKey?: string | symbol,
): boolean {
metadataKey = this.getMetadataKey(metadataKey);
if (propertyKey) {
return Reflect.deleteMetadata(metadataKey, target, propertyKey);
}
return Reflect.deleteMetadata(metadataKey, target);
}
getMetadataKeys(target: Object, propertyKey?: string | symbol): string[] {
let keys: string[];
if (propertyKey) {
keys = Reflect.getMetadataKeys(target, propertyKey);
} else {
keys = Reflect.getMetadataKeys(target);
}
const metaKeys = [];
if (keys) {
if (!this.namespace) return keys; // No normalization is needed
const prefix = this.namespace + ':';
for (const key of keys) {
if (key.indexOf(prefix) === 0) {
// Only add keys with the namespace prefix
metaKeys.push(key.substr(prefix.length));
}
}
}
return metaKeys;
}
getOwnMetadataKeys(target: Object, propertyKey?: string | symbol): string[] {
let keys: string[];
if (propertyKey) {
keys = Reflect.getOwnMetadataKeys(target, propertyKey);
} else {
keys = Reflect.getOwnMetadataKeys(target);
}
const metaKeys = [];
if (keys) {
if (!this.namespace) return keys; // No normalization is needed
const prefix = this.namespace + ':';
for (const key of keys) {
if (key.indexOf(prefix) === 0) {
// Only add keys with the namespace prefix
metaKeys.push(key.substr(prefix.length));
}
}
}
return metaKeys;
}
decorate(
decorators: (PropertyDecorator | MethodDecorator)[] | ClassDecorator[],
target: Object,
targetKey?: string | symbol,
descriptor?: PropertyDescriptor,
): PropertyDescriptor | Function {
if (targetKey) {
return Reflect.decorate(decorators, target, targetKey, descriptor);
} else {
return Reflect.decorate(<ClassDecorator[]>decorators, <Function>target);
}
}
/* tslint:disable-next-line:no-any */
metadata(
metadataKey: string,
metadataValue: any,
): {
(target: Function): void;
(target: Object, targetKey: string | symbol): void;
} {
metadataKey = this.getMetadataKey(metadataKey);
return Reflect.metadata(metadataKey, metadataValue);
}
}
/* tslint:disable-next-line:variable-name */
export const Reflector = new NamespacedReflect('loopback');