Skip to content

Commit

Permalink
rewrite common.yml.js
Browse files Browse the repository at this point in the history
  • Loading branch information
kaushik-rishi committed Apr 29, 2024
1 parent 038d50f commit 92ca38b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
9 changes: 3 additions & 6 deletions helpers/channels-topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,8 @@ export function toHermesTopic(str) {
return str.replace(/\{([^}]+)\}/g, ':$1');
}

export function channelNamesWithPublish(asyncapi) {
const result = [];
asyncapi.channelNames().forEach((name) => {
if (asyncapi.channel(name).hasPublish()) result.push(name);
});
export function channelNamesWithReceive(asyncapi) {
const result = asyncapi.channels().filterByReceive().map(channel => channel.id());
return result;
}

Expand Down Expand Up @@ -151,4 +148,4 @@ export function getProtocol(p) {
// https://mozilla.github.io/nunjucks/templating.html#dump
export function dump(obj) {
return JSON.stringify(obj);
}
}
22 changes: 12 additions & 10 deletions template/config/common.yml.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { File } from '@asyncapi/generator-react-sdk';
import { camelCase, channelNamesWithPublish, dump, host, port, queueName, stripProtocol, toAmqpTopic, toKafkaTopic, toMqttTopic } from '../../helpers/index';
import { camelCase, channelNamesWithReceive, dump, host, port, queueName, stripProtocol, toAmqpTopic, toKafkaTopic, toMqttTopic } from '../../helpers/index';
import { replaceServerVariablesWithValues } from '@asyncapi/generator-filters/src/customFilters';

export default function CommonConfigYAMLRender({ asyncapi, params }) {
const serverProtocol = asyncapi.server(params.server).protocol();
const serverVariables = asyncapi.server(params.server).variables();
const resolvedBrokerUrlWithReplacedVariables = replaceServerVariablesWithValues(asyncapi.server(params.server).url(), serverVariables);
const server = asyncapi.allServers().get(params.server);
const serverProtocol = server.protocol();
const serverVariables = server.variables();
const resolvedBrokerUrlWithReplacedVariables = replaceServerVariablesWithValues(server.url(), serverVariables);

return (
<File name={'common.yml'}>
Expand Down Expand Up @@ -47,7 +48,7 @@ function amqpBlock(url, asyncapi) {
password:
host: ${host(url)}
port:
topics: ${dump(toAmqpTopic(channelNamesWithPublish(asyncapi)))}
topics: ${dump(toAmqpTopic(channelNamesWithReceive(asyncapi)))}
queue: ${queueName(asyncapi.info().title(), asyncapi.info().version())}
queueOptions:
exclusive: false
Expand All @@ -57,9 +58,10 @@ function amqpBlock(url, asyncapi) {
}

function mqttBlock(url, asyncapi, params) {
const server = asyncapi.allServers().get(params.server);
return ` mqtt:
url: ${asyncapi.server(params.server).protocol()}://${stripProtocol(url)}
topics: ${dump(toMqttTopic(channelNamesWithPublish(asyncapi)))}
url: ${server.protocol()}://${stripProtocol(url)}
topics: ${dump(toMqttTopic(channelNamesWithReceive(asyncapi)))}
qos:
protocol: mqtt
retain:
Expand All @@ -75,7 +77,7 @@ function kafkaBlock(url, asyncapi) {
consumerOptions:
groupId: ${camelCase(asyncapi.info().title())}
topics:
${channelNamesWithPublish(asyncapi).map(topic => `- ${toKafkaTopic(topic)}`).join('\n')}
${channelNamesWithReceive(asyncapi).map(topic => `- ${toKafkaTopic(topic)}`).join('\n')}
topicSeparator: '__'
topicPrefix:
`;
Expand All @@ -87,12 +89,12 @@ function kafkaProductionBlock(params, asyncapi) {
ssl:
rejectUnauthorized: true
`;
if (params.securityScheme && asyncapi.components().securityScheme(params.securityScheme).type() !== 'X509') {
if (params.securityScheme && asyncapi.components().securitySchemes().get(params.securityScheme).type() !== 'X509') {
productionBlock += ` sasl:
mechanism: 'plain'
username:
password:
`;
}
return productionBlock;
}
}

0 comments on commit 92ca38b

Please sign in to comment.