-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdocumenter.js
171 lines (124 loc) · 4.61 KB
/
documenter.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
var tools = require('./tools');
var render = require('./renderer').render;
var format = tools.format;
var error = tools.error;
var Documenter = module.exports = {};
var specifications = Documenter.specifications = [];
// --------------------------------------------------------------------------
// Registers a heimdall-compliant API specification
var documentresource = Documenter.resource = function(resource){
var specification = {
__metadata:{
uri:'/api/'+resource.name,
type:'api.resource'
},
__deferred:{
uri:'/api/'+resource.name+'.html'
},
name:resource.name,
description:resource.description,
type:'resource',
methods:{}
};
specifications.push(specification);
return specification;
};
// --------------------------------------------------------------------------
// Registers a heimdall-compliant API method specification
var documentmethod = Documenter.method = function(root,specification,verb,methodtype,method) {
var url = tools.buildroutestring(specification.name,root,method);
var type = specification.name + '.' + methodtype.toLowerCase();
var doc = {verb:verb,description:method.description,path:url,type:type};
verb = verb.toUpperCase();
var inputspec = function(items) {
var obj = method[items];
if (obj) {
var list = [];
for (var o in obj) {
if(obj.hasOwnProperty(o)) {
list.push({
name:o,
type:obj[o].type,
description:obj[o].description,
required:obj[o].required
});
}
}
if(list.length) doc[items] = list;
}
};
inputspec('params');
inputspec('query');
inputspec('body');
inputspec('files');
inputspec('fields');
specification.methods[methodtype.toLowerCase()] = doc;
};
// --------------------------------------------------------------------------
// Creates API Documentation resources for all Heimdall-Compliant routes
var route = Documenter.route = function(app) {
var viewpath = __dirname+'/views/';
var apiview = viewpath + 'api';
var methodview = viewpath + 'method'
app.get("/api.html",function(req,res,next) {
req.heimdall = format(req.headers.host,req.url,'API.Resource',specifications);
next();
},render(apiview));
app.get("/api",function(req,res) {
res.json(format(req.headers.host,req.url,'API.Resource',specifications));
});
var getspec = function(name) {
var spec = null;
for(var i=0,l=specifications.length;i<l;i++) {
if (specifications[i].name === name) {
spec = specifications[i];
break;
}
}
return spec;
};
app.get("/api/:name.html",function(req,res,next) {
var spec = getspec(req.params.name);
if(!spec) {
res.status(404).send(error("The API resource specification '/api/" + req.params.name + "' could not be found, please check the URL and try again",404,"404 (not found)"));
return false;
}
req.heimdall = format(req.headers.host,req.url,'API.Resource',[spec]);
next();
},render(apiview));
app.get("/api/:name",function(req,res) {
var spec = getspec(req.params.name);
if(!spec) {
res.status(404).send(error("The API resource specification '/api/" + req.params.name + "' could not be found, please check the URL and try again",404,"404 (not found)"));
return false;
}
res.json(format(req.headers.host,req.url,'API.Resource',[spec]));
});
app.get("/api/:name/:type.html",function(req,res,next) {
var spec = getspec(req.params.name);
var meth = spec.methods[req.params.type.toLowerCase()];
if(!spec) {
res.status(404).send(error("The API resource specification '/api/" + req.params.name + "' could not be found, please check the URL and try again",404,"404 (not found)"));
return false;
}
if(!meth) {
res.status(404).send(error("The API resource specification method '/api/" + req.params.name + "/" + req.params.type + "' could not be found, please check the URL and try again",404,"404 (not found)"));
return false;
}
req.heimdall = format(req.headers.host,req.url,'API.Resource.Method',[meth]);
next();
},render(methodview));
app.get("/api/:name/:type",function(req,res) {
var spec = getspec(req.params.name);
var meth = spec.methods[req.params.type.toLowerCase()];
if(!spec) {
res.status(404).send(error("The API resource specification '/api/" + req.params.name + "' could not be found, please check the URL and try again",404,"404 (not found)"));
return false;
}
if(!meth) {
res.status(404).send(error("The API resource specification method '/api/" + req.params.name + "/" + req.params.type + "' could not be found, please check the URL and try again",404,"404 (not found)"));
return false;
}
res.json(format(req.headers.host,req.url,'API.Resource.Method',[meth]));
});
};