-
-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add routes for sub operation and fix sematics (#69)
Co-authored-by: Maciej Urbańczyk <urbanczyk.maciej.95@gmail.com>
- Loading branch information
1 parent
d594ce1
commit 2cde657
Showing
5 changed files
with
50 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; |