import 'reflect-metadata' ; import { Request , Response , RequestHandler , NextFunction } from 'express' ; import { AppRouter } from '../../AppRouter' ; import { Methods } from './Methods' ; import { MetadataKeys } from './MetadataKeys' ; function bodyValidators ( keys ) { return function ( req , res , next ) { if ( ! req . body ) { res . status ( 422 ) . send ( 'Invalid request' ) ; return ; } for ( let key of keys ) { if ( ! req . body [ key ] ) { res . status ( 422 ) . send ( `Missing property ${ key }` ) ; return ; } } next ( ) ; } ; } export function controller ( routePrefix ) { return function ( target ) { const router = AppRouter . getInstance ( ) ; for ( let key in target . prototype ) { const routeHandler = target . prototype [ key ] ; const path = Reflect . getMetadata ( MetadataKeys . path , target . prototype , key ) ; const method = Reflect . getMetadata ( MetadataKeys . method , target . prototype , key ) ; const middlewares = Reflect . getMetadata ( MetadataKeys . middleware , target . prototype , key ) || [ ] ; const requiredBodyProps = Reflect . getMetadata ( MetadataKeys . validator , target . prototype , key ) || [ ] ; const validator = bodyValidators ( requiredBodyProps ) ; if ( path ) { router [ method ] ( `${ routePrefix }${ path }` , ... middlewares , validator , routeHandler ) ; } } } ; }