-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathextractValues.js
More file actions
63 lines (50 loc) · 1.75 KB
/
Copy pathextractValues.js
File metadata and controls
63 lines (50 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const yaml = require('js-yaml');
const fs = require('fs');
// Helper function to extract parameters from a given path object
function extractParameters(pathObj) {
const parameters = {};
if (pathObj.parameters) {
pathObj.parameters.forEach(param => {
if (param.in === 'query') {
if (!parameters.query) parameters.query = [];
parameters.query.push(param.name);
} else if (param.in === 'header') {
if (!parameters.headers) parameters.headers = [];
parameters.headers.push(param.name);
}
});
}
return parameters;
}
// Helper function to extract request body from a given operation object
function extractRequestBody(operationObj) {
if (operationObj.requestBody && operationObj.requestBody.content) {
const contentTypes = Object.keys(operationObj.requestBody.content);
const bodies = {};
contentTypes.forEach(type => {
bodies[type] = operationObj.requestBody.content[type].schema;
});
return bodies;
}
return null;
}
// Main function to extract parameters and request bodies from the OpenAPI file
function extractFromOpenAPI(filepath) {
const fileContents = fs.readFileSync(filepath, 'utf8');
const openApiSpec = yaml.load(fileContents);
const result = {};
for (const [path, pathObj] of Object.entries(openApiSpec.paths)) {
for (const [method, operationObj] of Object.entries(pathObj)) {
const parameters = extractParameters(operationObj);
const requestBodies = extractRequestBody(operationObj);
result[`${method.toUpperCase()} ${path}`] = {
parameters,
requestBodies,
};
}
}
return result;
}
// Example usage
const openApiFilePath = 'path/to/your/openapi.yaml';
const extractedData = extractFromOpenAPI(openApiFilePath);