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

100% Statements 33/33
100% Branches 20/20
100% Functions 11/11
100% Lines 33/33

    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
    761x

    1x
    1x

    1x

    1x

    122x
    122x

    122x

    22x
    22x

    83x
    70x

    13x
    15x
    13x
    13x
    13x

    16x
    16x
    14x

    2x
    2x

    3x
    1x

    2x
    2x
    2x
    1x

    1x

    22x
    16x

    6x

    8x

     import { PIPES_METADATA } from '@nestjs/common/constants';
    import { Controller, PipeTransform, Transform } from '@nestjs/common/interfaces';
    import { isEmpty, isFunction, isUndefined } from '@nestjs/common/utils/shared.utils';
    import iterate from 'iterare';
    import { ApplicationConfig } from '../application-config';
    import { ContextCreator } from '../helpers/context-creator';
    import { NestContainer } from '../injector/container';

    export class PipesContextCreator extends ContextCreator {
      private moduleContext: string;

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

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

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

      public getPipeInstance(pipe: Function | PipeTransform) {
        const isObject = (pipe as PipeTransform).transform;
        if (isObject) {
          return pipe;
        }
        const instanceWrapper = this.getInstanceByMetatype(pipe);
        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);
        if (!module) {
          return undefined;
        }
        return module.injectables.get(metatype.name);
      }

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

      public setModuleContext(context: string) {
        this.moduleContext = context;
      }
    }