Skip to content

Commit 0174c71

Browse files
Adi-204derberg
andauthored
chore: add new helper function getInfo() (#1567)
Co-authored-by: Adi-204 <adiboghawala@gmail.com> Co-authored-by: Lukasz Gornicki <lpgornicki@gmail.com>
1 parent 21dbdb5 commit 0174c71

File tree

6 files changed

+69
-10
lines changed

6 files changed

+69
-10
lines changed

packages/helpers/src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { getMessageExamples, getOperationMessages } = require('./operations');
22
const { getServerUrl, getServer } = require('./servers');
3-
const { getClientName, listFiles } = require('./utils');
3+
const { getClientName, listFiles, getInfo } = require('./utils');
44
const { getQueryParams } = require('./bindings');
55

66
module.exports = {
@@ -10,5 +10,6 @@ module.exports = {
1010
listFiles,
1111
getQueryParams,
1212
getOperationMessages,
13-
getMessageExamples
13+
getMessageExamples,
14+
getInfo
1415
};

packages/helpers/src/utils.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,31 @@ const listFiles = async (dir) => {
3535
.map(dirE => dirE.name);
3636
};
3737

38+
/**
39+
* Validate and retrieve the AsyncAPI info object from an AsyncAPI document.
40+
*
41+
* Throws an error if the provided AsyncAPI document has no `info` section.
42+
*
43+
* @param {object} asyncapi - The AsyncAPI document object.
44+
* @returns {object} The validated info object from the AsyncAPI document.
45+
*/
46+
const getInfo = (asyncapi) => {
47+
if (!asyncapi) {
48+
throw new Error('Make sure you pass AsyncAPI document as an argument.');
49+
}
50+
if (!asyncapi.info) {
51+
throw new Error('Provided AsyncAPI document doesn\'t contain Info object.');
52+
}
53+
const info = asyncapi.info();
54+
if (!info) {
55+
throw new Error('AsyncAPI document info object cannot be empty.');
56+
}
57+
return info;
58+
};
59+
3860
module.exports = {
3961
getClientName,
40-
listFiles
62+
listFiles,
63+
getInfo
4164
};
4265

packages/helpers/test/utils.test.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const path = require('path');
22
const { Parser, fromFile } = require('@asyncapi/parser');
3-
const { getClientName } = require('@asyncapi/generator-helpers');
3+
const { getClientName, getInfo } = require('@asyncapi/generator-helpers');
44

55
const parser = new Parser();
66
const asyncapi_v3_path = path.resolve(__dirname, './__fixtures__/asyncapi-websocket-query.yml');
@@ -45,4 +45,39 @@ describe('getClientName integration test with AsyncAPI', () => {
4545
// Example assertion: Check if the name is formatted correctly
4646
expect(clientName).toBe(customClientName);
4747
});
48+
});
49+
50+
describe('getInfo integration test with AsyncAPI', () => {
51+
let parsedAsyncAPIDocument;
52+
53+
beforeAll(async () => {
54+
const parseResult = await fromFile(parser, asyncapi_v3_path).parse();
55+
parsedAsyncAPIDocument = parseResult.document;
56+
});
57+
58+
it('should return the exact info object when exists', () => {
59+
const expectedInfo = parsedAsyncAPIDocument.info();
60+
const actualInfo = getInfo(parsedAsyncAPIDocument);
61+
expect(actualInfo).toStrictEqual(expectedInfo);
62+
});
63+
64+
it('should throw error when info method returns empty value', () => {
65+
const invalidAsyncAPIDocument = { info: () => {} };
66+
expect(() => getInfo(invalidAsyncAPIDocument)).toThrowError(
67+
'AsyncAPI document info object cannot be empty.'
68+
);
69+
});
70+
71+
it('should throw error when info method is missing', () => {
72+
const invalidAsyncAPIDocument = {};
73+
expect(() => getInfo(invalidAsyncAPIDocument)).toThrowError(
74+
'Provided AsyncAPI document doesn\'t contain Info object.'
75+
);
76+
});
77+
78+
it('should throw error when AsyncAPI document is missing', () => {
79+
expect(() => {
80+
getInfo(null);
81+
}).toThrow('Make sure you pass AsyncAPI document as an argument.');
82+
});
4883
});

packages/templates/clients/websocket/dart/template/client.dart.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { File } from '@asyncapi/generator-react-sdk';
2-
import { getClientName, getServerUrl, getServer } from '@asyncapi/generator-helpers';
2+
import { getClientName, getServerUrl, getServer, getInfo } from '@asyncapi/generator-helpers';
33
import { FileHeaderInfo } from '../components/FileHeaderInfo';
44
import { Requires } from '../components/Requires';
55
import { ClientClass } from '../components/ClientClass';
66

77
export default function ({ asyncapi, params }) {
88
const server = getServer(asyncapi.servers(), params.server);
9-
const info = asyncapi.info();
9+
const info = getInfo(asyncapi);
1010
const title = info.title();
1111
const clientName = getClientName(info, params.appendClientSuffix, params.customClientName);
1212
const serverUrl = getServerUrl(server);

packages/templates/clients/websocket/javascript/template/client.js.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { File } from '@asyncapi/generator-react-sdk';
2-
import { getClientName, getServerUrl, getServer } from '@asyncapi/generator-helpers';
2+
import { getClientName, getServerUrl, getServer, getInfo } from '@asyncapi/generator-helpers';
33
import { FileHeaderInfo } from '../components/FileHeaderInfo';
44
import { Requires } from '../components/Requires';
55
import { ClientClass } from '../components/ClientClass';
66

77
export default function ({ asyncapi, params }) {
88
const server = getServer(asyncapi.servers(), params.server);
9-
const info = asyncapi.info();
9+
const info = getInfo(asyncapi);
1010
const title = info.title();
1111
const clientName = getClientName(info, params.appendClientSuffix, params.customClientName);
1212
const serverUrl = getServerUrl(server);

packages/templates/clients/websocket/python/template/client.py.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { File } from '@asyncapi/generator-react-sdk';
2-
import { getClientName, getServerUrl, getServer, getQueryParams } from '@asyncapi/generator-helpers';
2+
import { getClientName, getServerUrl, getServer, getQueryParams, getInfo } from '@asyncapi/generator-helpers';
33
import { FileHeaderInfo } from '../components/FileHeaderInfo';
44
import { Requires } from '../components/Requires';
55
import { ClientClass } from '../components/ClientClass';
66

77
export default function ({ asyncapi, params }) {
88
const server = getServer(asyncapi.servers(), params.server);
9-
const info = asyncapi.info();
9+
const info = getInfo(asyncapi);
1010
const title = info.title();
1111
const queryParams = getQueryParams(asyncapi.channels());
1212
const clientName = getClientName(info, params.appendClientSuffix, params.customClientName);

0 commit comments

Comments
 (0)