diff --git a/template/index.html b/template/index.html index cf8afcad8..1fd07671c 100644 --- a/template/index.html +++ b/template/index.html @@ -13,15 +13,17 @@ function displayHelp() { console.log('Available channels:') - {%- for channelName, channel in asyncapi.channels() %} - {%- if channel.hasSubscribe() %} - console.log('* {{ channelName }}'); + {%- for channel in asyncapi.channels() %} + {%- for operation in channel.operations() %} + {%- if operation.isApplicationSubscribing() %} + console.log('* {{ channel.path() }}'); {%- endif -%} {% endfor %} + {% endfor %} console.log('Available commands:') console.log('- listen: Establish a connection with the server at a given path.') console.log(' Usage: listen(channelName)'); - console.log(` Example: listen('{{ asyncapi.channelNames()[0] }}')`); + console.log(` Example: listen('{{ asyncapi.channels()[0].path() }}')`); console.log('- send: Send a message to the server at a currently connected path.') console.log(' Usage: send(message)'); console.log(` Example: send({ greet: 'Hello from client' })`); diff --git a/template/src/api/routes.js b/template/src/api/routes.js index 2c91423af..0bd7bbf89 100644 --- a/template/src/api/routes.js +++ b/template/src/api/routes.js @@ -1,31 +1,35 @@ const util = require('util'); const { Router } = require('express'); const { yellow } = require('../lib/colors'); -{%- for channelName, channel in asyncapi.channels() -%} -{%- if channel.hasSubscribe() %} -{%- if channel.subscribe().id() === undefined -%} + +{%- for channel in asyncapi.channels() -%} +{%- for operation in channel.operations() %} +{%- if operation.isClientSubscribing() %} +{%- if operation.id() === undefined -%} { { 'This template requires operationId to be set in every operation.' | logError }} {%- endif %} -const {{ channelName | camelCase }}Service = require('./services/{{ channelName | kebabCase }}'); +const {{ channel.path() | camelCase }}Service = require('./services/{{ channel.path() | kebabCase }}'); {%- endif -%} +{%- endfor -%} {%- endfor %} const router = Router(); module.exports = router; -{% for channelName, channel in asyncapi.channels() -%} -{%- if channel.hasSubscribe() %} - {%- if channel.subscribe().summary() %} +{%- for channel in asyncapi.channels() -%} +{%- for operation in channel.operations() %} +{%- if operation.isClientSubscribing() %} + {%- if operation.summary() %} /** - * {{ channel.subscribe().summary() }} + * {{ operation.summary() }} */ {%- endif %} -router.ws('{{ channelName | pathResolve }}', async (ws, req) => { +router.ws('{{ channel.path() | pathResolve }}', async (ws, req) => { ws.on('message', async (msg) => { const path = req.path.substr(0, req.path.length - '/.websocket'.length); console.log(`${yellow(path)} message was received:`); console.log(util.inspect(msg, { depth: null, colors: true })); - await {{ channelName | camelCase }}Service.{{ channel.subscribe().id() }}(ws, { message: msg, path, query: req.query }); + await {{ channel.path() | camelCase }}Service.{{ operation.id() }}(ws, { message: msg, path, query: req.query }); }); }); - {%- endif %} +{%- endfor %} {% endfor -%} diff --git a/template/src/api/services/$$channel$$.js b/template/src/api/services/$$channel$$.js index 724fb615c..d60e793de 100644 --- a/template/src/api/services/$$channel$$.js +++ b/template/src/api/services/$$channel$$.js @@ -1,47 +1,49 @@ const service = module.exports = {}; -{% if channel.hasPublish() %} + +{%- for operation in channel.operations() %} +{% if operation.isClientPublishing() %} /** - * {{ channel.publish().summary() }} + * {{ operation.summary() }} * @param {object} ws WebSocket connection. * @param {object} options - * @param {%raw%}{{%endraw%}{{channel.publish().message(0).payload().type()}}{%raw%}}{%endraw%} options.message The message to send. -{%- if channel.publish().message(0).headers() %} -{%- for fieldName, field in channel.publish().message(0).headers().properties() %} + * @param {%raw%}{{%endraw%}{{operation.messages()[0].payload().type()}}{%raw%}}{%endraw%} options.message The message to send. +{%- if operation.messages()[0].headers() %} +{%- for fieldName, field in operation.messages()[0].headers().properties() %} {{ field | docline(fieldName, 'options.message.headers') }} {%- endfor %} {%- endif %} -{%- if channel.publish().message(0).payload() %} -{%- for fieldName, field in channel.publish().message(0).payload().properties() %} +{%- if operation.messages()[0].payload() %} +{%- for fieldName, field in operation.messages()[0].payload().properties() %} {{ field | docline(fieldName, 'options.message.payload') }} {%- endfor %} {%- endif %} */ -service.{{ channel.publish().id() }} = async (ws, { message }) => { +service.{{ operation.id() }} = async (ws, { message }) => { ws.send('Message from the server: Implement your business logic here.'); }; - {%- endif %} -{%- if channel.hasSubscribe() %} + +{%- if operation.isClientSubscribing() %} /** - * {{ channel.subscribe().summary() }} + * {{ operation.summary() }} * @param {object} ws WebSocket connection. * @param {object} options * @param {string} options.path The path in which the message was received. * @param {object} options.query The query parameters used when connecting to the server. - * @param {%raw%}{{%endraw%}{{channel.subscribe().message(0).payload().type()}}{%raw%}}{%endraw%} options.message The received message. -{%- if channel.subscribe().message(0).headers() %} -{%- for fieldName, field in channel.subscribe().message(0).headers().properties() %} + * @param {%raw%}{{%endraw%}{{operation.messages()[0].payload().type()}}{%raw%}}{%endraw%} options.message The received message. +{%- if operation.messages()[0].headers() %} +{%- for fieldName, field in operation.messages()[0].headers().properties() %} {{ field | docline(fieldName, 'options.message.headers') }} {%- endfor %} {%- endif %} -{%- if channel.subscribe().message(0).payload() %} -{%- for fieldName, field in channel.subscribe().message(0).payload().properties() %} +{%- if operation.messages()[0].payload() %} +{%- for fieldName, field in operation.messages()[0].payload().properties() %} {{ field | docline(fieldName, 'options.message.payload') }} {%- endfor %} {%- endif %} */ -service.{{ channel.subscribe().id() }} = async (ws, { message, path }) => { +service.{{ operation.id() }} = async (ws, { message, path }) => { ws.send('Message from the server: Implement your business logic here.'); }; - {%- endif %} +{% endfor %} \ No newline at end of file