-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetadata.ts
More file actions
44 lines (37 loc) · 1.63 KB
/
metadata.ts
File metadata and controls
44 lines (37 loc) · 1.63 KB
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
import 'reflect-metadata'
export const defineMetadata = (metadataKey, metadataValue, target, propertyKey?) => {
return Reflect.defineMetadata(metadataKey, metadataValue, target.prototype ? target.prototype : target, propertyKey)
}
export const getMetadata = (metadataKey, target, propertyKey?) => {
return Reflect.getMetadata(metadataKey, target.prototype ? target.prototype : target, propertyKey)
}
export const getMetadataKeys = (target) => {
return Reflect.getMetadataKeys(target.prototype ? target.prototype : target)
}
export const getOwnMetadata = (metadataKey, target, propertyKey?) => {
return Reflect.getOwnMetadata(metadataKey, target.prototype ? target.prototype : target, propertyKey)
}
export const getOverridableMetadata = (metadataKey, target, propertyKey) => {
if (!propertyKey) {
// parameter decorators in constructors
} else {
// parameter decorators in static methods
if (target.hasOwnProperty(propertyKey)) {
return getOwnMetadata(metadataKey, target, propertyKey)
}
}
return getParentMetadata(metadataKey, target, propertyKey)
}
export const getParentMetadata = (metadataKey, target, propertyKey) => {
if (target === Function) return
const value = getOwnMetadata(metadataKey, target, propertyKey)
if (typeof value === 'undefined') {
if (target.prototype && target.__proto__) {
return getParentMetadata(metadataKey, target.__proto__, propertyKey)
}
if (!target.prototype && target.constructor) {
return getParentMetadata(metadataKey, target.constructor, propertyKey)
}
}
return value
}