-
Notifications
You must be signed in to change notification settings - Fork 24
/
v4-navigation.js
67 lines (64 loc) · 2.26 KB
/
v4-navigation.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
const TARGET_API = process.env.TARGET_API || 'ovp';
const openapi = require('./' + TARGET_API + '.openapi.json');
const fs = require('fs');
const FULL_PRERENDER = process.env.FULL_PRERENDER === "1";
function getOperations(tag) {
let ops = [];
for (let path in openapi.paths) {
for (let method in openapi.paths[path]) {
let op = openapi.paths[path][method];
if (op.tags.indexOf(tag) !== -1) ops.push(op.operationId);
}
}
return ops;
}
module.exports = function(config) {
let redirects = [];
function addRedirect(from, to) {
redirects.push({from: '/api-docs' + from, to: '/api-docs' + to});
redirects.push({from: '/console' + from, to: '/console' + to});
}
function setPathForItem(item, oldPath='') {
let nextPath = oldPath;
if (item.tag) {
item.prerender = FULL_PRERENDER;
nextPath += '/' + item.tag;
item.path = '/service/' + item.tag;
addRedirect(nextPath, item.path);
if (!item.children) {
item.children = getOperations(item.tag).map(op => {
return {operation: op}
})
}
} else if (item.operation) {
item.prerender = FULL_PRERENDER;
nextPath += '/' + item.operation.replace(/\W+/g, '_');
let [service, action] = item.operation.split('.');
item.path = '/service/' + service + '/action/' + action;
addRedirect(nextPath, item.path);
} else {
nextPath += item.path || '/' + (item.title || '').replace(/\W+/g, '_');
}
(item.children || []).forEach(child => setPathForItem(child, nextPath));
}
config.operationNavigation.forEach(item => {
setPathForItem(item);
});
config.routes['/console'].navigation = openapi.tags.map(tag => {
let item = {tag: tag.name, path: '/service/' + tag.name};
item.children = [];
for (let path in openapi.paths) {
for (let method in openapi.paths[path]) {
let op = openapi.paths[path][method];
if (op.tags.indexOf(tag.name) !== -1) {
item.children.push({
operation: op.operationId,
path: '/service/' + tag.name + '/action/' + op.operationId.split('.')[1],
})
}
}
}
return item;
})
if (process.env.WRITE_REDIRECTS) fs.writeFileSync(__dirname + '/v4-redirects.json', JSON.stringify(redirects, null, 2));
}