-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhook.ts
More file actions
47 lines (40 loc) · 1.73 KB
/
hook.ts
File metadata and controls
47 lines (40 loc) · 1.73 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
45
46
47
import { getMetadata, defineMetadata, constants, result } from '../../annotations'
import { getMiddlewares } from '../../annotations/classes/use'
const { PARAMETER_PARAMKEY, CLASS_ENVIRONMENTKEY } = constants
export class Hook {
public static handle(...params);
public static handle( @result res) {
return res
}
public static onDefineMiddlewareTo(target) {
const targetTkey = 'handle'
const injectedDefinitions: any[] = (getMetadata(PARAMETER_PARAMKEY, this, targetTkey) || [])
.filter(p => p.type === 'inject')
for (const { serviceType, targetKey, parameterIndex } of injectedDefinitions) {
if (typeof serviceType.onDefineInjectTo === 'function') {
serviceType.onDefineInjectTo(target, targetTkey, parameterIndex)
}
}
const middlewares = getMiddlewares(this)
for (const middleware of middlewares) {
if (typeof middleware.onDefineMiddlewareTo === 'function') {
middleware.onDefineMiddlewareTo(target)
}
}
const metadata = getMetadata(CLASS_ENVIRONMENTKEY, target) || {}
const injectMetadata = getMetadata(CLASS_ENVIRONMENTKEY, this) || {}
if (injectMetadata) {
Object.keys(injectMetadata).forEach((key) => {
metadata[key] = injectMetadata[key]
})
defineMetadata(CLASS_ENVIRONMENTKEY, { ...metadata }, target);
}
}
public static onInject({ parameter, context }) {
const key = parameter.serviceType && parameter.serviceType.name
if (key && context.context && typeof context.context[key] !== 'undefined') {
return context.context[key]
}
return null
}
}