From 2f7da394cf1fc14637ef9a289c5b220bdb6971ea Mon Sep 17 00:00:00 2001 From: Matatjahu Date: Fri, 25 Jun 2021 10:58:55 +0200 Subject: [PATCH] support 2.1.0 spec version --- components/Message.js | 21 ++++++--------- components/common.js | 9 +++++++ helpers/index.js | 1 + helpers/schema-examples.js | 53 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 71 insertions(+), 13 deletions(-) create mode 100644 helpers/index.js create mode 100644 helpers/schema-examples.js diff --git a/components/Message.js b/components/Message.js index 0c6fb2f86..dc0ad7d75 100644 --- a/components/Message.js +++ b/components/Message.js @@ -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' }) { @@ -45,16 +46,6 @@ export function Message({ message, title = 'Message' }) { ) } -function Tags({ tags = [] }) { - return ( - - {tags.map(tag => ( - {tag.name()} - ))} - - ); -} - function Example({ type = 'headers', message }) { if (type === 'headers') { const examples = getHeadersExamples(message); @@ -64,6 +55,8 @@ function Example({ type = 'headers', message }) {
Examples of headers
{examples.map(ex => ( + {ex.name && {ex.name}} + {ex.summary && {ex.summary}} {JSON.stringify(ex, null, 2)} ))} @@ -87,7 +80,9 @@ function Example({ type = 'headers', message }) {
Examples of payload
{examples.map(ex => ( - {JSON.stringify(ex, null, 2)} + {ex.name && {ex.name}} + {ex.summary && {ex.summary}} + {JSON.stringify(ex.example, null, 2)} ))} diff --git a/components/common.js b/components/common.js index 2d3509035..f4b34eed6 100644 --- a/components/common.js +++ b/components/common.js @@ -57,3 +57,12 @@ export function BlockQuote({ childrenContent = "" }) { return {`> ${childrenContent}`} } +export function Tags({ tags = [] }) { + return ( + + {tags.map(tag => ( + {tag.name()} + ))} + + ); +} diff --git a/helpers/index.js b/helpers/index.js new file mode 100644 index 000000000..1518fe59c --- /dev/null +++ b/helpers/index.js @@ -0,0 +1 @@ +export * from "./schema-examples"; \ No newline at end of file diff --git a/helpers/schema-examples.js b/helpers/schema-examples.js new file mode 100644 index 000000000..2fe9c4b2a --- /dev/null +++ b/helpers/schema-examples.js @@ -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; +}