express-ext 0.1.15 → 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/LoadSearchController.js +1 -1
- package/lib/LogController.js +51 -43
- package/lib/LowCodeController.js +1 -1
- package/lib/SearchController.js +1 -1
- package/lib/index.js +3 -1
- package/lib/log.js +3 -2
- package/lib/resources.js +17 -0
- package/lib/search.js +39 -13
- package/package.json +1 -1
- package/src/GenericSearchController.ts +3 -2
- package/src/LoadSearchController.ts +3 -2
- package/src/LogController.ts +51 -43
- package/src/LowCodeController.ts +3 -2
- package/src/SearchController.ts +3 -2
- package/src/index.ts +18 -0
- package/src/log.ts +6 -5
- package/src/resources.ts +27 -0
- 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)
|
|
@@ -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
|
@@ -10,9 +10,10 @@ exports.map = {
|
|
|
10
10
|
FATAL: 4
|
|
11
11
|
};
|
|
12
12
|
var LogController = (function () {
|
|
13
|
-
function LogController(logger, mp) {
|
|
13
|
+
function LogController(logger, updateL, mp) {
|
|
14
14
|
this.logger = logger;
|
|
15
15
|
this.map = (mp ? mp : exports.map);
|
|
16
|
+
this.update = updateL ? updateL : updateLog;
|
|
16
17
|
this.config = this.config.bind(this);
|
|
17
18
|
}
|
|
18
19
|
LogController.prototype.config = function (req, res) {
|
|
@@ -23,53 +24,12 @@ var LogController = (function () {
|
|
|
23
24
|
if (!this.logger) {
|
|
24
25
|
return res.status(503).end('Logger is not available');
|
|
25
26
|
}
|
|
26
|
-
var changed = false;
|
|
27
27
|
if (typeof obj.level === 'string' && obj.level.length > 0) {
|
|
28
28
|
if (!this.map) {
|
|
29
29
|
return res.status(503).end('Map is not available');
|
|
30
30
|
}
|
|
31
|
-
var lv = this.map[obj.level.toUpperCase()];
|
|
32
|
-
if (lv !== undefined) {
|
|
33
|
-
this.logger.level = lv;
|
|
34
|
-
changed = true;
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
if (obj.map) {
|
|
38
|
-
if (typeof obj.map.level === 'string' && obj.map.level.length > 0) {
|
|
39
|
-
this.logger.map.level = obj.map.level;
|
|
40
|
-
changed = true;
|
|
41
|
-
}
|
|
42
|
-
if (typeof obj.map.time === 'string' && obj.map.time.length > 0) {
|
|
43
|
-
this.logger.map.time = obj.map.time;
|
|
44
|
-
changed = true;
|
|
45
|
-
}
|
|
46
|
-
if (typeof obj.map.msg === 'string' && obj.map.msg.length > 0) {
|
|
47
|
-
this.logger.map.msg = obj.map.msg;
|
|
48
|
-
changed = true;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
if (obj.constants !== undefined && typeof obj.constants === 'object') {
|
|
52
|
-
var ks = Object.keys(obj.constants);
|
|
53
|
-
if (ks.length > 0) {
|
|
54
|
-
this.logger.constants = obj.constants;
|
|
55
|
-
}
|
|
56
|
-
else {
|
|
57
|
-
this.logger.constants = undefined;
|
|
58
|
-
}
|
|
59
|
-
changed = true;
|
|
60
|
-
}
|
|
61
|
-
if (obj.name) {
|
|
62
|
-
if (typeof obj.name.trace === 'string'
|
|
63
|
-
&& typeof obj.name.debug === 'string'
|
|
64
|
-
&& typeof obj.name.info === 'string'
|
|
65
|
-
&& typeof obj.name.warn === 'string'
|
|
66
|
-
&& typeof obj.name.error === 'string'
|
|
67
|
-
&& typeof obj.name.panic === 'string'
|
|
68
|
-
&& typeof obj.name.fatal === 'string') {
|
|
69
|
-
this.logger.name = obj.name;
|
|
70
|
-
changed = true;
|
|
71
|
-
}
|
|
72
31
|
}
|
|
32
|
+
var changed = this.update(this.logger, obj, this.map);
|
|
73
33
|
if (changed) {
|
|
74
34
|
return res.status(200).json(true).end();
|
|
75
35
|
}
|
|
@@ -80,3 +40,51 @@ var LogController = (function () {
|
|
|
80
40
|
return LogController;
|
|
81
41
|
}());
|
|
82
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/index.js
CHANGED
|
@@ -1 +1,3 @@
|
|
|
1
|
-
"use strict";function __export(
|
|
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 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
CHANGED
|
@@ -37,6 +37,7 @@ function removeUrlParams(url) {
|
|
|
37
37
|
return startParams !== -1 ? url.substring(0, startParams) : url;
|
|
38
38
|
}
|
|
39
39
|
exports.removeUrlParams = removeUrlParams;
|
|
40
|
+
var o = 'OPTIONS';
|
|
40
41
|
var MiddlewareLogger = (function () {
|
|
41
42
|
function MiddlewareLogger(write, conf, build) {
|
|
42
43
|
this.write = write;
|
|
@@ -46,9 +47,9 @@ var MiddlewareLogger = (function () {
|
|
|
46
47
|
}
|
|
47
48
|
MiddlewareLogger.prototype.log = function (req, res, next) {
|
|
48
49
|
var _this = this;
|
|
49
|
-
|
|
50
|
+
var m = req.method;
|
|
51
|
+
if (m !== o && this.conf.log && !skip(this.conf.skips, req.originalUrl)) {
|
|
50
52
|
var start_1 = process.hrtime();
|
|
51
|
-
var m = req.method;
|
|
52
53
|
var x_1 = this.conf.request;
|
|
53
54
|
var r_1 = false;
|
|
54
55
|
if (m !== 'GET' && m !== 'DELETE') {
|
package/lib/resources.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var fs = require("fs");
|
|
3
4
|
var resources = (function () {
|
|
4
5
|
function resources() {
|
|
5
6
|
}
|
|
@@ -35,3 +36,19 @@ function check(attributes, allowUndefined) {
|
|
|
35
36
|
return x.check;
|
|
36
37
|
}
|
|
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
|
@@ -3,7 +3,7 @@ import {ResultInfo, StatusConfig} from './edit';
|
|
|
3
3
|
import {GenericController, GenericService} from './GenericController';
|
|
4
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,6 +15,7 @@ export class GenericSearchController<T, ID, S extends Filter> extends GenericCon
|
|
|
15
15
|
numbers?: string[];
|
|
16
16
|
fields?: string;
|
|
17
17
|
excluding?: string;
|
|
18
|
+
array?: string[];
|
|
18
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);
|
|
@@ -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)
|
|
@@ -2,7 +2,7 @@ import {Request, Response} from 'express';
|
|
|
2
2
|
import {handleError, Log} from './http';
|
|
3
3
|
import {LoadController, ViewService} from './LoadController';
|
|
4
4
|
import {Attribute, Attributes} from './metadata';
|
|
5
|
-
import {Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
|
|
5
|
+
import {buildArray, Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
|
|
6
6
|
import {getMetadataFunc} from './search_func';
|
|
7
7
|
|
|
8
8
|
export class LoadSearchController<T, ID, S extends Filter> extends LoadController<T, ID> {
|
|
@@ -12,6 +12,7 @@ export class LoadSearchController<T, ID, S extends Filter> extends LoadControlle
|
|
|
12
12
|
numbers?: string[];
|
|
13
13
|
fields?: string;
|
|
14
14
|
excluding?: string;
|
|
15
|
+
array?: string[];
|
|
15
16
|
constructor(log: Log, public find: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>, viewService: ViewService<T, ID> | ((id: ID, ctx?: any) => Promise<T>), keys?: Attributes|Attribute[]|string[], config?: SearchConfig|boolean, dates?: string[], numbers?: string[]) {
|
|
16
17
|
super(log, viewService, keys);
|
|
17
18
|
this.search = this.search.bind(this);
|
|
@@ -37,7 +38,7 @@ export class LoadSearchController<T, ID, S extends Filter> extends LoadControlle
|
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
search(req: Request, res: Response) {
|
|
40
|
-
const s = fromRequest<S>(req, this.fields, this.excluding);
|
|
41
|
+
const s = fromRequest<S>(req, buildArray(this.array, this.fields, this.excluding));
|
|
41
42
|
const l = getParameters(s, this.config);
|
|
42
43
|
const s2 = format(s, this.dates, this.numbers);
|
|
43
44
|
this.find(s2, l.limit, l.skipOrRefId, l.fields)
|
package/src/LogController.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { SimpleMap } from './log';
|
|
|
4
4
|
export interface NumberMap {
|
|
5
5
|
[key: string]: number;
|
|
6
6
|
}
|
|
7
|
-
export interface
|
|
7
|
+
export interface LogConfig {
|
|
8
8
|
level?: string;
|
|
9
9
|
map?: LogMapConfig;
|
|
10
10
|
constants?: SimpleMap;
|
|
@@ -61,68 +61,76 @@ export const map: NumberMap = {
|
|
|
61
61
|
};
|
|
62
62
|
export class LogController {
|
|
63
63
|
map: NumberMap;
|
|
64
|
-
|
|
64
|
+
update: (logger: Logger, obj: LogConfig, mp: NumberMap) => boolean;
|
|
65
|
+
constructor(public logger: Logger, updateL?: (logger: Logger, obj: LogConfig, mp: NumberMap) => boolean, mp?: NumberMap) {
|
|
65
66
|
this.map = (mp ? mp : map);
|
|
67
|
+
this.update = updateL ? updateL : updateLog;
|
|
66
68
|
this.config = this.config.bind(this);
|
|
67
69
|
}
|
|
68
70
|
config(req: Request, res: Response) {
|
|
69
|
-
const obj:
|
|
71
|
+
const obj: LogConfig = req.body;
|
|
70
72
|
if (!obj || obj === '') {
|
|
71
73
|
return res.status(400).end('The request body cannot be empty');
|
|
72
74
|
}
|
|
73
75
|
if (!this.logger) {
|
|
74
76
|
return res.status(503).end('Logger is not available');
|
|
75
77
|
}
|
|
76
|
-
let changed = false;
|
|
77
78
|
if (typeof obj.level === 'string' && obj.level.length > 0) {
|
|
78
79
|
if (!this.map) {
|
|
79
80
|
return res.status(503).end('Map is not available');
|
|
80
81
|
}
|
|
81
|
-
const lv = this.map[obj.level.toUpperCase()];
|
|
82
|
-
if (lv !== undefined) {
|
|
83
|
-
this.logger.level = lv;
|
|
84
|
-
changed = true;
|
|
85
|
-
}
|
|
86
82
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (typeof obj.map.time === 'string' && obj.map.time.length > 0) {
|
|
93
|
-
this.logger.map.time = obj.map.time;
|
|
94
|
-
changed = true;
|
|
95
|
-
}
|
|
96
|
-
if (typeof obj.map.msg === 'string' && obj.map.msg.length > 0) {
|
|
97
|
-
this.logger.map.msg = obj.map.msg;
|
|
98
|
-
changed = true;
|
|
99
|
-
}
|
|
83
|
+
const changed = this.update(this.logger, obj, this.map);
|
|
84
|
+
if (changed) {
|
|
85
|
+
return res.status(200).json(true).end();
|
|
86
|
+
} else {
|
|
87
|
+
return res.status(204).json(false).end();
|
|
100
88
|
}
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export function updateLog(logger: Logger, obj: LogConfig, mp: NumberMap): boolean {
|
|
92
|
+
let changed = false;
|
|
93
|
+
if (typeof obj.level === 'string' && obj.level.length > 0) {
|
|
94
|
+
const lv = mp[obj.level.toUpperCase()];
|
|
95
|
+
if (lv !== undefined) {
|
|
96
|
+
logger.level = lv;
|
|
108
97
|
changed = true;
|
|
109
98
|
}
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
&& typeof obj.name.error === 'string'
|
|
116
|
-
&& typeof obj.name.panic === 'string'
|
|
117
|
-
&& typeof obj.name.fatal === 'string') {
|
|
118
|
-
this.logger.name = obj.name;
|
|
119
|
-
changed = true;
|
|
120
|
-
}
|
|
99
|
+
}
|
|
100
|
+
if (obj.map) {
|
|
101
|
+
if (typeof obj.map.level === 'string' && obj.map.level.length > 0) {
|
|
102
|
+
logger.map.level = obj.map.level;
|
|
103
|
+
changed = true;
|
|
121
104
|
}
|
|
122
|
-
if (
|
|
123
|
-
|
|
105
|
+
if (typeof obj.map.time === 'string' && obj.map.time.length > 0) {
|
|
106
|
+
logger.map.time = obj.map.time;
|
|
107
|
+
changed = true;
|
|
108
|
+
}
|
|
109
|
+
if (typeof obj.map.msg === 'string' && obj.map.msg.length > 0) {
|
|
110
|
+
logger.map.msg = obj.map.msg;
|
|
111
|
+
changed = true;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
if (obj.constants !== undefined && typeof obj.constants === 'object') {
|
|
115
|
+
const ks = Object.keys(obj.constants);
|
|
116
|
+
if (ks.length > 0) {
|
|
117
|
+
logger.constants = obj.constants;
|
|
124
118
|
} else {
|
|
125
|
-
|
|
119
|
+
logger.constants = undefined;
|
|
120
|
+
}
|
|
121
|
+
changed = true;
|
|
122
|
+
}
|
|
123
|
+
if (obj.name) {
|
|
124
|
+
if (typeof obj.name.trace === 'string'
|
|
125
|
+
&& typeof obj.name.debug === 'string'
|
|
126
|
+
&& typeof obj.name.info === 'string'
|
|
127
|
+
&& typeof obj.name.warn === 'string'
|
|
128
|
+
&& typeof obj.name.error === 'string'
|
|
129
|
+
&& typeof obj.name.panic === 'string'
|
|
130
|
+
&& typeof obj.name.fatal === 'string') {
|
|
131
|
+
logger.name = obj.name;
|
|
132
|
+
changed = true;
|
|
126
133
|
}
|
|
127
134
|
}
|
|
135
|
+
return changed;
|
|
128
136
|
}
|
package/src/LowCodeController.ts
CHANGED
|
@@ -3,7 +3,7 @@ import {ResultInfo, StatusConfig} from './edit';
|
|
|
3
3
|
import {GenericController, GenericService} from './GenericController';
|
|
4
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 LowCodeConfig extends StatusConfig, SearchConfig {
|
|
@@ -18,6 +18,7 @@ export class LowCodeController<T, ID, S extends Filter> extends GenericControlle
|
|
|
18
18
|
numbers?: string[];
|
|
19
19
|
fields?: string;
|
|
20
20
|
excluding?: string;
|
|
21
|
+
array?: string[];
|
|
21
22
|
constructor(log: Log, public lowCodeService: Service<T, ID, number|ResultInfo<T>, S>, config?: LowCodeConfig, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
|
|
22
23
|
super(log, lowCodeService, config, validate);
|
|
23
24
|
this.search = this.search.bind(this);
|
|
@@ -37,7 +38,7 @@ export class LowCodeController<T, ID, S extends Filter> extends GenericControlle
|
|
|
37
38
|
}
|
|
38
39
|
}
|
|
39
40
|
search(req: Request, res: Response) {
|
|
40
|
-
const s = fromRequest<S>(req, this.fields, this.excluding);
|
|
41
|
+
const s = fromRequest<S>(req, buildArray(this.array, this.fields, this.excluding));
|
|
41
42
|
const l = getParameters(s, this.config);
|
|
42
43
|
const s2 = format(s, this.dates, this.numbers);
|
|
43
44
|
this.lowCodeService.search(s2, l.limit, l.skipOrRefId, l.fields)
|
package/src/SearchController.ts
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
import {Request, Response} from 'express';
|
|
2
2
|
import {handleError, Log} from './http';
|
|
3
|
-
import {Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
|
|
3
|
+
import {buildArray, Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
|
|
4
4
|
|
|
5
5
|
export class SearchController<T, S extends Filter> {
|
|
6
6
|
config?: SearchConfig;
|
|
7
7
|
csv?: boolean;
|
|
8
8
|
fields?: string;
|
|
9
9
|
excluding?: string;
|
|
10
|
+
array?: string[];
|
|
10
11
|
constructor(protected log: Log, public find: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>, config?: SearchConfig|boolean, public dates?: string[], public numbers?: string[]) {
|
|
11
12
|
this.search = this.search.bind(this);
|
|
12
13
|
if (config) {
|
|
@@ -26,7 +27,7 @@ export class SearchController<T, S extends Filter> {
|
|
|
26
27
|
}
|
|
27
28
|
}
|
|
28
29
|
search(req: Request, res: Response) {
|
|
29
|
-
const s = fromRequest<S>(req, this.fields, this.excluding);
|
|
30
|
+
const s = fromRequest<S>(req, buildArray(this.array, this.fields, this.excluding));
|
|
30
31
|
const l = getParameters(s, this.config);
|
|
31
32
|
const s2 = format(s, this.dates, this.numbers);
|
|
32
33
|
this.find(s2, l.limit, l.skipOrRefId, l.fields)
|
package/src/index.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {NextFunction, Request, Response} from 'express';
|
|
1
2
|
import {GenericController} from './GenericController';
|
|
2
3
|
import {GenericSearchController} from './GenericSearchController';
|
|
3
4
|
import {HealthController} from './HealthController';
|
|
@@ -40,3 +41,20 @@ export * from './edit';
|
|
|
40
41
|
export * from './GenericController';
|
|
41
42
|
export * from './GenericSearchController';
|
|
42
43
|
export * from './LowCodeController';
|
|
44
|
+
|
|
45
|
+
export interface AccessConfig {
|
|
46
|
+
origin: string;
|
|
47
|
+
credentials: string;
|
|
48
|
+
methods: string;
|
|
49
|
+
headers: string;
|
|
50
|
+
}
|
|
51
|
+
export type AccessControlAllowConfig = AccessConfig;
|
|
52
|
+
export function allow(access: AccessConfig): (req: Request, res: Response, next: NextFunction) => void {
|
|
53
|
+
return (req: Request, res: Response, next: NextFunction) => {
|
|
54
|
+
res.header('Access-Control-Allow-Origin', access.origin);
|
|
55
|
+
res.header('Access-Control-Allow-Credentials', access.credentials);
|
|
56
|
+
res.header('Access-Control-Allow-Methods', access.methods);
|
|
57
|
+
res.setHeader('Access-Control-Allow-Headers', access.headers);
|
|
58
|
+
next();
|
|
59
|
+
};
|
|
60
|
+
}
|
package/src/log.ts
CHANGED
|
@@ -3,7 +3,7 @@ import { ParamsDictionary, Request, Response } from 'express-serve-static-core';
|
|
|
3
3
|
import { ParsedQs } from 'qs';
|
|
4
4
|
import { PassThrough } from 'stream';
|
|
5
5
|
|
|
6
|
-
export interface
|
|
6
|
+
export interface LogConf {
|
|
7
7
|
log?: boolean;
|
|
8
8
|
separate?: boolean;
|
|
9
9
|
skips?: string;
|
|
@@ -26,7 +26,7 @@ export interface MiddleLog {
|
|
|
26
26
|
export interface SimpleMap {
|
|
27
27
|
[key: string]: string|number|boolean|Date;
|
|
28
28
|
}
|
|
29
|
-
export function createConfig(c?:
|
|
29
|
+
export function createConfig(c?: LogConf): MiddleLog {
|
|
30
30
|
if (!c) {
|
|
31
31
|
return {skips: [], duration: 'duration', request: '', response: '', status: '', size: ''};
|
|
32
32
|
}
|
|
@@ -61,16 +61,17 @@ export function removeUrlParams(url: string): string {
|
|
|
61
61
|
export interface Middleware {
|
|
62
62
|
conf: MiddleLog;
|
|
63
63
|
}
|
|
64
|
+
const o = 'OPTIONS';
|
|
64
65
|
export class MiddlewareLogger {
|
|
65
|
-
constructor(public write: (msg: string, m?: SimpleMap) => void, conf?:
|
|
66
|
+
constructor(public write: (msg: string, m?: SimpleMap) => void, conf?: LogConf, public build?: (req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, m: SimpleMap) => SimpleMap) {
|
|
66
67
|
this.log = this.log.bind(this);
|
|
67
68
|
this.conf = createConfig(conf);
|
|
68
69
|
}
|
|
69
70
|
conf: MiddleLog;
|
|
70
71
|
log(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>, number>, next: NextFunction) {
|
|
71
|
-
|
|
72
|
+
const m = req.method;
|
|
73
|
+
if (m !== o && this.conf.log && !skip(this.conf.skips, req.originalUrl)) {
|
|
72
74
|
const start = process.hrtime();
|
|
73
|
-
const m = req.method;
|
|
74
75
|
const x = this.conf.request;
|
|
75
76
|
let r = false;
|
|
76
77
|
if (m !== 'GET' && m !== 'DELETE') {
|
package/src/resources.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
import * as fs from 'fs';
|
|
2
3
|
import { Attributes, ErrorMessage } from './metadata';
|
|
3
4
|
|
|
4
5
|
// tslint:disable-next-line:class-name
|
|
@@ -34,3 +35,29 @@ export function check(attributes: Attributes, allowUndefined?: boolean): Handler
|
|
|
34
35
|
const x = new TypeChecker(attributes, allowUndefined);
|
|
35
36
|
return x.check;
|
|
36
37
|
}
|
|
38
|
+
export interface Template {
|
|
39
|
+
name?: string | null;
|
|
40
|
+
text: string;
|
|
41
|
+
templates: TemplateNode[];
|
|
42
|
+
}
|
|
43
|
+
export interface TemplateNode {
|
|
44
|
+
type: string;
|
|
45
|
+
text: string;
|
|
46
|
+
property: string | null;
|
|
47
|
+
encode?: string | null;
|
|
48
|
+
value: string | null;
|
|
49
|
+
}
|
|
50
|
+
export function loadTemplates(ok: boolean|undefined, buildTemplates: (streams: string[], correct?: (stream: string) => string) => Map<string, Template>, correct?: (stream: string) => string, files?: string[]): Map<string, Template>|undefined {
|
|
51
|
+
if (!ok) {
|
|
52
|
+
return undefined;
|
|
53
|
+
}
|
|
54
|
+
if (!files) {
|
|
55
|
+
files = ['./src/query.xml'];
|
|
56
|
+
}
|
|
57
|
+
const mappers: string[] = [];
|
|
58
|
+
for (const file of files) {
|
|
59
|
+
const mapper = fs.readFileSync(file, 'utf8');
|
|
60
|
+
mappers.push(mapper);
|
|
61
|
+
}
|
|
62
|
+
return buildTemplates(mappers, correct);
|
|
63
|
+
}
|
package/src/search.ts
CHANGED
|
@@ -107,30 +107,58 @@ export function initializeConfig(conf?: SearchConfig): SearchConfig | undefined
|
|
|
107
107
|
}
|
|
108
108
|
return c;
|
|
109
109
|
}
|
|
110
|
-
export function fromRequest<S>(req: Request,
|
|
111
|
-
const s: any = (req.method === 'GET' ? fromUrl(req,
|
|
110
|
+
export function fromRequest<S>(req: Request, arr?: string[]): S {
|
|
111
|
+
const s: any = (req.method === 'GET' ? fromUrl(req, arr) : req.body);
|
|
112
112
|
return s;
|
|
113
113
|
}
|
|
114
|
-
export function
|
|
114
|
+
export function buildArray(arr?: string[], s0?: string, s1?: string, s2?: string): string[] {
|
|
115
|
+
const r: string[] = [];
|
|
116
|
+
if (arr && arr.length > 0) {
|
|
117
|
+
for (const a of arr) {
|
|
118
|
+
r.push(a);
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
if (s0 && s0.length > 0) {
|
|
122
|
+
r.push(s0);
|
|
123
|
+
}
|
|
124
|
+
if (s1 && s1.length > 0) {
|
|
125
|
+
r.push(s1);
|
|
126
|
+
}
|
|
127
|
+
if (s2 && s2.length > 0) {
|
|
128
|
+
r.push(s2);
|
|
129
|
+
}
|
|
130
|
+
return r;
|
|
131
|
+
}
|
|
132
|
+
export function fromUrl<S>(req: Request, arr?: string[]): S {
|
|
133
|
+
/*
|
|
115
134
|
if (!fields || fields.length === 0) {
|
|
116
135
|
fields = 'fields';
|
|
117
136
|
}
|
|
137
|
+
*/
|
|
118
138
|
const s: any = {};
|
|
119
139
|
const obj = req.query;
|
|
120
140
|
const keys = Object.keys(obj);
|
|
121
141
|
for (const key of keys) {
|
|
122
|
-
if (key
|
|
142
|
+
if (inArray(key, arr)) {
|
|
123
143
|
const x = (obj[key] as string).split(',');
|
|
124
|
-
s
|
|
125
|
-
} else if (key === excluding) {
|
|
126
|
-
const x = (obj[key] as string).split(',');
|
|
127
|
-
s[key] = x;
|
|
144
|
+
setValue(s, key, x);
|
|
128
145
|
} else {
|
|
129
146
|
setValue(s, key, obj[key] as string);
|
|
130
147
|
}
|
|
131
148
|
}
|
|
132
149
|
return s;
|
|
133
150
|
}
|
|
151
|
+
export function inArray(s: string, arr?: string[]): boolean {
|
|
152
|
+
if (!arr || arr.length === 0) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
for (const a of arr) {
|
|
156
|
+
if (s === a) {
|
|
157
|
+
return true;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return false;
|
|
161
|
+
}
|
|
134
162
|
/*
|
|
135
163
|
export function setValue<T>(obj: T, path: string, value: string): void {
|
|
136
164
|
const paths = path.split('.');
|