# All files / core/injector compiler.ts

100% Statements 13/13
100% Branches 2/2
100% Functions 4/4
100% Lines 12/12

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

    1x
    266x

    13x
    13x
    13x

    15x
    15x
    14x

    1x
    1x

    17x

     import { DynamicModule, Type } from '@nestjs/common/interfaces';
    import { ModuleTokenFactory } from './module-token-factory';

    export interface ModuleFactory {
      type: Type<any>;
      token: string;
      dynamicMetadata?: Partial<DynamicModule> | undefined;
    }

    export class ModuleCompiler {
      private readonly moduleTokenFactory = new ModuleTokenFactory();

      public async compile(
        metatype: Type<any> | DynamicModule | Promise<DynamicModule>,
        scope: Type<any>[],
      ): Promise<ModuleFactory> {
        const { type, dynamicMetadata } = await this.extractMetadata(metatype);
        const token = this.moduleTokenFactory.create(type, scope, dynamicMetadata);
        return { type, dynamicMetadata, token };
      }

      public async extractMetadata(
        metatype: Type<any> | DynamicModule | Promise<DynamicModule>,
      ): Promise<{
        type: Type<any>;
        dynamicMetadata?: Partial<DynamicModule> | undefined;
      }> {
        metatype = await metatype;
        if (!this.isDynamicModule(metatype)) {
          return { type: metatype };
        }
        const { module: type, ...dynamicMetadata } = metatype;
        return { type, dynamicMetadata };
      }

      public isDynamicModule(
        module: Type<any> | DynamicModule,
      ): module is DynamicModule {
        return !!(module as DynamicModule).module;
      }

    }