Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: support asyncapi v3 #294

Merged
merged 31 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c5db980
update packages to support v3
kaushik-rishi Apr 29, 2024
038d50f
rewrite package.json.js
kaushik-rishi Apr 29, 2024
92ca38b
rewrite common.yml.js
kaushik-rishi Apr 29, 2024
20691d6
rewrite entrypoint file
kaushik-rishi Apr 30, 2024
0c1a899
rewrite entrypoint file
kaushik-rishi Apr 30, 2024
dd4bbd5
rewrite handlers, readme, routes
kaushik-rishi Apr 30, 2024
80c1cf1
convert serverVariables to map
kaushik-rishi Apr 30, 2024
27eea8b
update snapshot tests - 1
kaushik-rishi Apr 30, 2024
8bc93ab
update snapshot tests - 2
kaushik-rishi Apr 30, 2024
78f4e44
fix linting
kaushik-rishi Apr 30, 2024
afb5e63
reduce duplicacy in handler
kaushik-rishi Apr 30, 2024
2d2b4f5
Merge branch 'master' into migrate-v3
kaushik-rishi Apr 30, 2024
0317007
remove intermediary variables
kaushik-rishi Apr 30, 2024
b31bb29
re-add test command
kaushik-rishi Apr 30, 2024
d5242f5
formatting issues
kaushik-rishi Apr 30, 2024
f370bec
add mocks for spec v3 version
kaushik-rishi Apr 30, 2024
e6f5fdc
add new snapshot tests
kaushik-rishi Apr 30, 2024
c32123c
update snapshot tests
kaushik-rishi Apr 30, 2024
3f1e6a7
update snapshot tests - kafka v3
kaushik-rishi Apr 30, 2024
ddc2d2f
update package.json
kaushik-rishi May 1, 2024
5327cf1
add linter ignores for integration.test.js
kaushik-rishi May 1, 2024
1f0370c
uncomment and enable security checks
kaushik-rishi May 1, 2024
88a8c66
readme v3 changes
kaushik-rishi May 1, 2024
0d953a8
disable validation for spec v3 and test pub-sub
kaushik-rishi May 4, 2024
2f60498
update snapshot tests - bug fixes
kaushik-rishi May 4, 2024
1c40743
update linting
kaushik-rishi May 4, 2024
c7914d5
disable validation package installation for v3
kaushik-rishi May 4, 2024
fef4790
disable validation package installation - update snapshots
kaushik-rishi May 4, 2024
0f0161c
Merge branch 'master' into migrate-v3
derberg May 6, 2024
53ad16c
disable validation module import in spec v3
kaushik-rishi May 6, 2024
b780a68
fix linting
kaushik-rishi May 7, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions helpers/channels-topics.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +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);
});
return result;
export function channelNamesWithReceive(asyncapi) {
return asyncapi.channels().filterByReceive().map(channel => channel.id());
}

export function host(url) {
Expand Down Expand Up @@ -151,4 +147,4 @@ export function getProtocol(p) {
// https://mozilla.github.io/nunjucks/templating.html#dump
export function dump(obj) {
return JSON.stringify(obj);
}
}
84 changes: 42 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@asyncapi/nodejs-template",
"version": "2.0.1",
"version": "2.0.0",
"description": "Node.js template for the AsyncAPI generator.",
"keywords": [
"asyncapi",
Expand All @@ -21,7 +21,6 @@
"scripts": {
"test": "rimraf test/temp && jest --modulePathIgnorePatterns='./template'",
"test:updateSnapshot": "rimraf test/temp && jest --updateSnapshot --modulePathIgnorePatterns='./template'",
"test:example": "node test/example/script.js",
kaushik-rishi marked this conversation as resolved.
Show resolved Hide resolved
"lint": "eslint --max-warnings 0 --config .eslintrc .",
"lint:fix": "eslint --fix --config .eslintrc .",
"generate:assets": "npm run generate:readme:toc",
Expand All @@ -34,23 +33,24 @@
"dependencies": {
"@asyncapi/generator-filters": "^2.1.0",
"@asyncapi/generator-hooks": "^0.1.0",
"@asyncapi/generator-react-sdk": "^1.0.18",
"@asyncapi/generator-react-sdk": "^1.0.14",
"eslint-plugin-react": "^7.34.1",
"filenamify": "^4.1.0",
"filenamify": "^4.3.0",
"js-beautify": "^1.15.1",
"lodash": "^4.17.15",
"lodash": "^4.17.21",
"markdown-toc": "^1.2.0"
},
"devDependencies": {
"@asyncapi/generator": "^1.17.25",
"eslint": "^8.7.0",
"@asyncapi/generator": "^1.17.12",
"eslint": "^8.57.0",
"eslint-plugin-jest": "^25.7.0",
"eslint-plugin-sonarjs": "^0.11.0",
"jest": "^27.3.1",
"node-fetch": "^2.6.1",
"rimraf": "^5.0.1"
"jest": "^27.5.1",
"node-fetch": "^2.7.0",
"rimraf": "^5.0.5"
},
"generator": {
"apiVersion": "v3",
"supportedProtocols": [
"amqp",
"mqtt",
Expand Down Expand Up @@ -94,4 +94,4 @@
"^nimma/(.*)": "<rootDir>/node_modules/nimma/dist/cjs/$1"
}
}
}
}
17 changes: 16 additions & 1 deletion template/README.md.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
import { File } from '@asyncapi/generator-react-sdk';

export default function readmeFile({asyncapi, params}) {
const server = asyncapi.allServers().get(params.server);
const protocol = server.protocol();
const security = server.security();

let hasSecuritySchemeX509 = false;
let securitySchemeType;
if (params.securityScheme && security && security.length > 0) {
const securityReq = security[0].all();
if (securityReq && securityReq.length > 0) {
securitySchemeType = securityReq[0].scheme().type();
}
}

hasSecuritySchemeX509 = (params.securityScheme && (protocol === 'kafka' || protocol === 'kafka-secure') && securitySchemeType === 'X509');

return <File name={'README.md'}>
{`# ${ asyncapi.info().title() }

Expand All @@ -12,7 +27,7 @@ ${ asyncapi.info().description() || '' }
\`\`\`sh
npm i
\`\`\`
${(params.securityScheme && (asyncapi.server(params.server).protocol() === 'kafka' || asyncapi.server(params.server).protocol() === 'kafka-secure') && asyncapi.components().securityScheme(params.securityScheme).type() === 'X509') ? '1. (Optional) For X509 security provide files with all data required to establish secure connection using certificates. Place files like `ca.pem`, `service.cert`, `service.key` in the root of the project or the location that you explicitly specified during generation.' : ''}
${ hasSecuritySchemeX509 ? '1. (Optional) For X509 security provide files with all data required to establish secure connection using certificates. Place files like `ca.pem`, `service.cert`, `service.key` in the root of the project or the location that you explicitly specified during generation.' : ''}

## Import and start

Expand Down
27 changes: 17 additions & 10 deletions template/config/common.yml.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
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 serverVariablesArray = server.variables();
const serverVariables = {};
serverVariablesArray.forEach(item => {
serverVariables[item.id()] = item;
});

const resolvedBrokerUrlWithReplacedVariables = replaceServerVariablesWithValues(server.url(), serverVariables);

return (
<File name={'common.yml'}>
Expand Down Expand Up @@ -47,7 +53,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 +63,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 +82,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 +94,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;
}
}
2 changes: 1 addition & 1 deletion template/package.json.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export default function packageFile({ asyncapi, params }) {
'node-yaml-config': '0.0.4',
};

const serverProtocol = asyncapi.server(params.server).protocol();
const serverProtocol = asyncapi.allServers().get(params.server).protocol();
if (serverProtocol === 'mqtt' || serverProtocol === 'mqtts') {
dependencies['hermesjs-mqtt'] = '2.x';
} else if (serverProtocol === 'kafka' || serverProtocol === 'kafka-secure') {
Expand Down
Loading
Loading