All files / common/decorators/core inject.decorator.ts

100% Statements 14/14
100% Branches 12/12
100% Functions 2/2
100% Lines 14/14
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 361x       1x           1x 18x 18x   18x   18x   16x   16x 16x 16x     2x   2x 2x              
import {
  PROPERTY_DEPS_METADATA,
  SELF_DECLARED_DEPS_METADATA,
} from '../../constants';
import { isFunction, isUndefined } from '../../utils/shared.utils';
 
/**
 * Injects provider which has to be available in the current injector (module) scope.
 * Providers are recognized by types or tokens.
 */
export function Inject<T = any>(token?: T) {
  return (target: Object, key: string | symbol, index?: number) => {
    token = token || Reflect.getMetadata('design:type', target, key);
    const type =
      token && isFunction(token) ? ((token as any) as Function).name : token;
 
    if (!isUndefined(index)) {
      const dependencies =
        Reflect.getMetadata(SELF_DECLARED_DEPS_METADATA, target) || [];
 
      dependencies.push({ index, param: type });
      Reflect.defineMetadata(SELF_DECLARED_DEPS_METADATA, dependencies, target);
      return;
    }
    const properties =
      Reflect.getMetadata(PROPERTY_DEPS_METADATA, target.constructor) || [];
 
    properties.push({ key, type });
    Reflect.defineMetadata(
      PROPERTY_DEPS_METADATA,
      properties,
      target.constructor,
    );
  };
}