Skip to content

Commit

Permalink
support 2.1.0 spec version
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu committed Jun 25, 2021
1 parent 57fd113 commit 2f7da39
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 13 deletions.
21 changes: 8 additions & 13 deletions components/Message.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Text } from "@asyncapi/generator-react-sdk";
import { generateExample, getHeadersExamples, getPayloadExamples } from "@asyncapi/generator-filters";
import { generateExample } from "@asyncapi/generator-filters";

import { Header, ListItem, CodeBlock, BlockQuote } from "./common";
import { Header, CodeBlock, BlockQuote, Tags } from "./common";
import { getPayloadExamples, getHeadersExamples } from "./helpers";
import { Schema } from "./Schema";

export function Message({ message, title = 'Message' }) {
Expand Down Expand Up @@ -45,16 +46,6 @@ export function Message({ message, title = 'Message' }) {
)
}

function Tags({ tags = [] }) {
return (
<Text>
{tags.map(tag => (
<ListItem>{tag.name()}</ListItem>
))}
</Text>
);
}

function Example({ type = 'headers', message }) {
if (type === 'headers') {
const examples = getHeadersExamples(message);
Expand All @@ -64,6 +55,8 @@ function Example({ type = 'headers', message }) {
<BlockQuote>Examples of headers</BlockQuote>
{examples.map(ex => (
<Text newLines={2}>
{ex.name && <Text>{ex.name}</Text>}
{ex.summary && <Text>{ex.summary}</Text>}
<CodeBlock language='json'>{JSON.stringify(ex, null, 2)}</CodeBlock>
</Text>
))}
Expand All @@ -87,7 +80,9 @@ function Example({ type = 'headers', message }) {
<BlockQuote>Examples of payload</BlockQuote>
{examples.map(ex => (
<Text newLines={2}>
<CodeBlock language='json'>{JSON.stringify(ex, null, 2)}</CodeBlock>
{ex.name && <Text>{ex.name}</Text>}
{ex.summary && <Text>{ex.summary}</Text>}
<CodeBlock language='json'>{JSON.stringify(ex.example, null, 2)}</CodeBlock>
</Text>
))}
</>
Expand Down
9 changes: 9 additions & 0 deletions components/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,12 @@ export function BlockQuote({ childrenContent = "" }) {
return <Text newLines={2}>{`> ${childrenContent}`}</Text>
}

export function Tags({ tags = [] }) {
return (
<Text>
{tags.map(tag => (
<ListItem>{tag.name()}</ListItem>
))}
</Text>
);
}
1 change: 1 addition & 0 deletions helpers/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./schema-examples";
53 changes: 53 additions & 0 deletions helpers/schema-examples.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
export function getPayloadExamples(msg) {
const examples = msg.examples();
if (Array.isArray(examples) && examples.some(e => e.payload)) {
const messageExamples = examples
.flatMap(e => {
if (!e.payload) return;
return {
name: e.name,
summary: e.summary,
example: e.payload,
};
})
.filter(Boolean);

if (messageExamples.length > 0) {
return messageExamples;
}
}

const payload = msg.payload();
if (payload && payload.examples()) {
return payload.examples().map(example => ({ example }));
}

return;
}

export function getHeadersExamples(msg) {
const examples = msg.examples();
if (Array.isArray(examples) && examples.some(e => e.headers)) {
const messageExamples = examples
.flatMap(e => {
if (!e.headers) return;
return {
name: e.name,
summary: e.summary,
example: e.headers,
};
})
.filter(Boolean);

if (messageExamples.length > 0) {
return messageExamples;
}
}

const headers = msg.headers();
if (headers && headers.examples()) {
return headers.examples().map(example => ({ example }));
}

return;
}

0 comments on commit 2f7da39

Please sign in to comment.