import { Reflector } from '../lib' ; import { Attribute } from '../constants' ; import { DataUtility , FunctionUtility } from '../utility' ; import { InvalidRouteException , InvalidArgumentException } from '../builtin/exceptions' ; import { IControllerAttribute , IControllerAttributeExtended , IParseAttribute , IParseHandler } from '../types' ; export abstract class AttributeMetadata { Throws private static defineHttpVerbMetadata ( route , verb , target , property ) { if ( DataUtility . isUndefinedOrNull ( route ) ) { throw new InvalidRouteException ( route , verb , property , target . constructor . name ) ; } Reflector . defineMetadata ( verb , route , target . constructor . prototype , property ) ; } Throws static parse = ( cb , data , isQueryParam ) : ( target : any , propertyKey : string , parameterIndex : number ) => void => { return ( target , propertyKey , parameterIndex ) : void => { let args = FunctionUtility . getParamNames ( target [ propertyKey ] ) ; let parameterKey = args [ parameterIndex ] ; if ( ! DataUtility . isFunction ( cb ) ) { throw new InvalidArgumentException ( cb , `Controller: ${ target . constructor . name }, Action: ${ propertyKey }, Key: ${ parameterKey }` ) ; } let meta = Reflector . getMetadata ( Attribute . parse , target . constructor . prototype , propertyKey ) ; const parseAttribute = { handler : cb , key : parameterKey , controller : target , action : propertyKey , data : data , isQueryParam : isQueryParam , paramIndex : parameterIndex } ; if ( ! DataUtility . isUndefinedOrNull ( meta ) ) { meta . push ( parseAttribute ) ; Reflector . defineMetadata ( Attribute . parse , meta , target . constructor . prototype , propertyKey ) ; } else { Reflector . defineMetadata ( Attribute . parse , [ parseAttribute ] , target . constructor . prototype , propertyKey ) ; } } ; } ; static sendsResponse = ( ) : ( target : any , propertyKey : string ) => void => { return ( target , propertyKey ) : void => { Reflector . defineMetadata ( Attribute . sendsResponse , 'sendsResponse' , target . constructor . prototype , propertyKey ) ; } ; } ; static asyncAttr = ( ) : ( target : any , propertyKey : string ) => void => { return ( target , propertyKey ) : void => { Reflector . defineMetadata ( Attribute . asyncAttr , 'asyncAttr' , target . constructor . prototype , propertyKey ) ; } ; } ; Throws static httpGet = ( route ) : ( target : any , propertyKey : string ) => void => { return ( target , propertyKey ) : void => { AttributeMetadata . defineHttpVerbMetadata ( route , Attribute . httpGet , target , propertyKey ) ; } ; } ; Throws static httpPost = ( route ) : ( target : any , propertyKey : string ) => void => { return ( target , propertyKey ) : void => { AttributeMetadata . defineHttpVerbMetadata ( route , Attribute . httpPost , target , propertyKey ) ; } ; } ; Throws static httpDelete = ( route ) : ( target : any , propertyKey : string ) => void => { return ( target , propertyKey ) : void => { AttributeMetadata . defineHttpVerbMetadata ( route , Attribute . httpDelete , target , propertyKey ) ; } ; } ; Throws static httpPatch = ( route ) : ( target : any , propertyKey : string ) => void => { return ( target , propertyKey ) : void => { AttributeMetadata . defineHttpVerbMetadata ( route , Attribute . httpPatch , target , propertyKey ) ; } ; } ; Throws static httpPut = ( route ) : ( target : any , propertyKey : string ) => void => { return ( target , propertyKey ) : void => { AttributeMetadata . defineHttpVerbMetadata ( route , Attribute . httpPut , target , propertyKey ) ; } ; } ; Throws static httpHead = ( route ) : ( target : any , propertyKey : string ) => void => { return ( target , propertyKey ) : void => { AttributeMetadata . defineHttpVerbMetadata ( route , Attribute . httpHead , target , propertyKey ) ; } ; } ; Throws static httpAll = ( route ) : ( target : any , propertyKey : string ) => void => { return ( target , propertyKey ) : void => { AttributeMetadata . defineHttpVerbMetadata ( route , Attribute . httpAll , target , propertyKey ) ; } ; } ; Throws static controller = ( prefix , attr ? ) : ( target : any , propertyKey : string ) => void => { return ( target , propertyKey ) : void => { let attrs = DataUtility . isUndefinedOrNull ( attr ) ? { } : attr ; if ( DataUtility . isUndefinedOrNull ( prefix ) ) { throw new InvalidRouteException ( prefix , undefined , undefined , target . constructor . name ) ; } let val = { prefix : prefix , middlewares : DataUtility . isUndefinedOrNull ( attrs . middlewares ) ? [ ] : attrs . middlewares , filters : DataUtility . isUndefinedOrNull ( attrs . filters ) ? [ ] : attrs . filters , exceptions : DataUtility . isUndefinedOrNull ( attrs . exceptions ) ? [ ] : attrs . exceptions , result : DataUtility . isUndefinedOrNull ( attrs . result ) ? [ ] : attrs . result , use : DataUtility . isUndefinedOrNull ( attrs . use ) ? [ ] : attrs . use } ; Reflector . defineMetadata ( Attribute . controller , val , target . prototype ) ; } ; } ; static returns = ( type ) : ( target : any , propertyKey : string ) => void => { return ( target , propertyKey ) : void => { Reflector . defineMetadata ( Attribute . returns , type , target . constructor . prototype , propertyKey ) ; } ; } ; }