This repository has been archived by the owner on May 22, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvert.js
65 lines (53 loc) · 1.75 KB
/
convert.js
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
var Collection = require('postman-collection').Collection,
_ = require('lodash'),
Helpers = require('./helpers.js'),
Converter = null;
Converter = {
collection: null,
createCollectionStructure: function (swaggerData, tree) {
for (var child in tree.children) {
if (tree.children.hasOwnProperty(child)) {
this.collection.items.add(
Helpers.convertChildToItemGroup(swaggerData, tree.children[child])
);
}
}
},
convert: function (json) {
var result = {
status: true,
collecion: null,
reason: null
},
parseResult,
swaggerData = {},
tree;
if (typeof json === 'string') {
//parse
parseResult = Helpers.parse(json);
if (!parseResult.result) {
throw new Exception('Invalid Swagger object');
}
json = parseResult.swagger;
}
swaggerData.globalConsumes = json.consumes || [];
swaggerData.globalProduces = json.produces || [];
// ポストマンの環境変数で変更できるbasePathにする。
swaggerData.basePath = "{{scheme}}://{{host}}{{basePath}}/";
swaggerData.baseParams = json.parameters;
swaggerData.securityDefs = json.securityDefinitions;
swaggerData.sampleDefinitions = json.definitions;
swaggerData.sampleResponses = json.responses;
this.collection = new Collection();
this.collection.name = json.info.title;
this.collection.describe(json.info.description);
this.collection.variables = _.map(swaggerData.sampleDefinitions);
tree = Helpers.getTreeFromPaths(json);
this.createCollectionStructure(swaggerData, tree);
result.collection = this.collection.toJSON();
return result;
}
};
module.exports = function (json) {
return Converter.convert(json);
};