-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinject.ts
More file actions
27 lines (22 loc) · 1000 Bytes
/
inject.ts
File metadata and controls
27 lines (22 loc) · 1000 Bytes
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
import { PARAMETER_PARAMKEY, CLASS_ENVIRONMENTKEY, CLASS_INJECTABLEKEY } from '../constants'
import { defineMetadata, getOwnMetadata } from '../metadata'
import { getFunctionName } from '../classes/functionName'
export const inject = (type: any, ...params): any => {
return (target, targetKey, parameterIndex: number) => {
if (!getOwnMetadata(CLASS_INJECTABLEKEY, type)) {
throw new Error(`type '${getFunctionName(type)}' not marked as injectable`)
}
const existingParameters: any[] = getOwnMetadata(PARAMETER_PARAMKEY, target, targetKey) || [];
existingParameters.push({
serviceType: type,
parameterIndex,
targetKey,
type: 'inject',
params
});
defineMetadata(PARAMETER_PARAMKEY, [...existingParameters], target, targetKey);
if (typeof type.onDefineInjectTo === 'function') {
type.onDefineInjectTo(target, targetKey, parameterIndex)
}
}
}