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

chore: switched to new intent API #65

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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: 6 additions & 4 deletions template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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' })`);
Expand Down
26 changes: 15 additions & 11 deletions template/src/api/routes.js
Original file line number Diff line number Diff line change
@@ -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 -%}
38 changes: 20 additions & 18 deletions template/src/api/services/$$channel$$.js
Original file line number Diff line number Diff line change
@@ -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 %}