Skip to content

Commit

Permalink
feat: add routes for sub operation and fix sematics (#69)
Browse files Browse the repository at this point in the history
Co-authored-by: Maciej Urbańczyk <urbanczyk.maciej.95@gmail.com>
  • Loading branch information
derberg and magicmatatjahu committed Apr 23, 2021
1 parent d594ce1 commit 2cde657
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 42 deletions.
21 changes: 21 additions & 0 deletions filters/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,24 @@ function pathResolve(pathName, basePath = '/') {
return path.resolve(basePath, pathName);
}
filter.pathResolve = pathResolve;

/*
* returns comma separated string of all operationIds of a given channel
*/
const parseOperationId = (channel, opName) => {
const id = opName === 'subscribe' ? channel.subscribe().id() : channel.publish().id();
if (!id) throw new Error('This template requires operationId to be set in every operation.');
return id;
}

function getOperationIds(channel) {
const list = [];
if (channel.hasSubscribe()) {
list.push(parseOperationId(channel, 'subscribe'));
}
if (channel.hasPublish()) {
list.push(parseOperationId(channel, 'publish'));
}
return list.filter(Boolean).join(', ');
}
filter.getOperationIds = getOperationIds;
2 changes: 1 addition & 1 deletion template/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
</head>

<body>
Open your browser console to see the logs.
<h1>Open your browser console to see the logs and details of API you can use to talk to the server.</h1>
<script>
let connection;

Expand Down
28 changes: 12 additions & 16 deletions template/src/api/routes.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
const util = require('util');
const { Router } = require('express');
const { pathParser } = require('../lib/path');
const { yellow } = require('../lib/colors');
{%- for channelName, channel in asyncapi.channels() -%}
{%- if channel.hasSubscribe() %}
{%- if channel.subscribe().id() === undefined -%}
{ { 'This template requires operationId to be set in every operation.' | logError }}
{%- endif %}
const {{ channelName | camelCase }}Service = require('./services/{{ channelName | kebabCase }}');
{%- endif -%}
{% set allOperationIds = channel | getOperationIds %}
const { {{ allOperationIds }} } = require('./services/{{ channelName | kebabCase }}');
{%- endfor %}
const router = Router();
module.exports = router;
{% for channelName, channel in asyncapi.channels() -%}
{%- if channel.hasSubscribe() %}
{%- if channel.subscribe().summary() %}
/**
* {{ channel.subscribe().summary() }}
*/
{%- endif %}
router.ws('{{ channelName | pathResolve }}', async (ws, req) => {
const path = pathParser(req.path);
console.log(`${yellow(path)} client connected.`);
{%- if channel.hasSubscribe() %}
await {{ channel.subscribe().id() }}(ws);
{%- endif %}

{%- if channel.hasPublish() %}
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.publish().id() }}(ws, { message: msg, path, query: req.query });
});
{%- endif %}
});

{%- endif %}
{% endfor -%}
38 changes: 13 additions & 25 deletions template/src/api/services/$$channel$$.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
const service = module.exports = {};
{% if channel.hasPublish() %}
{% if channel.hasSubscribe() %}
/**
* {{ channel.publish().summary() }}
* {{ channel.subscribe().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() %}
{{ field | docline(fieldName, 'options.message.headers') }}
{%- endfor %}
{%- endif %}
{%- if channel.publish().message(0).payload() %}
{%- for fieldName, field in channel.publish().message(0).payload().properties() %}
{{ field | docline(fieldName, 'options.message.payload') }}
{%- endfor %}
{%- endif %}
*/
service.{{ channel.publish().id() }} = async (ws, { message }) => {
ws.send('Message from the server: Implement your business logic here.');
service.{{ channel.subscribe().id() }} = async (ws) => {
ws.send('Message from the server: Implement here your business logic that sends messages to a client after it connects.');
};

{%- endif %}
{%- if channel.hasSubscribe() %}
{%- if channel.hasPublish() %}
/**
* {{ channel.subscribe().summary() }}
* {{ channel.publish().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%}{{channel.publish().message(0).payload().type()}}{%raw%}}{%endraw%} options.message The received message.
{%- if channel.publish().message(0).headers() %}
{%- for fieldName, field in channel.publish().message(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 channel.publish().message(0).payload() %}
{%- for fieldName, field in channel.publish().message(0).payload().properties() %}
{{ field | docline(fieldName, 'options.message.payload') }}
{%- endfor %}
{%- endif %}
*/
service.{{ channel.subscribe().id() }} = async (ws, { message, path }) => {
ws.send('Message from the server: Implement your business logic here.');
service.{{ channel.publish().id() }} = async (ws, { message, path, query }) => {
ws.send('Message from the server: Implement here your business logic that reacts on messages sent from a client.');
};

{%- endif %}
3 changes: 3 additions & 0 deletions template/src/lib/path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports.pathParser = (path) => {
return path.substr(0, path.length - '/.websocket'.length);
};

0 comments on commit 2cde657

Please sign in to comment.