ts-ioc-container
Version:
Typescript IoC container
16 lines (15 loc) • 918 B
JavaScript
import { getParameterMetadata, setParameterMetadata } from '../metadata';
import { constant, fillEmptyIndexes, isInstance } from '../utils';
import { hookMetaKey } from '../hooks/HookContext';
export const inject = (fn) => (target, propertyKey, parameterIndex) => {
setParameterMetadata(hookMetaKey(propertyKey), toInjectFn(fn))(isInstance(target) ? target.constructor : target, propertyKey, parameterIndex);
};
function isInjectBuilder(fn) {
return 'resolve' in fn && typeof fn['resolve'] === 'function';
}
export const toInjectFn = (fn) => isInjectBuilder(fn) ? (scope) => fn.resolve(scope) : fn;
export const resolveArgs = (Target, methodName) => {
const argsFns = getInjectFns(Target, methodName);
return (scope, ...deps) => fillEmptyIndexes(argsFns, deps.map(constant)).map((fn) => fn(scope));
};
const getInjectFns = (Target, methodName) => getParameterMetadata(hookMetaKey(methodName), Target);