import { Binding , Context , inject , injectable , Provider } from '@loopback/core' ; import { asMiddleware , Middleware , RestMiddlewareGroups } from '@loopback/rest' ; import { LOGGER_SERVICE } from '../keys' ; import { bindingScope , count , logContext , logContexts , logRequest , } from '../util' ; @ injectable ( asMiddleware ( { group : 'spy' , upstreamGroups : RestMiddlewareGroups . AUTHENTICATION , downstreamGroups : RestMiddlewareGroups . INVOKE_METHOD , } ) , { tags : { name : 'Spy' } , scope : bindingScope ( 'SpyMiddleware' ) , } ) export class SpyMiddlewareProvider implements Provider < Middleware > { constructor ( @ inject . context ( ) resolutionCtx , @ inject . binding ( ) private binding ) { logContexts ( resolutionCtx , binding ) ; } value ( ) { const spyMiddleware = async ( ctx , next ) => { logContext ( 'Request' , ctx , this . binding ) ; logRequest ( ctx . request ) ; ctx . bind ( LOGGER_SERVICE ) . toAlias ( 'services.RequestLoggerService' ) ; await count ( ctx , 'SpyMiddleware' ) ; const result = await next ( ) ; return result ; } ; return spyMiddleware ; } }