express-ext 0.1.6 → 0.1.11

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/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
- x[conf.list] = r.list;
19
- x[conf.total] = r.total;
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
- x[conf.token] = r.nextPageToken;
22
+ var t = (conf.token ? conf.token : 'token');
23
+ x[t] = r.nextPageToken;
22
24
  }
23
25
  if (r.last) {
24
- x[conf.last] = r.last;
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 = obj[sfield];
142
+ var fs = o[sfield];
139
143
  if (fs && Array.isArray(fs)) {
140
144
  fields = fs;
141
- delete obj[sfield];
145
+ delete o[sfield];
142
146
  }
143
- var refId = obj['refId'];
147
+ var refId = o['refId'];
144
148
  if (!refId) {
145
- refId = obj['nextPageToken'];
149
+ refId = o['nextPageToken'];
146
150
  }
147
151
  var r = { fields: fields, refId: refId };
148
- var pageSize = obj['limit'];
152
+ var pageSize = o['limit'];
149
153
  if (!pageSize) {
150
- pageSize = obj['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 = obj['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(obj);
166
+ deletePageInfo(o);
163
167
  return r;
164
168
  }
165
169
  }
166
- var pageIndex = obj['page'];
170
+ var pageIndex = o['page'];
167
171
  if (!pageIndex) {
168
- pageIndex = obj['pageIndex'];
172
+ pageIndex = o['pageIndex'];
169
173
  if (!pageIndex) {
170
- pageIndex = obj['pageNo'];
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 = obj['firstLimit'];
182
+ var firstPageSize = o['firstLimit'];
179
183
  if (!firstPageSize) {
180
- firstPageSize = obj['firstPageSize'];
184
+ firstPageSize = o['firstPageSize'];
181
185
  }
182
186
  if (!firstPageSize) {
183
- firstPageSize = obj['initPageSize'];
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(obj);
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(obj);
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(obj);
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(obj);
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 = obj[sfield];
223
+ var fs = o[sfield];
220
224
  if (fs && Array.isArray(fs)) {
221
225
  fields = fs;
222
- delete obj[sfield];
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 = obj[strRefId];
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 = obj[strLimit];
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 = obj[strSkip];
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(obj, arr);
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 = obj[strPage];
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 = obj[strFirstLimit];
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(obj, arr);
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(obj, arr);
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(obj, arr);
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(obj, arr);
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 || 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
- var d = parseInt(m[0], null);
410
- return new Date(d);
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, null);
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 = obj[s];
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
- var error = d.toString();
434
- if (!(d instanceof Date) || error === 'Invalid Date') {
435
- delete obj[s];
436
- }
437
- else {
438
- obj[s] = d;
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
- var error2 = d2.toString();
452
- if (!(d2 instanceof Date) || error2 === 'Invalid Date') {
453
- delete v[key];
454
- }
455
- else {
456
- v[key] = d2;
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 = obj[s];
479
+ var v = o[s];
468
480
  if (v) {
469
481
  if (v instanceof Date) {
470
- delete obj[s];
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 obj[s];
490
+ delete o[s];
479
491
  continue;
480
492
  }
481
493
  else {
482
494
  var i = parseFloat(v);
483
- obj[s] = i;
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 obj[key];
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 obj;
524
+ return o;
513
525
  }
514
526
  exports.format = format;
@@ -1,6 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getMetadataFunc = void 0;
4
3
  var search_1 = require("./search");
5
4
  function getMetadataFunc(viewService, dates, numbers, keys) {
6
5
  var m = { dates: dates, numbers: numbers };
package/lib/view.js CHANGED
@@ -1,22 +1,21 @@
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 null;
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 null;
18
+ return undefined;
20
19
  }
21
20
  if (attrs && attrs.length === 1) {
22
21
  var id = req.params['id'];
@@ -27,7 +26,7 @@ function buildId(req, attrs) {
27
26
  if (id && id.length > 0) {
28
27
  if (attrs[0].type === 'integer' || attrs[0].type === 'number') {
29
28
  if (isNaN(id)) {
30
- return null;
29
+ return undefined;
31
30
  }
32
31
  var v = parseFloat(id);
33
32
  return v;
@@ -38,13 +37,16 @@ function buildId(req, attrs) {
38
37
  var ids = {};
39
38
  for (var _i = 0, attrs_1 = attrs; _i < attrs_1.length; _i++) {
40
39
  var attr = attrs_1[_i];
40
+ if (!attr.name) {
41
+ return undefined;
42
+ }
41
43
  var v = req.params[attr.name];
42
44
  if (!v) {
43
- return null;
45
+ return undefined;
44
46
  }
45
47
  if (attr.type === 'integer' || attr.type === 'number') {
46
48
  if (isNaN(v)) {
47
- return null;
49
+ return undefined;
48
50
  }
49
51
  ids[attr.name] = parseFloat(v);
50
52
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.1.6",
3
+ "version": "0.1.11",
4
4
  "description": "express-ext",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -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: Attributes;
20
- constructor(log: (msg: string, ctx?: any) => void, public service: GenericService<T, ID, number|ResultInfo<T>>, status?: StatusConfig, public validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>) {
19
+ metadata?: Attributes;
20
+ constructor(log: (msg: string) => 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(id).then(count => {
61
- res.status(getDeleteStatus(count)).json(count).end();
62
- }).catch(err => handleError(err, res, this.log));
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: string, ctx?: any) => void, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>): void {
70
+ export function validateAndCreate<T>(req: Request, res: Response, status: StatusConfig, save: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log: (msg: string) => 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: string, ctx?: any) => void, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>): void {
88
+ export function validateAndUpdate<T>(res: Response, status: StatusConfig, obj: T, isPatch: boolean, save: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log: (msg: string) => 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, SearchModel, SearchResult} from './search';
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 SearchModel> extends GenericController<T, ID> {
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: string, 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[]) {
18
+ constructor(log: (msg: string) => 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);
@@ -5,7 +5,7 @@ export class HealthController {
5
5
  constructor(protected checkers: HealthChecker[]) {
6
6
  this.check = this.check.bind(this);
7
7
  }
8
- async check(req: Request, res: Response) {
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();
@@ -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: string) => 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,17 +2,17 @@ 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, Metadata, SearchConfig, SearchModel, SearchResult} from './search';
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 SearchModel> extends LoadController<T, ID> {
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[];
12
12
  numbers?: string[];
13
13
  fields?: string;
14
14
  excluding?: string;
15
- constructor(log: (msg: any, ctx?: any) => void, 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[]) {
15
+ constructor(log: (msg: string) => void, 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
16
  super(log, viewService, keys);
17
17
  this.search = this.search.bind(this);
18
18
  if (config) {
@@ -0,0 +1,70 @@
1
+ import {Request, Response} from 'express';
2
+
3
+ export interface NumberMap {
4
+ [key: string]: number;
5
+ }
6
+ export interface LogConfig {
7
+ level?: string;
8
+ map?: LogMapConfig;
9
+ }
10
+ export interface LogMapConfig {
11
+ time?: string;
12
+ level?: string;
13
+ msg?: string;
14
+ }
15
+ export interface LogMap {
16
+ time: string;
17
+ level: string;
18
+ msg: string;
19
+ }
20
+ export interface Logger {
21
+ level: number;
22
+ map: LogMap;
23
+ }
24
+
25
+ export class LogController {
26
+ map?: NumberMap;
27
+ constructor(public logger: Logger, mp?: NumberMap) {
28
+ this.map = mp;
29
+ this.config = this.config.bind(this);
30
+ }
31
+ config(req: Request, res: Response) {
32
+ const obj: LogConfig = req.body;
33
+ if (!obj || obj === '') {
34
+ return res.status(400).end('The request body cannot be empty');
35
+ }
36
+ if (!this.logger || !this.map) {
37
+ return res.status(503).end('Logger is not available');
38
+ }
39
+ if (!this.map) {
40
+ return res.status(503).end('Map is not available');
41
+ }
42
+ let changed = false;
43
+ if (obj.level && typeof obj.level === 'string' && obj.level.length > 0) {
44
+ const lv = this.map[obj.level.toUpperCase()];
45
+ if (lv !== undefined) {
46
+ this.logger.level = lv;
47
+ changed = true;
48
+ }
49
+ }
50
+ if (obj.map) {
51
+ if (obj.map.level && typeof obj.map.level === 'string' && obj.map.level.length > 0) {
52
+ this.logger.map.level = obj.map.level;
53
+ changed = true;
54
+ }
55
+ if (obj.map.time && typeof obj.map.time === 'string' && obj.map.time.length > 0) {
56
+ this.logger.map.time = obj.map.time;
57
+ changed = true;
58
+ }
59
+ if (obj.map.msg && typeof obj.map.msg === 'string' && obj.map.msg.length > 0) {
60
+ this.logger.map.msg = obj.map.msg;
61
+ changed = true;
62
+ }
63
+ }
64
+ if (changed) {
65
+ return res.status(200).end('true');
66
+ } else {
67
+ return res.status(204).end('false');
68
+ }
69
+ }
70
+ }