From 6b12c62bdeaacceabae3d4fcbf515cc6c995996b Mon Sep 17 00:00:00 2001 From: Khuda Dad Nomani <32505158+KhudaDad414@users.noreply.github.com> Date: Fri, 15 Mar 2024 09:31:13 +0000 Subject: [PATCH] fix: send messages to the address of the channel (#737) --- src/adapters/mqtt/index.ts | 11 ++++------- src/lib/functions.ts | 21 ++++++++++++++++++++- tsconfig.json | 3 ++- 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/adapters/mqtt/index.ts b/src/adapters/mqtt/index.ts index 6c0c95a48..bc196576e 100644 --- a/src/adapters/mqtt/index.ts +++ b/src/adapters/mqtt/index.ts @@ -248,14 +248,11 @@ class MqttAdapter extends Adapter { _send(message: GleeMessage): Promise { return new Promise((resolve, reject) => { - const binding = this.parsedAsyncAPI - .channels() - .get(message.channel) - .bindings() - .get('mqtt') - ?.value() + const channel = this.parsedAsyncAPI.channels().get(message.channel) + const address = channel.address() + const binding = channel.bindings().get('mqtt')?.value() this.client.publish( - message.channel, + address, message.payload, { qos: binding?.qos ? binding.qos : 2, diff --git a/src/lib/functions.ts b/src/lib/functions.ts index 6842fb88e..5432defbf 100644 --- a/src/lib/functions.ts +++ b/src/lib/functions.ts @@ -152,7 +152,26 @@ export async function trigger({ localServerProtocols.includes(serverProtocol) && !isRemoteServer(parsedAsyncAPI, msg.server) const channelName = msg.channel || message.channel - const operations = parsedAsyncAPI.channels().get(channelName).operations().filterBySend() + const channel = parsedAsyncAPI.channels().get(channelName) + + if (!channel) { + const warnMessage = `Failed to send: "${channelName}" channel not found. please make sure you have a channel named: "${channelName}" in your AsyncAPI file.` + logWarningMessage(warnMessage, { + highlightedWords: [channelName], + }) + return + } + + const operations = channel.operations().filterBySend() + + if (operations.length === 0) { + const warnMessage = `Failed to send: No 'send' operation found for channel "${channelName}".` + logWarningMessage(warnMessage, { + highlightedWords: [channelName], + }) + return + } + operations.forEach(operation => { app.send( new GleeMessage({ diff --git a/tsconfig.json b/tsconfig.json index b053474a9..63f8a793f 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,11 +1,12 @@ { "compilerOptions": { + "skipLibCheck": true, "outDir": "./dist", "allowJs": false, "target": "es6", "esModuleInterop": true, "moduleResolution": "nodenext", - "module": "es2020" + "module": "NodeNext" }, "include": [ "./src/**/*"