-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAPIHelper.js
More file actions
127 lines (108 loc) · 4.13 KB
/
OpenAPIHelper.js
File metadata and controls
127 lines (108 loc) · 4.13 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
const fs = require('fs');
const { Collection, Item } = require('postman-collection');
const codegen = require('postman-code-generators');
const { execSync } = require('child_process');
const yaml = require('js-yaml');
const path = require('path');
class OpenAPIHelper {
static convertOpenAPIToPostman(openAPIPath, postmanOutputPath) {
execSync(`openapi2postmanv2 -s ${openAPIPath} -o ${postmanOutputPath}`, { stdio: 'inherit' });
}
static getSupportedLanguagesAndVariants() {
const languages = codegen.getLanguageList();
const supportedLanguages = [];
languages.forEach(lang => lang.variants.forEach(vari => supportedLanguages.push({ language: lang.key, variant: vari.key }))) ;
return supportedLanguages;
}
static generateSampleCode(postmanCollectionPath, language = null, variant = null) {
const collection = JSON.parse(fs.readFileSync(postmanCollectionPath).toString());
if (!fs.existsSync('/tmp/sample_code')) {
fs.mkdirSync('/tmp/sample_code');
}
const generateForAll = (request, operationId) => {
codegen.getLanguageList().forEach(lang => {
lang.variants.forEach(vari => {
codegen.convert(lang.key, vari.key, request, {}, (error, snippet) => {
if (error) {
console.error(error);
} else {
const fileName = `${operationId}~${lang.key}~${vari.key}.pm`;
fs.writeFileSync(`/tmp/sample_code/${fileName}`, snippet);
}
});
});
});
};
const generateForSpecific = (request, operationId) => {
codegen.convert(language, variant, request, {}, (error, snippet) => {
if (error) {
console.error(error);
} else {
const fileName = `${operationId}_${language}_${variant}.pm`;
fs.writeFileSync(`/tmp/sample_code/${fileName}`, snippet);
}
});
};
function getOperationId(item) {
const name = item.request.name.toLowerCase().split(" "); // eg: "Users List" -> ["users", "list"]
const url = item.request.url.path; // eg: ["api", "v1", "users"]
const method = item.request.method.toLowerCase(); // eg: "get"
const operationId = `${name.join('_')}_${url.join('_')}_${method}`;
return operationId;
}
function processItems(items) {
items.forEach(item => {
if (item.item) {
processItems(item.item);
} else {
const request = new Item(item).request;
const operationId = getOperationId(item);
if (language && variant) {
generateForSpecific(request, operationId);
} else {
generateForAll(request, operationId);
}
}
});
}
processItems(collection.item);
}
static addSampleCodeToOpenAPI(openAPIPath, outputAPIPath) {
let openapiSchema;
const ext = path.extname(openAPIPath);
if (ext === '.yaml' || ext === '.yml') {
openapiSchema = yaml.load(fs.readFileSync(openAPIPath, 'utf8'));
} else {
openapiSchema = JSON.parse(fs.readFileSync(openAPIPath).toString());
}
const sampleCode = {};
fs.readdirSync('/tmp/sample_code').forEach(file => {
const code = fs.readFileSync(`/tmp/sample_code/${file}`).toString();
const [operationId, lang, vari] = file.replace('.pm', '').split('~');
if (!sampleCode[operationId]) {
sampleCode[operationId] = [];
}
sampleCode[operationId][`${lang}_${vari}`] = code;
sampleCode[operationId].push({
lang: lang,
source: code,
label: `${lang}(${vari})`
});
});
Object.keys(openapiSchema.paths).forEach(pathKey => {
const path = openapiSchema.paths[pathKey];
Object.keys(path).forEach(method => {
const operationId = path[method].operationId;
if (operationId && sampleCode[operationId]) {
path[method]['x-codeSamples'] = sampleCode[operationId];
}
});
});
if (ext === '.yaml' || ext === '.yml') {
fs.writeFileSync(outputAPIPath, yaml.dump(openapiSchema));
} else {
fs.writeFileSync(outputAPIPath, JSON.stringify(openapiSchema, null, 2));
}
}
}
module.exports = OpenAPIHelper;