Skip to content

Commit

Permalink
add components except Message and schema
Browse files Browse the repository at this point in the history
Signed-off-by: nikhilkalburgi <[email protected]>
  • Loading branch information
nikhilkalburgi committed Mar 6, 2024
1 parent be4795e commit 2d64859
Show file tree
Hide file tree
Showing 7 changed files with 380 additions and 13 deletions.
132 changes: 130 additions & 2 deletions src/Asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ import * as vscode from 'vscode';
import * as ejs from 'ejs';
import * as path from 'path';
import * as Markdownit from 'markdown-it';
import { Server } from 'http';

const md = Markdownit('commonmark');


const extRenderType = 'x-schema-private-render-type';
const extRenderAdditionalInfo = 'x-schema-private-render-additional-info';
const extRawValue = 'x-schema-private-raw-value';
const extParameterLocation = 'x-schema-private-parameter-location';
const jsonSchemaTypes: string[] = [
'string',
'number',
Expand All @@ -21,6 +23,30 @@ const jsonSchemaTypes: string[] = [
];

class SchemaHelper {
static parametersToSchema(parameters: any[]) {
if (parameters.length === 0) {
return;
}

const json:object = {
type: 'object',
properties: parameters.reduce(
(obj, parameter) => {
const parameterName = parameter.id();
obj[String(parameterName)] = Object.assign({}, parameter.schema() === undefined ? {type: 'string'} : parameter.schema().json());
obj[String(parameterName)].description =
parameter.description() || obj[String(parameterName)].description;
obj[String(parameterName)][extParameterLocation] = parameter.location();
return obj;
},
{},
),
required: parameters.map(parameter => parameter.id()),
[extRenderType]: false,
[extRenderAdditionalInfo]: false,
};
return new SchemaModel(json);
}
static jsonFieldToSchema(value: any): object {
if (value === undefined || value === null) {
return {
Expand Down Expand Up @@ -102,6 +128,96 @@ class SchemaHelper {
}
}

class ServerHelper {
static securityType(value: string) {
switch (value) {
case 'apiKey':
return 'API key';
case 'oauth2':
return 'OAuth2';
case 'openIdConnect':
return 'Open ID';
case 'http':
return 'HTTP';
case 'userPassword':
return 'User/Password';
case 'X509':
return 'X509';
case 'symmetricEncryption':
return 'Symmetric Encription';
case 'asymmetricEncryption':
return 'Asymmetric Encription';
case 'httpApiKey':
return 'HTTP API key';
case 'scramSha256':
return 'ScramSha256';
case 'scramSha512':
return 'ScramSha512';
case 'gssapi':
return 'GSSAPI';
case 'plain':
return 'PLAIN';
default:
return 'API key';
}
}

static flowName(value: string) {
switch (value) {
case 'implicit':
return 'Implicit';
case 'password':
return 'Password';
case 'clientCredentials':
return 'Client credentials';
case 'authorizationCode':
return 'Authorization Code';
default:
return 'Implicit';
}
}

static getKafkaSecurity(protocol: string, securitySchema: { type: () => any; }) {
let securityProtocol;
let saslMechanism;
if (protocol === 'kafka') {
if (securitySchema) {
securityProtocol = 'SASL_PLAINTEXT';
} else {
securityProtocol = 'PLAINTEXT';
}
} else if (securitySchema) {
securityProtocol = 'SASL_SSL';
} else {
securityProtocol = 'SSL';
}
if (securitySchema) {
switch (securitySchema.type()) {
case 'plain':
saslMechanism = 'PLAIN';
break;
case 'scramSha256':
saslMechanism = 'SCRAM-SHA-256';
break;
case 'scramSha512':
saslMechanism = 'SCRAM-SHA-512';
break;
case 'oauth2':
saslMechanism = 'OAUTHBEARER';
break;
case 'gssapi':
saslMechanism = 'GSSAPI';
break;
case 'X509':
securityProtocol = 'SSL';
break;
}
}

return { securityProtocol, saslMechanism };
}
}



export default async function info(asyncapi:AsyncAPIDocumentInterface, context: vscode.ExtensionContext) {
Expand All @@ -128,7 +244,17 @@ export default async function info(asyncapi:AsyncAPIDocumentInterface, context:
},
servers:{
servers: asyncapi.servers(),
schemaHelper: SchemaHelper
schemaHelper: SchemaHelper,
serverHelper: ServerHelper,
md
},
operations:{
channels: asyncapi.channels(),
isV3: asyncapi.version().split('.')[0] === '3',
schemaHelper: SchemaHelper,
serverHelper: ServerHelper,
allServersLength: asyncapi.servers().all().length,
md
},
path:{
infoPath: path.join(context.extensionPath,'dist', 'components','Info.ejs'),
Expand All @@ -137,7 +263,9 @@ export default async function info(asyncapi:AsyncAPIDocumentInterface, context:
securityPath: path.join(context.extensionPath,'dist', 'components','Security.ejs'),
bindingsPath: path.join(context.extensionPath,'dist', 'components','Bindings.ejs'),
extensionsPath: path.join(context.extensionPath,'dist', 'components','Extensions.ejs'),
schemaPath: path.join(context.extensionPath,'dist', 'components','Schema.ejs')
schemaPath: path.join(context.extensionPath,'dist', 'components','Schema.ejs'),
operationsPath: path.join(context.extensionPath,'dist', 'components','Operations.ejs'),
messagePath: path.join(context.extensionPath,'dist', 'components','Message.ejs')
}
});
}
3 changes: 2 additions & 1 deletion src/components/Asyncapi.ejs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
<%- include(path.infoPath,{...info,tagsPath: path.tagsPath}) %>
<%- include(path.serversPath,{...servers, ...path}) %>
<%- include(path.serversPath,{...servers, ...path}) %>
<%- include(path.operationsPath,{...operations, ...path}) %>
2 changes: 1 addition & 1 deletion src/components/Bindings.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if (!bindings.isEmpty()) { %>
<% for(let binding of bindings.all()){ %>
<%- include(SchemaPath,{ schemaName:`${binding.protocol().charAt(0).toUpperCase() + binding.protocol().slice(1)} ${name}`, schema: schemaHelper.jsonToSchema(binding), key: binding.protocol() }) %>
<%- include(schemaPath,{ schemaName:`${binding.protocol().charAt(0).toUpperCase() + binding.protocol().slice(1)} ${name}`, schema: schemaHelper.jsonToSchema(binding), key: binding.protocol() }) %>
<% } %>
<% } %>
4 changes: 1 addition & 3 deletions src/components/Extensions.ejs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@


<% if (Object.keys(schemaHelper.getCustomExtensions(extensions) || {}).length !== 0) { %>
<%- include(SchemaPath,{ schemaName:name, schema: schemaHelper.jsonToSchema(extensions) }) %>
<%- include(schemaPath,{ schemaName:name, schema: schemaHelper.jsonToSchema(extensions) }) %>
<% } %>
125 changes: 125 additions & 0 deletions src/components/Operations.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<% if(!channels.isEmpty()) { %>
<h2>Operations</h2>
<% for(let channel of channels.all()) { %>
<% for(let operation of channel.operations().all()) { %>
<% if(operation && channel) { %>
<% let type;
const applyToAllServers = allServersLength === channel.servers().all().length;
const servers = applyToAllServers ? [] : channel.servers().all();
const showInfoList = operation.operationId() || (servers && servers.length);
if (operation.isSend()) {
if (operation.reply() !== undefined) {
type = 'request';
} else {
type = 'send';
}
} else if (operation.isReceive()) {
if (operation.reply() !== undefined) {
type = 'reply';
} else {
type = 'receive';
}
} %>
<%- md.render(`${getRenderedTypeForOperation({type})} \`${channel.address()}\` Operation`) %>
<% if(operation.summary()) { %>
<%- md.render(`*${operation.summary().trim()}*`) %>
<% } %>
<% if(showInfoList) { %>
<ul>
<% if(operation.operationId()) { %>
<li>Operation ID: <%= operation.operationId() %></li>
<% if(servers && servers.length) { %>
<li>Available Only on Server:
<%= servers.map(s => {
const serverId = s.id();
const slug = FormatHelpers.slugify(serverId);
return `[${serverId}](#${slug}-server)`;
}).join(', ') %>
</li>
<% } %>
<% } %>
</ul>
<% } %>
<% if(channel.hasDescription()) { %>
<%- md.render(channel.description()) %>
<% } %>
<% if(operation.hasDescription()) { %>
<%- md.render(operation.description()) %>
<% } %>
<% if(operation.externalDocs()) { %>
<a href={operation.externalDocs().url()}><%= (operation.externalDocs().description() || 'Find more info here.') %></a>
<% } %>
<%- include(tagsPath,{ name:"Operation tags", tags: operation.tags() }) %>
<% const parameters = schemaHelper.parametersToSchema(channel.parameters().all()); %>
<% if(parameters) { %>
<h4>Parameters</h4>
<%- include(schemaPath,{}) %>
<% } %>
<%- include(securityPath,{ header:'Additional security requirements', protocol: null, security: operation.security(), serverHelper, md }) %>
<%- include(bindingsPath,{ name:"Channel specific information", bindings: channel.bindings(), schemaHelper, schemaPath }) %>
<%- include(bindingsPath,{ name:"Operation specific information", bindings: operation.bindings(), schemaHelper, schemaPath }) %>
<%- include(extensionsPath,{ name:"Channel extensions", extensions: channel.extensions(), schemaHelper, schemaPath }) %>
<%- include(extensionsPath,{ name:"Operation extensions", extensions: operation.extensions(), schemaHelper, schemaPath }) %>
<% const messages = operation.messages().all(); %>
<% if (messages.length !== 0) { %>
<% const messageText = getOperationMessageText({type}); %>
<% if(messages.length > 1) { %>
<p><%= messageText %></p>
<% } %>
<% for(let message of messages) { %>
<%- include(messagePath,{message}) %>
<% } %>
<% } %>
<% } %>
<% } %>
<% } %>
<% } %>



<% function getRenderedTypeForOperation({type}) {
if (isV3) {
switch (type) {
case 'request':
return 'REQUEST';
case 'send':
return 'SEND';
case 'reply':
return 'REPLY';
case 'receive':
return 'RECEIVE';
}
}
switch (type) {
case 'send':
return 'SUB';
case 'receive':
return 'PUB';
}
return 'UNKNOWN';
}
function getOperationMessageText({type}) {
let messagesText = 'Accepts **one of** the following messages:';
if (isV3) {
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;
} %>
Loading

0 comments on commit 2d64859

Please sign in to comment.