-
Notifications
You must be signed in to change notification settings - Fork 2
/
docs.js
201 lines (160 loc) · 3.84 KB
/
docs.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
'use strict';
var fs = require('fs');
var join = require('path').join;
var dot = require('dot');
var koa = require('koa');
var router = require('koa-joi-router');
var docs = module.exports = koa();
var r = router();
// If defined, custom middleware called by interceptRequest()
var requestHandler;
// holds the routes as they are added by koa-resourcer
var routes = [];
// expose routes to docs generator
docs.routes = r.routes;
var templatePath = join(__dirname, 'template.dot');
var template = loadTemplate(templatePath);
// holds the routes description object
var objCache = null;
// holds the compiled html routes description string data
var htmlCache = null;
/**
* API documentation in JSON format
*/
r.get('/index.json',
{
description: 'API documentation in JSON format.'
},
interceptRequest,
function*() {
if (objCache) {
this.body = objCache;
return;
}
this.body = objCache = {
docs: describeRoutes(routes)
};
});
/**
* API documentation in human-readable HTML format
*/
r.get('/',
{
description: 'API documentation in human-readable HTML format.'
},
interceptRequest,
function* () {
if (htmlCache) {
this.body = htmlCache;
return;
}
if (objCache) {
this.body = htmlCache = template(objCache);
return;
}
objCache = {
docs: describeRoutes(routes)
};
this.body = htmlCache = template(objCache);
}
);
docs.use(r.middleware());
/**
* Handles the route object passed to the resourcer callback
* @api public
*/
docs.addRoute = function addRoute(o) {
routes.push(o);
};
/**
* Clear documentation cache
* @api public
*/
docs.clearCache = function clearCache() {
htmlCache = null;
objCache = null;
};
/**
* Override request handling middleware
* @api public
*/
docs.useRequestHandler = function useRequestHandler(handler) {
requestHandler = handler;
};
/**
* Middleware to intercept the request and allow a custom handler.
* @api private
*/
function* interceptRequest(next) {
if (typeof requestHandler === 'function') {
return yield requestHandler.call(this, next);
} else {
return yield next;
}
}
/**
* Load the documentation template.
* @api private
*/
function loadTemplate(path) {
try {
return dot.compile(fs.readFileSync(path));
} catch (e) {
// Ignoring this line for code coverage until custom templates are supported.
/* istanbul ignore next */
return dot.compile('Failed to load documentation template from path: ' + path);
}
}
/**
* Produce the routes description object.
* @api private
*/
function describeRoutes(routes) {
return routes.filter(function(route) {
return isObject(route.resource.routes);
}).map(function(route) {
return {
path: route.path, routes: route.resource.routes.filter(function(route) {
// Prefer using route.meta.hide over route.hide
return !((route.meta && route.meta.hide) || route.hide);
}).map(describeRoute)
};
});
}
/**
* Produces a human readable description of the route.
* @api private
*/
function describeRoute(route) {
var ret = {};
Object.keys(route).forEach(function(key) {
if (key === 'handler') return;
if (key === 'validate') return;
ret[key] = route[key];
});
if (route.validate) {
// this is the validation object being used by the routes themselves,
// do not change this object.
ret.validate = describeObject(route.validate);
}
return ret;
}
/**
* Produces a human readable description of the route validators.
* @api private
*/
function describeObject(o) {
if (!isObject(o)) return o;
if (typeof o.describe === 'function') return o.describe();
var ret = {};
Object.keys(o).forEach(function(key) {
ret[key] = describeObject(o[key]);
});
return ret;
}
/**
* @api private
*/
function isObject(o) {
return typeof o === 'object' && o !== null;
}