Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 3 additions & 2 deletions packages/helpers/src/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { getMessageExamples, getOperationMessages } = require('./operations');
const { getServerUrl, getServer } = require('./servers');
const { getClientName, listFiles } = require('./utils');
const { getClientName, listFiles, getInfo } = require('./utils');
const { getQueryParams } = require('./bindings');

module.exports = {
Expand All @@ -10,5 +10,6 @@ module.exports = {
listFiles,
getQueryParams,
getOperationMessages,
getMessageExamples
getMessageExamples,
getInfo
};
25 changes: 24 additions & 1 deletion packages/helpers/src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,31 @@ const listFiles = async (dir) => {
.map(dirE => dirE.name);
};

/**
* Validate and retrieve the AsyncAPI info object from an AsyncAPI document.
*
* Throws an error if the provided AsyncAPI document has no `info` section.
*
* @param {object} asyncapi - The AsyncAPI document object.
* @returns {object} The validated info object from the AsyncAPI document.
*/
const getInfo = (asyncapi) => {
if (!asyncapi) {
throw new Error('Provided AsyncAPI document is invaild.');
}
if (!asyncapi.info) {
throw new Error('Provided AsyncAPI document doesn\'t contain info.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getInfo(asyncapi.info()) - this is how this function is used, so it doesn't really bring much value, except for the error - but error is not accurate, as it talks about AsyncAPI not containing info, although the argument of function is info object, so might be that template developer makes an errror and do not pass the info object - which means we provide wrong error to the user

imho this is how this helper should be used, and wrap parser code: getInfo(asyncapi)

}
const info = asyncapi.info();
if (!info) {
throw new Error('AsyncAPI document info object cannot be an empty.');
}
return info;
};

module.exports = {
getClientName,
listFiles
listFiles,
getInfo
};

37 changes: 36 additions & 1 deletion packages/helpers/test/utils.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const path = require('path');
const { Parser, fromFile } = require('@asyncapi/parser');
const { getClientName } = require('@asyncapi/generator-helpers');
const { getClientName, getInfo } = require('@asyncapi/generator-helpers');

const parser = new Parser();
const asyncapi_v3_path = path.resolve(__dirname, './__fixtures__/asyncapi-websocket-query.yml');
Expand Down Expand Up @@ -45,4 +45,39 @@ describe('getClientName integration test with AsyncAPI', () => {
// Example assertion: Check if the name is formatted correctly
expect(clientName).toBe(customClientName);
});
});

describe('getInfo integration test with AsyncAPI', () => {
let parsedAsyncAPIDocument;

beforeAll(async () => {
const parseResult = await fromFile(parser, asyncapi_v3_path).parse();
parsedAsyncAPIDocument = parseResult.document;
});

it('should return the exact info object when exists', () => {
const expectedInfo = parsedAsyncAPIDocument.info();
const actualInfo = getInfo(parsedAsyncAPIDocument);
expect(actualInfo).toStrictEqual(expectedInfo);
});

it('should throw error when info method returns empty value', () => {
const invalidAsyncAPIDocument = { info: () => {} };
expect(() => getInfo(invalidAsyncAPIDocument)).toThrowError(
'AsyncAPI document info object cannot be an empty.'
);
});

it('should throw error when info method is missing', () => {
const invalidAsyncAPIDocument = {};
expect(() => getInfo(invalidAsyncAPIDocument)).toThrowError(
'Provided AsyncAPI document doesn\'t contain info.'
);
});

it('should throw error when AsyncAPI document is invalid', () => {
expect(() => {
getInfo(null);
}).toThrow('Provided AsyncAPI document is invaild.');
});
});
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { File } from '@asyncapi/generator-react-sdk';
import { getClientName, getServerUrl, getServer } from '@asyncapi/generator-helpers';
import { getClientName, getServerUrl, getServer, getInfo } from '@asyncapi/generator-helpers';
import { FileHeaderInfo } from '../components/FileHeaderInfo';
import { Requires } from '../components/Requires';
import { ClientClass } from '../components/ClientClass';

export default function ({ asyncapi, params }) {
const server = getServer(asyncapi.servers(), params.server);
const info = asyncapi.info();
const info = getInfo(asyncapi);
const title = info.title();
const clientName = getClientName(info, params.appendClientSuffix, params.customClientName);
const serverUrl = getServerUrl(server);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { File } from '@asyncapi/generator-react-sdk';
import { getClientName, getServerUrl, getServer } from '@asyncapi/generator-helpers';
import { getClientName, getServerUrl, getServer, getInfo } from '@asyncapi/generator-helpers';
import { FileHeaderInfo } from '../components/FileHeaderInfo';
import { Requires } from '../components/Requires';
import { ClientClass } from '../components/ClientClass';

export default function ({ asyncapi, params }) {
const server = getServer(asyncapi.servers(), params.server);
const info = asyncapi.info();
const info = getInfo(asyncapi);
const title = info.title();
const clientName = getClientName(info, params.appendClientSuffix, params.customClientName);
const serverUrl = getServerUrl(server);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { File } from '@asyncapi/generator-react-sdk';
import { getClientName, getServerUrl, getServer, getQueryParams } from '@asyncapi/generator-helpers';
import { getClientName, getServerUrl, getServer, getQueryParams, getInfo } from '@asyncapi/generator-helpers';
import { FileHeaderInfo } from '../components/FileHeaderInfo';
import { Requires } from '../components/Requires';
import { ClientClass } from '../components/ClientClass';

export default function ({ asyncapi, params }) {
const server = getServer(asyncapi.servers(), params.server);
const info = asyncapi.info();
const info = getInfo(asyncapi);
const title = info.title();
const queryParams = getQueryParams(asyncapi.channels());
const clientName = getClientName(info, params.appendClientSuffix, params.customClientName);
Expand Down