-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathbuilder.js
62 lines (54 loc) · 1.66 KB
/
builder.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
'use strict';
var jsyaml = require('js-yaml');
var _ = require('lodash');
SwaggerEditor.service('Builder', function Builder(SwayWorker) {
var load = _.memoize(jsyaml.load);
/*
* Build spec docs from a string value
* @param {string} stringValue - the string to make the docs from
* @returns {promise} - Returns a promise that resolves to spec document
* object or get rejected because of HTTP failures of external $refs
*/
var buildDocs = function(stringValue) {
var json;
return new Promise(function(resolve, reject) {
// If stringVlue is empty, return emptyDocsError
if (!stringValue) {
reject({
specs: null,
errors: [{emptyDocsError: 'Empty Document Error'}]
});
}
// if jsyaml is unable to load the string value return yamlError
try {
json = load(stringValue);
} catch (yamlError) {
reject({
errors: [{yamlError: yamlError}],
specs: null
});
}
// Add `title` from object key to definitions
// if they are missing title
if (json && _.isObject(json.definitions)) {
for (var definition in json.definitions) {
if (_.isObject(json.definitions[definition]) &&
!_.startsWith(definition, 'x-') &&
_.isEmpty(json.definitions[definition].title)) {
json.definitions[definition].title = definition;
}
}
}
SwayWorker.run({
definition: json
}, function(data) {
if (data.errors.length) {
reject(data);
} else {
resolve(data);
}
});
});
};
this.buildDocs = buildDocs;
});