# All files / microservices listener-metadata-explorer.ts

90.91% Statements 20/22
66.67% Branches 4/6
100% Functions 5/5
100% Lines 20/20

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

    1x
    6x

    1x
    1x

    3x

    5x
    5x

    5x
    2x

    3x
    3x

    1x
    2x

    2x
    2x
    2x

    2x

    2x

     import { Controller } from '@nestjs/common/interfaces/controllers/controller.interface';
    import { isFunction, isUndefined } from '@nestjs/common/utils/shared.utils';
    import {
      PATTERN_METADATA,
      PATTERN_HANDLER_METADATA,
      CLIENT_CONFIGURATION_METADATA,
      CLIENT_METADATA,
    } from './constants';
    import { PatternMetadata } from './interfaces/pattern-metadata.interface';
    import { ClientOptions } from './interfaces/client-metadata.interface';
    import { MetadataScanner } from '@nestjs/core/metadata-scanner';

    export class ListenerMetadataExplorer {
      constructor(private readonly metadataScanner: MetadataScanner) {}

      public explore(instance: Controller): PatternProperties[] {
        const instancePrototype = Object.getPrototypeOf(instance);
        return this.metadataScanner.scanFromPrototype<
          Controller,
          PatternProperties
        >(instance, instancePrototype, method =>
          this.exploreMethodMetadata(instance, instancePrototype, method),
        );
      }

      public exploreMethodMetadata(
        instance,
        instancePrototype,
        methodName: string,
      ): PatternProperties {
        const targetCallback = instancePrototype[methodName];
        const isPattern = Reflect.getMetadata(
          PATTERN_HANDLER_METADATA,
          targetCallback,
        );

        if (isUndefined(isPattern)) {
          return null;
        }
        const pattern = Reflect.getMetadata(PATTERN_METADATA, targetCallback);
        return {
          targetCallback,
          pattern,
        };
      }

      public *scanForClientHooks(
        instance: Controller,
      ): IterableIterator<ClientProperties> {
        for (const propertyKey in instance) {
          Iif (isFunction(propertyKey)) continue;

          const property = String(propertyKey);
          const isClient = Reflect.getMetadata(CLIENT_METADATA, instance, property);
          Iif (isUndefined(isClient)) continue;

          const metadata = Reflect.getMetadata(
            CLIENT_CONFIGURATION_METADATA,
            instance,
            property,
          );
          yield { property, metadata };
        }
      }
    }

    export interface ClientProperties {
      property: string;
      metadata: ClientOptions;
    }

    export interface PatternProperties {
      pattern: PatternMetadata;
      targetCallback: (...args) => any;
    }