-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorators.ts
31 lines (25 loc) · 942 Bytes
/
decorators.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
import "reflect-metadata"
import { ModuleContainer } from "./container"
import { Class, DependencyConfig } from "./types"
export function Controller(path: string) {
return (cls: Class<any>) => {
ModuleContainer.set(cls, { type: "Controller", path });
};
}
export function Injectable(): ClassDecorator {
return (cls: any) => {
ModuleContainer.set(cls, { type: "Service" });
};
}
export function Inject(cls: Class): ParameterDecorator {
return (target, propertyKey, index) => {
// console.log({ target, propertyKey, index, cls });
const deps = Reflect.getMetadata("DEP", target) as DependencyConfig[] | undefined ?? [];
Reflect.defineMetadata("DEP", [{ dep: cls, index}, ...deps], target);
};
}
export function Module({injects, controllers}: {injects: Class[], controllers: Class[]}): ClassDecorator {
return (cls: any) => {
ModuleContainer.set(cls, { type: "Module", injects, controllers });
};
}