-
Notifications
You must be signed in to change notification settings - Fork 24
/
LucyBot.js
111 lines (98 loc) · 3.1 KB
/
LucyBot.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
const YAML = require('js-yaml');
const fs = require('fs');
const TARGET_API = process.env.TARGET_API || 'ovp';
const getYAML = function(name) {
return YAML.load(fs.readFileSync(__dirname + '/' + name + '.yml', 'utf8'));
}
const openapi = require('./' + TARGET_API + '.openapi.json');
const config = module.exports = getYAML('base.LucyBot');
const apiConfig = getYAML(TARGET_API + '.LucyBot');
Object.assign(config, apiConfig);
config.env = {
target_api: TARGET_API,
}
var definitions = openapi.definitions;
var enums = openapi['x-enums'];
function makeItem(def) {
return {definition: def};
}
function makeEnumItem(name) {
var enm = enums[name];
var table = '| Name | Value |\n'
+ '|------|-------|\n';
return {
title: name,
contents: table + enm.oneOf.map(function(s) {
return "| " + s.title + " | " + JSON.stringify(s.enum[0]) + " |";
}).join('\n'),
}
}
if (TARGET_API === 'ott') {
let tagItems = openapi.tags.map(t => ({tag: t.name}));
config.operationNavigation.push({children: tagItems, title: 'Services'});
} else {
config.javascript = [
'assets/mixpanel.js',
'assets/tracker.js',
].concat(config.javascript);
}
if (process.env.DISABLE_SWIFTYPE === 'true') {
config.javascript = config.javascript.filter(j => !j.endsWith('/search.js'));
console.log("FILTER", config.javascript);
}
let objectsItem = {title: 'General Objects', children: [], prerender: false};
config.operationNavigation.push(objectsItem);
objectsItem.children.push({
title: "Objects",
children: Object.keys(definitions).filter(function(d) {
return d.indexOf("Filter") === -1;
}).map(makeItem),
});
objectsItem.children.push({
title: "Enums",
children: Object.keys(enums).map(makeEnumItem),
})
objectsItem.children.push({
title: "Filters",
children: Object.keys(definitions).filter(function(d) {
return d.indexOf("Filter") !== -1;
}).map(makeItem),
});
config.operationNavigation.push({
title: "Error Codes",
prerender: false,
markdown: "# Error Codes\n\n" + openapi['x-errors'].map(e => {
let str = '* `' + e.name + '`';
if (e.code && e.code !== e.name) str += ' (code: ' + e.code + ')';
let desc = e.message || e.description;
if (desc) str += ' - ' + desc;
return str;
}).join('\n'),
});
function containsItem(tagOrOp, items) {
items = items || config.operationNavigation;
let matching = items.filter(item => {
if (item.tag === tagOrOp || item.operation === tagOrOp) return true;
return containsItem(tagOrOp, item.children || []);
});
return !!matching.length;
}
let miscItem = config.operationNavigation.filter(i => i.isMiscellaneous).pop();
if (miscItem) {
miscItem.isMiscellaneous = false;
miscItem.children = [];
openapi.tags.forEach(tag => {
if (!containsItem(tag.name)) {
miscItem.children.push({tag: tag.name});
}
});
for (let path in openapi.paths) {
for (let method in openapi.paths[path]) {
let op = openapi.paths[path][method];
if (!containsItem(op.operationId) && !containsItem(op.tags[0])) {
miscItem.children.push({operation: op.operationId});
}
}
}
}
require('./v4-navigation')(config);