import assert = require ( 'assert' ) ; import { injectable } from 'inversify' ; import is = require ( 'is' ) ; import { darukContainer } from '../core/inversify.config' ; import { TYPES } from '../core/types' ; import { Constructor } from '../typings/daruk' ; import { MIDDLEWARE_NAME } from './constants' ; desc param { string } middlewareName param options return export function middleware ( middlewareName , options ? ) { assert ( is . string ( middlewareName ) , `[Decorator @middleware] parameter must be a string` ) ; return ( target , propertyKey , descriptor ) => { const middleares = Reflect . getMetadata ( MIDDLEWARE_NAME , target . constructor , propertyKey ) || [ ] ; middleares . push ( { middlewareName , options } ) ; Reflect . defineMetadata ( MIDDLEWARE_NAME , middleares , target . constructor , propertyKey ) ; } ; } export function defineMiddleware ( middlewareName ) { return ( target ) => { injectable ( ) ( target ) ; darukContainer . bind < Constructor > ( TYPES . Middleware ) . to ( target ) . whenTargetNamed ( middlewareName ) ; } ; }