-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
206 lines (196 loc) · 7.28 KB
/
index.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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
var url = require('url'),
drafter = require('drafter.js'),
UriTemplate = require('uritemplate'),
jsonSchemaFromMSON = require('./src/mson_to_json_schema');
const { searchDataStructure, generateSchemaFromExample } = require('./src/util')
const { processRequests } = require('./src/requests')
const { processResponses } = require('./src/responses')
const { processParameters } = require('./src/parameters')
const toOpenApi = require('json-schema-to-openapi-schema')
var apib2swagger = module.exports.convertParsed = function (apib, options) {
const { openApi3, infoTitle } = options
var output = {};
if (openApi3) {
output.openapi = '3.0.3';
} else {
output.swagger = '2.0';
}
output.info = {
'title': infoTitle || apib.name,
'version': openApi3 ? '1.0.0' : '',
'description': apib.description
}
apib.metadata.forEach(function (meta) {
if (meta.name.toLowerCase() === 'host') {
if (openApi3) {
output.servers = [
{
url: meta.value
}
];
} else {
var urlParts = url.parse(meta.value);
output.host = urlParts.host;
output.basePath = urlParts.pathname;
output.schemes = [urlParts.protocol.replace(':', '')];
}
}
if (meta.name.toLowerCase() === 'version' && meta.value) {
output.info.version = meta.value
}
});
output.paths = {};
if (openApi3) {
output.components = { schemas: {} }
} else {
output.definitions = {}
output.securityDefinitions = {};
}
var converterContext = { swagger: output, options: options };
var tags = {};
apib.content.filter(function (content) {
return content.element === 'category';
}).forEach(function (category) {
var groupName = category.attributes ? category.attributes.name : '';
if (groupName && !tags[groupName]) {
tags[groupName] = { name: groupName };
}
category.content.forEach(function (content) {
if (content.element === 'resource') {
// (name, description) in Resource section are discarded
const definitions = swaggerDefinitions(content, options);
if (openApi3) {
output.components.schemas = { ...output.components.schemas, ...definitions }
} else {
output.definitions = { ...output.definitions, ...definitions }
}
swaggerPaths(converterContext, groupName, content);
} else if (content.element === 'copy') {
// group description here
tags[groupName].description = content.content;
} else if (content.element === 'dataStructure') {
const { id } = content.content[0].meta
const schema = jsonSchemaFromMSON(content, options);
if (openApi3) {
output.components.schemas[id] = toOpenApi(schema)
} else {
output.definitions[id] = schema
}
}
});
});
output.tags = [];
for (var key in tags) {
output.tags.push(tags[key]);
}
return output;
}
function swaggerPathName(uriTemplate) {
var params = {};
for (var i = 0; i < uriTemplate.expressions.length; i++) {
var exp = uriTemplate.expressions[i];
if (!exp.varspecs) continue;
if (exp.operator.symbol === '?') continue; // query
for (var j = 0; j < exp.varspecs.length; j++) {
var spec = exp.varspecs[j];
params[spec.varname] = '{' + spec.varname + '}';
}
}
return decodeURIComponent(uriTemplate.expand(params));
}
var swaggerDefinitions = function (resource, options) {
var scheme;
const result = {}
const { openApi3 } = options
if (resource.name) {
scheme = searchDataStructure(resource.content, options); // Attributes 1
if (openApi3) {
if (scheme) {
result[resource.name] = toOpenApi(scheme)
}
} else {
result[resource.name] = scheme ? scheme : {};
}
}
const model = resource.model;
if (model.content && model.name) {
scheme = searchDataStructure(model.content, options); // Attribute 2
// fall back to body
if (!scheme && model.content.length > 0) {
scheme = generateSchemaFromExample(model.headers, model.content[0].content, options);
}
if (openApi3) {
if (scheme) {
result[model.name + 'Model'] = toOpenApi(scheme)
}
} else {
result[model.name + 'Model'] = scheme ? scheme : {};
}
}
return result
};
var swaggerPaths = function (context, tag, resource) {
var paths = context.swagger.paths;
var uriTemplate = UriTemplate.parse(resource.uriTemplate),
pathName = swaggerPathName(uriTemplate);
var pathParams = processParameters(resource.parameters, uriTemplate, context.options); // for swagger ui
for (var k = 0; k < resource.actions.length; k++) {
var action = resource.actions[k];
if (!action.attributes.uriTemplate) {
if (!paths[pathName]) paths[pathName] = {};
paths[pathName][action.method.toLowerCase()] = swaggerOperation(context, pathParams, uriTemplate, action, tag);
continue;
}
var attrUriTemplate = UriTemplate.parse(action.attributes.uriTemplate),
attrPathName = swaggerPathName(attrUriTemplate);
if (!paths[attrPathName]) paths[attrPathName] = {};
paths[attrPathName][action.method.toLowerCase()] = swaggerOperation(context, [], attrUriTemplate, action, tag);
}
};
var swaggerOperation = function (context, pathParams, uriTemplate, action, tag) {
var operation = {
'responses': processResponses(action.examples, context.options),
'summary': action.name,
'operationId': action.name,
'description': action.description,
'tags': tag ? [tag] : [],
'parameters': pathParams.concat(processParameters(action.parameters, uriTemplate, context.options))
};
var produces = {}, producesExist = false;
for (var key in operation.responses) {
var response = operation.responses[key];
for (var mime in response.examples) {
producesExist = true;
produces[mime] = true;
}
}
if (producesExist) {
operation.produces = [];
for (var mime in produces) {
operation.produces.push(mime);
}
}
return processRequests(operation, action, context)
}
exports.noconvert = function (data, callback) {
try {
var result = drafter.parse(data, { type: 'ast' });
return callback(null, result);
} catch (error) {
return callback(error, {});
}
};
exports.convert = function (data, options, callback) {
if (typeof options === 'function') {
callback = options;
options = {};
}
try {
var result = drafter.parse(data, { type: 'ast' });
var swagger = apib2swagger(result.ast, options);
return callback(null, { swagger: swagger });
}
catch (error) {
return callback(error, {});
}
};