# All files / core/guards guards-context-creator.ts

100% Statements 31/31
100% Branches 19/19
100% Functions 9/9
100% Lines 31/31

    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
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    1x

    1x
    1x
    1x

    1x

    121x
    121x

    121x

    20x
    20x

    62x
    50x

    12x
    18x
    14x
    14x

    17x
    17x
    1x

    16x
    16x

    16x
    3x

    13x
    13x
    13x
    10x

    3x

    20x
    18x

    2x

     import { CanActivate } from '@nestjs/common';
    import { GUARDS_METADATA } from '@nestjs/common/constants';
    import { Controller } from '@nestjs/common/interfaces';
    import { ConfigurationProvider } from '@nestjs/common/interfaces/configuration-provider.interface';
    import { isEmpty, isFunction, isUndefined } from '@nestjs/common/utils/shared.utils';
    import iterate from 'iterare';
    import { ContextCreator } from '../helpers/context-creator';
    import { NestContainer } from '../injector/container';

    export class GuardsContextCreator extends ContextCreator {
      private moduleContext: string;

      constructor(
        private readonly container: NestContainer,
        private readonly config?: ConfigurationProvider,
      ) {
        super();
      }

      public create(
        instance: Controller,
        callback: (...args) => any,
        module: string,
      ): CanActivate[] {
        this.moduleContext = module;
        return this.createContext(instance, callback, GUARDS_METADATA);
      }

      public createConcreteContext<T extends any[], R extends any[]>(
        metadata: T,
      ): R {
        if (isEmpty(metadata)) {
          return [] as R;
        }
        return iterate(metadata)
          .filter((guard: any) => guard && (guard.name || guard.canActivate))
          .map(guard => this.getGuardInstance(guard))
          .filter((guard: CanActivate) => guard && isFunction(guard.canActivate))
          .toArray() as R;
      }

      public getGuardInstance(guard: Function | CanActivate) {
        const isObject = (guard as CanActivate).canActivate;
        if (isObject) {
          return guard;
        }
        const instanceWrapper = this.getInstanceByMetatype(guard);
        return instanceWrapper && instanceWrapper.instance
          ? instanceWrapper.instance
          : null;
      }

      public getInstanceByMetatype(guard): { instance: any } | undefined {
        if (!this.moduleContext) {
          return undefined;
        }
        const collection = this.container.getModules();
        const module = collection.get(this.moduleContext);
        if (!module) {
          return undefined;
        }
        return module.injectables.get(guard.name);
      }

      public getGlobalMetadata<T extends any[]>(): T {
        if (!this.config) {
          return [] as T;
        }
        return this.config.getGlobalGuards() as T;
      }
    }