express-ext 0.1.12 → 0.1.16
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/lib/GenericSearchController.js +1 -1
- package/lib/HealthController.js +4 -4
- package/lib/LoadSearchController.js +1 -1
- package/lib/LogController.js +67 -28
- package/lib/LowCodeController.js +1 -1
- package/lib/SearchController.js +1 -1
- package/lib/health.js +2 -2
- package/lib/index.js +2 -1
- package/lib/log.js +212 -0
- package/lib/resources.js +54 -2
- package/lib/search.js +39 -13
- package/package.json +1 -1
- package/src/GenericController.ts +4 -4
- package/src/GenericSearchController.ts +5 -4
- package/src/HealthController.ts +5 -5
- package/src/LoadController.ts +2 -2
- package/src/LoadSearchController.ts +5 -4
- package/src/LogController.ts +97 -31
- package/src/LowCodeController.ts +5 -4
- package/src/SearchController.ts +5 -4
- package/src/health.ts +1 -1
- package/src/http.ts +2 -0
- package/src/index.ts +19 -0
- package/src/log.ts +217 -0
- package/src/metadata.ts +6 -4
- package/src/resources.ts +54 -1
- package/src/search.ts +36 -8
|
@@ -41,7 +41,7 @@ var GenericSearchController = (function (_super) {
|
|
|
41
41
|
}
|
|
42
42
|
GenericSearchController.prototype.search = function (req, res) {
|
|
43
43
|
var _this = this;
|
|
44
|
-
var s = search_1.fromRequest(req, this.fields, this.excluding);
|
|
44
|
+
var s = search_1.fromRequest(req, search_1.buildArray(this.array, this.fields, this.excluding));
|
|
45
45
|
var l = search_1.getParameters(s, this.config);
|
|
46
46
|
var s2 = search_1.format(s, this.dates, this.numbers);
|
|
47
47
|
this.find(s2, l.limit, l.skipOrRefId, l.fields)
|
package/lib/HealthController.js
CHANGED
|
@@ -7,12 +7,12 @@ var HealthController = (function () {
|
|
|
7
7
|
this.check = this.check.bind(this);
|
|
8
8
|
}
|
|
9
9
|
HealthController.prototype.check = function (req, res) {
|
|
10
|
-
health_1.
|
|
11
|
-
if (
|
|
12
|
-
return res.status(200).json(
|
|
10
|
+
health_1.health(this.checkers).then(function (r) {
|
|
11
|
+
if (r.status === 'UP') {
|
|
12
|
+
return res.status(200).json(r).end();
|
|
13
13
|
}
|
|
14
14
|
else {
|
|
15
|
-
return res.status(500).json(
|
|
15
|
+
return res.status(500).json(r).end();
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
18
|
};
|
|
@@ -48,7 +48,7 @@ var LoadSearchController = (function (_super) {
|
|
|
48
48
|
}
|
|
49
49
|
LoadSearchController.prototype.search = function (req, res) {
|
|
50
50
|
var _this = this;
|
|
51
|
-
var s = search_1.fromRequest(req, this.fields, this.excluding);
|
|
51
|
+
var s = search_1.fromRequest(req, search_1.buildArray(this.array, this.fields, this.excluding));
|
|
52
52
|
var l = search_1.getParameters(s, this.config);
|
|
53
53
|
var s2 = search_1.format(s, this.dates, this.numbers);
|
|
54
54
|
this.find(s2, l.limit, l.skipOrRefId, l.fields)
|
package/lib/LogController.js
CHANGED
|
@@ -1,9 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.map = {
|
|
4
|
+
TRACE: -2,
|
|
5
|
+
DEBUG: -1,
|
|
6
|
+
INFO: 0,
|
|
7
|
+
WARN: 1,
|
|
8
|
+
ERROR: 2,
|
|
9
|
+
PANIC: 3,
|
|
10
|
+
FATAL: 4
|
|
11
|
+
};
|
|
3
12
|
var LogController = (function () {
|
|
4
|
-
function LogController(logger, mp) {
|
|
13
|
+
function LogController(logger, updateL, mp) {
|
|
5
14
|
this.logger = logger;
|
|
6
|
-
this.map = mp;
|
|
15
|
+
this.map = (mp ? mp : exports.map);
|
|
16
|
+
this.update = updateL ? updateL : updateLog;
|
|
7
17
|
this.config = this.config.bind(this);
|
|
8
18
|
}
|
|
9
19
|
LogController.prototype.config = function (req, res) {
|
|
@@ -11,41 +21,70 @@ var LogController = (function () {
|
|
|
11
21
|
if (!obj || obj === '') {
|
|
12
22
|
return res.status(400).end('The request body cannot be empty');
|
|
13
23
|
}
|
|
14
|
-
if (!this.logger
|
|
24
|
+
if (!this.logger) {
|
|
15
25
|
return res.status(503).end('Logger is not available');
|
|
16
26
|
}
|
|
17
|
-
if (
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
var changed = false;
|
|
21
|
-
if (obj.level && typeof obj.level === 'string' && obj.level.length > 0) {
|
|
22
|
-
var lv = this.map[obj.level.toUpperCase()];
|
|
23
|
-
if (lv !== undefined) {
|
|
24
|
-
this.logger.level = lv;
|
|
25
|
-
changed = true;
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
if (obj.map) {
|
|
29
|
-
if (obj.map.level && typeof obj.map.level === 'string' && obj.map.level.length > 0) {
|
|
30
|
-
this.logger.map.level = obj.map.level;
|
|
31
|
-
changed = true;
|
|
32
|
-
}
|
|
33
|
-
if (obj.map.time && typeof obj.map.time === 'string' && obj.map.time.length > 0) {
|
|
34
|
-
this.logger.map.time = obj.map.time;
|
|
35
|
-
changed = true;
|
|
36
|
-
}
|
|
37
|
-
if (obj.map.msg && typeof obj.map.msg === 'string' && obj.map.msg.length > 0) {
|
|
38
|
-
this.logger.map.msg = obj.map.msg;
|
|
39
|
-
changed = true;
|
|
27
|
+
if (typeof obj.level === 'string' && obj.level.length > 0) {
|
|
28
|
+
if (!this.map) {
|
|
29
|
+
return res.status(503).end('Map is not available');
|
|
40
30
|
}
|
|
41
31
|
}
|
|
32
|
+
var changed = this.update(this.logger, obj, this.map);
|
|
42
33
|
if (changed) {
|
|
43
|
-
return res.status(200).
|
|
34
|
+
return res.status(200).json(true).end();
|
|
44
35
|
}
|
|
45
36
|
else {
|
|
46
|
-
return res.status(204).
|
|
37
|
+
return res.status(204).json(false).end();
|
|
47
38
|
}
|
|
48
39
|
};
|
|
49
40
|
return LogController;
|
|
50
41
|
}());
|
|
51
42
|
exports.LogController = LogController;
|
|
43
|
+
function updateLog(logger, obj, mp) {
|
|
44
|
+
var changed = false;
|
|
45
|
+
if (typeof obj.level === 'string' && obj.level.length > 0) {
|
|
46
|
+
var lv = mp[obj.level.toUpperCase()];
|
|
47
|
+
if (lv !== undefined) {
|
|
48
|
+
logger.level = lv;
|
|
49
|
+
changed = true;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
if (obj.map) {
|
|
53
|
+
if (typeof obj.map.level === 'string' && obj.map.level.length > 0) {
|
|
54
|
+
logger.map.level = obj.map.level;
|
|
55
|
+
changed = true;
|
|
56
|
+
}
|
|
57
|
+
if (typeof obj.map.time === 'string' && obj.map.time.length > 0) {
|
|
58
|
+
logger.map.time = obj.map.time;
|
|
59
|
+
changed = true;
|
|
60
|
+
}
|
|
61
|
+
if (typeof obj.map.msg === 'string' && obj.map.msg.length > 0) {
|
|
62
|
+
logger.map.msg = obj.map.msg;
|
|
63
|
+
changed = true;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
if (obj.constants !== undefined && typeof obj.constants === 'object') {
|
|
67
|
+
var ks = Object.keys(obj.constants);
|
|
68
|
+
if (ks.length > 0) {
|
|
69
|
+
logger.constants = obj.constants;
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
logger.constants = undefined;
|
|
73
|
+
}
|
|
74
|
+
changed = true;
|
|
75
|
+
}
|
|
76
|
+
if (obj.name) {
|
|
77
|
+
if (typeof obj.name.trace === 'string'
|
|
78
|
+
&& typeof obj.name.debug === 'string'
|
|
79
|
+
&& typeof obj.name.info === 'string'
|
|
80
|
+
&& typeof obj.name.warn === 'string'
|
|
81
|
+
&& typeof obj.name.error === 'string'
|
|
82
|
+
&& typeof obj.name.panic === 'string'
|
|
83
|
+
&& typeof obj.name.fatal === 'string') {
|
|
84
|
+
logger.name = obj.name;
|
|
85
|
+
changed = true;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return changed;
|
|
89
|
+
}
|
|
90
|
+
exports.updateLog = updateLog;
|
package/lib/LowCodeController.js
CHANGED
|
@@ -41,7 +41,7 @@ var LowCodeController = (function (_super) {
|
|
|
41
41
|
}
|
|
42
42
|
LowCodeController.prototype.search = function (req, res) {
|
|
43
43
|
var _this = this;
|
|
44
|
-
var s = search_1.fromRequest(req, this.fields, this.excluding);
|
|
44
|
+
var s = search_1.fromRequest(req, search_1.buildArray(this.array, this.fields, this.excluding));
|
|
45
45
|
var l = search_1.getParameters(s, this.config);
|
|
46
46
|
var s2 = search_1.format(s, this.dates, this.numbers);
|
|
47
47
|
this.lowCodeService.search(s2, l.limit, l.skipOrRefId, l.fields)
|
package/lib/SearchController.js
CHANGED
|
@@ -28,7 +28,7 @@ var SearchController = (function () {
|
|
|
28
28
|
}
|
|
29
29
|
SearchController.prototype.search = function (req, res) {
|
|
30
30
|
var _this = this;
|
|
31
|
-
var s = search_1.fromRequest(req, this.fields, this.excluding);
|
|
31
|
+
var s = search_1.fromRequest(req, search_1.buildArray(this.array, this.fields, this.excluding));
|
|
32
32
|
var l = search_1.getParameters(s, this.config);
|
|
33
33
|
var s2 = search_1.format(s, this.dates, this.numbers);
|
|
34
34
|
this.find(s2, l.limit, l.skipOrRefId, l.fields)
|
package/lib/health.js
CHANGED
|
@@ -36,7 +36,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
function
|
|
39
|
+
function health(checkers) {
|
|
40
40
|
return __awaiter(this, void 0, void 0, function () {
|
|
41
41
|
var p, total, count, _i, checkers_1, checker, sub, r, err_1;
|
|
42
42
|
return __generator(this, function (_a) {
|
|
@@ -90,4 +90,4 @@ function check(checkers) {
|
|
|
90
90
|
});
|
|
91
91
|
});
|
|
92
92
|
}
|
|
93
|
-
exports.
|
|
93
|
+
exports.health = health;
|
package/lib/index.js
CHANGED
|
@@ -1,2 +1,3 @@
|
|
|
1
1
|
"use strict";function __export(m){for(var p in m)if(!exports.hasOwnProperty(p))exports[p]=m[p]}
|
|
2
|
-
Object.defineProperty(exports,"__esModule",{value:!0});var GenericController_1=require("./GenericController");exports.GenericHandler=GenericController_1.GenericController;var GenericSearchController_1=require("./GenericSearchController");exports.GenericSearchHandler=GenericSearchController_1.GenericSearchController;var HealthController_1=require("./HealthController");exports.HealthHandler=HealthController_1.HealthController;var LoadController_1=require("./LoadController");exports.LoadHandler=LoadController_1.LoadController;exports.ViewHandler=LoadController_1.LoadController;exports.ViewController=LoadController_1.LoadController;var LoadSearchController_1=require("./LoadSearchController");exports.LoadSearchHandler=LoadSearchController_1.LoadSearchController;var LowCodeController_1=require("./LowCodeController");exports.LowCodeHandler=LowCodeController_1.LowCodeController;exports.Handler=LowCodeController_1.LowCodeController;exports.Controller=LowCodeController_1.LowCodeController;var SearchController_1=require("./SearchController");exports.SearchHandler=SearchController_1.SearchController;
|
|
2
|
+
Object.defineProperty(exports,"__esModule",{value:!0});var GenericController_1=require("./GenericController");exports.GenericHandler=GenericController_1.GenericController;var GenericSearchController_1=require("./GenericSearchController");exports.GenericSearchHandler=GenericSearchController_1.GenericSearchController;var HealthController_1=require("./HealthController");exports.HealthHandler=HealthController_1.HealthController;var LoadController_1=require("./LoadController");exports.LoadHandler=LoadController_1.LoadController;exports.ViewHandler=LoadController_1.LoadController;exports.ViewController=LoadController_1.LoadController;var LoadSearchController_1=require("./LoadSearchController");exports.LoadSearchHandler=LoadSearchController_1.LoadSearchController;var LogController_1=require("./LogController");exports.LogHandler=LogController_1.LogController;var LowCodeController_1=require("./LowCodeController");exports.LowCodeHandler=LowCodeController_1.LowCodeController;exports.Handler=LowCodeController_1.LowCodeController;exports.Controller=LowCodeController_1.LowCodeController;var SearchController_1=require("./SearchController");exports.SearchHandler=SearchController_1.SearchController;__export(require("./health"));__export(require("./HealthController"));__export(require("./LogController"));__export(require("./log"));__export(require("./http"));__export(require("./view"));__export(require("./LoadController"));__export(require("./search_func"));__export(require("./search"));__export(require("./SearchController"));__export(require("./LoadSearchController"));__export(require("./resources"));__export(require("./edit"));__export(require("./GenericController"));__export(require("./GenericSearchController"));__export(require("./LowCodeController"));function allow(access){return function(req,res,next){res.header('Access-Control-Allow-Origin',access.origin);res.header('Access-Control-Allow-Credentials',access.credentials);res.header('Access-Control-Allow-Methods',access.methods);res.setHeader('Access-Control-Allow-Headers',access.headers);next()}}
|
|
3
|
+
exports.allow=allow
|
package/lib/log.js
ADDED
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var stream_1 = require("stream");
|
|
4
|
+
function createConfig(c) {
|
|
5
|
+
if (!c) {
|
|
6
|
+
return { skips: [], duration: 'duration', request: '', response: '', status: '', size: '' };
|
|
7
|
+
}
|
|
8
|
+
var l = {
|
|
9
|
+
log: c.log,
|
|
10
|
+
separate: c.separate,
|
|
11
|
+
skips: c.skips ? c.skips.split(',') : [],
|
|
12
|
+
duration: c.duration ? c.duration : 'duration',
|
|
13
|
+
request: c.request ? c.request : '',
|
|
14
|
+
response: c.response ? c.response : '',
|
|
15
|
+
status: c.status ? c.status : '',
|
|
16
|
+
size: c.size ? c.size : ''
|
|
17
|
+
};
|
|
18
|
+
return l;
|
|
19
|
+
}
|
|
20
|
+
exports.createConfig = createConfig;
|
|
21
|
+
function skip(skips, url) {
|
|
22
|
+
if (skips.length === 0) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
var u = removeUrlParams(url);
|
|
26
|
+
for (var _i = 0, skips_1 = skips; _i < skips_1.length; _i++) {
|
|
27
|
+
var s = skips_1[_i];
|
|
28
|
+
if (u.endsWith(s)) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
exports.skip = skip;
|
|
35
|
+
function removeUrlParams(url) {
|
|
36
|
+
var startParams = url.indexOf('?');
|
|
37
|
+
return startParams !== -1 ? url.substring(0, startParams) : url;
|
|
38
|
+
}
|
|
39
|
+
exports.removeUrlParams = removeUrlParams;
|
|
40
|
+
var o = 'OPTIONS';
|
|
41
|
+
var MiddlewareLogger = (function () {
|
|
42
|
+
function MiddlewareLogger(write, conf, build) {
|
|
43
|
+
this.write = write;
|
|
44
|
+
this.build = build;
|
|
45
|
+
this.log = this.log.bind(this);
|
|
46
|
+
this.conf = createConfig(conf);
|
|
47
|
+
}
|
|
48
|
+
MiddlewareLogger.prototype.log = function (req, res, next) {
|
|
49
|
+
var _this = this;
|
|
50
|
+
var m = req.method;
|
|
51
|
+
if (m !== o && this.conf.log && !skip(this.conf.skips, req.originalUrl)) {
|
|
52
|
+
var start_1 = process.hrtime();
|
|
53
|
+
var x_1 = this.conf.request;
|
|
54
|
+
var r_1 = false;
|
|
55
|
+
if (m !== 'GET' && m !== 'DELETE') {
|
|
56
|
+
r_1 = true;
|
|
57
|
+
}
|
|
58
|
+
var msg_1 = m + " " + req.originalUrl;
|
|
59
|
+
if (this.conf.separate && r_1) {
|
|
60
|
+
if (this.conf.request.length > 0) {
|
|
61
|
+
var op = {};
|
|
62
|
+
op[x_1] = JSON.stringify(req.body);
|
|
63
|
+
if (this.build) {
|
|
64
|
+
var op2 = this.build(req, op);
|
|
65
|
+
this.write(msg_1, op2);
|
|
66
|
+
}
|
|
67
|
+
else {
|
|
68
|
+
this.write(msg_1, op);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
var chunks_1 = [];
|
|
73
|
+
mapResponseBody(res, chunks_1);
|
|
74
|
+
res.on('finish', function () {
|
|
75
|
+
var duration = getDurationInMilliseconds(start_1);
|
|
76
|
+
var op = {};
|
|
77
|
+
if (r_1 && !_this.conf.separate && _this.conf.request.length > 0) {
|
|
78
|
+
op[x_1] = JSON.stringify(req.body);
|
|
79
|
+
}
|
|
80
|
+
if (_this.conf.response.length > 0) {
|
|
81
|
+
var rsBody = Buffer.concat(chunks_1).toString();
|
|
82
|
+
op[_this.conf.response] = rsBody;
|
|
83
|
+
}
|
|
84
|
+
if (_this.conf.status.length > 0) {
|
|
85
|
+
op[_this.conf.status] = res.statusCode;
|
|
86
|
+
}
|
|
87
|
+
if (_this.conf.size.length > 0) {
|
|
88
|
+
if ('_contentLength' in res) {
|
|
89
|
+
op[_this.conf.size] = res['_contentLength'];
|
|
90
|
+
}
|
|
91
|
+
else if (res.hasHeader('content-length')) {
|
|
92
|
+
var l = res.getHeader('content-length');
|
|
93
|
+
if (typeof l === 'number' || typeof l === 'string') {
|
|
94
|
+
op[_this.conf.size] = l;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
op[_this.conf.duration] = duration;
|
|
99
|
+
if (_this.build) {
|
|
100
|
+
var op2 = _this.build(req, op);
|
|
101
|
+
_this.write(msg_1, op2);
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
_this.write(msg_1, op);
|
|
105
|
+
}
|
|
106
|
+
});
|
|
107
|
+
next();
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
next();
|
|
111
|
+
}
|
|
112
|
+
};
|
|
113
|
+
return MiddlewareLogger;
|
|
114
|
+
}());
|
|
115
|
+
exports.MiddlewareLogger = MiddlewareLogger;
|
|
116
|
+
var mapResponseBody = function (res, chunks) {
|
|
117
|
+
var defaultWrite = res.write.bind(res);
|
|
118
|
+
var defaultEnd = res.end.bind(res);
|
|
119
|
+
var ps = new stream_1.PassThrough();
|
|
120
|
+
ps.on('data', function (data) { return chunks.push(data); });
|
|
121
|
+
res.write = function () {
|
|
122
|
+
var _a;
|
|
123
|
+
var args = [];
|
|
124
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
125
|
+
args[_i] = arguments[_i];
|
|
126
|
+
}
|
|
127
|
+
(_a = ps).write.apply(_a, args);
|
|
128
|
+
defaultWrite.apply(void 0, args);
|
|
129
|
+
};
|
|
130
|
+
res.end = function () {
|
|
131
|
+
var args = [];
|
|
132
|
+
for (var _i = 0; _i < arguments.length; _i++) {
|
|
133
|
+
args[_i] = arguments[_i];
|
|
134
|
+
}
|
|
135
|
+
ps.end.apply(ps, args);
|
|
136
|
+
defaultEnd.apply(void 0, args);
|
|
137
|
+
};
|
|
138
|
+
};
|
|
139
|
+
var NS_PER_SEC = 1e9;
|
|
140
|
+
var NS_TO_MS = 1e6;
|
|
141
|
+
var getDurationInMilliseconds = function (start) {
|
|
142
|
+
var diff = process.hrtime(start);
|
|
143
|
+
return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS;
|
|
144
|
+
};
|
|
145
|
+
var MiddlewareController = (function () {
|
|
146
|
+
function MiddlewareController(logger) {
|
|
147
|
+
this.logger = logger;
|
|
148
|
+
this.config = this.config.bind(this);
|
|
149
|
+
}
|
|
150
|
+
MiddlewareController.prototype.config = function (req, res) {
|
|
151
|
+
var obj = req.body;
|
|
152
|
+
if (!obj || obj === '') {
|
|
153
|
+
return res.status(400).end('The request body cannot be empty');
|
|
154
|
+
}
|
|
155
|
+
if (!this.logger) {
|
|
156
|
+
return res.status(503).end('Logger is not available');
|
|
157
|
+
}
|
|
158
|
+
var changed = false;
|
|
159
|
+
if (obj.log !== undefined) {
|
|
160
|
+
this.logger.conf.log = obj.log;
|
|
161
|
+
changed = true;
|
|
162
|
+
}
|
|
163
|
+
if (obj.separate !== undefined) {
|
|
164
|
+
this.logger.conf.separate = obj.separate;
|
|
165
|
+
changed = true;
|
|
166
|
+
}
|
|
167
|
+
if (Array.isArray(obj.skips)) {
|
|
168
|
+
if (isValidSkips(obj.skips)) {
|
|
169
|
+
this.logger.conf.skips = obj.skips;
|
|
170
|
+
changed = true;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (typeof obj.duration === 'string' && obj.duration.length > 0) {
|
|
174
|
+
this.logger.conf.duration = obj.duration;
|
|
175
|
+
changed = true;
|
|
176
|
+
}
|
|
177
|
+
if (typeof obj.request === 'string') {
|
|
178
|
+
this.logger.conf.request = obj.request;
|
|
179
|
+
changed = true;
|
|
180
|
+
}
|
|
181
|
+
if (typeof obj.response === 'string') {
|
|
182
|
+
this.logger.conf.response = obj.response;
|
|
183
|
+
changed = true;
|
|
184
|
+
}
|
|
185
|
+
if (typeof obj.status === 'string') {
|
|
186
|
+
this.logger.conf.status = obj.status;
|
|
187
|
+
changed = true;
|
|
188
|
+
}
|
|
189
|
+
if (typeof obj.size === 'string') {
|
|
190
|
+
this.logger.conf.size = obj.size;
|
|
191
|
+
changed = true;
|
|
192
|
+
}
|
|
193
|
+
if (changed) {
|
|
194
|
+
return res.status(200).json(true).end();
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
return res.status(204).json(false).end();
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
return MiddlewareController;
|
|
201
|
+
}());
|
|
202
|
+
exports.MiddlewareController = MiddlewareController;
|
|
203
|
+
function isValidSkips(s) {
|
|
204
|
+
for (var _i = 0, s_1 = s; _i < s_1.length; _i++) {
|
|
205
|
+
var x = s_1[_i];
|
|
206
|
+
if (!(typeof x === 'string')) {
|
|
207
|
+
return false;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return true;
|
|
211
|
+
}
|
|
212
|
+
exports.isValidSkips = isValidSkips;
|
package/lib/resources.js
CHANGED
|
@@ -1,2 +1,54 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var fs = require("fs");
|
|
4
|
+
var resources = (function () {
|
|
5
|
+
function resources() {
|
|
6
|
+
}
|
|
7
|
+
return resources;
|
|
8
|
+
}());
|
|
9
|
+
exports.resources = resources;
|
|
10
|
+
var TypeChecker = (function () {
|
|
11
|
+
function TypeChecker(attributes, allowUndefined) {
|
|
12
|
+
this.attributes = attributes;
|
|
13
|
+
this.allowUndefined = allowUndefined;
|
|
14
|
+
this.check = this.check.bind(this);
|
|
15
|
+
}
|
|
16
|
+
TypeChecker.prototype.check = function (req, res, next) {
|
|
17
|
+
var obj = req.body;
|
|
18
|
+
if (!obj || obj === '') {
|
|
19
|
+
return res.status(400).end('The request body cannot be empty');
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
var errors = resources.check(obj, this.attributes, this.allowUndefined);
|
|
23
|
+
if (errors.length > 0) {
|
|
24
|
+
res.status(400).json(errors).end();
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
next();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
return TypeChecker;
|
|
32
|
+
}());
|
|
33
|
+
exports.TypeChecker = TypeChecker;
|
|
34
|
+
function check(attributes, allowUndefined) {
|
|
35
|
+
var x = new TypeChecker(attributes, allowUndefined);
|
|
36
|
+
return x.check;
|
|
37
|
+
}
|
|
38
|
+
exports.check = check;
|
|
39
|
+
function loadTemplates(ok, buildTemplates, correct, files) {
|
|
40
|
+
if (!ok) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
43
|
+
if (!files) {
|
|
44
|
+
files = ['./src/query.xml'];
|
|
45
|
+
}
|
|
46
|
+
var mappers = [];
|
|
47
|
+
for (var _i = 0, files_1 = files; _i < files_1.length; _i++) {
|
|
48
|
+
var file = files_1[_i];
|
|
49
|
+
var mapper = fs.readFileSync(file, 'utf8');
|
|
50
|
+
mappers.push(mapper);
|
|
51
|
+
}
|
|
52
|
+
return buildTemplates(mappers, correct);
|
|
53
|
+
}
|
|
54
|
+
exports.loadTemplates = loadTemplates;
|
package/lib/search.js
CHANGED
|
@@ -83,27 +83,40 @@ function initializeConfig(conf) {
|
|
|
83
83
|
return c;
|
|
84
84
|
}
|
|
85
85
|
exports.initializeConfig = initializeConfig;
|
|
86
|
-
function fromRequest(req,
|
|
87
|
-
var s = (req.method === 'GET' ? fromUrl(req,
|
|
86
|
+
function fromRequest(req, arr) {
|
|
87
|
+
var s = (req.method === 'GET' ? fromUrl(req, arr) : req.body);
|
|
88
88
|
return s;
|
|
89
89
|
}
|
|
90
90
|
exports.fromRequest = fromRequest;
|
|
91
|
-
function
|
|
92
|
-
|
|
93
|
-
|
|
91
|
+
function buildArray(arr, s0, s1, s2) {
|
|
92
|
+
var r = [];
|
|
93
|
+
if (arr && arr.length > 0) {
|
|
94
|
+
for (var _i = 0, arr_1 = arr; _i < arr_1.length; _i++) {
|
|
95
|
+
var a = arr_1[_i];
|
|
96
|
+
r.push(a);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
if (s0 && s0.length > 0) {
|
|
100
|
+
r.push(s0);
|
|
101
|
+
}
|
|
102
|
+
if (s1 && s1.length > 0) {
|
|
103
|
+
r.push(s1);
|
|
94
104
|
}
|
|
105
|
+
if (s2 && s2.length > 0) {
|
|
106
|
+
r.push(s2);
|
|
107
|
+
}
|
|
108
|
+
return r;
|
|
109
|
+
}
|
|
110
|
+
exports.buildArray = buildArray;
|
|
111
|
+
function fromUrl(req, arr) {
|
|
95
112
|
var s = {};
|
|
96
113
|
var obj = req.query;
|
|
97
114
|
var keys = Object.keys(obj);
|
|
98
115
|
for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
|
|
99
116
|
var key = keys_1[_i];
|
|
100
|
-
if (key
|
|
101
|
-
var x = obj[key].split(',');
|
|
102
|
-
s[key] = x;
|
|
103
|
-
}
|
|
104
|
-
else if (key === excluding) {
|
|
117
|
+
if (inArray(key, arr)) {
|
|
105
118
|
var x = obj[key].split(',');
|
|
106
|
-
s
|
|
119
|
+
setValue(s, key, x);
|
|
107
120
|
}
|
|
108
121
|
else {
|
|
109
122
|
setValue(s, key, obj[key]);
|
|
@@ -112,6 +125,19 @@ function fromUrl(req, fields, excluding) {
|
|
|
112
125
|
return s;
|
|
113
126
|
}
|
|
114
127
|
exports.fromUrl = fromUrl;
|
|
128
|
+
function inArray(s, arr) {
|
|
129
|
+
if (!arr || arr.length === 0) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
for (var _i = 0, arr_2 = arr; _i < arr_2.length; _i++) {
|
|
133
|
+
var a = arr_2[_i];
|
|
134
|
+
if (s === a) {
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return false;
|
|
139
|
+
}
|
|
140
|
+
exports.inArray = inArray;
|
|
115
141
|
function setValue(obj, path, value) {
|
|
116
142
|
var paths = path.split('.');
|
|
117
143
|
if (paths.length === 1) {
|
|
@@ -315,8 +341,8 @@ function deletePageInfo(obj, arr) {
|
|
|
315
341
|
delete obj['nextPageToken'];
|
|
316
342
|
}
|
|
317
343
|
else {
|
|
318
|
-
for (var _i = 0,
|
|
319
|
-
var o =
|
|
344
|
+
for (var _i = 0, arr_3 = arr; _i < arr_3.length; _i++) {
|
|
345
|
+
var o = arr_3[_i];
|
|
320
346
|
if (o && o.length > 0) {
|
|
321
347
|
delete obj[o];
|
|
322
348
|
}
|
package/package.json
CHANGED
package/src/GenericController.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import {Request, Response} from 'express';
|
|
2
2
|
import {checkId, create, initializeStatus, isTypeError, ResultInfo, StatusConfig, update} from './edit';
|
|
3
|
-
import {handleError} from './http';
|
|
3
|
+
import {handleError, Log} from './http';
|
|
4
4
|
import {LoadController} from './LoadController';
|
|
5
5
|
import {Attribute, Attributes, ErrorMessage} from './metadata';
|
|
6
6
|
import {resources} from './resources';
|
|
@@ -17,7 +17,7 @@ export interface GenericService<T, ID, R> {
|
|
|
17
17
|
export class GenericController<T, ID> extends LoadController<T, ID> {
|
|
18
18
|
status: StatusConfig;
|
|
19
19
|
metadata?: Attributes;
|
|
20
|
-
constructor(log:
|
|
20
|
+
constructor(log: Log, public service: GenericService<T, ID, number|ResultInfo<T>>, status?: StatusConfig, public validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>) {
|
|
21
21
|
super(log, service);
|
|
22
22
|
this.status = initializeStatus(status);
|
|
23
23
|
if (service.metadata) {
|
|
@@ -67,7 +67,7 @@ export class GenericController<T, ID> extends LoadController<T, ID> {
|
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
}
|
|
70
|
-
export function validateAndCreate<T>(req: Request, res: Response, status: StatusConfig, save: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log:
|
|
70
|
+
export function validateAndCreate<T>(req: Request, res: Response, status: StatusConfig, save: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log: Log, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>): void {
|
|
71
71
|
const obj = req.body;
|
|
72
72
|
if (!obj || obj === '') {
|
|
73
73
|
return res.status(400).end('The request body cannot be empty.');
|
|
@@ -85,7 +85,7 @@ export function validateAndCreate<T>(req: Request, res: Response, status: Status
|
|
|
85
85
|
create(res, status, obj, save, log);
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
|
-
export function validateAndUpdate<T>(res: Response, status: StatusConfig, obj: T, isPatch: boolean, save: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log:
|
|
88
|
+
export function validateAndUpdate<T>(res: Response, status: StatusConfig, obj: T, isPatch: boolean, save: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log: Log, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>): void {
|
|
89
89
|
if (validate) {
|
|
90
90
|
validate(obj, isPatch).then(errors => {
|
|
91
91
|
if (errors && errors.length > 0) {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {Request, Response} from 'express';
|
|
2
2
|
import {ResultInfo, StatusConfig} from './edit';
|
|
3
3
|
import {GenericController, GenericService} from './GenericController';
|
|
4
|
-
import {handleError} from './http';
|
|
4
|
+
import {handleError, Log} from './http';
|
|
5
5
|
import {ErrorMessage} from './metadata';
|
|
6
|
-
import {Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
|
|
6
|
+
import {buildArray, Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
|
|
7
7
|
import {getMetadataFunc} from './search_func';
|
|
8
8
|
|
|
9
9
|
export interface Config extends StatusConfig, SearchConfig {
|
|
@@ -15,7 +15,8 @@ export class GenericSearchController<T, ID, S extends Filter> extends GenericCon
|
|
|
15
15
|
numbers?: string[];
|
|
16
16
|
fields?: string;
|
|
17
17
|
excluding?: string;
|
|
18
|
-
|
|
18
|
+
array?: string[];
|
|
19
|
+
constructor(log: Log, public find: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>, service: GenericService<T, ID, number|ResultInfo<T>>, config?: Config, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
|
|
19
20
|
super(log, service, config, validate);
|
|
20
21
|
this.search = this.search.bind(this);
|
|
21
22
|
this.config = initializeConfig(config);
|
|
@@ -34,7 +35,7 @@ export class GenericSearchController<T, ID, S extends Filter> extends GenericCon
|
|
|
34
35
|
}
|
|
35
36
|
}
|
|
36
37
|
search(req: Request, res: Response) {
|
|
37
|
-
const s = fromRequest<S>(req, this.fields, this.excluding);
|
|
38
|
+
const s = fromRequest<S>(req, buildArray(this.array, this.fields, this.excluding));
|
|
38
39
|
const l = getParameters(s, this.config);
|
|
39
40
|
const s2 = format(s, this.dates, this.numbers);
|
|
40
41
|
this.find(s2, l.limit, l.skipOrRefId, l.fields)
|
package/src/HealthController.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import {Request, Response} from 'express';
|
|
2
|
-
import {
|
|
2
|
+
import {health, HealthChecker} from './health';
|
|
3
3
|
|
|
4
4
|
export class HealthController {
|
|
5
5
|
constructor(protected checkers: HealthChecker[]) {
|
|
6
6
|
this.check = this.check.bind(this);
|
|
7
7
|
}
|
|
8
8
|
check(req: Request, res: Response) {
|
|
9
|
-
|
|
10
|
-
if (
|
|
11
|
-
return res.status(200).json(
|
|
9
|
+
health(this.checkers).then(r => {
|
|
10
|
+
if (r.status === 'UP') {
|
|
11
|
+
return res.status(200).json(r).end();
|
|
12
12
|
} else {
|
|
13
|
-
return res.status(500).json(
|
|
13
|
+
return res.status(500).json(r).end();
|
|
14
14
|
}
|
|
15
15
|
});
|
|
16
16
|
}
|
package/src/LoadController.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {Request, Response} from 'express';
|
|
2
|
-
import {attrs, handleError, respondModel} from './http';
|
|
2
|
+
import {attrs, handleError, Log, respondModel} from './http';
|
|
3
3
|
import {Attribute, Attributes} from './metadata';
|
|
4
4
|
import {buildAndCheckId, buildKeys} from './view';
|
|
5
5
|
|
|
@@ -39,7 +39,7 @@ function getKeysFunc<T, ID>(viewService: ViewService<T, ID> | ((id: ID, ctx?: an
|
|
|
39
39
|
export class LoadController<T, ID> {
|
|
40
40
|
protected keys?: Attribute[];
|
|
41
41
|
protected view: (id: ID, ctx?: any) => Promise<T|null>;
|
|
42
|
-
constructor(protected log:
|
|
42
|
+
constructor(protected log: Log, viewService: ViewService<T, ID> | ((id: ID, ctx?: any) => Promise<T|null>), keys?: Attributes|Attribute[]|string[]) {
|
|
43
43
|
this.load = this.load.bind(this);
|
|
44
44
|
this.view = getViewFunc(viewService);
|
|
45
45
|
this.keys = getKeysFunc(viewService, keys);
|