Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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('Make sure you pass AsyncAPI document as an argument.');
}
if (!asyncapi.info) {
throw new Error('Provided AsyncAPI document doesn\'t contain Info object.');
}
const info = asyncapi.info();
if (!info) {
throw new Error('AsyncAPI document info object cannot be 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 empty.'
);
});

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

it('should throw error when AsyncAPI document is missing', () => {
expect(() => {
getInfo(null);
}).toThrow('Make sure you pass AsyncAPI document as an argument.');
});
});
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