import { IView } from '../common/view.interface' ; import { Response } from 'express' ; import { HttpStatus , BadRequestException } from '@nestjs/common' ; import { IViewModel } from '../..' ; import { ValidationException } from '@btcp/bootcamp-blogic' ; export class NestView implements IView { private _viewModel : IViewModel ; get viewModel ( ) { return this . _viewModel ; } set viewModel ( viewModel ) { this . _viewModel = viewModel ; } private _errorModel : string | Error ; get errorModel ( ) { return this . _errorModel ; } set errorModel ( errorModel ) { this . _errorModel = errorModel ; } private _res : Response ; constructor ( res ) { this . _res = res ; } render ( ) { let httpStatus = HttpStatus . OK ; let content = this . _viewModel ; if ( this . errorModel ) { let errorResponse = null ; if ( typeof this . _errorModel === 'string' ) { errorResponse = { statusCode : 400 , error : "Bad Request" , message : this . _errorModel } ; } else if ( this . _errorModel instanceof ValidationException ) { errorResponse = { statusCode : 400 , error : "Bad Request" , message : this . _errorModel . message } ; } else if ( this . _errorModel instanceof BadRequestException ) { errorResponse = { statusCode : 400 , error : "Bad Request" , message : this . _errorModel . message } ; } else { console . log ( 'NestView:render::this._errorModel' ) ; console . dir ( this . _errorModel ) ; } return this . _res . status ( errorResponse . statusCode ) . json ( errorResponse ) ; } if ( ! this . _viewModel || ! Object . keys ( [ this . _viewModel ] ) . length ) { httpStatus = HttpStatus . NO_CONTENT ; content = null ; } this . _res . status ( httpStatus ) . json ( content ) ; } }