-
Notifications
You must be signed in to change notification settings - Fork 2
/
metadata.ts
59 lines (46 loc) · 1.45 KB
/
metadata.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
import { ServiceIdent } from "./service.ts";
// Alias Reflect to prevent type errors
const Ref = Reflect as any;
const classTag = Symbol("class-tag");
enum MetadataTags {
// Metadata tags used by TS.
TsParamTypes = "design:paramtypes",
TsType = "design:type",
// Metadata tags used by this module.
DiParamTypes = "di:paramtypes",
DiClassTag = "di:classtag",
}
export type DesignType = object | undefined;
export interface DiParamTypes {
prop: Map<string | symbol, ServiceIdent<any>>;
param: Map<number, ServiceIdent<any>>;
}
export function getClassParamTypes(
target: Function,
): DesignType[] | undefined {
return Ref.getOwnMetadata(MetadataTags.TsParamTypes, target);
}
export function getMemberType(
target: object,
propKey: string | symbol,
): DesignType | undefined {
return Ref.getMetadata(MetadataTags.TsType, target, propKey);
}
export function getDiClassParamTypes(
target: Function,
): DiParamTypes | undefined {
return Ref.getOwnMetadata(MetadataTags.DiParamTypes, target);
}
export function setDiClassParamTypes(
target: Function,
paramTypes: DiParamTypes,
): void {
Ref.defineMetadata(MetadataTags.DiParamTypes, paramTypes, target);
}
export function isClassTagged(target: Function): boolean {
const metadata = Ref.getOwnMetadata(MetadataTags.DiClassTag, target);
return typeof metadata !== "undefined";
}
export function tagClass(target: Function) {
Ref.defineMetadata(MetadataTags.DiClassTag, classTag, target);
}