Skip to content

Commit

Permalink
feat(table of contents): use new CommonHelpers class for `Operation…
Browse files Browse the repository at this point in the history
…` types heading
  • Loading branch information
korifey91 committed Apr 9, 2024
1 parent 400b4d0 commit deaa2eb
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 43 deletions.
30 changes: 2 additions & 28 deletions components/Operations.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function Operations({ asyncapi }) {
const channelName = channel.address();
const operations = channel.operations().all();
operations.map(operation => {
const type = CommonHelpers.getOperationType(operation);
const type = CommonHelpers.getOperationType(operation, asyncapi);

operationsList.push(
<Operation
Expand All @@ -55,31 +55,6 @@ 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 @@ -93,14 +68,13 @@ 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();
const renderedType = getRenderedTypeForOperation({asyncapi, type});

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

return (
<Text>
<Header type={3}>
{`${renderedType} \`${channelName}\` Operation`}
{`${type.toUpperCase()} \`${channelName}\` Operation`}
</Header>

{operation.summary() && (
Expand Down
2 changes: 1 addition & 1 deletion components/TableOfContents.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function TableOfContents({asyncapi}) {
{operations.map((operation) => {
const channel = operation.channels().all()[0];
const channelAddress = channel?.address() ?? '';
const type = CommonHelpers.getOperationType(operation);
const type = CommonHelpers.getOperationType(operation, asyncapi);
return (
<Indent
size={2}
Expand Down
32 changes: 27 additions & 5 deletions helpers/common.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,37 @@
const OPERATION_TYPES = {
V3: {
REQUEST: 'request',
SEND: 'send',
REPLY: 'reply',
RECEIVE: 'receive',
},
V2: {
REQUEST: 'request',
SEND: 'publish',
REPLY: 'reply',
RECEIVE: 'subscribe',
}
};

const getOperationTypesByVersion = (version) => {
const [majorVersion] = version.split('.');

return OPERATION_TYPES[`V${majorVersion}`];
};

export class CommonHelpers {
static getOperationType(operation) {
static getOperationType(operation, asyncApiDoc) {
const operationsTypes = getOperationTypesByVersion(asyncApiDoc.version());

if (operation.isSend()) {
if (operation.reply() !== undefined) {
return 'request';
return operationsTypes.REQUEST;
}
return 'send';
return operationsTypes.SEND;
}
if (operation.isReceive() && operation.reply() !== undefined) {
return 'reply';
return operationsTypes.REPLY;
}
return 'receive';
return operationsTypes.RECEIVE;
}
}
18 changes: 9 additions & 9 deletions test/helpers/common.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,17 @@ beforeAll(async () => {
describe('CommonHelpers', () => {
describe('v2', () => {
describe('.getOperationType', () => {
test('should return "send" - in case send operation', () => {
test('should return "publish" - in case send operation', () => {
const sendOperation = v2Doc.operations().filterBySend()[0];

const result = CommonHelpers.getOperationType(sendOperation);
expect(result).toEqual('send');
const result = CommonHelpers.getOperationType(sendOperation, v2Doc);
expect(result).toEqual('publish');
});
test('should return "receive" - in case receive operation', () => {
test('should return "subscribe" - in case receive operation', () => {
const receiveOperation = v2Doc.operations().filterByReceive()[0];

const result = CommonHelpers.getOperationType(receiveOperation);
expect(result).toEqual('receive');
const result = CommonHelpers.getOperationType(receiveOperation, v2Doc);
expect(result).toEqual('subscribe');
});
});
});
Expand All @@ -42,23 +42,23 @@ describe('CommonHelpers', () => {
.filterBySend()
.find((operation) => operation.reply() === undefined);

const result = CommonHelpers.getOperationType(sendOperationWithoutReply);
const result = CommonHelpers.getOperationType(sendOperationWithoutReply, v3Doc);
expect(result).toEqual('send');
});
test('should return "reply" - in case receive with reply', () => {
const receiveOperationWithReply = v3Doc.operations()
.filterByReceive()
.find((operation) => operation.reply() !== undefined);

const result = CommonHelpers.getOperationType(receiveOperationWithReply);
const result = CommonHelpers.getOperationType(receiveOperationWithReply, v3Doc);
expect(result).toEqual('reply');
});
test('should return "receive" - in case receive without reply', () => {
const receiveOperationWithoutReply = v3Doc.operations()
.filterByReceive()
.find((operation) => operation.reply() === undefined);

const result = CommonHelpers.getOperationType(receiveOperationWithoutReply);
const result = CommonHelpers.getOperationType(receiveOperationWithoutReply, v3Doc);
expect(result).toEqual('receive');
});
});
Expand Down

0 comments on commit deaa2eb

Please sign in to comment.