not-node 4.0.3 → 4.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.json +1 -0
- package/package.json +2 -1
- package/src/init/db/ioredis.js +0 -1
- package/src/init/index.js +6 -1
- package/src/init/middleware.js +3 -1
- package/src/init/routes.js +20 -7
- package/src/manifest/route.js +30 -0
- package/test/init/routes.js +6 -4
package/.eslintrc.json
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "not-node",
|
|
3
|
-
"version": "4.0.
|
|
3
|
+
"version": "4.0.4",
|
|
4
4
|
"description": "node complimentary part for client side notFramework.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"scripts": {
|
|
@@ -78,6 +78,7 @@
|
|
|
78
78
|
"eslint-plugin-promise": "^5.1.1",
|
|
79
79
|
"eslint-plugin-sonarjs": "^0.10.0",
|
|
80
80
|
"ink-docstrap": "^1.3.2",
|
|
81
|
+
"ioredis": "^4.28.1",
|
|
81
82
|
"jsdoc": "^3.6.7",
|
|
82
83
|
"mocha": "*",
|
|
83
84
|
"mocha-suppress-logs": "^0.3.1",
|
package/src/init/db/ioredis.js
CHANGED
package/src/init/index.js
CHANGED
|
@@ -78,7 +78,7 @@ class Init {
|
|
|
78
78
|
static setEnv(key, val) {
|
|
79
79
|
Env.setEnv(key, val);
|
|
80
80
|
}
|
|
81
|
-
|
|
81
|
+
|
|
82
82
|
static getEnv(key) {
|
|
83
83
|
return Env.getEnv(key);
|
|
84
84
|
}
|
|
@@ -149,6 +149,11 @@ class Init {
|
|
|
149
149
|
};
|
|
150
150
|
//running all prepared initalizers with current context
|
|
151
151
|
await initSequence.run(context);
|
|
152
|
+
await ADDS.run('post', {
|
|
153
|
+
config,
|
|
154
|
+
options,
|
|
155
|
+
manifest
|
|
156
|
+
});
|
|
152
157
|
log.info('Application initalization finished');
|
|
153
158
|
} catch (e) {
|
|
154
159
|
Init.throwError(e.message, 1);
|
package/src/init/middleware.js
CHANGED
|
@@ -18,7 +18,9 @@ module.exports = class InitMiddleware{
|
|
|
18
18
|
} else {
|
|
19
19
|
proc = require(warePath);
|
|
20
20
|
}
|
|
21
|
-
|
|
21
|
+
if(typeof proc === 'function'){
|
|
22
|
+
master.getServer().use(proc);
|
|
23
|
+
}
|
|
22
24
|
}
|
|
23
25
|
}
|
|
24
26
|
await ADDS.run('middleware.post', {config, options, master});
|
package/src/init/routes.js
CHANGED
|
@@ -4,6 +4,7 @@ const serveStatic = require('serve-static');
|
|
|
4
4
|
const log = require('not-log')(module, 'not-node//init');
|
|
5
5
|
const {
|
|
6
6
|
notError,
|
|
7
|
+
notValidationError,
|
|
7
8
|
notRequestError
|
|
8
9
|
} = require('not-error');
|
|
9
10
|
|
|
@@ -12,27 +13,39 @@ module.exports = class InitRoutes {
|
|
|
12
13
|
static finalError({
|
|
13
14
|
master
|
|
14
15
|
}) {
|
|
15
|
-
|
|
16
|
+
// eslint-disable-next-line no-unused-vars
|
|
17
|
+
return (err, req, res, next) => {
|
|
18
|
+
//reportable errors from known cases
|
|
16
19
|
if (err instanceof notError) {
|
|
17
20
|
master.getApp().report(err);
|
|
21
|
+
//if request params - ok, but result is not
|
|
18
22
|
if (err instanceof notRequestError) {
|
|
19
23
|
if (err.getRedirect()) {
|
|
20
|
-
res.redirect(err.getRedirect());
|
|
24
|
+
return res.redirect(err.getRedirect());
|
|
21
25
|
} else {
|
|
22
|
-
|
|
23
|
-
res.status(err.getCode()).json({
|
|
26
|
+
return res.status(err.getCode()).json({
|
|
24
27
|
status: 'error',
|
|
25
|
-
|
|
28
|
+
message: err.getResult().message,
|
|
26
29
|
errors: err.getResult().errors
|
|
27
30
|
});
|
|
28
31
|
}
|
|
32
|
+
//bad request params
|
|
33
|
+
}else if (err instanceof notValidationError){
|
|
34
|
+
return res.status(400).json({
|
|
35
|
+
status: 'error',
|
|
36
|
+
message: err.message,
|
|
37
|
+
errors: err.getFieldsErrors()
|
|
38
|
+
});
|
|
29
39
|
}
|
|
30
|
-
}
|
|
40
|
+
}
|
|
41
|
+
//other cases
|
|
42
|
+
if (err instanceof Error && (res && res.status && res.json)) {
|
|
31
43
|
res.status(err.statusCode || 500);
|
|
44
|
+
//reporting as unknown
|
|
32
45
|
master.getApp().report(new notError(`Internal error(${res.statusCode}): %${req.url} - %${err.message}`, {}, err));
|
|
33
46
|
res.json({
|
|
34
47
|
status: 'error',
|
|
35
|
-
|
|
48
|
+
message: err.message
|
|
36
49
|
});
|
|
37
50
|
} else {
|
|
38
51
|
log.error('Unknown error:', err);
|
package/src/manifest/route.js
CHANGED
|
@@ -133,11 +133,41 @@ class notRoute{
|
|
|
133
133
|
);
|
|
134
134
|
}
|
|
135
135
|
|
|
136
|
+
extractReturn(notRouteData){
|
|
137
|
+
if(objHas(notRouteData.rule, 'return')){
|
|
138
|
+
return [...notRouteData.rule.return];
|
|
139
|
+
}else if(objHas(notRouteData.actionData, 'return')){
|
|
140
|
+
return [...notRouteData.actionData.return];
|
|
141
|
+
}else{
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Removes fields from result object acctoding to actionData.return array
|
|
148
|
+
* if presented
|
|
149
|
+
* @param {ExpressRequest} req request object
|
|
150
|
+
* @param {object} result result returned by main action processor
|
|
151
|
+
**/
|
|
152
|
+
filterResultByReturnRule(req, result){
|
|
153
|
+
const returnList = this.extractReturn(req.notRouteData);
|
|
154
|
+
if(result && (typeof result === 'object') && returnList && Array.isArray(returnList)){
|
|
155
|
+
let presented = Object.keys(result);
|
|
156
|
+
presented.forEach(fieldName => {
|
|
157
|
+
if(!returnList.includes(fieldName)){
|
|
158
|
+
delete result[fieldName];
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
136
164
|
async executeRoute(modRoute, actionName, {req, res, next}){
|
|
137
165
|
//waiting preparation
|
|
138
166
|
let prepared = await this.executeFunction(modRoute, CONST_BEFORE_ACTION, [req, res, next]);
|
|
139
167
|
//waiting results
|
|
140
168
|
let result = await this.executeFunction(modRoute, actionName, [req, res, next, prepared]);
|
|
169
|
+
//filter result IF actionData.return specified
|
|
170
|
+
this.filterResultByReturnRule(req, result);
|
|
141
171
|
//run after with results, continue without waiting when it finished
|
|
142
172
|
return this.executeFunction(modRoute, CONST_AFTER_ACTION, [req, res, next, result]);
|
|
143
173
|
}
|
package/test/init/routes.js
CHANGED
|
@@ -17,7 +17,7 @@ module.exports = ({expect})=>{
|
|
|
17
17
|
json(dat){
|
|
18
18
|
expect(dat).to.be.deep.equal({
|
|
19
19
|
status: 'error',
|
|
20
|
-
|
|
20
|
+
message: 'Serious generic error'
|
|
21
21
|
});
|
|
22
22
|
done();
|
|
23
23
|
}
|
|
@@ -120,7 +120,7 @@ module.exports = ({expect})=>{
|
|
|
120
120
|
json(dat){
|
|
121
121
|
expect(dat).to.be.deep.equal({
|
|
122
122
|
status: 'error',
|
|
123
|
-
|
|
123
|
+
message: 'Serious not error request',
|
|
124
124
|
errors: {field: ['name']},
|
|
125
125
|
});
|
|
126
126
|
done();
|
|
@@ -142,8 +142,10 @@ module.exports = ({expect})=>{
|
|
|
142
142
|
|
|
143
143
|
|
|
144
144
|
describe('run', ()=>{
|
|
145
|
-
it('', async ()=>{
|
|
146
|
-
const fakeServer = { sarver: true
|
|
145
|
+
it('run', async ()=>{
|
|
146
|
+
const fakeServer = { sarver: true, use(fn){
|
|
147
|
+
expect(typeof fn).to.be.equal('function');
|
|
148
|
+
}};
|
|
147
149
|
const fakeApp = {
|
|
148
150
|
report(){},
|
|
149
151
|
use(fn){
|