Skip to content

Commit

Permalink
Auto-Complete Required Request Body Parameters when Inserting Commands (
Browse files Browse the repository at this point in the history
#156)

* Add Required Body Parameters when Inserting Commands

Signed-off-by: Snehil Shah <[email protected]>

* Format Code

Signed-off-by: Snehil Shah <[email protected]>

* Refactor

Signed-off-by: Snehil Shah <[email protected]>

---------

Signed-off-by: Snehil Shah <[email protected]>
  • Loading branch information
Snehil-Shah authored Mar 5, 2024
1 parent aa9afb1 commit 436be42
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Close from '@mui/icons-material/Close';
import useMediaQuery from '@mui/material/useMediaQuery';
import { useSnackbar } from 'notistack';
import { getSnackbarOptions } from '../../../Common/utils/snackbarOptions';
import { resolveRequiredBodyParams } from '../../config/CommandsDrawerUtils';

const CommandsDrawer = ({ open, toggleDrawer, handleInsertCommand }) => {
const [allCommands, setAllCommands] = useState([]);
Expand All @@ -26,13 +27,18 @@ const CommandsDrawer = ({ open, toggleDrawer, handleInsertCommand }) => {
const description = data.paths[path][method].summary;
const tags = data.paths[path][method].tags;
const hasRequestBody = !!data.paths[path][method].requestBody;
let requiredBodyParameters = null;
if (hasRequestBody) {
requiredBodyParameters = resolveRequiredBodyParams(data, data.paths[path][method].requestBody.content);
}

return {
method: method.toUpperCase(),
command,
description,
hasRequestBody,
tags,
requiredBodyParameters,
};
});
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,13 @@ const CommandsTable = ({ commands, handleInsertCommand }) => {

const handleClick = (command) => {
const commandText = `${command.method} ${command.command.substring(1)}${
command.hasRequestBody ? ' \n{\n \n}' : ''
command.hasRequestBody
? ` \n{\n ${
command.requiredBodyParameters
? command.requiredBodyParameters.map((param) => `"${param}": `).join(',\n ')
: ''
}\n}`
: ''
}`;

handleInsertCommand(commandText);
Expand Down
12 changes: 12 additions & 0 deletions src/components/CodeEditorWindow/config/CommandsDrawerUtils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const resolveRequiredBodyParams = function (openapi, requestBodyContent) {
const contentType = Object.keys(requestBodyContent)[0];
if ('$ref' in requestBodyContent[contentType].schema) {
const refPath = requestBodyContent[contentType].schema.$ref;
// parse and navigate to the ref
const pathComponents = refPath.slice(2).split('/');
const schema = pathComponents.reduce((doc, pathComponent) => doc[pathComponent], openapi);
return schema.required;
} else {
return requestBodyContent[contentType].schema.required;
}
};

0 comments on commit 436be42

Please sign in to comment.