All files / src/middlewares existsInAsyncAPI.ts

5.88% Statements 1/17
0% Branches 0/7
33.33% Functions 1/3
6.25% Lines 1/16

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31          4x                                                  
import { AsyncAPIDocumentInterface as AsyncAPIDocument } from '@asyncapi/parser'
import { MiddlewareCallback } from './index.js'
import GleeQuoreMessage from '../lib/message.js'
 
export default (asyncapi: AsyncAPIDocument) =>
  function existsInAsyncAPI(event: GleeQuoreMessage, next: MiddlewareCallback) {
    const messageChannel = asyncapi.channels().get(event.channel)
    Iif (!messageChannel) {
      return next(new Error(`Invalid or undefined channel: '${event.channel}'. Ensure that '${event.channel}' is both a valid name and defined in the AsyncAPI file.`))
    }
    const receiveOperations = messageChannel.operations().filterByReceive()
    const sendOperations = messageChannel.operations().filterBySend()
    Iif (event.isInbound()) {
      const hasReceiveOperations = receiveOperations.length > 0
      Iif (!hasReceiveOperations) {
        return next(new Error(`Your application is receiving a message on channel "${messageChannel.id()}" but none of the operations in your AsyncAPI document defines such behavior. If you belive your code is correct, you may want to add a 'receive' operation or a «reply» field to a 'send' operation in your AsyncAPI document.  Otherwise, check your code.`))
      }
    }
 
    Iif (event.isOutbound()) {
      const hasSendOperations = sendOperations.length > 0
      const hasReplyInOperation = receiveOperations.some(operation => operation.reply())
      Iif (!hasSendOperations && !hasReplyInOperation) {
        return next(new Error(`Your application is trying to send a message to channel "${messageChannel.id()}" but none of the operations in your AsyncAPI document defines such behavior. If you believe your code is correct, you may want to add a 'send' operation or a «reply» field to a 'receive' operation in your AsyncAPI document. Otherwise, check your code.`))
      }
    }
 
 
    return next()
  }