-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmetadata.ts
73 lines (68 loc) · 2.49 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// Copyright 2020 Liam Tan. All rights reserved. MIT license.
/**
* Metadata utility file - helper methods for assigning and retrieving
* values from the Reflect API, for use across lib
*/
import { Reflect } from "./lib/reflect.ts";
import { ControllerMetadata, RouteDefinition, EInjectionScope } from "./types.ts";
/**
* Key used to identify controller metadata across framework. This
* is not exposed to user, only used internally to assign and
* retrieve metadata appropriately.
*/
export const CONTROLLER_META_PROPKEY = Symbol("dactyl:controller_metadata");
export const INJECTION_SCOPE_META_TOKEN: Symbol = Symbol("dactyl:injection_scope");
export const CONSTRUCTOR_TYPE_META_TOKEN: string = "design:paramtypes";
export function getConstructorTypes(target: Function): Array<Function> {
return Reflect.getMetadata(CONSTRUCTOR_TYPE_META_TOKEN, target);
}
/**
* Helper method for setting metadata on injectable
*/
export function setInjectableMetadata(target: Function, scope: EInjectionScope): void {
Reflect.defineMetadata(INJECTION_SCOPE_META_TOKEN, scope, target);
}
/**
* Helper method for retrieving metadata of injectable
*/
export function getInjectableMetadata(target: Function): EInjectionScope {
return Reflect.getMetadata(INJECTION_SCOPE_META_TOKEN, target);
}
/**
* Helper method for retrieving metadata of controller definition
*/
export function getControllerOwnMeta(target: Function): ControllerMetadata | undefined {
return Reflect.getMetadata(CONTROLLER_META_PROPKEY, target);
}
/**
* Helper method for retrieving metadata of controller
*/
export function getControllerMeta(target: Object): ControllerMetadata | undefined {
return getControllerOwnMeta(target.constructor);
}
/**
* Helper method for setting controller own metadata with a given value.
*/
export function setControllerOwnMeta(target: Function, value: ControllerMetadata): void {
Reflect.defineMetadata(CONTROLLER_META_PROPKEY, value, target);
}
/**
* Helper method for setting controller metadata with a
* given updated value
*/
export function setControllerMeta(target: Object, value: ControllerMetadata): void {
setControllerOwnMeta(target.constructor, value);
}
/**
* Helper method that returns the default metadata object.
*/
export function defaultMetadata(): ControllerMetadata {
return {
prefix: null,
scope: EInjectionScope.REQUEST,
routes: new Map<string, RouteDefinition>(),
args: [],
defaultResponseCodes: new Map<string, number>(),
beforeFns: new Map<string, Array<Function>>(),
};
}