-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheimdall.js
400 lines (328 loc) · 13.8 KB
/
heimdall.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/****************************************************************************
*
* Heimdall - a self documenting API Guardian for Express
* (c)Copyright 2014, Max Irwin
* MIT License
*
****************************************************************************/
//Module dependencies
var fs = require('fs');
var datatype = require('datatype');
var tools = require('./tools');
var render = require('./renderer').render;
var documenter = require('./documenter');
//All valid resources loaded by Heimdall
var resources = [];
var routes = [];
//Environment context set by Express
var env;
//Default security middleware, can override
var security = {authenticate:function(req,res,next){next();},administrator:function(req,res,next){res.send(403);}};
//Builtin request parameters that are always allowed and do not need to be specified
var builtins = {
__start:datatype.int("The record number to start from"),
__rows:datatype.int("The number of rows to return")
};
//List of property names that are reserved
var reserved = {
session:true,
__start:true,
__rows:true
};
//Tool library shortcuts
var format = tools.format;
var error = function() {
var args = Array.prototype.slice.apply(arguments);
var data = tools.error.apply(this, args);
if (env === 'development') {
//Verbose console errors for development environments
console.error(data.error);
console.trace();
}
return data;
};
// ==========================================================================
// Heimdall Exports Object:
var Heimdall = module.exports = {datatypes:datatype};
// --------------------------------------------------------------------------
// Checks a set of request fields with the specification
// - Validates source data with defined datatypes (in-spec and builtins)
// - Groups into valid data and errors
var check = function(validation,apiurl,specification,source) {
var data = validation.data;
var errors = validation.errors;
var keytype;
if (source) {
//Check the request against the specification
for(var key in specification) {
var sk = source[key];
var sp = specification[key];
if (specification.hasOwnProperty(key) && datatype.isType(specification[key]) && (source[key]||specification[key].required)) {
keytype = specification[key];
if(keytype.validate(source[key])) {
data[key] = keytype.cast(source[key]);
} else {
var errormessage;
if (specification[key].required && !source[key]) {
errormessage = "Missing Required Parameter: a value must be supplied for '" + key + "'";
} else {
errormessage = "Type Error: '" + source[key] + "' is not a valid value for '" + key + "'";
}
errors.push({code:"449",message:"Retry with " + keytype.type,expects:keytype.description,description:errormessage,specification:apiurl});
}
}
}
//Check for builtin params
for(var key in builtins) {
if (builtins.hasOwnProperty(key) && source[key]) {
keytype = builtins[key];
if(keytype.validate(source[key])) {
data[key] = keytype.cast(source[key]);
} else {
var errormessage = "Type Error: '" + source[key] + "' is not a valid value for '" + key + "'";
errors.push({code:"449",message:"Retry with " + keytype.type,expects:keytype.description,description:errormessage,specification:apiurl});
}
}
}
}
return {data:data,errors:errors};
};
var command = function(name,type,data,resource,success,failure) {
try {
//Data object ready, call the resource command:
resource(name, type, data, success);
} catch (ex) {
//Exception in resource command
failure(ex);
}
}
// --------------------------------------------------------------------------
// Creates a heimdall route for a request
// - Collects all predefined data into an object
// - Ensures strict API definition
var route = function(name,type,method) {
resources[name+'_'+type] = function(context,data,callback) {
method.command.call(context, data, callback);
};
routes[name+'_'+type] = function(req,res,next) {
var validation = {data:{},errors:[]};
var apiurl = req.protocol + '://'+req.headers.host+'/api/'+name+'/'+type+'.html';
if(method.query || req.query) validation = check(validation,apiurl,method.query,req.query);
if(method.params || req.params) validation = check(validation,apiurl,method.params,req.params);
if(method.body || req.body) validation = check(validation,apiurl,method.body,req.body);
if(method.files || req.files) validation = check(validation,apiurl,method.files,req.files);
if(validation.errors.length) {
//Validation errors, respond to client
res.status(449).send(error("Invalid Data",449,"Validation Failed",validation.errors));
return false;
}
var data = validation.data;
//Add the request session to the data
data.session = {};
for(var s in req.session) {
if(req.session.hasOwnProperty(s) && s!=='cookie') {
data[s] = req.session[s]; //Convenience, with risk of override in the specification
data.session[s] = req.session[s]; //Safety, can only be set by Heimdall between request and controller
}
}
var commandSuccess = function(err,result) {
if (!isNaN(req.heimdallcacheduration)) {
res.setHeader('Cache-Control', 'public, max-age=' + req.heimdallcacheduration);
}
if (err) {
if(!isNaN(parseInt(err.code))) res.status(parseInt(err.code));
res.json(error(req.headers.host,err.code,name+'.'+type,err));
} else if (req.heimdallchain) {
req.heimdallchain = null;
req.heimdall = format(req.headers.host,req.url,name+'.'+type,result);
next();
} else if (data.redirect) {
res.redirect(data.redirect);
} else if (req.route.path.indexOf('.html')===-1) {
res.json(format(req.headers.host,req.url,name+'.'+type,result));
} else {
req.heimdall = format(req.headers.host,req.url,name+'.'+type,result);
next();
}
}
var commandFailure = function(ex) {
res.json(error(req.headers.host,req.url,"Internal Command Error",ex.toString()));
}
//Call the resource in a try/catch block
command(name,type,data,resource,commandSuccess,commandFailure);
};
return routes[name+'_'+type];
};
// --------------------------------------------------------------------------
// Verifies that an API specification is valid
// Will panic and halt the application if an error is found
var blessmethodresource = function(name,verb,method) {
var parts = 'query,params,body,files'.split(',');
for(var i=0;i<parts.length;i++) {
var part = method[parts[i]];
for(var key in part) {
if (reserved[key]) {
//Panic!
throw new Error('Unauthorized declaration of reserved specification key "' + key + '" in /' + name + '/' + verb);
process.exit();
}
}
}
};
// --------------------------------------------------------------------------
// Builds an REST resource based on an API specification
var buildmethodresource = function(name,root,resource,specification,verb,methodname,app) {
var method = resource.api[methodname];
var routestring = tools.buildroutestring(name,root,method);
var methodnamelc = methodname.toLowerCase();
var verblc = verb.toLowerCase();
blessmethodresource(name,verb,method);
documenter.method(root,specification,verb,methodname,method);
if (method.open) {
app[verblc](routestring, route(name,methodnamelc,method));
} else if (method.admin) {
app[verblc](routestring, security.administrator, route(name,methodnamelc,method));
} else {
app[verblc](routestring, security.authenticate, route(name,methodnamelc,method));
}
};
// --------------------------------------------------------------------------
// Registers all the resources for a heimdall-compliant API specification
var register = function(filename,resource,app) {
if (typeof resource.name !== "string") { throw (new Error("Resource " + filename + " requires a name"));}
if (typeof resource.description !== "string") { throw (new Error("Resource " + name + " at " + filename + " requires a description"));}
if (typeof resource.api !== "object") { throw (new Error("Resource " + name + " at " + filename + " requires an API definition"));}
if (typeof resource.root !== "string" && resource.root) { throw (new Error("Resource root for " + name + " at " + filename + " must be a string"));}
var root = resource.root?("/"+root+"/"):"/";
var specification = documenter.resource(resource);
for(var method in resource.api) {
if(resource.api.hasOwnProperty(method)) {
switch(method) {
case 'ENTRY': buildmethodresource(resource.name,root,resource,specification,'GET',method,app); break;
case 'COLLECTION': buildmethodresource(resource.name,root,resource,specification,'GET',method,app); break;
case 'ADD': buildmethodresource(resource.name,root,resource,specification,'POST',method,app); break;
case 'SAVE':
buildmethodresource(resource.name,root,resource,specification,'PUT',method,app);
buildmethodresource(resource.name,root,resource,specification,'POST',method,app);
break;
case 'REMOVE':buildmethodresource(resource.name,root,resource,specification,'DELETE',method,app); break;
}
}
}
};
// --------------------------------------------------------------------------
// Expose heimdall resource calls for use by other modules
var resource = Heimdall.resource = function(name,type,data,callback) {
if (resources[name+'_'+type]) {
resources[name+'_'+type]({name:name,type:type},data,callback);
} else {
var heimdall_resource_not_found = "ERROR - The Heimdall resource ["+name+"."+type+"] does not exist";
console.error(heimdall_resource_not_found);
callback(heimdall_resource_not_found);
}
};
//===========================================================================
// Public Heimdall Middleware methods
//===========================================================================
// --------------------------------------------------------------------------
//Public Heimdall render method
Heimdall.render = render;
// --------------------------------------------------------------------------
// Middleware to set a querystring value or values
var set = Heimdall.set = function(query) {
return function(req,res,next) {
for(var key in query) { if(query.hasOwnProperty(key)) { req.query[key] = query[key]; } }
next();
};
};
// --------------------------------------------------------------------------
// Expose Heimdall resource calls for use as connect/express middleware
// params:
// @name - The API resource name
// @type - The API resource method type (entry,collection,add,save,remove)
var middleware = Heimdall.middleware = function(name,type) {
if (routes[name+'_'+type]) {
return routes[name+'_'+type];
} else {
var heimdall_middleware_not_found = "ERROR - The Heimdall route ["+name+"."+type+"] does not exist";
throw new Error(heimdall_middleware_not_found);
}
};
// --------------------------------------------------------------------------
// Expose Heimdall resource calls for use as connect/express middleware
// params:
// @name - The API resource name
// @type - The API resource method type (entry,collection,add,save,remove)
var chain = Heimdall.chain = function(name,type) {
if (routes[name+'_'+type]) {
return function(req,res,next) {
req.heimdallchain = true;
routes[name+'_'+type].call(this,req,res,next);
};
} else {
var heimdall_chain_not_found = "ERROR - The Heimdall route ["+name+"."+type+"] does not exist";
throw new Error(heimdall_chain_not_found);
}
};
// --------------------------------------------------------------------------
// Middleware to set Cache-Control for the resource to a specific duration
// params:
// @duration - The cache duration in milliseconds
var cache = Heimdall.cache = function(duration) {
return function(req,res,next) {
req.heimdallcacheduration = duration;
next();
};
};
//===========================================================================
// Public Heimdall Initialization methods
//===========================================================================
// --------------------------------------------------------------------------
// Heimdall extended DataType creation method
// params:
// @datatype - the type object to create. Properties:
// - "name" - the name of the datatype (required)
// - "definition" - the textual definition (optional)
// - "validate" - the validation function (optional)
// - "cast" - the type casting function (optional)
// - "defaultvalue" - the default value to set (optional)
var type = Heimdall.type = datatype.add;
// --------------------------------------------------------------------------
// Heimdall extended DataType creation method
// params:
// @datatypes - an array of type objects to create
var types = Heimdall.types = datatype.add;
// --------------------------------------------------------------------------
// Heimdall Main entry point
// params:
// @path - the absolute path to the API definition files
// @app - the express app
// @auth - optional authentication middleware
var load = Heimdall.load = function(path,app,auth,admin) {
//Set Express environment context
env = app.get('env');
//Override security middleware if specified
if (typeof auth==='function') security.authenticate = auth;
if (typeof admin==='function') security.administrator = admin;
var revar = /\w+\.js$/i;
var files = fs.readdirSync(path);
var file, name, resource;
console.log('Heimdall found',files.length,'API specifications');
for (var i=0,l=files.length;i<l;i++) {
file = files[i];
if (revar.test(file)) {
console.log(' └── ',file);
name = file.substr(0,file.indexOf(".js"));
resource = require(path+name);
if(resource.resources && resource.resources instanceof Array) {
resource.resources.map(function(apispec){ register(path+name,apispec,app) });
} else {
register(path+name,resource,app);
}
}
}
//Load documentation routes
documenter.route(app);
//Chain after load:
return Heimdall;
};