Skip to content

Commit 3e73842

Browse files
authored
refactor: move FileHeaderInfo component to @asyncapi/generator-components (#1647)
Co-authored-by: Adi-204 <adiboghawala@gmail.com>
1 parent b2fe951 commit 3e73842

File tree

16 files changed

+246
-207
lines changed

16 files changed

+246
-207
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { Text } from '@asyncapi/generator-react-sdk';
2+
3+
/**
4+
* @typedef {'python' | 'javascript' | 'typescript' | 'java' | 'csharp' | 'rust' | 'dart'} Language
5+
* Supported programming languages.
6+
*/
7+
8+
/**
9+
* Mapping of supported programming languages to their respective comment syntax configurations.
10+
* @type {Record<Language, { commentChar: string, lineStyle: string }>}
11+
*/
12+
const commentConfig = {
13+
python: { commentChar: '#', lineStyle: '#'.repeat(50) },
14+
javascript: { commentChar: '//', lineStyle: '//'.repeat(25) },
15+
typescript: { commentChar: '//', lineStyle: '//'.repeat(25) },
16+
java: { commentChar: '//', lineStyle: '//'.repeat(25) },
17+
csharp: { commentChar: '//', lineStyle: '//'.repeat(25) },
18+
rust: { commentChar: '//', lineStyle: '//'.repeat(25) },
19+
dart: { commentChar: '///', lineStyle: '///' }
20+
};
21+
22+
/**
23+
* Renders a file header with metadata information such as title, version, protocol, host, and path.
24+
*
25+
* @param {Object} props - Component props.
26+
* @param {object} props.info - Info object from the AsyncAPI document.
27+
* @param {object} props.server - Server object from the AsyncAPI document.
28+
* @param {Language} props.language - Programming language used for comment formatting.
29+
* @returns {JSX.Element} Rendered file header.
30+
*/
31+
export function FileHeaderInfo({ info, server, language }) {
32+
const { commentChar, lineStyle } = commentConfig[language] || {
33+
commentChar: '//',
34+
lineStyle: '//'.repeat(25)
35+
};
36+
37+
return (
38+
<Text>
39+
<Text>{lineStyle}</Text>
40+
41+
<Text>{commentChar}</Text>
42+
43+
<Text>
44+
{commentChar} {info.title()} - {info.version()}
45+
</Text>
46+
47+
<Text>
48+
{commentChar} Protocol: {server.protocol()}
49+
</Text>
50+
51+
<Text>
52+
{commentChar} Host: {server.host()}
53+
</Text>
54+
55+
{server.hasPathname() && (
56+
<Text>
57+
{commentChar} Path: {server.pathname()}
58+
</Text>
59+
)}
60+
61+
<Text>{commentChar}</Text>
62+
63+
<Text>{lineStyle}</Text>
64+
</Text>
65+
);
66+
}
File renamed without changes.

packages/components/src/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export { Models } from './components/models';
1+
export { Models } from './components/Models';
2+
export { FileHeaderInfo } from './components/FileHeaderInfo';
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import path from 'path';
2+
import { render } from '@asyncapi/generator-react-sdk';
3+
import { Parser, fromFile } from '@asyncapi/parser';
4+
import { FileHeaderInfo } from '../../src/index';
5+
6+
const parser = new Parser();
7+
const asyncapi_websocket_query = path.resolve(__dirname, '../../../helpers/test/__fixtures__/asyncapi-websocket-query.yml');
8+
9+
describe('Testing of FileHeaderInfo function', () => {
10+
let parsedAsyncAPIDocument;
11+
12+
beforeAll(async () => {
13+
const parseResult = await fromFile(parser, asyncapi_websocket_query).parse();
14+
parsedAsyncAPIDocument = parseResult.document;
15+
});
16+
17+
test('render websockets file header info with pathname correctly', () => {
18+
const result = render(
19+
<FileHeaderInfo
20+
info={parsedAsyncAPIDocument.info()}
21+
server={parsedAsyncAPIDocument.servers().get('withPathname')}
22+
language="javascript"
23+
/>
24+
);
25+
const actual = result.trim();
26+
expect(actual).toMatchSnapshot();
27+
});
28+
29+
test('render websockets file header info without pathname correctly', () => {
30+
const result = render(
31+
<FileHeaderInfo
32+
info={parsedAsyncAPIDocument.info()}
33+
server={parsedAsyncAPIDocument.servers().get('withoutPathName')}
34+
language="javascript"
35+
/>
36+
);
37+
const actual = result.trim();
38+
expect(actual).toMatchSnapshot();
39+
});
40+
41+
test('render websockets file header info with pathname correctly (Python)', () => {
42+
const result = render(
43+
<FileHeaderInfo
44+
info={parsedAsyncAPIDocument.info()}
45+
server={parsedAsyncAPIDocument.servers().get('withPathname')}
46+
language="python"
47+
/>
48+
);
49+
const actual = result.trim();
50+
expect(actual).toMatchSnapshot();
51+
});
52+
53+
test('render websockets file header info without pathname correctly (Python)', () => {
54+
const result = render(
55+
<FileHeaderInfo
56+
info={parsedAsyncAPIDocument.info()}
57+
server={parsedAsyncAPIDocument.servers().get('withoutPathName')}
58+
language="python"
59+
/>
60+
);
61+
const actual = result.trim();
62+
expect(actual).toMatchSnapshot();
63+
});
64+
65+
test('render websockets file header info with pathname correctly (Dart)', () => {
66+
const result = render(
67+
<FileHeaderInfo
68+
info={parsedAsyncAPIDocument.info()}
69+
server={parsedAsyncAPIDocument.servers().get('withPathname')}
70+
language="dart"
71+
/>
72+
);
73+
const actual = result.trim();
74+
expect(actual).toMatchSnapshot();
75+
});
76+
77+
test('render websockets file header info without pathname correctly (Dart)', () => {
78+
const result = render(
79+
<FileHeaderInfo
80+
info={parsedAsyncAPIDocument.info()}
81+
server={parsedAsyncAPIDocument.servers().get('withoutPathName')}
82+
language="dart"
83+
/>
84+
);
85+
const actual = result.trim();
86+
expect(actual).toMatchSnapshot();
87+
});
88+
});
File renamed without changes.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`Testing of FileHeaderInfo function render websockets file header info with pathname correctly (Dart) 1`] = `
4+
"///
5+
///
6+
/// Gemini Market Data Websocket API - 1.0.0
7+
/// Protocol: wss
8+
/// Host: api.gemini.com
9+
/// Path: /v1/marketdata
10+
///
11+
///"
12+
`;
13+
14+
exports[`Testing of FileHeaderInfo function render websockets file header info with pathname correctly (Python) 1`] = `
15+
"##################################################
16+
#
17+
# Gemini Market Data Websocket API - 1.0.0
18+
# Protocol: wss
19+
# Host: api.gemini.com
20+
# Path: /v1/marketdata
21+
#
22+
##################################################"
23+
`;
24+
25+
exports[`Testing of FileHeaderInfo function render websockets file header info with pathname correctly 1`] = `
26+
"//////////////////////////////////////////////////
27+
//
28+
// Gemini Market Data Websocket API - 1.0.0
29+
// Protocol: wss
30+
// Host: api.gemini.com
31+
// Path: /v1/marketdata
32+
//
33+
//////////////////////////////////////////////////"
34+
`;
35+
36+
exports[`Testing of FileHeaderInfo function render websockets file header info without pathname correctly (Dart) 1`] = `
37+
"///
38+
///
39+
/// Gemini Market Data Websocket API - 1.0.0
40+
/// Protocol: wss
41+
/// Host: api.gemini.com
42+
///
43+
///"
44+
`;
45+
46+
exports[`Testing of FileHeaderInfo function render websockets file header info without pathname correctly (Python) 1`] = `
47+
"##################################################
48+
#
49+
# Gemini Market Data Websocket API - 1.0.0
50+
# Protocol: wss
51+
# Host: api.gemini.com
52+
#
53+
##################################################"
54+
`;
55+
56+
exports[`Testing of FileHeaderInfo function render websockets file header info without pathname correctly 1`] = `
57+
"//////////////////////////////////////////////////
58+
//
59+
// Gemini Market Data Websocket API - 1.0.0
60+
// Protocol: wss
61+
// Host: api.gemini.com
62+
//
63+
//////////////////////////////////////////////////"
64+
`;

packages/components/test/components/__snapshots__/models.test.js.snap renamed to packages/components/test/components/__snapshots__/Models.test.js.snap

File renamed without changes.

packages/templates/clients/websocket/dart/components/FileHeaderInfo.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

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

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

@@ -16,6 +16,7 @@ export default function ({ asyncapi, params }) {
1616
<FileHeaderInfo
1717
info={info}
1818
server={server}
19+
language="dart"
1920
/>
2021
<Requires />
2122
<ClientClass clientName={clientName} serverUrl={serverUrl} title={title} />

packages/templates/clients/websocket/javascript/components/FileHeaderInfo.js

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)