-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathindex.js
106 lines (85 loc) · 3.35 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
var j2w = require('./json2wadl'),
pug = require('pug'),
fs = require('fs'),
methods = require('./methods'),
path = require('path');
var pugTemplate = fs.readFileSync(path.join(__dirname, 'doc.pug'), 'utf8');
var DocRouter = function (connectRouter, baseUrl) {
if (!connectRouter) throw new Error("Connect router function is missing.");
if (!baseUrl) throw new Error("A base url is missing.");
this.connectRouter = null;
this.methodJsons = [];
this.baseUrl = baseUrl;
this.wadl = null;
var self = this;
self.connectRouter = connectRouter;
this.connectRouter.get("/!!", function (req, res) {
getWadl(req, res);
});
this.connectRouter.options("/", function (req, res) {
getWadl(req, res);
});
var method,
i,
l;
for (i = 0, l = methods.length; i < l; i++) {
method = methods[i];
// override the original router method
this.connectRouter[method] = (function (method, originalMethodFunction) {
return function (route) {
var args = arguments;
var handlersCount = args.length;
// Multiple arguments may exist for multiple middlewares. The Json describing the method is the last argument.
var methodJson = {};
if (typeof(args[args.length - 1]) === 'object'){
methodJson = args[args.length - 1];
handlersCount -= 1;
}
methodJson.method = method.toUpperCase();
methodJson.path = route;
self.methodJsons.push(methodJson);
// call the original router with the original arguments
var originalArgs = Array.prototype.slice.call(args, 0, handlersCount);
originalMethodFunction.apply(self.connectRouter, originalArgs);
};
}(method, self.connectRouter[method]));
}
function getWadl(req, res) {
var htmlTemplate;
if (req.headers.accept &&
(~req.headers.accept.indexOf('application/json') || ~req.headers.accept.indexOf('text/json')))
{
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify(self.methodJsons));
return;
}
if (req.headers.accept &&
(~req.headers.accept.indexOf('text/html') || ~req.headers.accept.indexOf('text/plain')))
{
res.writeHead(200, { 'Content-Type': 'text/html' });
if (!self.html) {
try {
htmlTemplate = pug.compile(pugTemplate);
self.html = htmlTemplate({methodJsons: self.methodJsons, baseUrl: self.baseUrl});
} catch(e) {
console.error(e);
}
}
res.end(self.html);
return;
}
if (!self.wadl) {
self.wadl = j2w.toWadl(self.methodJsons, self.baseUrl, { pretty: true });
}
res.writeHead(200, { 'Content-Type': 'application/xml' });
res.end(self.wadl);
}
};
exports.docRouter = function (connectRouter, baseUrl, fn) {
if (!connectRouter) throw new Error("connectRouter is missing");
var docRouter = new DocRouter(connectRouter, baseUrl);
if (fn) {
fn(docRouter.connectRouter);
}
return docRouter.connectRouter;
};