-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
168 lines (154 loc) · 4.43 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
'use strict';
var pug = require('pug');
var express = require('express');
var tv4 = require('tv4');
var util = require('util');
var STRING_TYPE = '<String>';
var self;
function RouterDocs(app, opts) {
self = this;
opts = opts || {};
if(opts.pretty) {
app.engine('pug', pug.renderFile);
app.set('views', __dirname + '/views');
app.set('view engine', 'pug');
app.use('/docs', express.static(__dirname + '/views'));
}
if('disable' in opts) {
app.docs = function() { };
return;
}
app.docs = function(obj, literal) {
var idx = app._router.stack.length-1;
var layer = app._router.stack[idx];
layer.docs = {};
if(literal) {
return Object.keys(obj).forEach(function(k) {
layer.docs[k] = obj[k];
});
}
if(typeof obj === 'string') {
obj = {description: obj};
}
['title', 'method', 'resource', 'path', 'description', 'headers']
.forEach(function(k) {
if(obj[k]) {
layer.docs[k] = obj[k];
}
}
);
if('requestBody' in obj) {
layer.docs.requestBody = {};
if(Array.isArray(obj.requestBody)) {
layer.docs.requestBody = obj.requestBody;
} else {
Object.keys(obj.requestBody).sort(function(a,b) {
a = obj.requestBody[a].$required;
b = obj.requestBody[b].$required;
return a && b ? 0 : !a && b ? 1 : !b && a ? -1 : 0;
}).forEach(function(x) {
layer.docs.requestBody[x] = obj.requestBody[x];
});
}
} else if('requestArray' in obj) {
layer.docs.requestArray = obj.requestArray;
} else if('requestVariants' in obj) {
layer.docs.requestVariants = {};
obj.requestVariants.forEach(function(x,i) {
layer.docs.requestVariants[i] = x;
});
}
if(layer.keys.length > 0) {
layer.docs.requestParameters = {};
layer.keys.forEach(function(x,i) {
layer.docs.requestParameters[':' + x.name] = {};
if('requestParameters' in obj && obj.requestParameters[i]) {
Object.keys(obj.requestParameters[i]).forEach(function(k) {
layer.docs.requestParameters[':' + x.name][k] =
obj.requestParameters[i][k];
});
switch(obj.requestParameters[i].type) {
case 'string':
layer.docs.requestParameters[':' + x.name].type = STRING_TYPE;
break;
}
}
});
}
if('queryParameters' in obj) {
layer.docs.queryParameters = obj.queryParameters;
}
if('responseCodes' in obj) {
layer.docs.responseCodes = obj.responseCodes;
}
if('exampleResponse' in obj) {
layer.docs.exampleResponse = obj.exampleResponse;
}
if('exampleRequest' in obj) {
layer.docs.exampleRequest = obj.exampleRequest;
}
};
var copyData = function(opts) {
var resource = opts.resource;
var routes = opts.target;
resource = resource.substr(0,1).toUpperCase() + resource.substr(1);
if(!(resource in routes)) {
routes[resource] = [];
}
routes[resource].push({
path: opts.path,
verb: opts.method.toUpperCase()
});
Object.keys(opts.docs).forEach(function(k) {
routes[resource][routes[resource].length - 1][k] = opts.docs[k];
});
};
app.get('/docs', function(req, res) {
var routes = {};
app._router.stack.forEach(function(x) {
if(!x.docs) {
return;
}
if(!x.route) {
copyData({
method: x.docs.method,
path: x.docs.path,
docs: x.docs,
resource: x.docs.resource,
target: routes
});
return;
}
Object.keys(x.route.methods).sort().forEach(function(method) {
copyData({
method: method,
path: x.route.path,
docs: x.docs,
resource: x.route.path.split('/')[1],
target: routes
});
});
});
var routesSorted = {};
Object.keys(routes).sort().forEach(function(x) {
routesSorted[x] = routes[x].sort(function(a,b) {
return a.title >= b.title;
});
});
if('pretty' in opts && !!opts.pretty) {
return res.render('index', {
appLocals: app.locals,
routes: routesSorted
});
}
res.json(routesSorted);
});
}
RouterDocs.prototype.render = function(res, routes) {
res.render('index', {
title: 'API Docs',
appLocals: {version: '1.0.0', fqdn: 'api.example.com'},
routes: routes
});
};
module.exports = RouterDocs;