-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlazy.controller.js
505 lines (463 loc) · 16.1 KB
/
lazy.controller.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
const express = require('express');
const LazyModel = require('./lazy.model');
const {LazyTypes} = require('./lazy.utils');
const {LazyTest, LazyEndpointTest, LazyTestEnv, LazyMethodTest} = require('./lazy.test');
const { Router } = require('express');
/** @constant {Array} ALLOWED_API_METHODS List of allowed API methods */
const ALLOWED_API_METHODS= [
"get",
"list",
"post",
"patch",
"delete"
];
/**
* Describes an endpoint method controller
* @class
*/
class LazyEndpointMethod{
/**
*
* @param {Object} route Endpoint's route basepath and route identifier
* @param {String} type Method type (get/list/post/patch/delete)
* @param {Object} body Endpoint's method allowed body parameters
* @param {Function} controller Endpoint's method controller
* @param {Object} io Endpoint's io configuration object
* @param {Array} tests Endpoint's method tests
* @param {Boolean} virtual Endpoint's virtuality
*/
constructor(route, type, body, controller, tests, virtual, isProtected){
this._route = route;
this.type = type;
this.body = body;
this.virtual = virtual;
this.protected = isProtected
this.controller = controller;
this.tests = tests;
}
set type(type){
if(!ALLOWED_API_METHODS.includes(type)){
throw new Error(`Invalid controller "${type}". valid values: ${ALLOWED_API_METHODS.join(',')}`);
}
this._type = type;
}
set body(body = {}){
if(!LazyTypes.isObject(body)){
throw new Error(`Body should be an object. got ${LazyTypes.getFullType(body)}.`);
}
this._body = body;
}
set controller(controller){
if(!LazyTypes.isFunction(controller)){
throw new Error(`Method controller should be a function. got ${LazyTypes.getFullType(controller)}.`);
}
this._controller = controller;
}
set tests(tests){
if(tests){
if(!LazyTypes.isObject(tests)){
throw new Error(`Enpoint's "tests" should be an object. got ${LazyTypes.getFullType(tests)}.`);
}
if(!tests.dataset){
throw new Error(`"${controller}" method tests are missing dataset.`);
}else if(!LazyTypes.isArray(tests.dataset)){
throw new Error(`Test's dataset should be an array. got ${LazyTypes.getFullType(tests.dataset)}`);
}
let _tests = tests.dataset.map(data => {
return new LazyTest(
tests.it, this.route, this.method, data.context, data.props, data.params,
data.qs, data.body, data.fail ,tests.before, tests.expect, tests.after
);
})
this._tests = new LazyMethodTest(this.method, _tests);
}
}
set virtual(isVirtual = false){
if(!LazyTypes.isBoolean(isVirtual)){
throw new Error(`Enpoint's "virtual" should be a boolean. got ${LazyTypes.getFullType(isVirtual)}.`);
}
this._virtual = isVirtual;
}
set protected(isProtected = false){
if(!LazyTypes.isBoolean(isProtected)){
throw new Error(`Enpoint's "protected" should be a boolean. got ${LazyTypes.getFullType(isProtected)}.`);
}
this._protected = isProtected;
}
/**
* Endpoint's method allowed body parameters
* @type {Object}
*/
get body(){
return this._body;
}
/**
* Endpoint's method controller
* @type {Function}
*/
get controller(){
return this._controller;
}
/**
* Endpoint's LazyIOEndpoint
* @type {LazyIOEndpoint}
*/
get io(){
return this._io;
}
/**
* Endpoint's IO middleware function
* @type {Function}
*/
get ioMiddlewareFunction(){
return (req, res, next) => {
req.$lazy.io = this.io;
next();
}
}
/**
* Endpoint's method
* @type {String}
*/
get method(){
return this._type === "list" ? "get" : this._type;
}
/**
* Endpoint's method type
* @type {String}
*/
get type(){
return this._type;
}
/**
* Endpoint's method route
* @type {String}
*/
get route(){
return this.type != "list" && this.type != "post" && !this.virtual ? `${this._route.path}/:${this._route.identifier}` : this._route.path;
}
/**
* Endpoint's method tests
* @type {Array<LazyEndpointTest>}
*/
get tests(){
return this._tests;
}
/**
* Endpoint's virtuality
* @type {Boolean}
*/
get virtual(){
return this._virtual;
}
/**
* Endpoint's protection
* @type {Boolean}
*/
get protected(){
return this._protected;
}
/**
* Endpoint's method controller middleware in charge
* of checking request's body validity
* @type {Function}
*/
get bodyChecker(){
var self = this;
return function(req, res, next){
let err = { status: 400 };
let typeOfBody = Object.prototype.toString.call(req.body).split(" ").pop().split("]")[0];
switch(self.type){
case "patch":
err.hint = typeOfBody != "Object" ? err.hint = `Request body should be an object. Got ${typeOfBody}` : "";
Object.keys(req.body).length === 0 ? err.hint = `Request body cannot be empty` : err.hint;
let wrongParams = Object.keys(req.body).flatMap((key) => self.body.hasOwnProperty(key) ? [] : key);
err.hint = wrongParams.length > 0 ? `Body contains invalid parameters: ${wrongParams.join(",")}. Allowed parameters: ${Object.keys(self.body).join()}` : err.hint;
break;
case "post":
err.hint = typeOfBody != "Object" ? err.hint = `Request body should be an object. Got ${typeOfBody}` : ""
let missingParams = Object.keys(self.body).flatMap((param) => req.body.hasOwnProperty(param) ? [] : param);
err.hint = missingParams.length > 0 ? `Missing parameters: ${missingParams.join(",")}` : err.hint
break;
}
return err.hint ? next(err) : next();
}
}
}
/**
* Describes LazyEndpoint
* @class
* @extends LazyModel
*/
module.exports = class LazyEndpoint extends LazyModel{
/**
* @param {String} name endpoint unique name identifier
* @param {Object} parent endpoint's parent
* @param {Boolean} isVirtual is endpoint virtual ?
* @param {Boolean} isProtected does the endpoint need authentication ?
* @param {String} root endpoint's root
* @param {Object} controllers enpoint's methods controllers
*/
constructor(name, model, endpoint, methods){
super(model);
this.name = name;
this._bindControllerMethods(methods);
if(endpoint){
this._router = null;
this._parent = endpoint.parent; //TODO: Can't remember why I set this to private. Please check. EDIT: parent is a proxy.
this.root = endpoint.root;
this.protected = endpoint.protected;
this.methods = endpoint.methods;
}else{
//TODO: delete methods here
}
}
set name(name){
if(!LazyTypes.isString(name)){
throw new Error(`Endpoint's "name" should be a string. got ${LazyTypes.getFullType(name)}.`);
}
this._name = name;
}
set root(root){
if(!LazyTypes.isString(root)){
throw new Error(`Endpoint's "root" should be a string. got ${LazyTypes.getFullType(root)}.`);
}
this._root = root;
}
set protected(isProtected = false){
if(!LazyTypes.isBoolean(isProtected)){
throw new Error(`Endpoint's "protected" should be a boolean. got ${LazyTypes.getFullType(isProtected)}.`);
}
this._protected = isProtected;
}
set methods(methods){
if(!LazyTypes.isObject(methods)){
throw new Error(`Method configuration should be an object. got ${LazyTypes.getFullType(methods)}.`);
}
this._methods = Object.keys(methods).map(type => {
if(!LazyTypes.isObject(methods[type].request)){
throw new Error(`Method's request configuration should be an object. got ${LazyTypes.getFullType(methods[type].request)}.`);
}
return new LazyEndpointMethod(
this.route,
type,
methods[type].request.body,
methods[type].request.controller,
methods[type].tests,
this.virtual,
this.protected
);
})
let tests = this._methods.map(method => method.tests);
LazyTestEnv.push(new LazyEndpointTest(this.route.path, tests));
}
/**
* Endpoint's name (unique identifier)
* @type {String}
*/
get name(){
return this._name;
}
/**
* Endpoint's parent
* @type {LazyEndpoint}
*/
get parent(){
return this._parent;
}
/**
* Endpoint's route root
* @type {string}
*/
get root(){
return this._root;
}
/**
* Endpoint's protection
* @type {Boolean}
*/
get protected(){
return this._protected || false;
}
/**
* Endpoint's methods controllers
* @type {Object}
*/
get methods(){
return this._methods || [];
}
/**
* Endpoint's route
* @type {Objects}
* @property {String} path Endpoint's path
* @prioperty {String} identifier Endpoint's route identifier
* @todo Maybe separate these ?
*/
get route(){
let parent = this.parent;
let route = "";
while(parent){
route = `${parent.root}${parent.pk ? `/:${parent.route.identifier}` : ""}` + route;
parent = parent.parent;
}
return {
path: route += this.root,
identifier: `${this.name}RouteId`
}
}
/**
* That seems to be quite a nasty... Need to find a way to handle authentication a bit
* more elegantly. SHould do for the moment.
*/
get authMiddlewareFunction(){
return this.protected ? require('./lazy').serverAuth : (req, res, next) => {next()};;
}
/**
* Endpoint's router
* @type {Router}
*/
get router(){
if(!this._router){
// this._router = this.parent ? this.parent.router : express.Router();
this._router = express.Router();
this.methods.forEach((method) => {
this._router[method.method](
method.route,
this.authMiddlewareFunction,
method.ioMiddlewareFunction, //TODO find a way
this._makeMiddleware(method),
method.bodyChecker,
method.controller.bind(this)
)
});
if(!this.virtual && !this.methods.every(method => ["get", "patch", "delete"].includes(method.method)))
this._router.all(`${this.route.path}/:${this.route.identifier}`, LazyEndpoint._fallbackInvalidMethod);
this._router.all(`${this.route.path}`, LazyEndpoint._fallbackInvalidMethod);
this._router.use(LazyEndpoint._handleError);
}
return this._router;
}
/**
* Endpoint's tests
* @type {}
*/
get tests(){
return this._tests;
}
/**
* Binds controller methods
* @param {Object} methods collection of controller methods
*/
_bindControllerMethods(methods = {}){
if(!LazyTypes.isObject(methods)){
throw new Error(`Controller "methods" should be an object. got ${LazyTypes.getFullType(body)}.`);
}
Object.keys(methods).forEach(methodName => {
let method = methods[methodName];
if(!LazyTypes.isFunction(method)){
throw new Error(`Lazy method "${methodName}" should be a function. got ${LazyTypes.getFullType(method)}.`);
}else if(this.hasOwnProperty(methodName)){
throw new Error(`Reserved method name "${methodName}" should be a function.`);
}else{
this[methodName] = method.bind(this);
}
})
}
/**
* Sets up method controller's middleware
* @method makeMiddleware
* @private
* @param {LazyEndpointMethod} method endpoint method
*/
//TODO: Refactor this. It's UGLY.
_makeMiddleware(method){
var parent = this.parent;
let params = [];
while(parent){
let parent_cpy = parent;
if(parent_cpy.pk){
params.push(async (req, res, next) => {
let resource = await parent_cpy.findOne({ [parent_cpy.pk]: req.params[parent_cpy.route.identifier]})
if(resource){
req.$lazy.params.push(resource)
next();
}else{
next({
status: '404',
hint: `Cannot find ${req.params[parent_cpy.route.identifier]} within ${parent_cpy.table}`
})
}
});
}
parent = parent.parent;
}
if(method.type != "post" && method.type != "list" && this.pk && !this.virtual){
let self = this;
params.push(async (req, res, next) => {
let resource = await self.findOne({ [self.pk]: req.params[self.route.identifier]})
if(resource){
req.$lazy.params.push(resource)
next();
}else{
next({
status: '404',
hint: `Cannot find ${req.params[self.route.identifier]} within ${self.table}`
})
}
})
}
return params;
}
/**
* Middleware function handling Method Not Allowed Error
* @param err Error to be sent back to the querier
* @param req Middleware request object
* @param res Middleware response object
* @param next Pointer to the next middleware function in the stack
*/
static _fallbackInvalidMethod (req, res, next) {
next({
status: 405,
hint: `${req.method} method not allowed on this route.`
})
}
/**
* Middleware function handling Method Not Allowed Error
* @param err Error to be sent back to the querier
* @param req Middleware request object
* @param res Middleware response object
* @param next Pointer to the next middleware function in the stack
*/
static _fallbackNotFound (req, res, next) {
next({
status: 404,
hint: `Invalid endpoint: ${req.url}`
})
}
/**
* Handles middleware error
* @param err Error to be sent back to the querier
* @param req Middleware request object
* @param res Middleware response object
*/
static _handleError(err, req, res, next) {
// console.log(err);
err.success = false;
err.status = err.status || 500;
err.message = ( { //Setting error messages for each HTTP error code
400: "Bad Request" , //Status: 400 Bad Request
401: "Unauthorized" , //Status: 401 Unhautorized
403: "Forbidden" , //Status: 403 Forbidden
404: "Not Found" , //Status: 404 Not Found
405: "Method Not Allowed" , //Status: 405 Method Not Allowed
409: "Confict" , //Status: 409 Conflict
413: "Payload Too Large" , //Status: 404 Not Found
415: "Unsupported Media Type" , //Status: 404 Not Found
429: "Too Many Requests" , //Status: 429 Too Many Requests
500: "Internal Server Error" //Status: 500 Internal Server Error
} )[ err.status ]
err.hint = err.hint || "Unknown Error";
res.status(err.status);
res.send(err)
}
}