generated from asyncapi/template-for-generator-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
101 lines (92 loc) · 3.53 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import { File, render } from '@asyncapi/generator-react-sdk';
// Import custom components from file
import { HTML, Head, Body } from '../components/common';
import { ListChannels } from '../components/ListChannels';
import { DiagramContent } from '../components/DiagramContent';
/*
* Each template to be rendered must have as a root component a File component,
* otherwise it will be skipped.
*
* If you don't want to render anything, you can return `null` or `undefined` and then Generator will skip the given template.
*
* Below you can see how reusable chunks (components) could be called.
* Just write a new component (or import it) and place it inside the File or another component.
*
* Notice that you can pass parameters to components. In fact, underneath, each component is a pure Javascript function.
*/
export default function({ asyncapi, params }) {
if (!asyncapi.hasComponents()) {
return null;
}
const cssLinks = [
'https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css',
'style.css',
];
// Notice that root component is the `File` component.
return (
<File name="index.html">
<HTML>
<Head
title={asyncapi.info().title()}
cssLinks={cssLinks}
/>
<Body>
<BodyContent asyncapi={asyncapi} />
<Scripts params={params} />
</Body>
</HTML>
</File>
);
}
/*
* Below you can see how reusable chunks could be called in another way.
* If you need process the React component to string you should use `render` function from `@asyncapi/generator-react-sdk` package.
* This function transforms given component (and its children) and returns pure string.
*
* Notice also how to retrieve passed properties to custom component, by the destruction of the first argument.
* Accessing document data is made easier thanks to what AsyncAPI JavaScript Parser is doing to the AsyncAPI document.
*/
function BodyContent({ asyncapi }) {
const apiName = asyncapi.info().title();
const channels = asyncapi.channels();
return `
<div class="container mx-auto px-4">
<p>
<h1>${apiName}</h1>
${render(<ListChannels channels={channels} operationType='subscribe' />)}
${render(<ListChannels channels={channels} operationType='publish' />)}
${render(<DiagramContent asyncapi={asyncapi} />)}
${render(<Extension asyncapi={asyncapi} />)}
${render(<ExternalDocs asyncapi={asyncapi} />)}
</p>
</div>
`;
}
/*
* This is an example how you can access values from AsyncAPI file from its extensions
*/
function Extension({ asyncapi }) {
if (!asyncapi.info().hasExt('x-twitter')) return null;
return `Share your feedback with us on <a href="http://twitter.com/${asyncapi.info().ext('x-twitter')}">Twitter</a>.`;
}
function ExternalDocs({ asyncapi }) {
if (!asyncapi.hasExternalDocs()) return null;
const url = asyncapi.externalDocs().url();
return `Don't forget to visit our website <a href="${url}">${url}</a>.`;
}
/*
* You can access "maxTextSide" parameter value without any conditions in case user didn't provide such a parameter.
* It is possible thanks to the functionality that makes it possible for template developer to specify default values for parameters.
* Check out package.json file and look for `generator.parameters.maxTextSize` and its description and default value.
*/
function Scripts({ params }) {
return `
<script src="https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js"></script>
<script>
mermaid.initialize({
startOnLoad: true,
maxTextSize: ${params.maxTextSize},
});
</script>
`;
}