import { posix } from 'path' ; import { ROUTE_PREFIX , MW_PREFIX , PARAMS_PREFIX , ASYNC_PARAMS_PREFIX , ACTION_TYPES } from './constants' ; Controller export param { string } path returns export function Controller ( path = '' ) { return function ( target ) : void { const proto = target . prototype ; const mws = Reflect . getMetadata ( MW_PREFIX , target ) || [ ] ; const routeDefs = Reflect . getMetadata ( ROUTE_PREFIX , proto ) || [ ] ; const routes = [ ] ; for ( const route of routeDefs ) { const fnMws = Reflect . getMetadata ( `${ MW_PREFIX }_${ route . name }` , proto ) || [ ] ; const params = Reflect . getMetadata ( `${ PARAMS_PREFIX }_${ route . name }` , proto ) ; const asyncParams = Reflect . getMetadata ( `${ ASYNC_PARAMS_PREFIX }_${ route . name }` , proto ) ; routes . push ( { method : route . method , url : posix . join ( '/' , path , route . path ) , middleware : [ ... mws , ... fnMws ] , name : route . name , params , asyncParams } ) ; } Reflect . defineMetadata ( ROUTE_PREFIX , routes , target ) ; } ; } Controller Use Get Use export param { ... any [ ] } middlewares returns export function Use ( ... middlewares ) { return ( target , propertyKey , descriptor ) => { if ( ! propertyKey ) { propertyKey = '' ; } else { propertyKey = '_' + propertyKey ; } Reflect . defineMetadata ( `${ MW_PREFIX }${ propertyKey }` , middlewares , target ) ; } ; } Controller Route export param { string } method param { string } path returns export function Route ( method , path = '' ) { return ( target , name , descriptor ) => { const meta = Reflect . getMetadata ( ROUTE_PREFIX , target ) || [ ] ; meta . push ( { method , path , name } ) ; Reflect . defineMetadata ( ROUTE_PREFIX , meta , target ) ; } ; } Controller Get export function Get ( path ? ) { return Route ( ACTION_TYPES . GET , path ) ; } Controller Post export function Post ( path ? ) { return Route ( ACTION_TYPES . POST , path ) ; } Controller Put export function Put ( path ? ) { return Route ( ACTION_TYPES . PUT , path ) ; } Controller Patch export function Patch ( path ? ) { return Route ( ACTION_TYPES . PATCH , path ) ; } Controller Delete export function Delete ( path ? ) { return Route ( ACTION_TYPES . DELETE , path ) ; } export param { any } fn returns export function Inject ( fn ) { return function ( target , name , index ) { const meta = Reflect . getMetadata ( `${ PARAMS_PREFIX }_${ name }` , target ) || [ ] ; meta . push ( { index , name , fn } ) ; Reflect . defineMetadata ( `${ PARAMS_PREFIX }_${ name }` , meta , target ) ; } ; } export param { any } fn returns export function InjectAsync ( fn ) { return function ( target , name , index ) { const meta = Reflect . getMetadata ( `${ ASYNC_PARAMS_PREFIX }_${ name }` , target ) || [ ] ; meta . push ( { index , name , fn } ) ; Reflect . defineMetadata ( `${ ASYNC_PARAMS_PREFIX }_${ name }` , meta , target ) ; } ; } Controller Post export returns export function Ctx ( ) { return Inject ( ctx => ctx ) ; } Controller Post export returns export function Req ( ) { return Inject ( ctx => ctx . req ) ; } Controller Post export returns export function Request ( ) { return Inject ( ctx => ctx . request ) ; } Controller Post export returns export function Res ( ) { return Inject ( ctx => ctx . res ) ; } Controller Post export returns export function Response ( ) { return Inject ( ctx => ctx . response ) ; } Controller Post export returns export function Body ( ) { return Inject ( ctx => ctx . request . body ) ; } Controller Post export returns export function Fields ( ) { return Inject ( ctx => ctx . request . fields ) ; } Controller Post export returns export function File ( ) { return Inject ( ctx => { if ( ctx . request . files . length ) return ctx . request . files [ 0 ] ; return ctx . request . files ; } ) ; } Controller Post export returns export function Files ( ) { return Inject ( ctx => ctx . request . files ) ; } Controller Post export returns export function QueryParam ( prop ? ) { return Inject ( ctx => { if ( ! prop ) return ctx . query ; return ctx . query [ prop ] ; } ) ; } Controller Post export returns export function QueryParams ( ) { return QueryParam ( ) ; } Controller Post export returns export function Param ( prop ? ) { return Inject ( ctx => { if ( ! prop ) return ctx . params ; return ctx . params [ prop ] ; } ) ; } Controller Post export returns export function Params ( ) { return Param ( ) ; }