import { Response , Request , HttpRequestMethod } from "../_http/http" ; import { Container } from "../container" ; wahtItDoes export class MethodHandler { route : string ; methodName : string ; paramsTypes : any [ ] ; paramsNames : any [ ] ; controller : string ; httpRequestMethod : HttpRequestMethod ; hasResponseBodyDecorator : boolean = false ; requestBodyParams : boolean [ ] = [ ] ; call ( req ? , res ? , next ? ) { let controller = Container . get ( this . controller ) ; let method = controller [ this . methodName ] ; let paramsValues = this . getparamsValues ( req , res ) ; let dataToBeSent = method . call ( controller , ... paramsValues ) ; if ( this . hasResponseBodyDecorator ) { this . sendData ( res , dataToBeSent ) ; } } getparamsValues ( req , res ) { let paramsValues = [ ] ; for ( let i = 0 ; i < this . paramsNames . length ; i ++ ) { paramsValues [ i ] = this . getParamValue ( this . paramsNames [ i ] , this . paramsTypes [ i ] , req , res ) ; } return paramsValues ; } getParamValue ( paramName , paramType , req , res ) { if ( this . requestBodyParams [ paramName ] ) { return req . body ; } else if ( paramType === Request ) { return req ; } else if ( paramType === Response ) { return res ; } else { return req . params [ paramName ] || req . body [ paramName ] || req . query [ paramName ] ; } } sendData ( res , data ) { if ( data instanceof Promise ) { < Promise < any > > data . then ( ( dataToBeSent ) => { res . json ( dataToBeSent ) ; } ) ; } else { res . json ( data ) ; } } isRequest ( param ) { return param . baseUrl !== undefined && param . method !== undefined ; } isResponse ( param ) { return param . send !== undefined && param . end !== undefined ; } }