forked from Mermade/openapi_optimise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.js
118 lines (100 loc) · 2.19 KB
/
common.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
'use strict';
/** common functions
*
*/
const crypto = require('crypto');
const _ = require('lodash');
const jptr = require('reftools/lib/jptr.js');
const recurse = require('reftools/lib/recurse.js').recurse;
const actions = ['get','head','post','put','delete','patch','options','trace']; // trace is OAS 3+
var empty = [];
empty.push({});
var e2 = {};
e2.type = 'object';
empty.push(e2);
var e3 = {};
e3.type = 'object';
e3.additionalProperties = true;
empty.push(e3);
var e4 = {};
e4.type = 'object';
e4.properties = {};
e4.additionalProperties = true;
empty.push(e4);
var commonLogger;
function logger(verbosity) {
this.verbosity = verbosity;
this.write = function(s) {
console.log(s);
}
this.log = function(s) {
if (this.verbosity>0) console.log(s);
};
this.info = function(s) {
if (this.verbosity>1) console.log(s);
};
this.debug = function(s) {
if (this.verbosity>2) console.log(s);
};
commonLogger = this;
}
module.exports = {
recurse : recurse,
clean : function(parent,name) {
if ((parent[name]) && (Object.keys(parent[name]).length<=0)) {
commonLogger.log('No '+name+' required');
delete parent[name];
}
},
actions : actions,
empty : empty,
isEmpty : function(obj) {
for (var e in empty) {
if (_.isEqual(empty[e],obj)) return true;
}
return false;
},
forEachPath : function(src,callback) {
for (var p in src.paths) {
var pptr = '#/'+jptr.jpescape(p);
callback(src.paths[p],pptr,p);
}
},
forEachAction : function(src,callback) {
for (var p in src.paths) {
var path = src.paths[p];
for (var a in actions) {
if (path[actions[a]]) {
var aptr = '#/'+jptr.jpescape(p)+'/'+actions[a];
callback(path[actions[a]],aptr,actions[a],path);
}
}
}
},
logger : function(verbosity) {
return new logger(verbosity);
},
sha1 : function(s) {
var shasum = crypto.createHash('sha1');
shasum.update(s);
return shasum.digest('hex');
},
locations : function(api) {
if (api.swagger) {
return {
parameters:'#/parameters',
schemas:'#/definitions'
};
}
if (api.openapi) {
return {
parameters:'#/components/parameters',
schemas:'#/components/schemas'
};
}
return {
parameters:'#/',
schemas:'#/'
}
}
};