import { MetaKey } from '../../consts' ; import express from 'express' ; const metaKey = MetaKey . handlerParam ; export type HandlerParamSelector < T > = ( req : express . Request , res : express . Response , next : express . NextFunction , meta : HandlerParamMeta < T > ) => T ; export interface HandlerParamMeta < T > { index : number ; selector : HandlerParamSelector < T > ; paramType : any ; } export type HandlerParamMetaList = HandlerParamMeta < any > [ ] ; export function getHandlerParamMetaList ( controller , propertyKey ) { const metaList = Reflect . getMetadata ( metaKey , controller , propertyKey ) ; if ( metaList == null ) return [ ] ; return metaList ; } export function setHandlerParamMetaList ( controller , meta , propertyKey ) { Reflect . defineMetadata ( metaKey , meta , controller , propertyKey ) ; } export function handlerParam < T > ( selector ) { return function controllerDecorator ( target , propertyKey , index ) { const previousHandlerParamList = getHandlerParamMetaList ( target . constructor , propertyKey ) ; const meta = [ { index , selector , paramType : Reflect . getMetadata ( 'design:paramtypes' , target , propertyKey ) [ index ] } , ... previousHandlerParamList ] ; setHandlerParamMetaList ( target . constructor , meta , propertyKey ) ; } ; }