# All files / core/router router-response-controller.ts

100% Statements 16/16
100% Branches 6/6
100% Functions 7/7
100% Lines 16/16

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

    1x
    44x

    3x
    3x

    2x
    2x

    8x
    1x

    7x

    8x

    2x

    6x

    1x
    1x

     import { RequestMethod, HttpStatus, HttpServer } from '@nestjs/common';
    import { isFunction } from '@nestjs/common/utils/shared.utils';

    export interface CustomHeader {
      name: string;
      value: string;
    }

    export class RouterResponseController {
      constructor(private readonly applicationRef: HttpServer) {}

      public async apply(resultOrDeffered, response, httpStatusCode: number) {
        const result = await this.transformToResult(resultOrDeffered);
        return this.applicationRef.reply(response, result, httpStatusCode);
      }

      public async render(resultOrDeffered, response, template: string) {
        const result = await this.transformToResult(resultOrDeffered);
        this.applicationRef.render(response, template, result);
      }

      public async transformToResult(resultOrDeffered) {
        if (resultOrDeffered && isFunction(resultOrDeffered.subscribe)) {
          return resultOrDeffered.toPromise();
        }
        return resultOrDeffered;
      }

      public getStatusByMethod(requestMethod: RequestMethod): number {
        switch (requestMethod) {
          case RequestMethod.POST:
            return HttpStatus.CREATED;
          default:
            return HttpStatus.OK;
        }
      }

      public setHeaders(response, headers: CustomHeader[]) {
        headers.forEach(({ name, value }) =>
          this.applicationRef.setHeader(response, name, value),
        );
      }
    }