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

100% Statements 8/8
75% Branches 3/4
100% Functions 3/3
100% Lines 8/8

    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

    1x

    70x
    70x
    70x
    70x

    70x
    70x

    70x

     import { Controller } from '@nestjs/common/interfaces';

    export abstract class ContextCreator {
      public abstract createConcreteContext<T extends any[], R extends any[]>(
        metadata: T,
      ): R;
      public getGlobalMetadata?<T extends any[]>(): T;

      public createContext<T extends any[], R extends any[]>(
        instance: Controller,
        callback: (...args) => any,
        metadataKey: string,
      ): R {
        const globalMetadata =
          this.getGlobalMetadata && this.getGlobalMetadata<T>();
        const classMetadata = this.reflectClassMetadata<T>(instance, metadataKey);
        const methodMetadata = this.reflectMethodMetadata<T>(callback, metadataKey);
        return [
          ...this.createConcreteContext<T, R>(globalMetadata || ([] as T)),
          ...this.createConcreteContext<T, R>(classMetadata),
          ...this.createConcreteContext<T, R>(methodMetadata),
        ] as R;
      }

      public reflectClassMetadata<T>(instance: Controller, metadataKey: string): T {
        const prototype = Object.getPrototypeOf(instance);
        return Reflect.getMetadata(metadataKey, prototype.constructor);
      }

      public reflectMethodMetadata<T>(
        callback: (...args) => any,
        metadataKey: string,
      ): T {
        return Reflect.getMetadata(metadataKey, callback);
      }
    }