From d63293f9b3a1d7dac9a4fa9cc1bb4e5185df39a0 Mon Sep 17 00:00:00 2001 From: Bishal Date: Thu, 5 Sep 2024 14:49:11 +0000 Subject: [PATCH] support for multi channels --- package.json | 4 -- template/src/api/services/services.js | 19 ++++-- test/__snapshots__/integration.test.js.snap | 11 ++++ test/integration.test.js | 7 +-- test/mocks/asyncapi.yml | 66 +++++++++++++++++++++ 5 files changed, 92 insertions(+), 15 deletions(-) create mode 100644 test/mocks/asyncapi.yml diff --git a/package.json b/package.json index ab78f7abf..22916b84f 100644 --- a/package.json +++ b/package.json @@ -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" }, @@ -91,9 +90,6 @@ } }, "generator": ">=0.50.0 <2.0.0", - "filters": [ - "@asyncapi/generator-filters" - ], "hooks": { "@asyncapi/generator-hooks": "createAsyncapiFile" } diff --git a/template/src/api/services/services.js b/template/src/api/services/services.js index 353e0f6e4..28d2f8813 100644 --- a/template/src/api/services/services.js +++ b/template/src/api/services/services.js @@ -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( + +{`const service_${convertToFilename(channelName)} = {}; - return {content}; +${service} +`} + + ); + }); + return files; } \ No newline at end of file diff --git a/test/__snapshots__/integration.test.js.snap b/test/__snapshots__/integration.test.js.snap index d73ae7152..73a4d710d 100644 --- a/test/__snapshots__/integration.test.js.snap +++ b/test/__snapshots__/integration.test.js.snap @@ -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) => { @@ -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 }); + }); +}); " `; diff --git a/test/integration.test.js b/test/integration.test.js index ec233d99e..1fefb9d4a 100644 --- a/test/integration.test.js +++ b/test/integration.test.js @@ -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()); @@ -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' }; @@ -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', diff --git a/test/mocks/asyncapi.yml b/test/mocks/asyncapi.yml new file mode 100644 index 000000000..304dfc658 --- /dev/null +++ b/test/mocks/asyncapi.yml @@ -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. \ No newline at end of file