import { Body , Controller , Inject , Post } from '@nestjs/common' ; import { ClientProxy , Ctx , MessagePattern , Payload } from '@nestjs/microservices' ; import { Observable , of } from 'rxjs' ; import { catchError , timeout } from 'rxjs/operators' ; import { PgNotifyContext , PgNotifyEventPattern , PgNotifyMessagePattern , PgNotifyResponse } from '../../../src' ; import { AppToken } from './app.token' ; @ Controller ( ) export class AppController { private readonly onEventPatternCalls : Map < string , { payload : any ; context : PgNotifyContext ; } > ; constructor ( @ Inject ( AppToken . PgNotifyClient ) private readonly client ) { this . onEventPatternCalls = new Map ( ) ; } @ MessagePattern ( { event : 'event' } ) onFakeMessagePattern ( ) { return null ; } @ PgNotifyEventPattern ( 'event' ) onEventPattern ( @ Payload ( ) data , @ Ctx ( ) context ) { this . onEventPatternCalls . set ( data . eventId , { payload : data . payload , context : context , } ) ; return 'Event: Ok' ; } @ PgNotifyMessagePattern ( { event : 'event' } ) onMessagePattern ( @ Payload ( ) payload , @ Ctx ( ) context ) { return { payload : payload , context : context , response : 'Request: Ok' } ; } @ PgNotifyMessagePattern ( 'event-with-unified-response' ) onMessagePatternWithUnifiedResponse ( @ Payload ( ) payload , @ Ctx ( ) context ) { return PgNotifyResponse . success ( { payload : payload , context : context , response : 'Request: Ok' } ) ; } @ Post ( 'send-request' ) sendRequest ( @ Body ( ) body ) { return this . client . send ( { event : 'event' } , body ) . pipe ( timeout ( 1000 ) , catchError ( err => of ( { status : 500 , error : err . message } ) ) ) ; } @ Post ( 'send-request-with-unified-response' ) sendRequestWithUnifiedResponse ( @ Body ( ) body ) { return this . client . send ( 'event-with-unified-response' , body ) . pipe ( timeout ( 1000 ) , catchError ( err => of ( { status : 500 , error : err . message } ) ) ) ; } @ Post ( 'emit-event' ) emitEvent ( @ Body ( ) body ) { return this . client . emit ( 'event' , body ) . pipe ( timeout ( 1000 ) , catchError ( err => of ( { status : 500 , error : err . message } ) ) ) ; } getOnEventPatternCall ( id ) { return this . onEventPatternCalls . get ( id ) ; } }