Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 2 additions & 1 deletion 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, getInfo } = require('./utils');
const { getClientName, listFiles, getTitle,getInfo } = require('./utils');
const { getQueryParams } = require('./bindings');

module.exports = {
Expand All @@ -11,5 +11,6 @@ module.exports = {
getQueryParams,
getOperationMessages,
getMessageExamples,
getTitle,
getInfo
};
25 changes: 23 additions & 2 deletions packages/helpers/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
const { readdir } = require('fs/promises');

/**
* Validate and retrieve the AsyncAPI title parameter in the info object.
*
* Throws an error if the provided AsyncAPI info object lacks a `title` parameter.
*
* @param {object} info - The `info` object from an AsyncAPI document.
* @throws {Error} When `title` is `null` or `undefined` or `empty string` .
* @returns {string} The retrieved `title` parameter.
*/
const getTitle = info => {
if (!info.title) {
throw new Error('Provided AsyncAPI document info field doesn\'t contain title.');
}
const title = info.title();
if (title === '') {
throw new Error('AsyncAPI document title cannot be an empty string.');
}

return title;
};
/**
* Get client name from AsyncAPI info.title or uses a custom name if provided.
*
Expand All @@ -13,7 +33,7 @@ const getClientName = (info, appendClientSuffix, customClientName) => {
if (customClientName) {
return customClientName;
}
const title = info.title();
const title = getTitle(info);
const baseName = `${title.replace(/\s+/g, '') // Remove all spaces
.replace(/^./, char => char.toUpperCase())}`; // Make the first letter uppercase
return appendClientSuffix ? `${baseName}Client` : baseName;
Expand Down Expand Up @@ -60,6 +80,7 @@ const getInfo = (asyncapi) => {
module.exports = {
getClientName,
listFiles,
getInfo
getInfo,
getTitle
};

34 changes: 33 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, getInfo } = require('@asyncapi/generator-helpers');
const { getClientName, getTitle, 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 @@ -80,4 +80,36 @@ describe('getInfo integration test with AsyncAPI', () => {
getInfo(null);
}).toThrow('Make sure you pass AsyncAPI document as an argument.');
});
});
describe('getTitle 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 title parameter when exists', () => {
const info = parsedAsyncAPIDocument.info();
const expectedTitle = info.title();
const actualTitle = getTitle(parsedAsyncAPIDocument.info());
expect(actualTitle).toStrictEqual(expectedTitle);
});
it('should throw error when title function does not exist', () => {
const infoWithoutTitle = { /* missing title property */ };
expect(() => {
getTitle(infoWithoutTitle);
}).toThrow('Provided AsyncAPI document info field doesn\'t contain title.');
});

it('should throw error when title is an empty string', () => {
// Mock an info object where title() returns an empty string
const infoWithEmptyTitle = {
title: () => ''
};

expect(() => {
getTitle(infoWithEmptyTitle);
}).toThrow('AsyncAPI document title cannot be an empty string.');
});
});
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, getInfo } from '@asyncapi/generator-helpers';
import { getClientName, getServerUrl, getServer, getInfo, getTitle } from '@asyncapi/generator-helpers';

Check failure on line 2 in packages/templates/clients/websocket/dart/template/client.dart.js

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

'getTitle' is defined but never used

Check failure on line 2 in packages/templates/clients/websocket/dart/template/client.dart.js

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

'getInfo' is defined but never used
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 = getInfo(asyncapi);
const info = asyncapi.info();
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,13 +1,13 @@
import { File } from '@asyncapi/generator-react-sdk';
import { getClientName, getServerUrl, getServer, getInfo } from '@asyncapi/generator-helpers';
import { getClientName, getServerUrl, getServer, getInfo, getTitle } 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 = getInfo(asyncapi);
const title = info.title();
const title = getTitle(info);
const clientName = getClientName(info, params.appendClientSuffix, params.customClientName);
const serverUrl = getServerUrl(server);
const sendOperations = asyncapi.operations().filterBySend();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { File } from '@asyncapi/generator-react-sdk';
import { getClientName, getServerUrl, getServer, getQueryParams, getInfo } from '@asyncapi/generator-helpers';
import { getClientName, getServerUrl, getServer, getQueryParams, getInfo, getTitle } 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 = getInfo(asyncapi);
const title = info.title();
const title = getTitle(info);
const queryParams = getQueryParams(asyncapi.channels());
const clientName = getClientName(info, params.appendClientSuffix, params.customClientName);
const serverUrl = getServerUrl(server);
Expand Down
Loading