import { Request , Response } from 'express' ; import { inject } from 'inversify' ; import { controller , httpGet , interfaces , request , requestParam , response } from 'inversify-express-utils' ; import { timeout } from 'rxjs/operators' ; import SERVICE_IDENTIFIER from '../../../common/constants/identifiers' ; import { ILogger , IMetrics } from '../../../common/interfaces' ; import { IStarwars } from '../../interfaces' ; import { APIResponse , HttpError } from '../../models' ; import { ErrorResponseBuilder , HttpStatus } from '../../services' ; /**
 * Controller for StarWars APIs
 */ @ controller ( '/starwars' ) class StarwarsController implements interfaces . Controller { public starwarsService : IStarwars ; public loggerService : ILogger ; public metricsService : IMetrics ; public constructor ( @ inject ( SERVICE_IDENTIFIER . STARWARS ) starwarsService , @ inject ( SERVICE_IDENTIFIER . LOGGER ) loggerService , @ inject ( SERVICE_IDENTIFIER . METRICS ) metricsService ) { this . starwarsService = starwarsService ; this . loggerService = loggerService ; this . metricsService = metricsService ; } param id param req param res @ httpGet ( '/people/:id' ) public async getPeopleById ( @ requestParam ( 'id' ) id , @ request ( ) req , @ response ( ) res ) { const result = await new Promise ( ( resolve , reject ) => { this . starwarsService . getPeopleById ( id ) . pipe ( timeout ( + process . env . API_TIME_OUT ) ) . subscribe ( r => { if ( r === undefined ) { this . loggerService . logAPITrace ( req , res , HttpStatus . INTERNAL_SERVER_ERROR ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . INTERNAL_SERVER_ERROR ) ; reject ( { data : r , status : HttpStatus . INTERNAL_SERVER_ERROR } ) ; } else { this . loggerService . logAPITrace ( req , res , HttpStatus . OK ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . OK ) ; resolve ( { data : r , status : HttpStatus . OK } ) ; } } , err => { const error = err as HttpError ; const resp = new ErrorResponseBuilder ( ) . setTitle ( error . name ) . setStatus ( HttpStatus . NOT_FOUND ) . setDetail ( error . stack ) . setMessage ( error . message ) . setSource ( req . url ) . build ( ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . NOT_FOUND , error ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . NOT_FOUND ) ; reject ( { errors : [ resp ] , status : HttpStatus . NOT_FOUND } ) ; } ) ; } ) ; res . status ( result . status ) . json ( result ) ; } } export default StarwarsController ;