// The rational behind the verbose Reflect-feature check below is the fact that there are polyfills
|
// which add an implementation for Reflect.defineMetadata but not for Reflect.getOwnMetadataKeys.
|
// Without this check consumers will encounter hard to track down runtime errors.
|
export function reflectionIsSupported() {
|
return typeof Reflect !== 'undefined' && Reflect.defineMetadata && Reflect.getOwnMetadataKeys;
|
}
|
export function copyReflectionMetadata(to, from) {
|
forwardMetadata(to, from);
|
Object.getOwnPropertyNames(from.prototype).forEach(key => {
|
forwardMetadata(to.prototype, from.prototype, key);
|
});
|
Object.getOwnPropertyNames(from).forEach(key => {
|
forwardMetadata(to, from, key);
|
});
|
}
|
function forwardMetadata(to, from, propertyKey) {
|
const metaKeys = propertyKey
|
? Reflect.getOwnMetadataKeys(from, propertyKey)
|
: Reflect.getOwnMetadataKeys(from);
|
metaKeys.forEach(metaKey => {
|
const metadata = propertyKey
|
? Reflect.getOwnMetadata(metaKey, from, propertyKey)
|
: Reflect.getOwnMetadata(metaKey, from);
|
if (propertyKey) {
|
Reflect.defineMetadata(metaKey, metadata, to, propertyKey);
|
}
|
else {
|
Reflect.defineMetadata(metaKey, metadata, to);
|
}
|
});
|
}
|