import { Container } from "../container" ; import { ControllerHandler } from "../handlers/controllerHandler" ; import { MethodHandler } from "../handlers/methodHandler" ; import { HttpRequestMethod } from "../_http/http" ; import { Injectable } from "injection-js" ; import "reflect-metadata" ; import getFunctionParametersNames = require ( "get-parameter-names" ) ; whatItDoes howToUse Controller export function Controller ( target ) { const dependencies = Reflect . getMetadata ( "design:paramtypes" , target ) ; let controllerName = target [ "name" ] ; let controllerHandler = Container . controllerHandlers [ controllerName ] ; if ( ! controllerHandler ) { controllerHandler = new ControllerHandler ( ) ; } Injectable ( ) . call ( null , target ) ; Container . components [ controllerName ] = target ; } export function RestController ( target ) { const dependencies = Reflect . getMetadata ( "design:paramtypes" , target ) ; let controllerName = target [ "name" ] ; let controllerHandler = Container . controllerHandlers [ controllerName ] ; if ( ! controllerHandler ) { controllerHandler = new ControllerHandler ( ) ; } controllerHandler . isRest = true ; Injectable ( ) . call ( null , target ) ; Container . components [ controllerName ] = target ; } whatItDoes howToUse Controller Route export function Route ( route ) { return ( target ) => { if ( ! route ) { throw "Route must not be Empty" ; } let controllerName = target [ "name" ] ; let controllerHandler = Container . controllerHandlers [ controllerName ] ; if ( ! controllerHandler ) { controllerHandler = new ControllerHandler ( ) ; } controllerHandler . route = route ; } ; } whatItDoes Get Put Delete function helperFunction ( target , methodName , route , httpRequestMethod ) { /**
     * looking for the method's controllerHandler
     */ let controllerName = target . constructor [ "name" ] ; let controllerHandler = Container . controllerHandlers [ controllerName ] ; if ( ! controllerHandler ) { controllerHandler = new ControllerHandler ( ) ; } let methodHandler = controllerHandler . methodsHandlers [ methodName ] ; if ( ! methodHandler ) { methodHandler = new MethodHandler ( ) ; } /**
     * attach the method information to the methodHandler
     */ methodHandler . route = route ; methodHandler . methodName = methodName ; methodHandler . paramsTypes = Reflect . getMetadata ( "design:paramtypes" , target , methodName ) ; methodHandler . paramsNames = getFunctionParametersNames ( target [ methodName ] ) ; methodHandler . controller = target . constructor [ "name" ] ; methodHandler . httpRequestMethod = httpRequestMethod ; controllerHandler . methodsHandlers [ methodName ] = methodHandler ; Container . controllerHandlers [ controllerName ] = controllerHandler ; } whatItDoes howToUse Get description export function Get ( route ) { return ( target , methodName ) => { helperFunction ( target , methodName , route , HttpRequestMethod . GET ) ; } ; } whatItDoes howToUse Post description export function Post ( route ) { return ( target , methodName ) => { helperFunction ( target , methodName , route , HttpRequestMethod . POST ) ; } ; } whatItDoes howToUse Put description export function Put ( route ) { return ( target , methodName ) => { helperFunction ( target , methodName , route , HttpRequestMethod . PUT ) ; } ; } whatItDoes howToUse Delete description export function Delete ( route ) { return ( target , methodName ) => { helperFunction ( target , methodName , route , HttpRequestMethod . DELETE ) ; } ; } whatItDoes howToUse Put RequestBody description export function RequestBody ( target , methodName , parameterIndex ) { let controllerName = target . constructor [ "name" ] ; let controllerHandler = Container . controllerHandlers [ controllerName ] ; if ( ! controllerHandler ) { controllerHandler = new ControllerHandler ( ) ; } let methodHandler = controllerHandler . methodsHandlers [ methodName ] ; if ( ! methodHandler ) { methodHandler = new MethodHandler ( ) ; } let paramName = getFunctionParametersNames ( target [ methodName ] ) [ parameterIndex ] ; methodHandler . requestBodyParams [ paramName ] = true ; controllerHandler . methodsHandlers [ methodName ] = methodHandler ; Container . controllerHandlers [ controllerName ] = controllerHandler ; } whatItDoes howToUse Get ResponseBody description export function ResponseBody ( target , methodName ) { let controllerName = target . constructor [ "name" ] ; let controllerHandler = Container . controllerHandlers [ controllerName ] ; if ( ! controllerHandler ) { controllerHandler = new ControllerHandler ( ) ; } let methodHandler = controllerHandler . methodsHandlers [ methodName ] ; if ( ! methodHandler ) { methodHandler = new MethodHandler ( ) ; } let type = Reflect . getMetadata ( "design:returntype" , target , methodName ) ; if ( ! type ) { throw controllerName + "." + methodName + " return type must be specified" ; } methodHandler . hasResponseBodyDecorator = true ; controllerHandler . methodsHandlers [ methodName ] = methodHandler ; Container . controllerHandlers [ controllerName ] = controllerHandler ; }