Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: generate request body #3121

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Tests",
"runtimeExecutable": "npm",
"runtimeArgs": [
"run",
"test",
"--",
"--watch",
"open-api-parser.spec.ts"
],
"console": "integratedTerminal",
"internalConsoleOptions": "neverOpen",
"disableOptimisticBPs": true
}
]
}
25 changes: 0 additions & 25 deletions src/app/utils/open-api-parser.spec.ts

This file was deleted.

31 changes: 31 additions & 0 deletions src/app/utils/open-api-parser/parse/open-api-parser.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { parseSampleUrl } from '../../sample-url-generation';
import { parseOpenApiResponse } from './open-api-parser';
import { getSample } from './open-api-sample';
import { generateRequestBody } from './request-body';

function parseResponse() {
const sampleUrl = 'https://graph.microsoft.com/v1.0/me/calendar';
const { requestUrl } = parseSampleUrl(sampleUrl);
return parseOpenApiResponse({
response: getSample(),
url: requestUrl
});
}

describe('Open api spec parser should', () => {

it('generate parameters', async () => {
const result = parseResponse();
expect(result.parameters).toBeDefined();
expect(result.requestBody).toBeDefined();
});

it('generate request body', async () => {
const methodValue = getSample().paths['/me/calendar'].patch;
const requestBody = generateRequestBody(methodValue);
expect(requestBody).toBeDefined();
});

});


Original file line number Diff line number Diff line change
@@ -1,42 +1,48 @@
import {
IOpenApiParseContent,
IParameters,
IParameterValue,
IParameters,
IParsedOpenApiResponse,
IPathValue,
IQueryParameter
} from '../../types/open-api';
IQueryParameter,
MethodValue
} from '../../../../types/open-api';
import { generateRequestBody } from './request-body';

export function parseOpenApiResponse(
params: IOpenApiParseContent
): IParsedOpenApiResponse {
const {
response: { paths },
response,
url
} = params;

try {
const parameters: IParameters[] = [];
const requestUrl = Object.keys(paths)[0];
const verbs = Object.keys(paths[`${requestUrl}`]);
const pathValues: any = Object.values(paths)[0];
const requestUrl = `/${url}`;
const verbs = Object.keys(response.paths[`${requestUrl}`]);
const pathValues: any = Object.values(response.paths)[0];
const requestBody: { [method: string]: any } = {}

verbs.forEach((verb: string) => {
const methodValue: MethodValue = pathValues[`${verb}`];
if (methodValue.requestBody) {
requestBody[verb] = generateRequestBody(methodValue);
}
parameters.push({
verb,
values: getVerbParameterValues(pathValues[`${verb}`]),
links: getLinkValues(pathValues[`${verb}`])
values: getVerbParameterValues(methodValue),
links: getLinkValues(methodValue)
});
});

const createdAt = new Date().toISOString();
return { url, parameters, createdAt };
return { url, parameters, createdAt, requestBody };
} catch (error: any) {
throw new Error(error);
}
}

function getVerbParameterValues(values: IPathValue): IParameterValue[] {
function getVerbParameterValues(values: MethodValue): IParameterValue[] {
const parameterValues: IParameterValue[] = [];
const queryParameters = values.parameters;
if (queryParameters && queryParameters.length > 0) {
Expand All @@ -55,7 +61,7 @@ function getVerbParameterValues(values: IPathValue): IParameterValue[] {
return parameterValues;
}

function getLinkValues(values: IPathValue): string[] {
function getLinkValues(values: MethodValue): string[] {
const responses = values.responses;
if (responses) {
const responsesAtIndex200 = responses['200'];
Expand Down
Loading
Loading