forked from Mermade/openapi_optimise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
responses.js
49 lines (45 loc) · 1.12 KB
/
responses.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
var _ = require('lodash');
var common = require('./common.js');
var logger;
var state = {};
state.responses = [];
function gatherResponses(src) {
for (var t in src.responses) {
var response = {};
response.definition = src.responses[t];
response.seen = 0;
state.responses.push(response);
}
common.forEachAction(src,function(action){
for (var t in action.responses) {
var response = action.responses[t];
for (var st in state.responses) {
var sresponse = state.responses[st];
if (sresponse.definition.name == response) {
sresponse.seen++;
}
}
}
});
return state.responses;
}
module.exports = {
optimise : function(src,options) {
logger = new common.logger(options.verbose);
if (src.responses) {
logger.log('Removing unused responses');
state.responses = gatherResponses(src);
for (var t in state.responses) {
var response = state.responses[t];
if (response.seen<=0) {
logger.log(' Deleting '+response.definition.name);
_.remove(src.responses,function(o){
return (o.name == response.name);
});
}
}
common.clean(src,'responses');
}
return src;
}
};