-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathindex.js
148 lines (132 loc) · 5.55 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
'use strict';
const recurse = require('reftools/lib/recurse.js').recurse;
const clone = require('reftools/lib/clone.js').clone;
const jptr = require('reftools/lib/jptr.js').jptr;
function filter(obj,options) {
const defaults = {};
defaults.flags = ['x-internal'];
defaults.flagValues = [];
defaults.checkTags = false;
defaults.inverse = false;
defaults.strip = false;
defaults.overrides = [];
options = Object.assign({},defaults,options);
let src = clone(obj);
let filtered = {};
let filteredpaths = [];
recurse(src,{},function(obj,key,state){
for (let override of options.overrides) {
if (key.startsWith(override)) {
obj[key.substring(override.length)] = obj[key];
if (options.strip) {
delete obj[key];
}
}
}
for (let flag of options.flags) {
if ((options.checkTags == false && (obj[key] && ((options.flagValues.length == 0 && obj[key][flag]) || options.flagValues.includes(obj[key][flag])))) || (options.checkTags && (obj[key] && obj[key].tags && Array.isArray(obj[key].tags) && obj[key].tags.includes(flag)))) {
if (options.inverse) {
if (options.strip) {
delete obj[key][flag];
}
if (Array.isArray(obj)) {
// we need to seed the presence of an empty array
// otherwise jptr won't know whether it's setting
// an array entry or a property with a numeric key #26
const components = state.path.split('/');
components.pop(); // throw away last item
if (jptr(filtered,components.join('/')) === false) {
jptr(filtered,components.join('/'),[]);
}
}
jptr(filtered,state.path,clone(obj[key]));
}
filteredpaths.push(state.path);
delete obj[key];
break;
}
}
});
// remove undefined properties (important for YAML output)
recurse((options.inverse ? filtered : src),{},function(obj,key,state){
if (Array.isArray(obj[key])) {
obj[key] = obj[key].filter(function(e){
return typeof e !== 'undefined';
});
}
});
recurse(src,{},function(obj,key,state){
if (Array.isArray(obj) && obj.length > 0) {
for (let idx = 0; idx < obj.length; idx++) {
if (obj[idx] && obj[idx].hasOwnProperty('$ref') && filteredpaths.includes(obj[idx].$ref)) {
obj.splice(idx, 1);
idx--;
}
}
}
});
// tidy up any paths where we have removed all the operations
for (let p in src.paths) {
if (Object.keys(src.paths[p]).length === 0) {
delete src.paths[p];
}
}
if (options.inverse && options.valid) {
// ensure any components being reffed are still included in output
let checkForReferences = true;
while (checkForReferences) {
checkForReferences = false;
let changesMade = false;
recurse(filtered, {}, function (o, key, state) {
if ((key === '$ref') && (typeof o[key] === 'string') && (o[key].startsWith('#'))) {
if (!jptr(filtered, o.$ref)) {
jptr(filtered, o.$ref, jptr(obj, o.$ref));
changesMade = true;
}
}
checkForReferences = changesMade;
});
}
let info = {};
if (src.info && (!filtered.info || !filtered.info.version || !filtered.info.title)) {
info = Object.assign({}, filtered.info, options.info ? src.info : { title: src.info.title, version: src.info.version });
}
if (src.asyncapi && !filtered.asyncpi) {
filtered = Object.assign({ asyncapi: src.asyncapi, info: info }, filtered);
}
if (src.swagger && !filtered.swagger) {
filtered = Object.assign({ swagger: src.swagger, info: info }, filtered);
}
if (src.openapi && !filtered.openapi) {
filtered = Object.assign({ openapi: src.openapi, info: info }, filtered);
}
if (!filtered.security && Array.isArray(src.security)) {
const filteredsecurityschemes = [];
// OAS2
if (filtered.securityDefinitions) {
filteredsecurityschemes.push(...Object.keys(filtered.securityDefinitions));
}
// OAS3
if (filtered.components && filtered.components.securitySchemes) {
filteredsecurityschemes.push(...Object.keys(filtered.components.securitySchemes));
}
filtered.security = src.security.filter(req => {
const filteredreq = {};
Object.getOwnPropertyNames(req).forEach(function(n){
if (filteredsecurityschemes.includes(n)) {
filteredreq[n] = clone(req[n]);
}
});
return Object.getOwnPropertyNames(filteredreq).length !== 0;
});
}
if (!filtered.paths && !filtered.asyncapi) filtered.paths = {};
if (options.servers && !filtered.servers && Array.isArray(src.servers)) {
filtered.servers = src.servers;
}
}
return (options.inverse ? filtered : src);
}
module.exports = {
filter : filter
};