not-node 4.0.0 → 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/index.js +1 -0
- package/package.json +2 -1
- package/src/domain.js +24 -26
- package/src/env.js +31 -0
- package/src/init/app.js +7 -7
- package/src/init/db/index.js +5 -4
- package/src/init/db/ioredis.js +24 -0
- package/src/init/db/mongoose.js +1 -1
- package/src/init/db/redis.js +1 -1
- package/src/init/index.js +43 -23
- package/src/init/middleware.js +3 -1
- package/src/init/rateLimiter.js +1 -1
- package/src/init/routes.js +42 -18
- package/src/init/sessions/mongoose.js +1 -1
- package/src/init/sessions/redis.js +1 -1
- package/src/manifest/module.js +4 -1
- package/src/manifest/registrator/routes.js +2 -2
- package/src/manifest/route.js +30 -0
- package/src/model/proto.js +3 -4
- package/test/env.js +24 -0
- package/test/init/app.js +4 -1
- package/test/init/routes.js +6 -4
- package/test/init/sessions/redis.js +3 -0
- package/test/notDomain.js +0 -29
- package/test/notInit.js +3 -2
package/.eslintrc.json
CHANGED
package/index.js
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/domain.js
CHANGED
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
*
|
|
5
5
|
*/
|
|
6
6
|
const EventEmitter = require('events');
|
|
7
|
-
const {
|
|
7
|
+
const {isFunc,isAsync, objHas, firstLetterToUpper} = require('./common');
|
|
8
8
|
|
|
9
|
+
const Env = require('./env');
|
|
9
10
|
const
|
|
10
11
|
notModule = require('./manifest/module'),
|
|
11
12
|
path = require('path'),
|
|
@@ -250,7 +251,13 @@ class notDomain extends EventEmitter {
|
|
|
250
251
|
async execInModules(methodName) {
|
|
251
252
|
for (let mod of Object.values(this.modules)) {
|
|
252
253
|
try{
|
|
253
|
-
|
|
254
|
+
if(isFunc(mod.exec)){
|
|
255
|
+
if(isAsync(mod.exec)){
|
|
256
|
+
await mod.exec(methodName);
|
|
257
|
+
}else{
|
|
258
|
+
mod.exec(methodName);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
254
261
|
}catch(e){
|
|
255
262
|
this.report(e);
|
|
256
263
|
}
|
|
@@ -267,30 +274,6 @@ class notDomain extends EventEmitter {
|
|
|
267
274
|
}
|
|
268
275
|
}
|
|
269
276
|
|
|
270
|
-
/**
|
|
271
|
-
* Returns application environment variable
|
|
272
|
-
* @param {string} key name of var
|
|
273
|
-
* @return {object|undefined} value or undefined
|
|
274
|
-
*/
|
|
275
|
-
getEnv(key) {
|
|
276
|
-
if (objHas(this.envs,key)) {
|
|
277
|
-
return this.envs[key];
|
|
278
|
-
} else {
|
|
279
|
-
return undefined;
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
|
|
283
|
-
/**
|
|
284
|
-
* Setting application environment variable
|
|
285
|
-
* @param {string} key name of var
|
|
286
|
-
* @param {object} val value
|
|
287
|
-
* @return {notDomain} chainable
|
|
288
|
-
*/
|
|
289
|
-
setEnv(key, val) {
|
|
290
|
-
this.envs[key] = val;
|
|
291
|
-
return this;
|
|
292
|
-
}
|
|
293
|
-
|
|
294
277
|
/**
|
|
295
278
|
* logger
|
|
296
279
|
*/
|
|
@@ -443,6 +426,21 @@ class notDomain extends EventEmitter {
|
|
|
443
426
|
return stats;
|
|
444
427
|
}
|
|
445
428
|
|
|
429
|
+
getEnv(key) {
|
|
430
|
+
return Env.getEnv(key);
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* Setting application environment variable
|
|
435
|
+
* @param {string} key name of var
|
|
436
|
+
* @param {object} val value
|
|
437
|
+
* @return {notDomain} chainable
|
|
438
|
+
*/
|
|
439
|
+
static setEnv(key, val) {
|
|
440
|
+
Env.setEnv(key, val);
|
|
441
|
+
return this;
|
|
442
|
+
}
|
|
443
|
+
|
|
446
444
|
}
|
|
447
445
|
|
|
448
446
|
module.exports = notDomain;
|
package/src/env.js
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
const {objHas} = require('./common');
|
|
2
|
+
|
|
3
|
+
const ENVS = {
|
|
4
|
+
process: process.env.NODE_ENV || 'development'
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
module.exports = class notEnv {
|
|
8
|
+
/**
|
|
9
|
+
* Returns application environment variable
|
|
10
|
+
* @param {string} key name of var
|
|
11
|
+
* @return {object|undefined} value or undefined
|
|
12
|
+
*/
|
|
13
|
+
static getEnv(key) {
|
|
14
|
+
if (objHas(ENVS,key)) {
|
|
15
|
+
return ENVS[key];
|
|
16
|
+
} else {
|
|
17
|
+
return undefined;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Setting application environment variable
|
|
23
|
+
* @param {string} key name of var
|
|
24
|
+
* @param {object} val value
|
|
25
|
+
* @return {notDomain} chainable
|
|
26
|
+
*/
|
|
27
|
+
static setEnv(key, val) {
|
|
28
|
+
ENVS[key] = val;
|
|
29
|
+
return notEnv;
|
|
30
|
+
}
|
|
31
|
+
};
|
package/src/init/app.js
CHANGED
|
@@ -21,13 +21,13 @@ module.exports = class InitApp{
|
|
|
21
21
|
|
|
22
22
|
static async setAppEnvs({config, options, master}){
|
|
23
23
|
await emit('app.setEnv.pre', {config, options, master});
|
|
24
|
-
master.
|
|
25
|
-
master.
|
|
26
|
-
master.
|
|
27
|
-
master.
|
|
28
|
-
master.
|
|
29
|
-
master.
|
|
30
|
-
master.
|
|
24
|
+
master.setEnv('hostname', config.get('hostname'));
|
|
25
|
+
master.setEnv('server', `https://` + config.get('host'));
|
|
26
|
+
master.setEnv('appPath', config.get('appPath'));
|
|
27
|
+
master.setEnv('name', master.getManifest().name);
|
|
28
|
+
master.setEnv('fullServerName', config.get('fullServerName'));
|
|
29
|
+
master.setEnv('dbDumpsPath', config.get('dbDumpsPath'));
|
|
30
|
+
master.setEnv('rolesPriority', master.getManifest().targets.server.roles);
|
|
31
31
|
master.getApp().ENV = ENV;
|
|
32
32
|
await emit('app.setEnv.post', {config, options, master});
|
|
33
33
|
}
|
package/src/init/db/index.js
CHANGED
|
@@ -6,6 +6,7 @@ module.exports = class InitDB{
|
|
|
6
6
|
static default = path.resolve(__dirname, './mongoose.js');
|
|
7
7
|
|
|
8
8
|
static drivers = {
|
|
9
|
+
'ioredis': path.resolve(__dirname, './ioredis.js'),
|
|
9
10
|
'redis': path.resolve(__dirname, './redis.js'),
|
|
10
11
|
'mongoose': path.resolve(__dirname, './mongoose.js')
|
|
11
12
|
};
|
|
@@ -28,9 +29,9 @@ module.exports = class InitDB{
|
|
|
28
29
|
* [db_driver_name_1]: options,
|
|
29
30
|
* [db_driver_name_2]: options,
|
|
30
31
|
* }
|
|
31
|
-
* to get driver require('not-node').
|
|
32
|
-
* require('not-node').
|
|
33
|
-
* require('not-node').
|
|
32
|
+
* to get driver require('not-node').getEnv(db_driver_name_2)
|
|
33
|
+
* require('not-node').getEnv('mongoose')
|
|
34
|
+
* require('not-node').getEnv('redis')
|
|
34
35
|
**/
|
|
35
36
|
async run({master, config, options}) {
|
|
36
37
|
const conf = config.get('db');
|
|
@@ -40,7 +41,7 @@ module.exports = class InitDB{
|
|
|
40
41
|
master,
|
|
41
42
|
config,
|
|
42
43
|
options,
|
|
43
|
-
conf,
|
|
44
|
+
conf: conf[driver],
|
|
44
45
|
alias: driver
|
|
45
46
|
});
|
|
46
47
|
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const log = require('not-log')(module, 'not-node//init');
|
|
2
|
+
const ADDS = require('../additional');
|
|
3
|
+
|
|
4
|
+
module.exports = class InitDBRedisIO{
|
|
5
|
+
|
|
6
|
+
static async initRedis({ conf, master, alias}) {
|
|
7
|
+
log.info('Setting up ioredis connection...');
|
|
8
|
+
const Redis = require('ioredis');
|
|
9
|
+
const redisClient = new Redis(conf);
|
|
10
|
+
redisClient.on('error', log.error);
|
|
11
|
+
master.setEnv(`db.${alias}`, redisClient);
|
|
12
|
+
log.log('redis client');
|
|
13
|
+
log.log(Object.keys(redisClient));
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async run({config, options, master, conf, alias}){
|
|
17
|
+
await ADDS.run(`db.${alias}.pre`, {config, options, master, conf, alias});
|
|
18
|
+
await InitDBRedisIO.initRedis({conf, master, alias});
|
|
19
|
+
await ADDS.run(`db.${alias}.post`, {config, options, master, conf, alias});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
};
|
package/src/init/db/mongoose.js
CHANGED
|
@@ -24,7 +24,7 @@ module.exports = class InitDBMongoose{
|
|
|
24
24
|
Increment.init(mongoose);
|
|
25
25
|
}
|
|
26
26
|
master.setMongoose(mongoose);
|
|
27
|
-
master.
|
|
27
|
+
master.setEnv(`db.${alias}`, mongoose);
|
|
28
28
|
}
|
|
29
29
|
|
|
30
30
|
async run({config, options, master, conf, alias}){
|
package/src/init/db/redis.js
CHANGED
|
@@ -9,7 +9,7 @@ module.exports = class InitDBRedis{
|
|
|
9
9
|
const redis = require('redis');
|
|
10
10
|
const redisClient = redis.createClient(conf);
|
|
11
11
|
InitDBRedis.bindClientEvents({master, redisClient});
|
|
12
|
-
master.
|
|
12
|
+
master.setEnv(`db.${alias}`, redisClient);
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
async run({config, options, master, conf, alias}){
|
package/src/init/index.js
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
const path = require('path');
|
|
3
3
|
const logger = require('not-log');
|
|
4
4
|
const log = logger(module, 'not-node:Init');
|
|
5
|
+
|
|
6
|
+
const Env = require('../env');
|
|
5
7
|
//
|
|
6
8
|
const ADDS = require('./additional');
|
|
7
9
|
//
|
|
@@ -9,21 +11,21 @@ const InitSequence = require('./sequence.js');
|
|
|
9
11
|
const STANDART_INIT_SEQUENCE = require('./sequence.standart.js');
|
|
10
12
|
|
|
11
13
|
/**
|
|
12
|
-
* @example <caption>Application initialization</caption>
|
|
13
|
-
* let App = new notApp({
|
|
14
|
-
* mongoose: mongooseLink
|
|
15
|
-
* modulesCollectionPaths: [__dirname + '/modules'], //each path to folder with modules
|
|
16
|
-
* modulesPaths: [], //each path to module
|
|
17
|
-
* modules: {
|
|
18
|
-
* filestore: require('not-filestore') //each npm not-* module with custom overriden name as key
|
|
19
|
-
* }
|
|
20
|
-
* })
|
|
21
|
-
* .importModuleFrom(__dirname+'/anotherModule', 'anotherCustomModuleName') //import module from path
|
|
22
|
-
* .importModulesFrom(__dirname+'/directoryOfUsefullessModules')
|
|
23
|
-
* .importModule(require('notModule'), 'notModule')
|
|
24
|
-
* .expose(ExpressApp);
|
|
25
|
-
**/
|
|
26
|
-
class Init{
|
|
14
|
+
* @example <caption>Application initialization</caption>
|
|
15
|
+
* let App = new notApp({
|
|
16
|
+
* mongoose: mongooseLink
|
|
17
|
+
* modulesCollectionPaths: [__dirname + '/modules'], //each path to folder with modules
|
|
18
|
+
* modulesPaths: [], //each path to module
|
|
19
|
+
* modules: {
|
|
20
|
+
* filestore: require('not-filestore') //each npm not-* module with custom overriden name as key
|
|
21
|
+
* }
|
|
22
|
+
* })
|
|
23
|
+
* .importModuleFrom(__dirname+'/anotherModule', 'anotherCustomModuleName') //import module from path
|
|
24
|
+
* .importModulesFrom(__dirname+'/directoryOfUsefullessModules')
|
|
25
|
+
* .importModule(require('notModule'), 'notModule')
|
|
26
|
+
* .expose(ExpressApp);
|
|
27
|
+
**/
|
|
28
|
+
class Init {
|
|
27
29
|
static options = false;
|
|
28
30
|
static manifest = false;
|
|
29
31
|
static config = false;
|
|
@@ -34,14 +36,19 @@ class Init{
|
|
|
34
36
|
static WSServer = false;
|
|
35
37
|
static WSClient = false;
|
|
36
38
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
+
|
|
40
|
+
static getAbsolutePath(subPath) {
|
|
41
|
+
return path.resolve(Init.options.pathToApp, subPath);
|
|
39
42
|
}
|
|
40
43
|
|
|
41
44
|
static setManifest(manifest) {
|
|
42
45
|
Init.manifest = manifest;
|
|
43
46
|
}
|
|
44
47
|
|
|
48
|
+
static getManifest() {
|
|
49
|
+
return Init.manifest;
|
|
50
|
+
}
|
|
51
|
+
|
|
45
52
|
static setMongoose(val) {
|
|
46
53
|
Init.mongoose = val;
|
|
47
54
|
}
|
|
@@ -50,29 +57,37 @@ class Init{
|
|
|
50
57
|
return Init.mongoose;
|
|
51
58
|
}
|
|
52
59
|
|
|
53
|
-
static setServer(val){
|
|
60
|
+
static setServer(val) {
|
|
54
61
|
Init.server = val;
|
|
55
62
|
return Init;
|
|
56
63
|
}
|
|
57
64
|
|
|
58
|
-
static getServer(){
|
|
65
|
+
static getServer() {
|
|
59
66
|
return Init.server;
|
|
60
67
|
}
|
|
61
68
|
|
|
62
|
-
static setHTTPServer(val){
|
|
69
|
+
static setHTTPServer(val) {
|
|
63
70
|
Init.httpServer = val;
|
|
64
71
|
return Init;
|
|
65
72
|
}
|
|
66
73
|
|
|
67
|
-
static getHTTPServer(){
|
|
74
|
+
static getHTTPServer() {
|
|
68
75
|
return Init.httpServer;
|
|
69
76
|
}
|
|
70
77
|
|
|
71
|
-
static
|
|
78
|
+
static setEnv(key, val) {
|
|
79
|
+
Env.setEnv(key, val);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
static getEnv(key) {
|
|
83
|
+
return Env.getEnv(key);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
static getApp() {
|
|
72
87
|
return Init.notApp;
|
|
73
88
|
}
|
|
74
89
|
|
|
75
|
-
static setApp(val){
|
|
90
|
+
static setApp(val) {
|
|
76
91
|
Init.notApp = val;
|
|
77
92
|
return Init;
|
|
78
93
|
}
|
|
@@ -134,6 +149,11 @@ class Init{
|
|
|
134
149
|
};
|
|
135
150
|
//running all prepared initalizers with current context
|
|
136
151
|
await initSequence.run(context);
|
|
152
|
+
await ADDS.run('post', {
|
|
153
|
+
config,
|
|
154
|
+
options,
|
|
155
|
+
manifest
|
|
156
|
+
});
|
|
137
157
|
log.info('Application initalization finished');
|
|
138
158
|
} catch (e) {
|
|
139
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/rateLimiter.js
CHANGED
|
@@ -27,7 +27,7 @@ module.exports = class InitRateLimiter{
|
|
|
27
27
|
static createRateLimiter({master}){
|
|
28
28
|
const {RateLimiterRedis} = require('rate-limiter-flexible');
|
|
29
29
|
return new RateLimiterRedis({
|
|
30
|
-
storeClient: master.
|
|
30
|
+
storeClient: master.getEnv('db.redis'),
|
|
31
31
|
keyPrefix: 'middleware',
|
|
32
32
|
points: 10, // 10 requests
|
|
33
33
|
duration: 1, // per 1 second by IP
|
package/src/init/routes.js
CHANGED
|
@@ -2,34 +2,52 @@
|
|
|
2
2
|
|
|
3
3
|
const serveStatic = require('serve-static');
|
|
4
4
|
const log = require('not-log')(module, 'not-node//init');
|
|
5
|
-
const {
|
|
5
|
+
const {
|
|
6
|
+
notError,
|
|
7
|
+
notValidationError,
|
|
8
|
+
notRequestError
|
|
9
|
+
} = require('not-error');
|
|
6
10
|
|
|
7
11
|
module.exports = class InitRoutes {
|
|
8
12
|
|
|
9
|
-
static finalError({
|
|
10
|
-
|
|
11
|
-
|
|
13
|
+
static finalError({
|
|
14
|
+
master
|
|
15
|
+
}) {
|
|
16
|
+
// eslint-disable-next-line no-unused-vars
|
|
17
|
+
return (err, req, res, next) => {
|
|
18
|
+
//reportable errors from known cases
|
|
19
|
+
if (err instanceof notError) {
|
|
12
20
|
master.getApp().report(err);
|
|
13
|
-
if
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
res.status(err.getCode()).json({
|
|
21
|
+
//if request params - ok, but result is not
|
|
22
|
+
if (err instanceof notRequestError) {
|
|
23
|
+
if (err.getRedirect()) {
|
|
24
|
+
return res.redirect(err.getRedirect());
|
|
25
|
+
} else {
|
|
26
|
+
return res.status(err.getCode()).json({
|
|
19
27
|
status: 'error',
|
|
20
|
-
|
|
28
|
+
message: err.getResult().message,
|
|
21
29
|
errors: err.getResult().errors
|
|
22
30
|
});
|
|
23
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
|
+
});
|
|
24
39
|
}
|
|
25
|
-
}
|
|
40
|
+
}
|
|
41
|
+
//other cases
|
|
42
|
+
if (err instanceof Error && (res && res.status && res.json)) {
|
|
26
43
|
res.status(err.statusCode || 500);
|
|
44
|
+
//reporting as unknown
|
|
27
45
|
master.getApp().report(new notError(`Internal error(${res.statusCode}): %${req.url} - %${err.message}`, {}, err));
|
|
28
46
|
res.json({
|
|
29
47
|
status: 'error',
|
|
30
|
-
|
|
48
|
+
message: err.message
|
|
31
49
|
});
|
|
32
|
-
}else{
|
|
50
|
+
} else {
|
|
33
51
|
log.error('Unknown error:', err);
|
|
34
52
|
res.status(500).json({
|
|
35
53
|
status: 'error'
|
|
@@ -38,13 +56,19 @@ module.exports = class InitRoutes {
|
|
|
38
56
|
};
|
|
39
57
|
}
|
|
40
58
|
|
|
41
|
-
async run({
|
|
59
|
+
async run({
|
|
60
|
+
master,
|
|
61
|
+
config,
|
|
62
|
+
options
|
|
63
|
+
}) {
|
|
42
64
|
log.info('Setting up routes...');
|
|
43
65
|
master.getApp().expose(master.getServer());
|
|
44
66
|
require(options.routesPath)(master.getServer(), master.getApp());
|
|
45
|
-
master.
|
|
46
|
-
master.
|
|
47
|
-
master.
|
|
67
|
+
master.getServer().use(serveStatic(config.get('staticPath')));
|
|
68
|
+
master.getServer().use(options.indexRoute);
|
|
69
|
+
master.getServer().use(InitRoutes.finalError({
|
|
70
|
+
master
|
|
71
|
+
}));
|
|
48
72
|
}
|
|
49
73
|
|
|
50
74
|
};
|
|
@@ -5,7 +5,7 @@ const ADDS = require('../additional');
|
|
|
5
5
|
module.exports = class InitSessionsMongo{
|
|
6
6
|
static createStore({config, master, expressSession}){
|
|
7
7
|
const MongoDBStore = require('connect-mongodb-session')(expressSession);
|
|
8
|
-
const mongooseOptions = config.get('mongoose.options');
|
|
8
|
+
const mongooseOptions = config.get('db.mongoose.options');
|
|
9
9
|
let store = new MongoDBStore({
|
|
10
10
|
uri: `mongodb://${mongooseOptions.user}:${mongooseOptions.pass}@${mongooseOptions.host}/${mongooseOptions.db}`,
|
|
11
11
|
databaseName: mongooseOptions.db,
|
|
@@ -6,7 +6,7 @@ module.exports = class InitSessionsRedis{
|
|
|
6
6
|
log.info('Setting up user sessions handler(redis)...');
|
|
7
7
|
await ADDS.run('sessions.pre', {config, options, master});
|
|
8
8
|
const expressSession = require('express-session');
|
|
9
|
-
const redisClient = master.
|
|
9
|
+
const redisClient = master.getEnv('db.redis');
|
|
10
10
|
const redisStore = require('connect-redis')(expressSession);
|
|
11
11
|
master.getServer().use(expressSession({
|
|
12
12
|
secret: config.get('session:secret'),
|
package/src/manifest/module.js
CHANGED
|
@@ -164,7 +164,6 @@ class notModule {
|
|
|
164
164
|
}
|
|
165
165
|
}
|
|
166
166
|
|
|
167
|
-
|
|
168
167
|
getModelSchema(modelName) {
|
|
169
168
|
let modelFile = this.getModelFile(modelName);
|
|
170
169
|
if (modelFile && objHas(modelFile, modelName) && modelFile.thisSchema) {
|
|
@@ -285,6 +284,10 @@ class notModule {
|
|
|
285
284
|
this.routes[key] = val;
|
|
286
285
|
}
|
|
287
286
|
|
|
287
|
+
setLogic(key, val){
|
|
288
|
+
this.logics[key] = val;
|
|
289
|
+
}
|
|
290
|
+
|
|
288
291
|
getRoute(routeName) {
|
|
289
292
|
if (this.routes && objHas(this.routes, routeName)) {
|
|
290
293
|
return this.routes[routeName];
|
|
@@ -167,7 +167,7 @@ module.exports = class notModuleRegistratorRoutes{
|
|
|
167
167
|
});
|
|
168
168
|
}
|
|
169
169
|
|
|
170
|
-
static registerManifest({nModule,
|
|
171
|
-
nModule.setManifest(routeName,
|
|
170
|
+
static registerManifest({nModule, routeManifest, routeName}) {
|
|
171
|
+
nModule.setManifest(routeName, routeManifest);
|
|
172
172
|
}
|
|
173
173
|
};
|
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/src/model/proto.js
CHANGED
|
@@ -54,10 +54,9 @@ module.exports = class ModelFabricate{
|
|
|
54
54
|
if (targetModule.thisStatics) {
|
|
55
55
|
Object.assign(schema.statics, targetModule.thisStatics);
|
|
56
56
|
}
|
|
57
|
-
this.extendSchemaFrom(targetModule.thisVirtuals, schema.virtual);
|
|
58
|
-
this.extendSchemaFrom(targetModule.thisPre, schema.pre);
|
|
59
|
-
this.extendSchemaFrom(targetModule.thisPost, schema.post);
|
|
60
|
-
|
|
57
|
+
this.extendSchemaFrom(targetModule.thisVirtuals, schema.virtual.bind(schema));
|
|
58
|
+
this.extendSchemaFrom(targetModule.thisPre, schema.pre.bind(schema));
|
|
59
|
+
this.extendSchemaFrom(targetModule.thisPost, schema.post.bind(schema));
|
|
61
60
|
}
|
|
62
61
|
|
|
63
62
|
static enrichByFields(targetModule){
|
package/test/env.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const expect = require('chai').expect,
|
|
2
|
+
Env = require('../src/env');
|
|
3
|
+
|
|
4
|
+
describe('Env', function() {
|
|
5
|
+
describe('getEnv', function() {
|
|
6
|
+
it('key exists', function() {
|
|
7
|
+
const res = Env.getEnv('process');
|
|
8
|
+
expect(res).to.be.equal('development');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('key not exists', function() {
|
|
12
|
+
const res = Env.getEnv('key' + Math.random().toString());
|
|
13
|
+
expect(res).to.be.undefined;
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
describe('setEnv', function() {
|
|
18
|
+
it('set', function() {
|
|
19
|
+
const res = Env.setEnv('key', 'happy');
|
|
20
|
+
expect(res).to.be.deep.equal(Env);
|
|
21
|
+
expect(res.getEnv('key')).to.be.deep.equal('happy');
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
});
|
package/test/init/app.js
CHANGED
|
@@ -106,6 +106,9 @@ module.exports = ({expect})=>{
|
|
|
106
106
|
},
|
|
107
107
|
getManifest(){
|
|
108
108
|
return fakeManifest;
|
|
109
|
+
},
|
|
110
|
+
setEnv(){
|
|
111
|
+
|
|
109
112
|
}
|
|
110
113
|
};
|
|
111
114
|
|
|
@@ -332,7 +335,6 @@ module.exports = ({expect})=>{
|
|
|
332
335
|
};
|
|
333
336
|
const options = {fake: 'options'};
|
|
334
337
|
const fakeApp = {
|
|
335
|
-
|
|
336
338
|
setEnv(){},
|
|
337
339
|
importModulesFrom(modsPath){
|
|
338
340
|
expect(modsPath).to.be.equal('modulesPath_fake');
|
|
@@ -342,6 +344,7 @@ module.exports = ({expect})=>{
|
|
|
342
344
|
}
|
|
343
345
|
};
|
|
344
346
|
const master = {
|
|
347
|
+
setEnv(){},
|
|
345
348
|
setApp(app){expect(app).to.be.instanceof(FakeApp);},
|
|
346
349
|
throwError(e){
|
|
347
350
|
throw new Error(e)
|
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){
|
package/test/notDomain.js
CHANGED
|
@@ -507,36 +507,7 @@ describe('notDomain', function() {
|
|
|
507
507
|
});
|
|
508
508
|
|
|
509
509
|
|
|
510
|
-
describe('getEnv', function() {
|
|
511
|
-
it('key exists', function() {
|
|
512
|
-
const ctx = {
|
|
513
|
-
envs:{
|
|
514
|
-
key: 'happy'
|
|
515
|
-
}
|
|
516
|
-
};
|
|
517
|
-
const res = notDomain.prototype.getEnv.call(ctx, 'key');
|
|
518
|
-
expect(res).to.be.equal('happy');
|
|
519
|
-
});
|
|
520
|
-
it('key not exists', function() {
|
|
521
|
-
const ctx = {
|
|
522
|
-
envs:{
|
|
523
|
-
}
|
|
524
|
-
};
|
|
525
|
-
const res = notDomain.prototype.getEnv.call(ctx, 'key');
|
|
526
|
-
expect(res).to.be.undefined;
|
|
527
|
-
});
|
|
528
|
-
});
|
|
529
510
|
|
|
530
|
-
describe('setEnv', function() {
|
|
531
|
-
it('set', function() {
|
|
532
|
-
const ctx = {
|
|
533
|
-
envs:{}
|
|
534
|
-
};
|
|
535
|
-
const res = notDomain.prototype.setEnv.call(ctx, 'key', 'happy');
|
|
536
|
-
expect(res).to.be.deep.equal(ctx);
|
|
537
|
-
expect(res.envs.key).to.be.deep.equal('happy');
|
|
538
|
-
});
|
|
539
|
-
});
|
|
540
511
|
|
|
541
512
|
|
|
542
513
|
describe('logger', function() {
|
package/test/notInit.js
CHANGED
|
@@ -7,9 +7,10 @@ describe('initialization', function() {
|
|
|
7
7
|
describe('notInit', ()=>{
|
|
8
8
|
describe('getAbsolutePath', ()=>{
|
|
9
9
|
it('all set', ()=>{
|
|
10
|
-
|
|
10
|
+
Init.options = {
|
|
11
11
|
pathToApp: '/home/admin/server/test'
|
|
12
|
-
}
|
|
12
|
+
};
|
|
13
|
+
const res = Init.getAbsolutePath('../src/index', );
|
|
13
14
|
expect(res).to.be.equal('/home/admin/server/src/index');
|
|
14
15
|
});
|
|
15
16
|
});
|