Skip to content

Commit

Permalink
support for multi channels
Browse files Browse the repository at this point in the history
  • Loading branch information
ibishal committed Sep 5, 2024
1 parent 57b15fa commit d63293f
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 15 deletions.
4 changes: 0 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
"access": "public"
},
"dependencies": {
"@asyncapi/generator-filters": "^2.1.0",
"@asyncapi/generator-hooks": "^0.1.0",
"@asyncapi/generator-react-sdk": "^1.1.1"
},
Expand Down Expand Up @@ -91,9 +90,6 @@
}
},
"generator": ">=0.50.0 <2.0.0",
"filters": [
"@asyncapi/generator-filters"
],
"hooks": {
"@asyncapi/generator-hooks": "createAsyncapiFile"
}
Expand Down
19 changes: 13 additions & 6 deletions template/src/api/services/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@ service.${publish.id()} = async (ws, { message, path, query }) => {
}

module.exports = function servicesRender({ asyncapi }) {
const channelName = Object.keys(asyncapi.channels())[0];
const channel = asyncapi.channels()[channelName];
const service = generateService(channel);
const files = [];

const content = `const service = module.exports = {};
${service}`;
Object.keys(asyncapi.channels()).forEach(channelName => {
const channel = asyncapi.channels()[channelName];
const service = generateService(channel);
files.push(
<File name={`${convertToFilename(channelName)}.js`}>
{`const service_${convertToFilename(channelName)} = {};
return <File name={`${convertToFilename(channelName)}.js`}>{content}</File>;
${service}
`}
</File>
);
});
return files;
}
11 changes: 11 additions & 0 deletions test/__snapshots__/integration.test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const { Router } = require('express');
const { pathParser } = require('../lib/path');
const { yellow } = require('../lib/colors');
const { onEcho, sendEcho } = require('./services/echo');
const { onBroadcastReceive, sendBroadcast } = require('./services/broadcast');
const router = Router();
module.exports = router;
router.ws('/echo', async (ws, req) => {
Expand All @@ -49,6 +50,16 @@ router.ws('/echo', async (ws, req) => {
await sendEcho(ws, { message: msg, path, query: req.query });
});
});
router.ws('/broadcast', async (ws, req) => {
const path = pathParser(req.path);
console.log(\`\${yellow(path)} client connected.\`);
await onBroadcastReceive(ws);
ws.on('message', async (msg) => {
console.log(\`\${yellow(path)} message was received:\`);
console.log(util.inspect(msg, { depth: null, colors: true }));
await sendBroadcast(ws, { message: msg, path, query: req.query });
});
});
"
`;
Expand Down
7 changes: 2 additions & 5 deletions test/integration.test.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
const path = require('path');
const Generator = require('@asyncapi/generator');
const {readFile} = require('fs').promises;
const fetch = require('node-fetch');
const console = require('console');

const MAIN_TEST_RESULT_PATH = path.join('test', 'temp', ' integrationTestResult');
const URL = 'https://raw.githubusercontent.com/asyncapi/generator/master/apps/generator/test/docs/ws.yml';

describe('template integration test using generator', () => {
const generateFolderName = () => {
return path.resolve(MAIN_TEST_RESULT_PATH, Date.now().toString());
Expand All @@ -16,7 +13,6 @@ describe('template integration test using generator', () => {

it('should generate application files ', async () => {
const outputDir = generateFolderName();
const asyncapiFile = await fetch(URL);
const params = {
server: 'localhost'
};
Expand All @@ -25,7 +21,8 @@ describe('template integration test using generator', () => {
templateParams: params
});
console.log(outputDir);
await generator.generateFromString(await asyncapiFile.text());
const asyncApiPath = './mocks/asyncapi.yml';
await generator.generateFromFile(path.resolve('test', asyncApiPath));
const expectedFiles = [
'src/api/index.js',
'src/api/routes.js',
Expand Down
66 changes: 66 additions & 0 deletions test/mocks/asyncapi.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
asyncapi: 2.0.0
info:
title: WebSockets echo server
version: 1.0.0
description: "Hello from ws"

servers:
localhost:
url: localhost
protocol: ws

channels:
/echo:
bindings:
ws:
query:
type: object
properties:
times:
type: integer
description: How many times the message should be echoed.
minimum: 1
bindingVersion: 0.1.0
subscribe:
operationId: onEcho
message:
$ref: '#/components/messages/echo'
publish:
operationId: sendEcho
message:
$ref: '#/components/messages/echo'

/broadcast:
bindings:
ws:
query:
type: object
properties:
messageId:
type: string
description: Unique identifier for the broadcast message.
bindingVersion: 0.1.0
subscribe:
operationId: onBroadcastReceive
message:
$ref: '#/components/messages/broadcastMessage'
publish:
operationId: sendBroadcast
message:
$ref: '#/components/messages/broadcastMessage'

components:
messages:
echo:
payload:
type: string
broadcastMessage:
payload:
type: object
properties:
message:
type: string
description: The message to be broadcasted.
sender:
type: string
description: The sender of the message.

0 comments on commit d63293f

Please sign in to comment.