express-ext 0.1.5 → 0.1.10
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/GenericController.js +16 -8
- package/lib/GenericSearchController.js +0 -1
- package/lib/HealthController.js +7 -49
- package/lib/LoadController.js +0 -1
- package/lib/LoadSearchController.js +0 -1
- package/lib/LowCodeController.js +0 -1
- package/lib/SearchController.js +0 -1
- package/lib/edit.js +12 -13
- package/lib/health.js +5 -8
- package/lib/http.js +33 -7
- package/lib/index.js +2 -41
- package/lib/resources.js +2 -9
- package/lib/search.js +70 -58
- package/lib/search_func.js +0 -1
- package/lib/view.js +14 -9
- package/package.json +1 -1
- package/src/GenericController.ts +21 -13
- package/src/GenericSearchController.ts +3 -3
- package/src/HealthController.ts +1 -1
- package/src/LoadController.ts +6 -6
- package/src/LoadSearchController.ts +2 -2
- package/src/LowCodeController.ts +4 -4
- package/src/SearchController.ts +2 -2
- package/src/edit.ts +18 -18
- package/src/health.ts +3 -4
- package/src/http.ts +46 -23
- package/src/index.ts +7 -0
- package/src/metadata.ts +2 -1
- package/src/search.ts +75 -67
- package/src/search_func.ts +1 -1
- package/src/view.ts +17 -11
- package/tsconfig.json +1 -0
package/lib/search.js
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.format = exports.buildMetadata = exports.toCsv = exports.deletePageInfo = exports.getParameters = exports.setValue = exports.fromUrl = exports.fromRequest = exports.initializeConfig = exports.buildResult = exports.jsonResult = void 0;
|
|
4
3
|
function jsonResult(res, result, quick, fields, config) {
|
|
5
4
|
if (quick && fields && fields.length > 0) {
|
|
6
5
|
res.status(200).json(toCsv(fields, result)).end();
|
|
@@ -15,13 +14,17 @@ function buildResult(r, conf) {
|
|
|
15
14
|
return r;
|
|
16
15
|
}
|
|
17
16
|
var x = {};
|
|
18
|
-
|
|
19
|
-
x[
|
|
17
|
+
var li = (conf.list ? conf.list : 'list');
|
|
18
|
+
x[li] = r.list;
|
|
19
|
+
var to = (conf.total ? conf.total : 'total');
|
|
20
|
+
x[to] = r.total;
|
|
20
21
|
if (r.nextPageToken && r.nextPageToken.length > 0) {
|
|
21
|
-
|
|
22
|
+
var t = (conf.token ? conf.token : 'token');
|
|
23
|
+
x[t] = r.nextPageToken;
|
|
22
24
|
}
|
|
23
25
|
if (r.last) {
|
|
24
|
-
|
|
26
|
+
var l = (conf.last ? conf.last : 'last');
|
|
27
|
+
x[l] = r.last;
|
|
25
28
|
}
|
|
26
29
|
return x;
|
|
27
30
|
}
|
|
@@ -132,42 +135,43 @@ function setValue(obj, path, value) {
|
|
|
132
135
|
}
|
|
133
136
|
exports.setValue = setValue;
|
|
134
137
|
function getParameters(obj, config) {
|
|
138
|
+
var o = obj;
|
|
135
139
|
if (!config) {
|
|
136
140
|
var sfield = 'fields';
|
|
137
141
|
var fields = void 0;
|
|
138
|
-
var fs =
|
|
142
|
+
var fs = o[sfield];
|
|
139
143
|
if (fs && Array.isArray(fs)) {
|
|
140
144
|
fields = fs;
|
|
141
|
-
delete
|
|
145
|
+
delete o[sfield];
|
|
142
146
|
}
|
|
143
|
-
var refId =
|
|
147
|
+
var refId = o['refId'];
|
|
144
148
|
if (!refId) {
|
|
145
|
-
refId =
|
|
149
|
+
refId = o['nextPageToken'];
|
|
146
150
|
}
|
|
147
151
|
var r = { fields: fields, refId: refId };
|
|
148
|
-
var pageSize =
|
|
152
|
+
var pageSize = o['limit'];
|
|
149
153
|
if (!pageSize) {
|
|
150
|
-
pageSize =
|
|
154
|
+
pageSize = o['pageSize'];
|
|
151
155
|
}
|
|
152
156
|
if (pageSize && !isNaN(pageSize)) {
|
|
153
157
|
var ipageSize = Math.floor(parseFloat(pageSize));
|
|
154
158
|
if (ipageSize > 0) {
|
|
155
159
|
r.limit = ipageSize;
|
|
156
|
-
var skip =
|
|
160
|
+
var skip = o['skip'];
|
|
157
161
|
if (skip && !isNaN(skip)) {
|
|
158
162
|
var iskip = Math.floor(parseFloat(skip));
|
|
159
163
|
if (iskip >= 0) {
|
|
160
164
|
r.skip = iskip;
|
|
161
165
|
r.skipOrRefId = r.skip;
|
|
162
|
-
deletePageInfo(
|
|
166
|
+
deletePageInfo(o);
|
|
163
167
|
return r;
|
|
164
168
|
}
|
|
165
169
|
}
|
|
166
|
-
var pageIndex =
|
|
170
|
+
var pageIndex = o['page'];
|
|
167
171
|
if (!pageIndex) {
|
|
168
|
-
pageIndex =
|
|
172
|
+
pageIndex = o['pageIndex'];
|
|
169
173
|
if (!pageIndex) {
|
|
170
|
-
pageIndex =
|
|
174
|
+
pageIndex = o['pageNo'];
|
|
171
175
|
}
|
|
172
176
|
}
|
|
173
177
|
if (pageIndex && !isNaN(pageIndex)) {
|
|
@@ -175,39 +179,39 @@ function getParameters(obj, config) {
|
|
|
175
179
|
if (ipageIndex < 1) {
|
|
176
180
|
ipageIndex = 1;
|
|
177
181
|
}
|
|
178
|
-
var firstPageSize =
|
|
182
|
+
var firstPageSize = o['firstLimit'];
|
|
179
183
|
if (!firstPageSize) {
|
|
180
|
-
firstPageSize =
|
|
184
|
+
firstPageSize = o['firstPageSize'];
|
|
181
185
|
}
|
|
182
186
|
if (!firstPageSize) {
|
|
183
|
-
firstPageSize =
|
|
187
|
+
firstPageSize = o['initPageSize'];
|
|
184
188
|
}
|
|
185
189
|
if (firstPageSize && !isNaN(firstPageSize)) {
|
|
186
190
|
var ifirstPageSize = Math.floor(parseFloat(firstPageSize));
|
|
187
191
|
if (ifirstPageSize > 0) {
|
|
188
192
|
r.skip = ipageSize * (ipageIndex - 2) + ifirstPageSize;
|
|
189
193
|
r.skipOrRefId = r.skip;
|
|
190
|
-
deletePageInfo(
|
|
194
|
+
deletePageInfo(o);
|
|
191
195
|
return r;
|
|
192
196
|
}
|
|
193
197
|
}
|
|
194
198
|
r.skip = ipageSize * (ipageIndex - 1);
|
|
195
199
|
r.skipOrRefId = r.skip;
|
|
196
|
-
deletePageInfo(
|
|
200
|
+
deletePageInfo(o);
|
|
197
201
|
return r;
|
|
198
202
|
}
|
|
199
203
|
r.skip = 0;
|
|
200
204
|
if (r.refId && r.refId.length > 0) {
|
|
201
205
|
r.skipOrRefId = r.refId;
|
|
202
206
|
}
|
|
203
|
-
deletePageInfo(
|
|
207
|
+
deletePageInfo(o);
|
|
204
208
|
return r;
|
|
205
209
|
}
|
|
206
210
|
}
|
|
207
211
|
if (r.refId && r.refId.length > 0) {
|
|
208
212
|
r.skipOrRefId = r.refId;
|
|
209
213
|
}
|
|
210
|
-
deletePageInfo(
|
|
214
|
+
deletePageInfo(o);
|
|
211
215
|
return r;
|
|
212
216
|
}
|
|
213
217
|
else {
|
|
@@ -216,22 +220,22 @@ function getParameters(obj, config) {
|
|
|
216
220
|
sfield = 'fields';
|
|
217
221
|
}
|
|
218
222
|
var fields = void 0;
|
|
219
|
-
var fs =
|
|
223
|
+
var fs = o[sfield];
|
|
220
224
|
if (fs && Array.isArray(fs)) {
|
|
221
225
|
fields = fs;
|
|
222
|
-
delete
|
|
226
|
+
delete o[sfield];
|
|
223
227
|
}
|
|
224
228
|
var strRefId = config.refId;
|
|
225
229
|
if (!strRefId || strRefId.length === 0) {
|
|
226
230
|
strRefId = 'refId';
|
|
227
231
|
}
|
|
228
|
-
var refId =
|
|
232
|
+
var refId = o[strRefId];
|
|
229
233
|
var r = { fields: fields, refId: refId };
|
|
230
234
|
var strLimit = config.limit;
|
|
231
235
|
if (!strLimit || strLimit.length === 0) {
|
|
232
236
|
strLimit = 'limit';
|
|
233
237
|
}
|
|
234
|
-
var pageSize =
|
|
238
|
+
var pageSize = o[strLimit];
|
|
235
239
|
var arr = [config.page, config.limit, config.skip, config.refId, config.firstLimit];
|
|
236
240
|
if (pageSize && !isNaN(pageSize)) {
|
|
237
241
|
var ipageSize = Math.floor(parseFloat(pageSize));
|
|
@@ -241,13 +245,13 @@ function getParameters(obj, config) {
|
|
|
241
245
|
if (!strSkip || strSkip.length === 0) {
|
|
242
246
|
strSkip = 'skip';
|
|
243
247
|
}
|
|
244
|
-
var skip =
|
|
248
|
+
var skip = o[strSkip];
|
|
245
249
|
if (skip && !isNaN(skip)) {
|
|
246
250
|
var iskip = Math.floor(parseFloat(skip));
|
|
247
251
|
if (iskip >= 0) {
|
|
248
252
|
r.skip = iskip;
|
|
249
253
|
r.skipOrRefId = r.skip;
|
|
250
|
-
deletePageInfo(
|
|
254
|
+
deletePageInfo(o, arr);
|
|
251
255
|
return r;
|
|
252
256
|
}
|
|
253
257
|
}
|
|
@@ -255,7 +259,7 @@ function getParameters(obj, config) {
|
|
|
255
259
|
if (!strPage || strPage.length === 0) {
|
|
256
260
|
strPage = 'page';
|
|
257
261
|
}
|
|
258
|
-
var pageIndex =
|
|
262
|
+
var pageIndex = o[strPage];
|
|
259
263
|
if (pageIndex && !isNaN(pageIndex)) {
|
|
260
264
|
var ipageIndex = Math.floor(parseFloat(pageIndex));
|
|
261
265
|
if (ipageIndex < 1) {
|
|
@@ -265,33 +269,33 @@ function getParameters(obj, config) {
|
|
|
265
269
|
if (!strFirstLimit || strFirstLimit.length === 0) {
|
|
266
270
|
strFirstLimit = 'firstLimit';
|
|
267
271
|
}
|
|
268
|
-
var firstPageSize =
|
|
272
|
+
var firstPageSize = o[strFirstLimit];
|
|
269
273
|
if (firstPageSize && !isNaN(firstPageSize)) {
|
|
270
274
|
var ifirstPageSize = Math.floor(parseFloat(firstPageSize));
|
|
271
275
|
if (ifirstPageSize > 0) {
|
|
272
276
|
r.skip = ipageSize * (ipageIndex - 2) + ifirstPageSize;
|
|
273
277
|
r.skipOrRefId = r.skip;
|
|
274
|
-
deletePageInfo(
|
|
278
|
+
deletePageInfo(o, arr);
|
|
275
279
|
return r;
|
|
276
280
|
}
|
|
277
281
|
}
|
|
278
282
|
r.skip = ipageSize * (ipageIndex - 1);
|
|
279
283
|
r.skipOrRefId = r.skip;
|
|
280
|
-
deletePageInfo(
|
|
284
|
+
deletePageInfo(o, arr);
|
|
281
285
|
return r;
|
|
282
286
|
}
|
|
283
287
|
r.skip = 0;
|
|
284
288
|
if (r.refId && r.refId.length > 0) {
|
|
285
289
|
r.skipOrRefId = r.refId;
|
|
286
290
|
}
|
|
287
|
-
deletePageInfo(
|
|
291
|
+
deletePageInfo(o, arr);
|
|
288
292
|
return r;
|
|
289
293
|
}
|
|
290
294
|
}
|
|
291
295
|
if (r.refId && r.refId.length > 0) {
|
|
292
296
|
r.skipOrRefId = r.refId;
|
|
293
297
|
}
|
|
294
|
-
deletePageInfo(
|
|
298
|
+
deletePageInfo(o, arr);
|
|
295
299
|
return r;
|
|
296
300
|
}
|
|
297
301
|
}
|
|
@@ -394,7 +398,7 @@ exports.buildMetadata = buildMetadata;
|
|
|
394
398
|
var _datereg = '/Date(';
|
|
395
399
|
var _re = /-?\d+/;
|
|
396
400
|
function toDate(v) {
|
|
397
|
-
if (!v
|
|
401
|
+
if (!v) {
|
|
398
402
|
return null;
|
|
399
403
|
}
|
|
400
404
|
if (v instanceof Date) {
|
|
@@ -406,36 +410,43 @@ function toDate(v) {
|
|
|
406
410
|
var i = v.indexOf(_datereg);
|
|
407
411
|
if (i >= 0) {
|
|
408
412
|
var m = _re.exec(v);
|
|
409
|
-
|
|
410
|
-
|
|
413
|
+
if (m !== null) {
|
|
414
|
+
var d = parseInt(m[0], 10);
|
|
415
|
+
return new Date(d);
|
|
416
|
+
}
|
|
417
|
+
else {
|
|
418
|
+
return null;
|
|
419
|
+
}
|
|
411
420
|
}
|
|
412
421
|
else {
|
|
413
422
|
if (isNaN(v)) {
|
|
414
423
|
return new Date(v);
|
|
415
424
|
}
|
|
416
425
|
else {
|
|
417
|
-
var d = parseInt(v,
|
|
426
|
+
var d = parseInt(v, 10);
|
|
418
427
|
return new Date(d);
|
|
419
428
|
}
|
|
420
429
|
}
|
|
421
430
|
}
|
|
422
431
|
function format(obj, dates, nums) {
|
|
432
|
+
var o = obj;
|
|
423
433
|
if (dates && dates.length > 0) {
|
|
424
434
|
for (var _i = 0, dates_1 = dates; _i < dates_1.length; _i++) {
|
|
425
435
|
var s = dates_1[_i];
|
|
426
|
-
var v =
|
|
436
|
+
var v = o[s];
|
|
427
437
|
if (v) {
|
|
428
438
|
if (v instanceof Date) {
|
|
429
439
|
continue;
|
|
430
440
|
}
|
|
431
441
|
if (typeof v === 'string' || typeof v === 'number') {
|
|
432
442
|
var d = toDate(v);
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
443
|
+
if (d) {
|
|
444
|
+
if (!(d instanceof Date) || d.toString() === 'Invalid Date') {
|
|
445
|
+
delete o[s];
|
|
446
|
+
}
|
|
447
|
+
else {
|
|
448
|
+
o[s] = d;
|
|
449
|
+
}
|
|
439
450
|
}
|
|
440
451
|
}
|
|
441
452
|
else if (typeof v === 'object') {
|
|
@@ -448,12 +459,13 @@ function format(obj, dates, nums) {
|
|
|
448
459
|
}
|
|
449
460
|
if (typeof v2 === 'string' || typeof v2 === 'number') {
|
|
450
461
|
var d2 = toDate(v2);
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
462
|
+
if (d2) {
|
|
463
|
+
if (!(d2 instanceof Date) || d2.toString() === 'Invalid Date') {
|
|
464
|
+
delete v[key];
|
|
465
|
+
}
|
|
466
|
+
else {
|
|
467
|
+
v[key] = d2;
|
|
468
|
+
}
|
|
457
469
|
}
|
|
458
470
|
}
|
|
459
471
|
}
|
|
@@ -464,10 +476,10 @@ function format(obj, dates, nums) {
|
|
|
464
476
|
if (nums && nums.length > 0) {
|
|
465
477
|
for (var _b = 0, nums_1 = nums; _b < nums_1.length; _b++) {
|
|
466
478
|
var s = nums_1[_b];
|
|
467
|
-
var v =
|
|
479
|
+
var v = o[s];
|
|
468
480
|
if (v) {
|
|
469
481
|
if (v instanceof Date) {
|
|
470
|
-
delete
|
|
482
|
+
delete o[s];
|
|
471
483
|
continue;
|
|
472
484
|
}
|
|
473
485
|
if (typeof v === 'number') {
|
|
@@ -475,12 +487,12 @@ function format(obj, dates, nums) {
|
|
|
475
487
|
}
|
|
476
488
|
if (typeof v === 'string') {
|
|
477
489
|
if (!isNaN(v)) {
|
|
478
|
-
delete
|
|
490
|
+
delete o[s];
|
|
479
491
|
continue;
|
|
480
492
|
}
|
|
481
493
|
else {
|
|
482
494
|
var i = parseFloat(v);
|
|
483
|
-
|
|
495
|
+
o[s] = i;
|
|
484
496
|
}
|
|
485
497
|
}
|
|
486
498
|
else if (typeof v === 'object') {
|
|
@@ -489,7 +501,7 @@ function format(obj, dates, nums) {
|
|
|
489
501
|
var key = keys_4[_c];
|
|
490
502
|
var v2 = v[key];
|
|
491
503
|
if (v2 instanceof Date) {
|
|
492
|
-
delete
|
|
504
|
+
delete o[key];
|
|
493
505
|
continue;
|
|
494
506
|
}
|
|
495
507
|
if (typeof v2 === 'number') {
|
|
@@ -509,6 +521,6 @@ function format(obj, dates, nums) {
|
|
|
509
521
|
}
|
|
510
522
|
}
|
|
511
523
|
}
|
|
512
|
-
return
|
|
524
|
+
return o;
|
|
513
525
|
}
|
|
514
526
|
exports.format = format;
|
package/lib/search_func.js
CHANGED
package/lib/view.js
CHANGED
|
@@ -1,30 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.buildKeys = exports.buildId = exports.buildAndCheckId = void 0;
|
|
4
3
|
function buildAndCheckId(req, res, keys) {
|
|
5
4
|
var id = buildId(req, keys);
|
|
6
5
|
if (!id) {
|
|
7
6
|
res.status(400).end('invalid parameters');
|
|
8
|
-
return
|
|
7
|
+
return undefined;
|
|
9
8
|
}
|
|
10
9
|
return id;
|
|
11
10
|
}
|
|
12
11
|
exports.buildAndCheckId = buildAndCheckId;
|
|
13
12
|
function buildId(req, attrs) {
|
|
14
|
-
if (!attrs) {
|
|
13
|
+
if (!attrs || attrs.length === 0) {
|
|
15
14
|
var id = req.params['id'];
|
|
16
15
|
if (id && id.length > 0) {
|
|
17
16
|
return id;
|
|
18
17
|
}
|
|
19
|
-
return
|
|
18
|
+
return undefined;
|
|
20
19
|
}
|
|
21
20
|
if (attrs && attrs.length === 1) {
|
|
22
|
-
var
|
|
23
|
-
var
|
|
21
|
+
var id = req.params['id'];
|
|
22
|
+
var n = attrs[0].name;
|
|
23
|
+
if ((!id || id.length === 0) && n && n.length > 0) {
|
|
24
|
+
id = req.params[n];
|
|
25
|
+
}
|
|
24
26
|
if (id && id.length > 0) {
|
|
25
27
|
if (attrs[0].type === 'integer' || attrs[0].type === 'number') {
|
|
26
28
|
if (isNaN(id)) {
|
|
27
|
-
return
|
|
29
|
+
return undefined;
|
|
28
30
|
}
|
|
29
31
|
var v = parseFloat(id);
|
|
30
32
|
return v;
|
|
@@ -35,13 +37,16 @@ function buildId(req, attrs) {
|
|
|
35
37
|
var ids = {};
|
|
36
38
|
for (var _i = 0, attrs_1 = attrs; _i < attrs_1.length; _i++) {
|
|
37
39
|
var attr = attrs_1[_i];
|
|
40
|
+
if (!attr.name) {
|
|
41
|
+
return undefined;
|
|
42
|
+
}
|
|
38
43
|
var v = req.params[attr.name];
|
|
39
44
|
if (!v) {
|
|
40
|
-
return
|
|
45
|
+
return undefined;
|
|
41
46
|
}
|
|
42
47
|
if (attr.type === 'integer' || attr.type === 'number') {
|
|
43
48
|
if (isNaN(v)) {
|
|
44
|
-
return
|
|
49
|
+
return undefined;
|
|
45
50
|
}
|
|
46
51
|
ids[attr.name] = parseFloat(v);
|
|
47
52
|
}
|
package/package.json
CHANGED
package/src/GenericController.ts
CHANGED
|
@@ -7,8 +7,8 @@ import {resources} from './resources';
|
|
|
7
7
|
import {buildAndCheckId, buildId} from './view';
|
|
8
8
|
|
|
9
9
|
export interface GenericService<T, ID, R> {
|
|
10
|
-
metadata?(): Attributes;
|
|
11
|
-
load(id: ID, ctx?: any): Promise<T>;
|
|
10
|
+
metadata?(): Attributes|undefined;
|
|
11
|
+
load(id: ID, ctx?: any): Promise<T|null>;
|
|
12
12
|
insert(obj: T, ctx?: any): Promise<R>;
|
|
13
13
|
update(obj: T, ctx?: any): Promise<R>;
|
|
14
14
|
patch?(obj: T, ctx?: any): Promise<R>;
|
|
@@ -16,8 +16,8 @@ export interface GenericService<T, ID, R> {
|
|
|
16
16
|
}
|
|
17
17
|
export class GenericController<T, ID> extends LoadController<T, ID> {
|
|
18
18
|
status: StatusConfig;
|
|
19
|
-
metadata
|
|
20
|
-
constructor(log: (msg:
|
|
19
|
+
metadata?: Attributes;
|
|
20
|
+
constructor(log: (msg: any, ctx?: any) => void, 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) {
|
|
@@ -43,27 +43,31 @@ export class GenericController<T, ID> extends LoadController<T, ID> {
|
|
|
43
43
|
validateAndCreate(req, res, this.status, this.service.insert, this.log, this.validate);
|
|
44
44
|
}
|
|
45
45
|
update(req: Request, res: Response) {
|
|
46
|
-
const id = buildAndCheckIdWithBody<T, ID>(req, res, this.keys);
|
|
46
|
+
const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.update);
|
|
47
47
|
if (id) {
|
|
48
48
|
validateAndUpdate(res, this.status, req.body, false, this.service.update, this.log, this.validate);
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
patch(req: Request, res: Response) {
|
|
52
|
-
const id = buildAndCheckIdWithBody<T, ID>(req, res, this.keys);
|
|
53
|
-
if (id) {
|
|
52
|
+
const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.patch);
|
|
53
|
+
if (id && this.service.patch) {
|
|
54
54
|
validateAndUpdate(res, this.status, req.body, true, this.service.patch, this.log, this.validate);
|
|
55
55
|
}
|
|
56
56
|
}
|
|
57
57
|
delete(req: Request, res: Response) {
|
|
58
58
|
const id = buildAndCheckId<ID>(req, res, this.keys);
|
|
59
59
|
if (id) {
|
|
60
|
-
this.service.delete
|
|
61
|
-
res.status(
|
|
62
|
-
}
|
|
60
|
+
if (!this.service.delete) {
|
|
61
|
+
res.status(405).end('Method Not Allowed');
|
|
62
|
+
} else {
|
|
63
|
+
this.service.delete(id).then(count => {
|
|
64
|
+
res.status(getDeleteStatus(count)).json(count).end();
|
|
65
|
+
}).catch(err => handleError(err, res, this.log));
|
|
66
|
+
}
|
|
63
67
|
}
|
|
64
68
|
}
|
|
65
69
|
}
|
|
66
|
-
export function validateAndCreate<T>(req: Request, res: Response, status: StatusConfig, save: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log: (msg:
|
|
70
|
+
export function validateAndCreate<T>(req: Request, res: Response, status: StatusConfig, save: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log: (msg: any, ctx?: any) => void, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>): void {
|
|
67
71
|
const obj = req.body;
|
|
68
72
|
if (!obj || obj === '') {
|
|
69
73
|
return res.status(400).end('The request body cannot be empty.');
|
|
@@ -81,7 +85,7 @@ export function validateAndCreate<T>(req: Request, res: Response, status: Status
|
|
|
81
85
|
create(res, status, obj, save, log);
|
|
82
86
|
}
|
|
83
87
|
}
|
|
84
|
-
export function validateAndUpdate<T>(res: Response, status: StatusConfig, obj: T, isPatch: boolean, save: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log: (msg:
|
|
88
|
+
export function validateAndUpdate<T>(res: Response, status: StatusConfig, obj: T, isPatch: boolean, save: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log: (msg: any, ctx?: any) => void, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>): void {
|
|
85
89
|
if (validate) {
|
|
86
90
|
validate(obj, isPatch).then(errors => {
|
|
87
91
|
if (errors && errors.length > 0) {
|
|
@@ -95,12 +99,16 @@ export function validateAndUpdate<T>(res: Response, status: StatusConfig, obj: T
|
|
|
95
99
|
update(res, status, obj, save, log);
|
|
96
100
|
}
|
|
97
101
|
}
|
|
98
|
-
export function buildAndCheckIdWithBody<T, ID>(req: Request, res: Response, keys?: Attribute[]): ID {
|
|
102
|
+
export function buildAndCheckIdWithBody<T, ID, R>(req: Request, res: Response, keys?: Attribute[], patch?: (obj: T, ctx?: any) => Promise<R>): ID | undefined {
|
|
99
103
|
const obj = req.body;
|
|
100
104
|
if (!obj || obj === '') {
|
|
101
105
|
res.status(400).end('The request body cannot be empty.');
|
|
102
106
|
return undefined;
|
|
103
107
|
}
|
|
108
|
+
if (!patch) {
|
|
109
|
+
res.status(405).end('Method Not Allowed');
|
|
110
|
+
return undefined;
|
|
111
|
+
}
|
|
104
112
|
const id = buildId<ID>(req, keys);
|
|
105
113
|
if (!id) {
|
|
106
114
|
res.status(400).end('Invalid parameters');
|
|
@@ -3,19 +3,19 @@ import {ResultInfo, StatusConfig} from './edit';
|
|
|
3
3
|
import {GenericController, GenericService} from './GenericController';
|
|
4
4
|
import {handleError} from './http';
|
|
5
5
|
import {ErrorMessage} from './metadata';
|
|
6
|
-
import {format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig,
|
|
6
|
+
import {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 {
|
|
10
10
|
}
|
|
11
|
-
export class GenericSearchController<T, ID, S extends
|
|
11
|
+
export class GenericSearchController<T, ID, S extends Filter> extends GenericController<T, ID> {
|
|
12
12
|
config?: SearchConfig;
|
|
13
13
|
csv?: boolean;
|
|
14
14
|
dates?: string[];
|
|
15
15
|
numbers?: string[];
|
|
16
16
|
fields?: string;
|
|
17
17
|
excluding?: string;
|
|
18
|
-
constructor(log: (msg:
|
|
18
|
+
constructor(log: (msg: any, ctx?: any) => void, 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
19
|
super(log, service, config, validate);
|
|
20
20
|
this.search = this.search.bind(this);
|
|
21
21
|
this.config = initializeConfig(config);
|
package/src/HealthController.ts
CHANGED
|
@@ -5,7 +5,7 @@ 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
9
|
check(this.checkers).then(heath => {
|
|
10
10
|
if (heath.status === 'UP') {
|
|
11
11
|
return res.status(200).json(heath).end();
|
package/src/LoadController.ts
CHANGED
|
@@ -4,16 +4,16 @@ import {Attribute, Attributes} from './metadata';
|
|
|
4
4
|
import {buildAndCheckId, buildKeys} from './view';
|
|
5
5
|
|
|
6
6
|
export interface ViewService<T, ID> {
|
|
7
|
-
metadata?(): Attributes;
|
|
8
|
-
load(id: ID, ctx?: any): Promise<T>;
|
|
7
|
+
metadata?(): Attributes|undefined;
|
|
8
|
+
load(id: ID, ctx?: any): Promise<T|null>;
|
|
9
9
|
}
|
|
10
|
-
function getViewFunc<T, ID>(viewService: ViewService<T, ID> | ((id: ID, ctx?: any) => Promise<T>)): (id: ID, ctx?: any) => Promise<T> {
|
|
10
|
+
function getViewFunc<T, ID>(viewService: ViewService<T, ID> | ((id: ID, ctx?: any) => Promise<T|null>)): (id: ID, ctx?: any) => Promise<T|null> {
|
|
11
11
|
if (typeof viewService === 'function') {
|
|
12
12
|
return viewService;
|
|
13
13
|
}
|
|
14
14
|
return viewService.load;
|
|
15
15
|
}
|
|
16
|
-
function getKeysFunc<T, ID>(viewService: ViewService<T, ID> | ((id: ID, ctx?: any) => Promise<T>), keys?: Attributes|Attribute[]|string[]): Attribute[] {
|
|
16
|
+
function getKeysFunc<T, ID>(viewService: ViewService<T, ID> | ((id: ID, ctx?: any) => Promise<T>), keys?: Attributes|Attribute[]|string[]): Attribute[] | undefined {
|
|
17
17
|
if (keys) {
|
|
18
18
|
if (Array.isArray(keys)) {
|
|
19
19
|
if (keys.length > 0) {
|
|
@@ -38,8 +38,8 @@ function getKeysFunc<T, ID>(viewService: ViewService<T, ID> | ((id: ID, ctx?: an
|
|
|
38
38
|
}
|
|
39
39
|
export class LoadController<T, ID> {
|
|
40
40
|
protected keys?: Attribute[];
|
|
41
|
-
protected view: (id: ID, ctx?: any) => Promise<T>;
|
|
42
|
-
constructor(protected log: (msg: any, ctx?: any) => void, viewService: ViewService<T, ID> | ((id: ID, ctx?: any) => Promise<T>), keys?: Attributes|Attribute[]|string[]) {
|
|
41
|
+
protected view: (id: ID, ctx?: any) => Promise<T|null>;
|
|
42
|
+
constructor(protected log: (msg: any, ctx?: any) => void, 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);
|
|
@@ -2,10 +2,10 @@ import {Request, Response} from 'express';
|
|
|
2
2
|
import {handleError} from './http';
|
|
3
3
|
import {LoadController, ViewService} from './LoadController';
|
|
4
4
|
import {Attribute, Attributes} from './metadata';
|
|
5
|
-
import {format, fromRequest, getParameters, initializeConfig, jsonResult,
|
|
5
|
+
import {Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
|
|
6
6
|
import {getMetadataFunc} from './search_func';
|
|
7
7
|
|
|
8
|
-
export class LoadSearchController<T, ID, S extends
|
|
8
|
+
export class LoadSearchController<T, ID, S extends Filter> extends LoadController<T, ID> {
|
|
9
9
|
config?: SearchConfig;
|
|
10
10
|
csv?: boolean;
|
|
11
11
|
dates?: string[];
|
package/src/LowCodeController.ts
CHANGED
|
@@ -3,22 +3,22 @@ import {ResultInfo, StatusConfig} from './edit';
|
|
|
3
3
|
import {GenericController, GenericService} from './GenericController';
|
|
4
4
|
import {handleError} from './http';
|
|
5
5
|
import {ErrorMessage} from './metadata';
|
|
6
|
-
import {format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig,
|
|
6
|
+
import {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 {
|
|
10
10
|
}
|
|
11
|
-
export interface
|
|
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 LowCodeController<T, ID, S extends
|
|
14
|
+
export class LowCodeController<T, ID, S extends Filter> extends GenericController<T, ID> {
|
|
15
15
|
config?: SearchConfig;
|
|
16
16
|
csv?: boolean;
|
|
17
17
|
dates?: string[];
|
|
18
18
|
numbers?: string[];
|
|
19
19
|
fields?: string;
|
|
20
20
|
excluding?: string;
|
|
21
|
-
constructor(log: (msg:
|
|
21
|
+
constructor(log: (msg: any, ctx?: any) => void, public lowCodeService: Service<T, ID, number|ResultInfo<T>, S>, config?: LowCodeConfig, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
|
|
22
22
|
super(log, lowCodeService, config, validate);
|
|
23
23
|
this.search = this.search.bind(this);
|
|
24
24
|
this.config = initializeConfig(config);
|
package/src/SearchController.ts
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import {Request, Response} from 'express';
|
|
2
2
|
import {handleError} from './http';
|
|
3
|
-
import {format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig,
|
|
3
|
+
import {Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
|
|
4
4
|
|
|
5
|
-
export class SearchController<T, S extends
|
|
5
|
+
export class SearchController<T, S extends Filter> {
|
|
6
6
|
config?: SearchConfig;
|
|
7
7
|
csv?: boolean;
|
|
8
8
|
fields?: string;
|