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

96.77% Statements 30/31
94.74% Branches 18/19
100% Functions 9/9
96.77% Lines 30/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
    73
    74
    75
    76
    771x

    1x
    1x
    1x

    1x

    121x
    121x

    121x

    22x
    22x

    68x
    66x

    2x

    8x

    4x

    4x

    7x
    7x
    1x

    6x
    6x

    6x
    3x

    3x
    3x
    3x

    3x

    22x
    20x

    2x

     import { INTERCEPTORS_METADATA } from '@nestjs/common/constants';
    import { Controller, NestInterceptor } 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 InterceptorsContextCreator extends ContextCreator {
      private moduleContext: string;

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

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

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

      public getInterceptorInstance(interceptor: Function | NestInterceptor) {
        const isObject = (interceptor as NestInterceptor).intercept;
        if (isObject) {
          return interceptor;
        }
        const instanceWrapper = this.getInstanceByMetatype(interceptor);
        return instanceWrapper && instanceWrapper.instance
          ? instanceWrapper.instance
          : null;
      }

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

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