import { MetaKey } from '../consts' ; import { RequestHandler } from 'express' ; const metaKey = MetaKey . httpMethod ; export interface HttpMethodMeta { method : string ; path : string ; propertyKey : string ; middlewares : RequestHandler [ ] ; } export type HttpMethodMetaList = HttpMethodMeta [ ] ; export function getHttpMethodMetaList ( controller ) { const metaList = Reflect . getMetadata ( metaKey , controller ) ; if ( metaList == null ) return [ ] ; return metaList ; } export function setHttpMethodMetaList ( controller , meta ) { Reflect . defineMetadata ( metaKey , meta , controller ) ; } export function httpMethod ( method , path , middlewares = [ ] ) { return function httpMethodDecorator ( target , propertyKey , descriptor ) { const previousHttpMethodList = getHttpMethodMetaList ( target . constructor ) ; const newHttpMethodList = [ ... previousHttpMethodList , { method , path , propertyKey , middlewares } ] ; setHttpMethodMetaList ( target . constructor , newHttpMethodList ) ; } ; } export function httpGet ( path , middlewares ? ) { return httpMethod ( 'get' , path , middlewares ) ; } export function httpPost ( path , middlewares ? ) { return httpMethod ( 'post' , path , middlewares ) ; } export function httpPut ( path , middlewares ? ) { return httpMethod ( 'put' , path , middlewares ) ; } export function httpPatch ( path , middlewares ? ) { return httpMethod ( 'patch' , path , middlewares ) ; } export function httpDelete ( path , middlewares ? ) { return httpMethod ( 'delete' , path , middlewares ) ; } export function httpOptions ( path , middlewares ? ) { return httpMethod ( 'options' , path , middlewares ) ; } export function httpHead ( path , middlewares ? ) { return httpMethod ( 'head' , path , middlewares ) ; } export function httpAll ( path , middlewares ? ) { return httpMethod ( 'all' , path , middlewares ) ; }