import { Validation } from "../Validation" ; export const CONTROLLER_METADATA = "controller_metadata" ; export const ROUTE_METADATA = "method_metadata" ; export const PARAM_METADATA = "param_metadata" ; export function Controller ( path = "" ) { return ( target ) => { Reflect . defineMetadata ( CONTROLLER_METADATA , path , target ) ; } ; } export function createMethodDecorator ( method = "get" ) { return ( path = "/" ) : MethodDecorator => ( target , name , descriptor ) => { Reflect . defineMetadata ( ROUTE_METADATA , { type : method , path } , descriptor . value ) ; } ; } export const Get = createMethodDecorator ( "get" ) ; export const Post = createMethodDecorator ( "post" ) ; export const Ws = createMethodDecorator ( "ws" ) ; export type Param = "params" | "query" | "body" | "headers" | "cookies" | "uploadFile" ; export interface ParamType { key : string ; index : number ; type : string ; validator ? : Validation ; paramType ? : any ; } export function createParamDecorator ( type ) { return ( key ? , validator ? ) : ParameterDecorator => ( target , name , index ) => { const paramsTypes = Reflect . getMetadata ( "design:paramtypes" , target , name ) ; const preMetadata = Reflect . getMetadata ( PARAM_METADATA , target , name ) || [ ] ; const newMetadata = [ { key , index , type , validator , paramType : paramsTypes [ index ] , } , ... preMetadata , ] ; Reflect . defineMetadata ( PARAM_METADATA , newMetadata , target , name ) ; } ; } export function Query ( ) ; export function Query ( property ) ; export function Query ( validator ) ; export function Query ( property , validator ) ; export function Query ( property ? , validator ? ) { const hasParamData = property && typeof property === "string" ; const key = hasParamData ? property : undefined ; const vail = hasParamData ? validator : property ; return createParamDecorator ( "query" ) ( key , vail ) ; } export function Body ( ) ; export function Body ( property ) ; export function Body ( validator ) ; export function Body ( property , validator ) ; export function Body ( property ? , validator ? ) { const hasParamData = property && typeof property === "string" ; const key = hasParamData ? property : undefined ; const vail = hasParamData ? validator : property ; return createParamDecorator ( "body" ) ( key , vail ) ; } export function Headers ( ) ; export function Headers ( property ) ; export function Headers ( validator ) ; export function Headers ( property , validator ) ; export function Headers ( property ? , validator ? ) { const hasParamData = property && typeof property === "string" ; const key = hasParamData ? property : undefined ; const vail = hasParamData ? validator : property ; return createParamDecorator ( "headers" ) ( key , vail ) ; } export function UploadedFile ( ) { return createParamDecorator ( "uploadFile" ) ( ) ; }