All files / utils/container/decorators InjectMany.ts

23.53% Statements 4/17
0% Branches 0/8
0% Functions 0/4
25% Lines 4/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 538x 8x 8x                                       8x                                                            
import { Container } from '../Container';
import { Token } from '../Token';
import { CannotInjectError } from '../error/CannotInjectError';
 
/**
 * Injects a service into a class property or constructor parameter.
 */
export function InjectMany(type?: (type?: any) => Function): Function;
 
/**
 * Injects a service into a class property or constructor parameter.
 */
export function InjectMany(serviceName?: string): Function;
 
/**
 * Injects a service into a class property or constructor parameter.
 */
export function InjectMany(token: Token<any>): Function;
 
/**
 * Injects a service into a class property or constructor parameter.
 */
export function InjectMany(typeOrName?: ((type?: any) => Function) | string | Token<any>): Function {
    return function (target: Object, propertyName: string, index?: number) {
 
        if (!typeOrName)
            typeOrName = () => (Reflect as any).getMetadata('design:type', target, propertyName);
 
        Container.registerHandler({
            object: target,
            propertyName: propertyName,
            index: index,
            value: containerInstance => {
                let identifier: any;
                if (typeof typeOrName === 'string') {
                    identifier = typeOrName;
 
                } else if (typeOrName instanceof Token) {
                    identifier = typeOrName;
 
                } else {
                    identifier = typeOrName();
                }
 
                if (identifier === Object)
                    throw new CannotInjectError(target, propertyName);
 
                return containerInstance.getMany<any>(identifier);
            }
        });
    };
}