# All files / microservices/server server.ts

96% Statements 24/25
100% Branches 13/13
91.67% Functions 11/12
96% Lines 24/25

    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

    1x
    92x
    92x

    2x
    2x

    2x

    4x

    9x

    2x
    2x

    9x

    7x

    11x
    1x
    10x
    8x

    2x

    162x

    88x

     import { Logger } from '@nestjs/common/services/logger.service';
    import { loadPackage } from '@nestjs/common/utils/load-package.util';
    import { isFunction, isString } from '@nestjs/common/utils/shared.utils';
    import {
      EMPTY as empty,
      from as fromPromise,
      Observable,
      of,
      Subscription,
    } from 'rxjs';
    import { catchError, finalize } from 'rxjs/operators';
    import { MicroserviceOptions, WritePacket } from '../interfaces';
    import { MessageHandlers } from '../interfaces/message-handlers.interface';

    export abstract class Server {
      protected readonly messageHandlers: MessageHandlers = {};
      protected readonly logger = new Logger(Server.name);

      public addHandler(
        pattern: any,
        callback: (data) => Promise<Observable<any>>,
      ) {
        const key = isString(pattern) ? pattern : JSON.stringify(pattern);
        this.messageHandlers[key] = callback;
      }

      public getHandlers(): MessageHandlers {
        return this.messageHandlers;
      }

      public getHandlerByPattern(
        pattern: string,
      ): (data) => Promise<Observable<any>> | null {
        return this.messageHandlers[pattern] ? this.messageHandlers[pattern] : null;
      }

      public send(
        stream$: Observable<any>,
        respond: (data: WritePacket) => void,
      ): Subscription {
        return stream$
          .pipe(
            catchError(err => {
              respond({ err, response: null });
              return empty;
            }),
            finalize(() => respond({ isDisposed: true })),
          )
          .subscribe(response => respond({ err: null, response }));
      }

      public transformToObservable<T = any>(resultOrDeffered): Observable<T> {
        if (resultOrDeffered instanceof Promise) {
          return fromPromise(resultOrDeffered);
        } else if (!(resultOrDeffered && isFunction(resultOrDeffered.subscribe))) {
          return of(resultOrDeffered);
        }
        return resultOrDeffered;
      }

      public getOptionsProp<T extends { options? }>(
        obj: MicroserviceOptions['options'],
        prop: keyof T['options'],
        defaultValue = undefined,
      ) {
        return obj ? obj[prop as string] : defaultValue;
      }

      protected handleError(error: string) {
        this.logger.error(error);
      }

      protected loadPackage(name: string, ctx: string) {
        return loadPackage(name, ctx);
      }
    }