-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStrider.js
More file actions
194 lines (180 loc) · 6.04 KB
/
Strider.js
File metadata and controls
194 lines (180 loc) · 6.04 KB
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
/** Strider.js
@def:
@TODO:
[ ] auto import all controller
*/
"use strict";
global.Controller = require('./controllers/Controller.js');
// controller imports
const UserController = require('./controllers/UserController.js'),
ArticalController = require('./controllers/ArticalController.js');
const models = require('./dbModelLoader.js');
//
module.exports = class Strider {
// constructor()
constructor(req, res, next) {
// TODO: write validation for each parameter's value i.e. req, res...
this.configs = {};
this.data = {};
this.error = {};
this.model = {};
this.user = {};
this.promise = Q.defer();
this.sessionToken = null;
// set http request handlers
this.configs.HTTPRequest = {
request: req,
response: res,
next: next,
};
// db
this.configs.models = models;
//
this.steps = [];
this.timepoint;
}
setUser(userData) {
this.user = userData;
return this;
}
setSessionToken(sessionToken) {
this.sessionToken =sessionToken;
return this;
}
/** Initial run method to start execution of the flow */
setup(steps, onErrorShouldBreak) {
Logger.dev.debug('Strider.setup()...');
Logger.dev.debug('Get param', this.configs.HTTPRequest.request.query);
// Copy steps object into local object
this.steps = [];
for (let s in steps)
this.steps.push(steps[s]);
this.onErrorShouldBreak = onErrorShouldBreak || false;
return this;
}
// start step execution
run() {
Logger.dev.debug('Strider.run()...').br();
// create timepoint
this.timepoint = new Date();
this.next();
return this.promise;
}
/** next()
@def: loops through each steps, one at a time in sequence,
and executes related controller
@param: [void]
@return: [Boolean] resolves promise
*/
next() {
Logger.dev.debug('Strider.next()')
.debug('Time taken:', (new Date() - this.timepoint), 'ms')
.line(30);
// reset timepoint
this.timepoint = new Date();
const currentStep = this.steps.shift();
// TODO: hack, remove this
if (!currentStep) {
this.configs.HTTPRequest
.response.status(200)
.json({
data: this.data,
error: this.error,
model: this.model,
});
//
this.promise.resolve(true);
return false; // exit function from here
}
//
let steps = currentStep.name.split('.'),
stepName = steps.shift();
Logger.dev.debug("Found Step ", stepName);
Logger.dev.debug("Found Step method/s: ", steps);
// create controller instance for current step
let controllerObj = this.findController(stepName);
// prepare controller instance
controllerObj
.map(currentStep.mappedFields)
.setSelf(currentStep.dataAccessPath);
Logger.dev.debug("Mapped fields are: ", controllerObj.argv).br();
/** A step can have chain of methods, add each to a queue */
let executionQueue = Q.resolve(true);
for (let s in steps) {
if (steps[s] == 'GET')
executionQueue = executionQueue.then(() => {
return controllerObj.GET();
});
else if (steps[s] == 'POST')
executionQueue = executionQueue.then(() => {
return controllerObj.POST();
});
else if (steps[s] == 'PATCH')
executionQueue = executionQueue.then(() => {
return controllerObj.PATCH();
});
else if (steps[s] == 'PUT')
executionQueue = executionQueue.then(() => {
return controllerObj.PUT();
});
else if (steps[s] == 'DELETE')
executionQueue = executionQueue.then(() => {
return controllerObj.DELETE();
});
else
continue;
//
executionQueue = executionQueue.fail((err) => {
Logger.error("Error in executionQueue: ", err.stack || err).br();
// stop as soon as error occurs
if (this.onErrorShouldBreak) {
this.stop();
return true;
}
});
}
// end of execution chain
executionQueue.then((shouldStop) => {
Logger.dev.debug(stepName, 'executed, Jumping to next.').br();
if (!shouldStop)
this.next();
});
}
// stop the execution for current endpoint
stop() {
// TODO: set error code based on error type
this.configs.HTTPRequest
.response.status(501)
.json({
data: this.data,
error: this.error,
model: this.model,
});
}
/** findController()
@def: finds related controller from name,
creates and returns controller instance
@param: [String] name of controller
@return: [Object] Controller
*/
findController(controller) {
let controllerInstance;
switch (controller) {
case "User":
controllerInstance = new UserController();
break;
case "Artical" :
controllerInstance = new ArticalController();
break;
}
// feed data and return instance
controllerInstance.configs = this.configs;
controllerInstance.data = this.data;
controllerInstance.error = this.error;
controllerInstance.model = this.model;
controllerInstance.user = this.user;
controllerInstance.sessionToken = this.sessionToken;
return controllerInstance;
}
}
//