Skip to content

Commit

Permalink
refactor operations
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Oct 6, 2023
1 parent 5f66a73 commit d77b5c6
Show file tree
Hide file tree
Showing 9 changed files with 1,251 additions and 736 deletions.
8 changes: 1 addition & 7 deletions components/Asyncapi.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,19 @@ import { TableOfContents } from './TableOfContents';

// eslint-disable-next-line no-unused-vars
import { AsyncAPIDocumentInterface } from '@asyncapi/parser';
import { Operationsv3 } from './Operationsv3';

/**
* @param {{asyncapi: AsyncAPIDocumentInterface, params: any}} param0
*/
export function Asyncapi({ asyncapi, params }) {
const isV3 = asyncapi.version().split('.')[0] === '3';
return (
<>
{params.frontMatter && <FrontMatter asyncapi={asyncapi} params={params} />}
<Info asyncapi={asyncapi} params={params} />
{params.toc !== 'false' && <TableOfContents asyncapi={asyncapi} />}

<Servers asyncapi={asyncapi} />
{
isV3 ?
<Operationsv3 asyncapi={asyncapi} /> :
<Operations asyncapi={asyncapi} />
}
<Operations asyncapi={asyncapi} />
</>
);
}
Expand Down
152 changes: 135 additions & 17 deletions components/Operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ import { FormatHelpers } from '../helpers/format';
// eslint-disable-next-line no-unused-vars
import { AsyncAPIDocumentInterface, OperationInterface, ChannelInterface } from '@asyncapi/parser';

function isV3({asyncapi}) {
return asyncapi.version().split('.')[0] === '3';
}

/**
* @param {{asyncapi: AsyncAPIDocumentInterface}} param0
*/
Expand All @@ -31,13 +35,13 @@ export function Operations({ asyncapi }) {
if (operation.reply() !== undefined) {
type = 'request';
} else {
type = 'publish';
type = 'send';
}
} else if (operation.isReceive()) {
if (operation.reply() !== undefined) {
type = 'reply';
} else {
type = 'subscribe';
type = 'receive';
}
}
operationsList.push(
Expand All @@ -62,7 +66,31 @@ export function Operations({ asyncapi }) {
</>
);
}

function getRenderedTypeForOperation({asyncapi, type}) {
const isv3 = isV3({asyncapi});
if (isv3) {
switch (type) {
case 'request':
return 'REQUEST';
case 'send':
return 'SEND';
case 'reply':
return 'REPLY';
case 'receive':
return 'RECEIVE';
}
}
// For v2, we render the application view still
// Meaning the when you use publish operation it means other publish to your application because your application is subscribing to it.
switch (type) {
case 'send': // This is the publish operation
return 'SUB';
case 'receive': // This is the subscribe operation
return 'PUB';
}
// This case should never happen, if it does this function needs to be changed
return 'UNKNOWN';
}
/**
* @param {{asyncapi: AsyncAPIDocumentInterface, type: string, operation: OperationInterface, channelName: string, channel: ChannelInterface}} param0
*/
Expand All @@ -76,15 +104,8 @@ function Operation({ asyncapi, type, operation, channelName, channel }) { // NOS
const applyToAllServers = asyncapi.servers().all().length === channel.servers().all().length;
const servers = applyToAllServers ? [] : channel.servers().all();
const security = operation.security();
let renderedType;
switch (type) {
case 'publish':
renderedType = 'PUB';
break;
case 'subscribe':
renderedType = 'SUB';
break;
}
const renderedType = getRenderedTypeForOperation({asyncapi, type});

const showInfoList = operationId || (servers && servers.length);

return (
Expand All @@ -95,7 +116,7 @@ function Operation({ asyncapi, type, operation, channelName, channel }) { // NOS

{operation.summary() && (
<Text newLines={2}>
*{operation.summary()}*
*{operation.summary().trim()}*
</Text>
)}

Expand Down Expand Up @@ -154,10 +175,13 @@ function Operation({ asyncapi, type, operation, channelName, channel }) { // NOS
<Extensions name="Channel extensions" item={channel} />
<Extensions name="Operation extensions" item={operation} />

<OperationMessages operation={operation} />
<OperationMessages operation={operation} asyncapi={asyncapi} type={type} />

<OperationReply operation={operation} />
</Text>
);
}

/**
* @param {{channel: ChannelInterface}} param0
*/
Expand All @@ -174,20 +198,36 @@ function OperationParameters({ channel }) {
</Text>
);
}
function getOperationMessageText({asyncapi, type}) {
let messagesText = 'Accepts **one of** the following messages:';
if (isV3({asyncapi})) {
if (type === 'send') {
messagesText = 'Sending **one of** the following messages:';
} else if (type === 'request') {
messagesText = 'Request contains **one of** the following messages:';
} else if (type === 'receive') {
messagesText = 'Receive **one of** the following messages:';
} else if (type === 'reply') {
messagesText = 'Request contains **one of** the following messages:';
}
}
return messagesText;
}
/**
* @param {{operation: OperationInterface}} param0
* @param {{operation: OperationInterface, asyncapi: AsyncAPIDocumentInterface, type: string}} param0
*/
function OperationMessages({ operation }) {
function OperationMessages({ asyncapi, operation, type }) {
const messages = operation.messages().all();
if (messages.length === 0) {
return null;
}
const messageText = getOperationMessageText({asyncapi, type});

return (
<>
{messages.length > 1 && (
<Text newLines={2}>
Accepts **one of** the following messages:
{messageText}
</Text>
)}
{messages.map((msg, idx) => (
Expand All @@ -196,3 +236,81 @@ function OperationMessages({ operation }) {
</>
);
}

/**
* @param {{operation: OperationInterface}} param0
*/
function OperationReply({ operation, type }) {
const reply = operation.reply();
if (reply === undefined) {
return null;
}
const explicitChannel = reply.channel();

let typeText;
if (operation.isSend()) {
typeText = 'Request';
} else if (operation.isReceive()) {
typeText = 'Response';
}

let messagesText;
if (type === 'request') {
messagesText = 'Receive **one of** the following messages as a response to the request:';
} else if (type === 'reply') {
messagesText = 'Replying with **one of** the following messages:';
}

return (
<Text>
<Header type={4}>
{`${typeText} information`}
</Header>

{explicitChannel && <ListItem>{type} should be done to channel: `{explicitChannel.address()}`</ListItem>}

<OperationReplyAddress name="Operation reply address" reply={reply} />

<>
{reply.messages().length > 1 && (
<Text newLines={2}>
{messagesText}
</Text>
)}
{reply.messages().length > 1 && reply.messages().map((msg, idx) => (
<Message message={msg} key={`message-${idx}`} />
))}
</>
<Extensions name="Operation reply extensions" item={reply} />
</Text>
);
}

/**
* @param {{reply: OperationReplyInterface}} param0
*/
function OperationReplyAddress({ reply }) {
const address = reply.address();
if (address === undefined) {
return null;
}
const location = address.location();

return (
<Text>
<Header type={4}>
{'Operation reply address information'}
</Header>

{address.hasDescription() && (
<Text newLines={2}>
{address.description()}
</Text>
)}

<ListItem>Operation reply address location: `{location}`</ListItem>

<Extensions name="Operation reply address extensions" item={address} />
</Text>
);
}
Loading

0 comments on commit d77b5c6

Please sign in to comment.