From 78f3dfd7e0c9f4054abd763c76cfda7dae163065 Mon Sep 17 00:00:00 2001 From: Prince Rajpoot Date: Sun, 11 Jun 2023 17:45:31 +0530 Subject: [PATCH 01/19] Initial Draft for Help Endpoint --- openapi.yaml | 46 ++++++++++++++++++++++++++++++ src/controllers/help.controller.ts | 46 ++++++++++++++++++++++++++++++ src/server.ts | 2 ++ 3 files changed, 94 insertions(+) create mode 100644 src/controllers/help.controller.ts diff --git a/openapi.yaml b/openapi.yaml index 176431dd..19b6c8be 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -226,6 +226,52 @@ paths: schema: $ref: '#/components/schemas/Problem' + /help: + get: + summary: Retrieve help information for the given command. + operationId: help + tags: + - help + requestBody: + description: Command for which help information is needed. + required: true + content: + application/json: + schema: + type: object + properties: + command: + type: string + example: "command1.subcommand1" + responses: + "200": + description: Help information retrieved successfully. + content: + application/json: + schema: + type: object + properties: + message: + type: string + "400": + description: Failed to get help. The given AsyncAPI command is not valid.. + content: + application/json: + schema: + $ref: "#/components/schemas/Problem" + "422": + description: The given AsyncAPI document(s) is/are not valid due to invalid parameters in the request. + content: + application/json: + schema: + $ref: "#/components/schemas/Problem" + default: + description: Unexpected problem. + content: + application/json: + schema: + $ref: "#/components/schemas/Problem" + /diff: post: summary: Compare the given AsyncAPI documents. diff --git a/src/controllers/help.controller.ts b/src/controllers/help.controller.ts new file mode 100644 index 00000000..77f6ac40 --- /dev/null +++ b/src/controllers/help.controller.ts @@ -0,0 +1,46 @@ +import { Router, Request, Response } from 'express'; +import { Controller } from '../interfaces'; + +const helpData = { + command1: { + info: 'Details about command1', + subcommand1: 'Details about command1 > subcommand1', + subcommand2: 'Details about command1 > subcommand2' + }, + command2: { + info: 'Details about command2', + subcommand1: 'Details about command2 > subcommand1', + subcommand2: 'Details about command2 > subcommand2' + } +}; + +export class HelpController implements Controller { + public basepath = '/help'; + + public boot(): Router { + const router: Router = Router(); + + router.get(this.basepath, (req: Request, res: Response) => { + const command = req.body.command; + + if (!command) { + return res.status(400).json({ message: 'Command parameter is required' }); + } + + const subCommands = command.split('.'); + let currentData: any = helpData; + + for (const subCommand of subCommands) { + currentData = currentData[subCommand]; + + if (!currentData) { + return res.status(404).json({ message: 'Help information not found' }); + } + } + + res.json(currentData); + }); + + return router; + } +} diff --git a/src/server.ts b/src/server.ts index f090e523..be2f8cd6 100644 --- a/src/server.ts +++ b/src/server.ts @@ -12,6 +12,7 @@ import { ConvertController } from './controllers/convert.controller'; import { BundleController } from './controllers/bundle.controller'; import { DiffController } from './controllers/diff.controller'; import { DocsController } from './controllers/docs.controller'; +import { HelpController } from './controllers/help.controller'; async function main() { const app = new App([ @@ -22,6 +23,7 @@ async function main() { new BundleController(), new DiffController(), new DocsController(), + new HelpController(), ]); await app.init(); app.listen(); From 70d474df4bf6f33baf950ddc6c746d8ccf7ef791 Mon Sep 17 00:00:00 2001 From: Prince Rajpoot Date: Tue, 13 Jun 2023 02:57:37 +0530 Subject: [PATCH 02/19] [Update]: Change in the structure, and split will work with space --- src/controllers/help.controller.ts | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/controllers/help.controller.ts b/src/controllers/help.controller.ts index 77f6ac40..0cbad85c 100644 --- a/src/controllers/help.controller.ts +++ b/src/controllers/help.controller.ts @@ -3,12 +3,12 @@ import { Controller } from '../interfaces'; const helpData = { command1: { - info: 'Details about command1', + info: 'Details about command1. It has subcommand1 and subcommand2', subcommand1: 'Details about command1 > subcommand1', subcommand2: 'Details about command1 > subcommand2' }, command2: { - info: 'Details about command2', + info: 'Details about command2. It has subcommand1 and subcommand2', subcommand1: 'Details about command2 > subcommand1', subcommand2: 'Details about command2 > subcommand2' } @@ -21,24 +21,31 @@ export class HelpController implements Controller { const router: Router = Router(); router.get(this.basepath, (req: Request, res: Response) => { - const command = req.body.command; + const command = req.body.command.trim(); if (!command) { return res.status(400).json({ message: 'Command parameter is required' }); } - const subCommands = command.split('.'); - let currentData: any = helpData; + const [mainCommand, subCommand] = command.split(' '); - for (const subCommand of subCommands) { - currentData = currentData[subCommand]; + // Check if the main command exists + if (!helpData[mainCommand]) { + return res.status(404).json({ message: 'Help information not found' }); + } - if (!currentData) { + // If a subcommand is specified, check if it exists + if (subCommand) { + const subCommandInfo = helpData[mainCommand][subCommand]; + if (!subCommandInfo) { return res.status(404).json({ message: 'Help information not found' }); } + return res.json({ info: subCommandInfo }); } - res.json(currentData); + // If no subcommand is specified, return the main command info + const mainCommandInfo = helpData[mainCommand].info; + return res.json({ info: mainCommandInfo }); }); return router; From 3d0c0c0d8cccdb8a13ff8aa2712c88908a281920 Mon Sep 17 00:00:00 2001 From: Prince Rajpoot Date: Thu, 15 Jun 2023 02:10:05 +0530 Subject: [PATCH 03/19] [feat] : Added functionality of fetching data from github repo and make changes in commands.md file --- src/controllers/help.controller.ts | 79 +++++++++++++++++------------- 1 file changed, 46 insertions(+), 33 deletions(-) diff --git a/src/controllers/help.controller.ts b/src/controllers/help.controller.ts index 0cbad85c..edb22299 100644 --- a/src/controllers/help.controller.ts +++ b/src/controllers/help.controller.ts @@ -1,16 +1,21 @@ import { Router, Request, Response } from 'express'; import { Controller } from '../interfaces'; +import axios from 'axios'; +import MarkdownIt from 'markdown-it'; -const helpData = { - command1: { - info: 'Details about command1. It has subcommand1 and subcommand2', - subcommand1: 'Details about command1 > subcommand1', - subcommand2: 'Details about command1 > subcommand2' - }, - command2: { - info: 'Details about command2. It has subcommand1 and subcommand2', - subcommand1: 'Details about command2 > subcommand1', - subcommand2: 'Details about command2 > subcommand2' +const md = new MarkdownIt(); + +const fetchCommands = async (user, repo) => { + try { + const url = `https://api.github.com/repos/${user}/${repo}/contents/commands.md`; + const response = await axios.get(url, { + headers: { + 'Accept': 'application/vnd.github.v3.raw', + }, + }); + return response.data; + } catch (error) { + console.error(`Error fetching commands: ${error}`); } }; @@ -20,33 +25,41 @@ export class HelpController implements Controller { public boot(): Router { const router: Router = Router(); - router.get(this.basepath, (req: Request, res: Response) => { - const command = req.body.command.trim(); - + router.get(this.basepath, async (req: Request, res: Response) => { + const command = req.body.command ? req.body.command.trim() : null; + + const commandsMarkdown = await fetchCommands('princerajpoot20', 'api_endpoint'); + + if (!commandsMarkdown) { + return res.status(500).json({ message: 'Error fetching help information' }); + } + if (!command) { - return res.status(400).json({ message: 'Command parameter is required' }); + // Return information for only the list of commands + const endOfList = commandsMarkdown.indexOf('##', commandsMarkdown.indexOf('')); + const commandsListSection = commandsMarkdown.substring(0, endOfList).trim(); + + if (!commandsListSection) { + return res.status(404).json({ message: 'Commands list not found' }); + } + + const htmlContent = md.render(commandsListSection); + return res.send(htmlContent); } - - const [mainCommand, subCommand] = command.split(' '); - - // Check if the main command exists - if (!helpData[mainCommand]) { + + const sections = commandsMarkdown.split('##'); + const commandSection = sections.find(section => section.trim().startsWith(`\`${command}\``)); + + if (!commandSection) { return res.status(404).json({ message: 'Help information not found' }); } - - // If a subcommand is specified, check if it exists - if (subCommand) { - const subCommandInfo = helpData[mainCommand][subCommand]; - if (!subCommandInfo) { - return res.status(404).json({ message: 'Help information not found' }); - } - return res.json({ info: subCommandInfo }); - } - - // If no subcommand is specified, return the main command info - const mainCommandInfo = helpData[mainCommand].info; - return res.json({ info: mainCommandInfo }); - }); + + const htmlContent = md.render(`##${commandSection}`); + + return res.send(htmlContent); + }); + + return router; } From 58fe75c63ec3811561cf8611c9db9b9ac17e25ac Mon Sep 17 00:00:00 2001 From: Prince Rajpoot <44585452+princerajpoot20@users.noreply.github.com> Date: Sun, 18 Jun 2023 21:30:05 +0530 Subject: [PATCH 04/19] Update src/controllers/help.controller.ts Co-authored-by: David Pereira --- src/controllers/help.controller.ts | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/controllers/help.controller.ts b/src/controllers/help.controller.ts index edb22299..854b4659 100644 --- a/src/controllers/help.controller.ts +++ b/src/controllers/help.controller.ts @@ -59,8 +59,6 @@ export class HelpController implements Controller { return res.send(htmlContent); }); - - return router; } } From d6879d539f38e5bea42ce9dd1ad654957ce9447c Mon Sep 17 00:00:00 2001 From: Prince Rajpoot Date: Mon, 19 Jun 2023 00:12:42 +0530 Subject: [PATCH 05/19] fix : Improved error handling, added commands.md file and corrected the path of it --- commands.md | 164 +++++++++++++++++++++++++++++ src/controllers/help.controller.ts | 44 ++++++-- 2 files changed, 202 insertions(+), 6 deletions(-) create mode 100644 commands.md diff --git a/commands.md b/commands.md new file mode 100644 index 00000000..99bbe5ed --- /dev/null +++ b/commands.md @@ -0,0 +1,164 @@ +# Commands + + +* [`command1`](#command1) +* [`command1 subcommand1`](#command1-subcommand1) +* [`command1 subcommand2`](#command1-subcommand2) +* [`command2`](#command2) +* [`command2 subcommand1`](#command2-subcommand1) +* [`command2 subcommand2`](#command2-subcommand2) +* [`generate`](#generate) +* [`generate models`](#generate-models) + +## `command1` + +data for command1 + +``` +more data for command1. + +``` + + +## `command1 subcommand1` + +data for command1 subcommand1 + +``` +more data for command1 subcommand 1 +``` + + +## `command1 subcommand2` + +data for command1 subcommand2 + +``` +more data for command1 subcommand 2 + +``` + + +## `command2` + +data for command2 + +``` +more data for command2. + +``` + + +## `command2 subcommand1` + +data for command2 subcommand1 + +``` +USAGE + $ asyncapi config context add CONTEXT-NAME SPEC-FILE-PATH [-h] + +ARGUMENTS + CONTEXT-NAME context name + SPEC-FILE-PATH file path of the spec file + +FLAGS + -h, --help Show CLI help. + +DESCRIPTION + Add or modify a context in the store +``` + + +## `command2 subcommand2` + +data for command2 subcommand2 + +``` +more data for command2 subcommand 2 + +``` + + + +## `generate` + +Generate typed models or other things like clients, applications or docs using AsyncAPI Generator templates. + +``` +USAGE + $ asyncapi generate + +DESCRIPTION + Generate typed models or other things like clients, applications or docs using AsyncAPI Generator templates. +``` + +_See code: [src/commands/generate/index.ts](https://github.com/asyncapi/cli/blob/v0.48.0/src/commands/generate/index.ts)_ + + +## `generate models` + +Generates typed models + +``` +USAGE + $ asyncapi generate models LANGUAGE FILE [-h] [-o ] [--tsModelType class|interface] [--tsEnumType + enum|union] [--tsModuleSystem ESM|CJS] [--tsIncludeComments] [--tsExportType default|named] [--tsJsonBinPack] + [--packageName ] [--namespace ] [--csharpAutoImplement] [--csharpNewtonsoft] [--csharpArrayType + Array|List] [--csharpHashcode] [--csharpEqual] [--csharpSystemJson] [--log-diagnostics] [--diagnostics-format + json|stylish|junit|html|text|teamcity|pretty] [--fail-severity error|warn|info|hint] + +ARGUMENTS + LANGUAGE (typescript|csharp|golang|java|javascript|dart|python|rust|kotlin|php|cplusplus) The language you want the + typed models generated for. + FILE Path or URL to the AsyncAPI document, or context-name + +FLAGS + -h, --help Show CLI help. + -o, --output= The output directory where the models should be + written to. Omitting this flag will write the + models to `stdout`. + --csharpArrayType=