import { Request , Response } from 'express' ; import { inject } from 'inversify' ; import { BaseHttpController , controller , httpGet , httpPost , 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 { authMiddleware } from '../../../common/middleware/auth-middleware' ; import { IExample } from '../../interfaces' ; import { HttpError , Quote } from '../../models' ; import { ErrorResponseBuilder , HttpStatus } from '../../services' ; /**
 * Examples Controller
 * JWT Auth middleware as well as logging middleware added
 */ @ controller ( '/examples' , authMiddleware ( { role : 'admin' } as any ) , SERVICE_IDENTIFIER . LOGGER_MIDDLEWARE ) class ExampleController extends BaseHttpController { public exampleService : IExample ; public loggerService : ILogger ; public metricsService : IMetrics ; public constructor ( @ inject ( SERVICE_IDENTIFIER . EXAMPLE ) exampleService , @ inject ( SERVICE_IDENTIFIER . LOGGER ) loggerService , @ inject ( SERVICE_IDENTIFIER . METRICS ) metricsService ) { super ( ) ; this . exampleService = exampleService ; this . loggerService = loggerService ; this . metricsService = metricsService ; } param req param res @ httpGet ( '/' ) public async all ( @ request ( ) req , @ response ( ) res ) { return this . exampleService . all ( ) ; } param id param req param res @ httpGet ( '/:id' ) public async byPostsByID ( @ requestParam ( 'id' ) id , @ request ( ) req , @ response ( ) res ) { this . loggerService . info ( req . originalUrl ) ; return new Promise ( ( resolve , reject ) => { this . exampleService . byPostsByID ( id ) . pipe ( timeout ( + process . env . API_TIME_OUT ) ) . subscribe ( result => { this . loggerService . info ( result . data as Quote ) ; this . loggerService . info ( result . timings ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . OK ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . OK ) ; resolve ( result . data ) ; } , 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 ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . NOT_FOUND ) ; reject ( resp ) ; } ) ; } ) ; } param req param res public byId ( req , res ) { this . exampleService . byId ( + req . params . id ) . then ( r => { if ( r ) { res . json ( r ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . OK ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . OK ) ; } else { res . status ( HttpStatus . NOT_FOUND ) . end ( ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . NOT_FOUND ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . NOT_FOUND ) ; } } , error => { const resp = new ErrorResponseBuilder ( ) . setTitle ( error . name ) . setStatus ( HttpStatus . NOT_FOUND ) . setDetail ( error . stack ) . setMessage ( error . message ) . setSource ( req . url ) . build ( ) ; res . status ( HttpStatus . NOT_FOUND ) . json ( resp ) ; this . loggerService . logAPITrace ( req , res , HttpStatus . NOT_FOUND ) ; this . metricsService . logAPIMetrics ( req , res , HttpStatus . NOT_FOUND ) ; } ) ; } param req param res @ httpPost ( '/' ) public async create ( @ request ( ) req , @ response ( ) res ) { return this . exampleService . create ( req . body . name ) ; } } export default ExampleController ;