express-ext 0.1.16 → 0.1.19
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/LoadController.js +1 -1
- package/lib/LowCodeController.js +6 -6
- package/lib/http.js +41 -0
- package/lib/index.js +1 -3
- package/lib/log.js +36 -1
- package/lib/resources.js +36 -1
- package/lib/search.js +2 -1
- package/package.json +1 -1
- package/src/LoadController.ts +2 -2
- package/src/LowCodeController.ts +1 -1
- package/src/http.ts +35 -0
- package/src/index.ts +4 -4
- package/src/log.ts +35 -1
- package/src/resources.ts +54 -2
- package/src/search.ts +2 -1
package/lib/LoadController.js
CHANGED
|
@@ -45,7 +45,7 @@ var LoadController = (function () {
|
|
|
45
45
|
var id = view_1.buildAndCheckId(req, res, this.keys);
|
|
46
46
|
if (id) {
|
|
47
47
|
this.view(id)
|
|
48
|
-
.then(function (obj) { return http_1.respondModel(obj, res); })
|
|
48
|
+
.then(function (obj) { return http_1.respondModel(http_1.minimize(obj), res); })
|
|
49
49
|
.catch(function (err) { return http_1.handleError(err, res, _this.log); });
|
|
50
50
|
}
|
|
51
51
|
};
|
package/lib/LowCodeController.js
CHANGED
|
@@ -17,9 +17,9 @@ var GenericController_1 = require("./GenericController");
|
|
|
17
17
|
var http_1 = require("./http");
|
|
18
18
|
var search_1 = require("./search");
|
|
19
19
|
var search_func_1 = require("./search_func");
|
|
20
|
-
var
|
|
21
|
-
__extends(
|
|
22
|
-
function
|
|
20
|
+
var Controller = (function (_super) {
|
|
21
|
+
__extends(Controller, _super);
|
|
22
|
+
function Controller(log, lowCodeService, config, validate, dates, numbers) {
|
|
23
23
|
var _this = _super.call(this, log, lowCodeService, config, validate) || this;
|
|
24
24
|
_this.lowCodeService = lowCodeService;
|
|
25
25
|
_this.search = _this.search.bind(_this);
|
|
@@ -39,7 +39,7 @@ var LowCodeController = (function (_super) {
|
|
|
39
39
|
}
|
|
40
40
|
return _this;
|
|
41
41
|
}
|
|
42
|
-
|
|
42
|
+
Controller.prototype.search = function (req, res) {
|
|
43
43
|
var _this = this;
|
|
44
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);
|
|
@@ -48,6 +48,6 @@ var LowCodeController = (function (_super) {
|
|
|
48
48
|
.then(function (result) { return search_1.jsonResult(res, result, _this.csv, l.fields, _this.config); })
|
|
49
49
|
.catch(function (err) { return http_1.handleError(err, res, _this.log); });
|
|
50
50
|
};
|
|
51
|
-
return
|
|
51
|
+
return Controller;
|
|
52
52
|
}(GenericController_1.GenericController));
|
|
53
|
-
exports.
|
|
53
|
+
exports.Controller = Controller;
|
package/lib/http.js
CHANGED
|
@@ -10,6 +10,7 @@ function handleError(err, res, log) {
|
|
|
10
10
|
}
|
|
11
11
|
}
|
|
12
12
|
exports.handleError = handleError;
|
|
13
|
+
exports.error = handleError;
|
|
13
14
|
function toString(v) {
|
|
14
15
|
if (typeof v === 'string') {
|
|
15
16
|
return v;
|
|
@@ -174,6 +175,7 @@ function param(req, res, name) {
|
|
|
174
175
|
return v;
|
|
175
176
|
}
|
|
176
177
|
exports.param = param;
|
|
178
|
+
exports.getParam = param;
|
|
177
179
|
function params(req, name, d, split) {
|
|
178
180
|
var v = req.params[name];
|
|
179
181
|
if (!v || v.length === 0) {
|
|
@@ -185,6 +187,7 @@ function params(req, name, d, split) {
|
|
|
185
187
|
return v.split(split);
|
|
186
188
|
}
|
|
187
189
|
exports.params = params;
|
|
190
|
+
exports.getParams = params;
|
|
188
191
|
function getRequiredParameters(req, res, name, split) {
|
|
189
192
|
var v = req.params[name];
|
|
190
193
|
if (!v || v.length === 0) {
|
|
@@ -262,3 +265,41 @@ function getDate(req, name, d) {
|
|
|
262
265
|
return date;
|
|
263
266
|
}
|
|
264
267
|
exports.getDate = getDate;
|
|
268
|
+
var o = 'object';
|
|
269
|
+
function minimize(obj) {
|
|
270
|
+
if (!obj || typeof obj !== o) {
|
|
271
|
+
return obj;
|
|
272
|
+
}
|
|
273
|
+
var keys = Object.keys(obj);
|
|
274
|
+
for (var _i = 0, keys_2 = keys; _i < keys_2.length; _i++) {
|
|
275
|
+
var key = keys_2[_i];
|
|
276
|
+
var v = obj[key];
|
|
277
|
+
if (v == null) {
|
|
278
|
+
delete obj[key];
|
|
279
|
+
}
|
|
280
|
+
else if (Array.isArray(v) && v.length > 0) {
|
|
281
|
+
var v1 = v[0];
|
|
282
|
+
if (typeof v1 === o && !(v1 instanceof Date)) {
|
|
283
|
+
for (var _a = 0, v_1 = v; _a < v_1.length; _a++) {
|
|
284
|
+
var item = v_1[_a];
|
|
285
|
+
minimize(item);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
return obj;
|
|
291
|
+
}
|
|
292
|
+
exports.minimize = minimize;
|
|
293
|
+
function minimizeArray(arrs) {
|
|
294
|
+
if (!arrs) {
|
|
295
|
+
return arrs;
|
|
296
|
+
}
|
|
297
|
+
if (arrs.length > 0) {
|
|
298
|
+
for (var _i = 0, arrs_1 = arrs; _i < arrs_1.length; _i++) {
|
|
299
|
+
var obj = arrs_1[_i];
|
|
300
|
+
minimize(obj);
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
return arrs;
|
|
304
|
+
}
|
|
305
|
+
exports.minimizeArray = minimizeArray;
|
package/lib/index.js
CHANGED
|
@@ -1,3 +1 @@
|
|
|
1
|
-
"use strict";function __export(
|
|
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
|
|
1
|
+
"use strict";function __export(r){for(var e in r)exports.hasOwnProperty(e)||(exports[e]=r[e])}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.Controller,exports.LowCodeController=LowCodeController_1.Controller;var SearchController_1=require("./SearchController");function allow(r){return function(e,o,l){o.header("Access-Control-Allow-Origin",r.origin),o.header("Access-Control-Allow-Credentials",r.credentials),o.header("Access-Control-Allow-Methods",r.methods),o.setHeader("Access-Control-Allow-Headers",r.headers),l()}}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")),exports.allow=allow;
|
package/lib/log.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
var stream_1 = require("stream");
|
|
4
|
+
var resources_1 = require("./resources");
|
|
4
5
|
function createConfig(c) {
|
|
5
6
|
if (!c) {
|
|
6
7
|
return { skips: [], duration: 'duration', request: '', response: '', status: '', size: '' };
|
|
@@ -78,7 +79,7 @@ var MiddlewareLogger = (function () {
|
|
|
78
79
|
op[x_1] = JSON.stringify(req.body);
|
|
79
80
|
}
|
|
80
81
|
if (_this.conf.response.length > 0) {
|
|
81
|
-
var rsBody = Buffer.concat(chunks_1).toString();
|
|
82
|
+
var rsBody = Buffer.concat(chunks_1).toString(resources_1.resources.encoding);
|
|
82
83
|
op[_this.conf.response] = rsBody;
|
|
83
84
|
}
|
|
84
85
|
if (_this.conf.status.length > 0) {
|
|
@@ -210,3 +211,37 @@ function isValidSkips(s) {
|
|
|
210
211
|
return true;
|
|
211
212
|
}
|
|
212
213
|
exports.isValidSkips = isValidSkips;
|
|
214
|
+
function mask(s, start, end, replace) {
|
|
215
|
+
if (start < 0) {
|
|
216
|
+
start = 0;
|
|
217
|
+
}
|
|
218
|
+
if (end < 0) {
|
|
219
|
+
end = 0;
|
|
220
|
+
}
|
|
221
|
+
var t = start + end;
|
|
222
|
+
if (t >= s.length) {
|
|
223
|
+
return replace.repeat(s.length);
|
|
224
|
+
}
|
|
225
|
+
return s.substr(0, start) + replace.repeat(s.length - t) + s.substr(s.length - end);
|
|
226
|
+
}
|
|
227
|
+
exports.mask = mask;
|
|
228
|
+
function margin(s, start, end, replace) {
|
|
229
|
+
if (start >= end) {
|
|
230
|
+
return '';
|
|
231
|
+
}
|
|
232
|
+
if (start < 0) {
|
|
233
|
+
start = 0;
|
|
234
|
+
}
|
|
235
|
+
if (end < 0) {
|
|
236
|
+
end = 0;
|
|
237
|
+
}
|
|
238
|
+
if (start >= s.length) {
|
|
239
|
+
return replace.repeat(s.length);
|
|
240
|
+
}
|
|
241
|
+
if (end >= s.length) {
|
|
242
|
+
return replace.repeat(start) + s.substr(start);
|
|
243
|
+
}
|
|
244
|
+
return replace.repeat(start) + s.substr(start, end - start) + replace.repeat(s.length - end);
|
|
245
|
+
}
|
|
246
|
+
exports.margin = margin;
|
|
247
|
+
exports.maskMargin = margin;
|
package/lib/resources.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
var fs = require("fs");
|
|
4
|
+
var http = require("http");
|
|
5
|
+
var https = require("https");
|
|
4
6
|
var resources = (function () {
|
|
5
7
|
function resources() {
|
|
6
8
|
}
|
|
9
|
+
resources.encoding = 'utf-8';
|
|
7
10
|
return resources;
|
|
8
11
|
}());
|
|
9
12
|
exports.resources = resources;
|
|
@@ -16,7 +19,7 @@ var TypeChecker = (function () {
|
|
|
16
19
|
TypeChecker.prototype.check = function (req, res, next) {
|
|
17
20
|
var obj = req.body;
|
|
18
21
|
if (!obj || obj === '') {
|
|
19
|
-
|
|
22
|
+
res.status(400).end('The request body cannot be empty');
|
|
20
23
|
}
|
|
21
24
|
else {
|
|
22
25
|
var errors = resources.check(obj, this.attributes, this.allowUndefined);
|
|
@@ -52,3 +55,35 @@ function loadTemplates(ok, buildTemplates, correct, files) {
|
|
|
52
55
|
return buildTemplates(mappers, correct);
|
|
53
56
|
}
|
|
54
57
|
exports.loadTemplates = loadTemplates;
|
|
58
|
+
function start(a, s) {
|
|
59
|
+
process.on('uncaughtException', function (err) {
|
|
60
|
+
console.log(err);
|
|
61
|
+
});
|
|
62
|
+
if (s.https) {
|
|
63
|
+
if (s.options) {
|
|
64
|
+
https.createServer(s.options, a).listen(s.port, function () {
|
|
65
|
+
console.log('Use https and start server at port ' + s.port);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
else if (s.key && s.cert && s.key.length > 0 && s.cert.length > 0) {
|
|
69
|
+
var options = {
|
|
70
|
+
key: fs.readFileSync(s.key),
|
|
71
|
+
cert: fs.readFileSync(s.cert)
|
|
72
|
+
};
|
|
73
|
+
https.createServer(options, a).listen(s.port, function () {
|
|
74
|
+
console.log('Use https and start server at port ' + s.port);
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
http.createServer(a).listen(s.port, function () {
|
|
79
|
+
console.log('Start server at port ' + s.port);
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
http.createServer(a).listen(s.port, function () {
|
|
85
|
+
console.log('Start server at port ' + s.port);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.start = start;
|
package/lib/search.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var http_1 = require("./http");
|
|
3
4
|
function jsonResult(res, result, quick, fields, config) {
|
|
4
5
|
if (quick && fields && fields.length > 0) {
|
|
5
6
|
res.status(200).json(toCsv(fields, result)).end();
|
|
@@ -15,7 +16,7 @@ function buildResult(r, conf) {
|
|
|
15
16
|
}
|
|
16
17
|
var x = {};
|
|
17
18
|
var li = (conf.list ? conf.list : 'list');
|
|
18
|
-
x[li] = r.list;
|
|
19
|
+
x[li] = http_1.minimizeArray(r.list);
|
|
19
20
|
var to = (conf.total ? conf.total : 'total');
|
|
20
21
|
x[to] = r.total;
|
|
21
22
|
if (r.nextPageToken && r.nextPageToken.length > 0) {
|
package/package.json
CHANGED
package/src/LoadController.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {Request, Response} from 'express';
|
|
2
|
-
import {attrs, handleError, Log, respondModel} from './http';
|
|
2
|
+
import {attrs, handleError, Log, minimize, respondModel} from './http';
|
|
3
3
|
import {Attribute, Attributes} from './metadata';
|
|
4
4
|
import {buildAndCheckId, buildKeys} from './view';
|
|
5
5
|
|
|
@@ -48,7 +48,7 @@ export class LoadController<T, ID> {
|
|
|
48
48
|
const id = buildAndCheckId<ID>(req, res, this.keys);
|
|
49
49
|
if (id) {
|
|
50
50
|
this.view(id)
|
|
51
|
-
.then(obj => respondModel(obj, res))
|
|
51
|
+
.then(obj => respondModel(minimize(obj), res))
|
|
52
52
|
.catch(err => handleError(err, res, this.log));
|
|
53
53
|
}
|
|
54
54
|
}
|
package/src/LowCodeController.ts
CHANGED
|
@@ -11,7 +11,7 @@ export interface LowCodeConfig extends StatusConfig, SearchConfig {
|
|
|
11
11
|
export interface Service<T, ID, R, S extends Filter> extends GenericService<T, ID, R> {
|
|
12
12
|
search: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>;
|
|
13
13
|
}
|
|
14
|
-
export class
|
|
14
|
+
export class Controller<T, ID, S extends Filter> extends GenericController<T, ID> {
|
|
15
15
|
config?: SearchConfig;
|
|
16
16
|
csv?: boolean;
|
|
17
17
|
dates?: string[];
|
package/src/http.ts
CHANGED
|
@@ -11,6 +11,7 @@ export function handleError(err: any, res: Response, log?: (msg: string) => void
|
|
|
11
11
|
res.status(500).end(toString(err));
|
|
12
12
|
}
|
|
13
13
|
}
|
|
14
|
+
export const error = handleError;
|
|
14
15
|
export function toString(v: any): string {
|
|
15
16
|
if (typeof v === 'string') {
|
|
16
17
|
return v;
|
|
@@ -158,6 +159,7 @@ export function param(req: Request, res: Response, name: string): string|undefin
|
|
|
158
159
|
}
|
|
159
160
|
return v;
|
|
160
161
|
}
|
|
162
|
+
export const getParam = param;
|
|
161
163
|
export function params(req: Request, name: string, d?: string[], split?: string): string[]|undefined {
|
|
162
164
|
const v = req.params[name];
|
|
163
165
|
if (!v || v.length === 0) {
|
|
@@ -168,6 +170,7 @@ export function params(req: Request, name: string, d?: string[], split?: string)
|
|
|
168
170
|
}
|
|
169
171
|
return v.split(split);
|
|
170
172
|
}
|
|
173
|
+
export const getParams = params;
|
|
171
174
|
export function getRequiredParameters(req: Request, res: Response, name: string, split?: string): string[]|undefined {
|
|
172
175
|
const v = req.params[name];
|
|
173
176
|
if (!v || v.length === 0) {
|
|
@@ -239,3 +242,35 @@ export function getDate(req: Request, name: string, d?: Date): Date|undefined {
|
|
|
239
242
|
}
|
|
240
243
|
return date;
|
|
241
244
|
}
|
|
245
|
+
const o = 'object';
|
|
246
|
+
export function minimize(obj: any): any {
|
|
247
|
+
if (!obj || typeof obj !== o) {
|
|
248
|
+
return obj;
|
|
249
|
+
}
|
|
250
|
+
const keys = Object.keys(obj);
|
|
251
|
+
for (const key of keys) {
|
|
252
|
+
const v = obj[key];
|
|
253
|
+
if (v == null) {
|
|
254
|
+
delete obj[key];
|
|
255
|
+
} else if (Array.isArray(v) && v.length > 0) {
|
|
256
|
+
const v1 = v[0];
|
|
257
|
+
if (typeof v1 === o && !(v1 instanceof Date)) {
|
|
258
|
+
for (const item of v) {
|
|
259
|
+
minimize(item);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
return obj;
|
|
265
|
+
}
|
|
266
|
+
export function minimizeArray<T>(arrs: T[]): T[] {
|
|
267
|
+
if (!arrs) {
|
|
268
|
+
return arrs;
|
|
269
|
+
}
|
|
270
|
+
if (arrs.length > 0) {
|
|
271
|
+
for (const obj of arrs) {
|
|
272
|
+
minimize(obj);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
return arrs;
|
|
276
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -5,7 +5,7 @@ import {HealthController} from './HealthController';
|
|
|
5
5
|
import {LoadController} from './LoadController';
|
|
6
6
|
import {LoadSearchController} from './LoadSearchController';
|
|
7
7
|
import {LogController} from './LogController';
|
|
8
|
-
import {
|
|
8
|
+
import {Controller} from './LowCodeController';
|
|
9
9
|
import {Service} from './LowCodeController';
|
|
10
10
|
import {SearchController} from './SearchController';
|
|
11
11
|
|
|
@@ -19,9 +19,9 @@ export {GenericController as GenericHandler};
|
|
|
19
19
|
export {SearchController as SearchHandler};
|
|
20
20
|
export {LoadSearchController as LoadSearchHandler};
|
|
21
21
|
export {GenericSearchController as GenericSearchHandler};
|
|
22
|
-
export {
|
|
23
|
-
export {
|
|
24
|
-
export {
|
|
22
|
+
export {Controller as LowCodeHandler};
|
|
23
|
+
// export {Controller as Handler};
|
|
24
|
+
export {Controller as LowCodeController};
|
|
25
25
|
export {Service as LowCodeService};
|
|
26
26
|
|
|
27
27
|
export * from './health';
|
package/src/log.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { NextFunction } from 'express';
|
|
|
2
2
|
import { ParamsDictionary, Request, Response } from 'express-serve-static-core';
|
|
3
3
|
import { ParsedQs } from 'qs';
|
|
4
4
|
import { PassThrough } from 'stream';
|
|
5
|
+
import { resources } from './resources';
|
|
5
6
|
|
|
6
7
|
export interface LogConf {
|
|
7
8
|
log?: boolean;
|
|
@@ -99,7 +100,7 @@ export class MiddlewareLogger {
|
|
|
99
100
|
op[x] = JSON.stringify(req.body);
|
|
100
101
|
}
|
|
101
102
|
if (this.conf.response.length > 0) {
|
|
102
|
-
const rsBody = Buffer.concat(chunks).toString();
|
|
103
|
+
const rsBody = Buffer.concat(chunks).toString(resources.encoding);
|
|
103
104
|
op[this.conf.response] = rsBody;
|
|
104
105
|
}
|
|
105
106
|
if (this.conf.status.length > 0) {
|
|
@@ -153,6 +154,7 @@ const getDurationInMilliseconds = (start: [number, number] | undefined) => {
|
|
|
153
154
|
return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS;
|
|
154
155
|
};
|
|
155
156
|
|
|
157
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
156
158
|
export class MiddlewareController {
|
|
157
159
|
constructor(public logger: Middleware) {
|
|
158
160
|
this.config = this.config.bind(this);
|
|
@@ -215,3 +217,35 @@ export function isValidSkips(s: string[]): boolean {
|
|
|
215
217
|
}
|
|
216
218
|
return true;
|
|
217
219
|
}
|
|
220
|
+
export function mask(s: string, start: number, end: number, replace: string): string {
|
|
221
|
+
if (start < 0) {
|
|
222
|
+
start = 0;
|
|
223
|
+
}
|
|
224
|
+
if (end < 0) {
|
|
225
|
+
end = 0;
|
|
226
|
+
}
|
|
227
|
+
const t = start + end;
|
|
228
|
+
if (t >= s.length) {
|
|
229
|
+
return replace.repeat(s.length);
|
|
230
|
+
}
|
|
231
|
+
return s.substr(0, start) + replace.repeat(s.length - t) + s.substr(s.length - end);
|
|
232
|
+
}
|
|
233
|
+
export function margin(s: string, start: number, end: number, replace: string): string {
|
|
234
|
+
if (start >= end) {
|
|
235
|
+
return '';
|
|
236
|
+
}
|
|
237
|
+
if (start < 0) {
|
|
238
|
+
start = 0;
|
|
239
|
+
}
|
|
240
|
+
if (end < 0) {
|
|
241
|
+
end = 0;
|
|
242
|
+
}
|
|
243
|
+
if (start >= s.length) {
|
|
244
|
+
return replace.repeat(s.length);
|
|
245
|
+
}
|
|
246
|
+
if (end >= s.length) {
|
|
247
|
+
return replace.repeat(start) + s.substr(start);
|
|
248
|
+
}
|
|
249
|
+
return replace.repeat(start) + s.substr(start, end - start) + replace.repeat(s.length - end);
|
|
250
|
+
}
|
|
251
|
+
export const maskMargin = margin;
|
package/src/resources.ts
CHANGED
|
@@ -1,17 +1,21 @@
|
|
|
1
|
-
import { NextFunction, Request, Response } from 'express';
|
|
1
|
+
import { Application, NextFunction, Request, Response } from 'express';
|
|
2
2
|
import * as fs from 'fs';
|
|
3
|
+
import * as http from 'http';
|
|
4
|
+
import * as https from 'https';
|
|
3
5
|
import { Attributes, ErrorMessage } from './metadata';
|
|
4
6
|
|
|
5
7
|
// tslint:disable-next-line:class-name
|
|
6
8
|
export class resources {
|
|
7
9
|
static createValidator?: <T>(attributes: Attributes, allowUndefined?: boolean, max?: number) => Validator<T>;
|
|
8
10
|
static check: (obj: any, attributes: Attributes, allowUndefined?: boolean, patch?: boolean) => ErrorMessage[];
|
|
11
|
+
static encoding?: BufferEncoding = 'utf-8';
|
|
9
12
|
}
|
|
10
13
|
|
|
11
14
|
export interface Validator<T> {
|
|
12
15
|
validate(obj: T, patch?: boolean): Promise<ErrorMessage[]>;
|
|
13
16
|
}
|
|
14
17
|
|
|
18
|
+
// tslint:disable-next-line:max-classes-per-file
|
|
15
19
|
export class TypeChecker {
|
|
16
20
|
constructor(public attributes: Attributes, public allowUndefined?: boolean) {
|
|
17
21
|
this.check = this.check.bind(this);
|
|
@@ -19,7 +23,7 @@ export class TypeChecker {
|
|
|
19
23
|
check(req: Request, res: Response, next: NextFunction): void {
|
|
20
24
|
const obj = req.body;
|
|
21
25
|
if (!obj || (obj as any) === '') {
|
|
22
|
-
|
|
26
|
+
res.status(400).end('The request body cannot be empty');
|
|
23
27
|
} else {
|
|
24
28
|
const errors = resources.check(obj, this.attributes, this.allowUndefined);
|
|
25
29
|
if (errors.length > 0) {
|
|
@@ -35,6 +39,14 @@ export function check(attributes: Attributes, allowUndefined?: boolean): Handler
|
|
|
35
39
|
const x = new TypeChecker(attributes, allowUndefined);
|
|
36
40
|
return x.check;
|
|
37
41
|
}
|
|
42
|
+
export interface Parameter {
|
|
43
|
+
name: string;
|
|
44
|
+
type: string;
|
|
45
|
+
}
|
|
46
|
+
export interface StringFormat {
|
|
47
|
+
texts: string[];
|
|
48
|
+
parameters: Parameter[];
|
|
49
|
+
}
|
|
38
50
|
export interface Template {
|
|
39
51
|
name?: string | null;
|
|
40
52
|
text: string;
|
|
@@ -46,6 +58,11 @@ export interface TemplateNode {
|
|
|
46
58
|
property: string | null;
|
|
47
59
|
encode?: string | null;
|
|
48
60
|
value: string | null;
|
|
61
|
+
format: StringFormat;
|
|
62
|
+
array?: string | null;
|
|
63
|
+
separator?: string | null;
|
|
64
|
+
suffix?: string | null;
|
|
65
|
+
prefix?: string | null;
|
|
49
66
|
}
|
|
50
67
|
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
68
|
if (!ok) {
|
|
@@ -61,3 +78,38 @@ export function loadTemplates(ok: boolean|undefined, buildTemplates: (streams: s
|
|
|
61
78
|
}
|
|
62
79
|
return buildTemplates(mappers, correct);
|
|
63
80
|
}
|
|
81
|
+
export interface Server {
|
|
82
|
+
port: number;
|
|
83
|
+
https?: boolean;
|
|
84
|
+
options?: any;
|
|
85
|
+
key?: string;
|
|
86
|
+
cert?: string;
|
|
87
|
+
}
|
|
88
|
+
export function start(a: Application, s: Server): void {
|
|
89
|
+
process.on('uncaughtException', (err) => {
|
|
90
|
+
console.log(err);
|
|
91
|
+
});
|
|
92
|
+
if (s.https) {
|
|
93
|
+
if (s.options) {
|
|
94
|
+
https.createServer(s.options, a).listen(s.port, () => {
|
|
95
|
+
console.log('Use https and start server at port ' + s.port);
|
|
96
|
+
});
|
|
97
|
+
} else if (s.key && s.cert && s.key.length > 0 && s.cert.length > 0) {
|
|
98
|
+
const options = {
|
|
99
|
+
key: fs.readFileSync(s.key),
|
|
100
|
+
cert: fs.readFileSync(s.cert)
|
|
101
|
+
};
|
|
102
|
+
https.createServer(options, a).listen(s.port, () => {
|
|
103
|
+
console.log('Use https and start server at port ' + s.port);
|
|
104
|
+
});
|
|
105
|
+
} else {
|
|
106
|
+
http.createServer(a).listen(s.port, () => {
|
|
107
|
+
console.log('Start server at port ' + s.port);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
} else {
|
|
111
|
+
http.createServer(a).listen(s.port, () => {
|
|
112
|
+
console.log('Start server at port ' + s.port);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
}
|
package/src/search.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import {Request, Response} from 'express';
|
|
2
|
+
import {minimizeArray} from './http';
|
|
2
3
|
import {Attribute, Attributes} from './metadata';
|
|
3
4
|
|
|
4
5
|
export interface Filter {
|
|
@@ -41,7 +42,7 @@ export function buildResult<T>(r: SearchResult<T>, conf?: SearchConfig): any {
|
|
|
41
42
|
}
|
|
42
43
|
const x: any = {};
|
|
43
44
|
const li = (conf.list ? conf.list : 'list');
|
|
44
|
-
x[li] = r.list;
|
|
45
|
+
x[li] = minimizeArray(r.list);
|
|
45
46
|
const to = (conf.total ? conf.total : 'total');
|
|
46
47
|
x[to] = r.total;
|
|
47
48
|
if (r.nextPageToken && r.nextPageToken.length > 0) {
|