import { RequestStartMiddleware , RequestEndMiddleware , Middleware , ActionFilter , ResultFilter , ExceptionFilter , IUserIdentity } from '../../../../index' ; import { injectable } from 'inversify' ; @ injectable ( ) export class TokenStartMiddleware extends RequestStartMiddleware { constructor ( private userIdentity ) { super ( ) ; } invoke ( request , response , next ) { setTimeout ( ( ) => { this . userIdentity . set ( 'role' , `admin - ${ request . body . name }` ) ; next ( ) ; } , 20000 ) ; } } @ injectable ( ) export class ResponseMiddleware extends RequestEndMiddleware { constructor ( private userIdentity ) { super ( ) ; } invoke ( request , response , next , result ) { this . userIdentity . set ( 'FinalResponseFilter' , `executed - ${ request . body . name }` ) ; response . json ( { result : result , data : this . userIdentity } ) ; } } @ injectable ( ) export class LogMiddleware extends Middleware { constructor ( private userIdentity ) { super ( ) ; } invoke ( request , response , next , data ? ) { this . userIdentity . set ( 'loggedIn' , { yesterday : true , name : request . body . name } ) ; next ( ) ; } } @ injectable ( ) export class RequestFilter extends ActionFilter { constructor ( private userIdentity ) { super ( ) ; } beforeExecution ( request , response , next , data ? ) { this . userIdentity . set ( 'allow' , true ) ; next ( ) ; } afterExecution ( request , response , next , result , data ? ) { this . userIdentity . set ( 'afterFilter' , `executed - ${ request . body . name }` ) ; next ( ) ; } } @ injectable ( ) export class JsonFilter extends ResultFilter { constructor ( private userIdentity ) { super ( ) ; } invoke ( request , response , next , result , data ? ) { this . userIdentity . set ( 'JsonFilter' , `executed - ${ request . body . name }` ) ; next ( ) ; } } @ injectable ( ) export class NException extends ExceptionFilter { constructor ( private userIdentity ) { super ( ) ; } invoke ( err , request , response , next ) { } }