julian.schmidt
2022-08-16 11:52
Hello guys, we hope you can help us or push us into a direction :slightly_smiling_face:
*Nestjs with Google PubSub (consumer driven event architecture)*
We have multiple Nestjs applications that talk via Google PubSub via Push Configuration. Simplified, we send an event to Google PubSub and it will then send an HTTP Request to every subscriber. We want to make sure, that the provider sends the right data and thus want to create consumer driven contracts.
The Provider sends a payload and Google PubSub sends the event payload wrapped. It looks something like this:
`{`
`messageId: '234857239457234',`
`data: 'ewpmb286ICdiYXInCn0='`
`}`
The json payload is base64 encoded.
The nestjs controller/route uses a pipe to convert the base64 encoded payload back to json format. Theres also a validation pipe that checks if the data is valid.
`@Controller('orders')`
`export class OrdersController {`
`public constructor(private readonly ordersService: OrdersService) {}`
`@Post('events/order-placed')`
`@UsePipes(new PubsubPipe<OrderPlacedEvent>(), new ValidationPipe())`
`public async create(@Body() createOrderDto: OrderPlacedEvent): Promise<void> {`
`await this.ordersService.create(createOrderDto);`
`}`
`}`
The messaging approach doesn't make a request and thus the pipes are not invoked. Should we find a way to still invoke the pipes when just calling the method (With the syncronousBodyHandler)? Or should we make a request with something like supertest? But how would the verify argument look like?
Also we would like to write a typesafe test. Is there a way to use typescript to its fullest and have compilation errors when you define the expected objected and it doesn't match the Endpoint/Controller method?