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/src/search.ts CHANGED
@@ -1,15 +1,11 @@
1
1
  import {Request, Response} from 'express';
2
2
  import {Attribute, Attributes} from './metadata';
3
3
 
4
- export interface ArrayMap {
5
- [key: string]: string[]|number[];
6
- }
7
- export interface SearchModel {
4
+ export interface Filter {
8
5
  fields?: string[];
9
6
  sort?: string;
10
7
 
11
- keyword?: string;
12
- excluding?: ArrayMap;
8
+ q?: string;
13
9
  }
14
10
  export interface SearchConfig {
15
11
  excluding?: string;
@@ -44,17 +40,21 @@ export function buildResult<T>(r: SearchResult<T>, conf?: SearchConfig): any {
44
40
  return r;
45
41
  }
46
42
  const x: any = {};
47
- x[conf.list] = r.list;
48
- x[conf.total] = r.total;
43
+ const li = (conf.list ? conf.list : 'list');
44
+ x[li] = r.list;
45
+ const to = (conf.total ? conf.total : 'total');
46
+ x[to] = r.total;
49
47
  if (r.nextPageToken && r.nextPageToken.length > 0) {
50
- x[conf.token] = r.nextPageToken;
48
+ const t = (conf.token ? conf.token : 'token');
49
+ x[t] = r.nextPageToken;
51
50
  }
52
51
  if (r.last) {
53
- x[conf.last] = r.last;
52
+ const l = (conf.last ? conf.last : 'last');
53
+ x[l] = r.last;
54
54
  }
55
55
  return x;
56
56
  }
57
- export function initializeConfig(conf: SearchConfig): SearchConfig {
57
+ export function initializeConfig(conf?: SearchConfig): SearchConfig | undefined {
58
58
  if (!conf) {
59
59
  return undefined;
60
60
  }
@@ -153,9 +153,9 @@ export function setValue<T>(obj: T, path: string, value: string): void {
153
153
  export function setValue<T, V>(obj: T, path: string, value: V): void {
154
154
  const paths = path.split('.');
155
155
  if (paths.length === 1) {
156
- obj[path] = value;
156
+ (obj as any)[path] = value;
157
157
  } else {
158
- let o = obj;
158
+ let o: any = obj;
159
159
  const l = paths.length - 1;
160
160
  for (let i = 0; i < l - 1; i++) {
161
161
  const p = paths[i];
@@ -177,42 +177,43 @@ export interface Limit {
177
177
  skipOrRefId?: string|number;
178
178
  }
179
179
  export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
180
+ const o: any = obj;
180
181
  if (!config) {
181
182
  const sfield = 'fields';
182
183
  let fields;
183
- const fs = obj[sfield];
184
+ const fs = o[sfield];
184
185
  if (fs && Array.isArray(fs)) {
185
186
  fields = fs;
186
- delete obj[sfield];
187
+ delete o[sfield];
187
188
  }
188
- let refId = obj['refId'];
189
+ let refId = o['refId'];
189
190
  if (!refId) {
190
- refId = obj['nextPageToken'];
191
+ refId = o['nextPageToken'];
191
192
  }
192
193
  const r: Limit = {fields, refId};
193
- let pageSize = obj['limit'];
194
+ let pageSize = o['limit'];
194
195
  if (!pageSize) {
195
- pageSize = obj['pageSize'];
196
+ pageSize = o['pageSize'];
196
197
  }
197
198
  if (pageSize && !isNaN(pageSize)) {
198
199
  const ipageSize = Math.floor(parseFloat(pageSize));
199
200
  if (ipageSize > 0) {
200
201
  r.limit = ipageSize;
201
- const skip = obj['skip'];
202
+ const skip = o['skip'];
202
203
  if (skip && !isNaN(skip)) {
203
204
  const iskip = Math.floor(parseFloat(skip));
204
205
  if (iskip >= 0) {
205
206
  r.skip = iskip;
206
207
  r.skipOrRefId = r.skip;
207
- deletePageInfo(obj);
208
+ deletePageInfo(o);
208
209
  return r;
209
210
  }
210
211
  }
211
- let pageIndex = obj['page'];
212
+ let pageIndex = o['page'];
212
213
  if (!pageIndex) {
213
- pageIndex = obj['pageIndex'];
214
+ pageIndex = o['pageIndex'];
214
215
  if (!pageIndex) {
215
- pageIndex = obj['pageNo'];
216
+ pageIndex = o['pageNo'];
216
217
  }
217
218
  }
218
219
  if (pageIndex && !isNaN(pageIndex)) {
@@ -220,39 +221,39 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
220
221
  if (ipageIndex < 1) {
221
222
  ipageIndex = 1;
222
223
  }
223
- let firstPageSize = obj['firstLimit'];
224
+ let firstPageSize = o['firstLimit'];
224
225
  if (!firstPageSize) {
225
- firstPageSize = obj['firstPageSize'];
226
+ firstPageSize = o['firstPageSize'];
226
227
  }
227
228
  if (!firstPageSize) {
228
- firstPageSize = obj['initPageSize'];
229
+ firstPageSize = o['initPageSize'];
229
230
  }
230
231
  if (firstPageSize && !isNaN(firstPageSize)) {
231
232
  const ifirstPageSize = Math.floor(parseFloat(firstPageSize));
232
233
  if (ifirstPageSize > 0) {
233
234
  r.skip = ipageSize * (ipageIndex - 2) + ifirstPageSize;
234
235
  r.skipOrRefId = r.skip;
235
- deletePageInfo(obj);
236
+ deletePageInfo(o);
236
237
  return r;
237
238
  }
238
239
  }
239
240
  r.skip = ipageSize * (ipageIndex - 1);
240
241
  r.skipOrRefId = r.skip;
241
- deletePageInfo(obj);
242
+ deletePageInfo(o);
242
243
  return r;
243
244
  }
244
245
  r.skip = 0;
245
246
  if (r.refId && r.refId.length > 0) {
246
247
  r.skipOrRefId = r.refId;
247
248
  }
248
- deletePageInfo(obj);
249
+ deletePageInfo(o);
249
250
  return r;
250
251
  }
251
252
  }
252
253
  if (r.refId && r.refId.length > 0) {
253
254
  r.skipOrRefId = r.refId;
254
255
  }
255
- deletePageInfo(obj);
256
+ deletePageInfo(o);
256
257
  return r;
257
258
  } else {
258
259
  let sfield = config.fields;
@@ -260,23 +261,23 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
260
261
  sfield = 'fields';
261
262
  }
262
263
  let fields;
263
- const fs = obj[sfield];
264
+ const fs = o[sfield];
264
265
  if (fs && Array.isArray(fs)) {
265
266
  fields = fs;
266
- delete obj[sfield];
267
+ delete o[sfield];
267
268
  }
268
269
  let strRefId = config.refId;
269
270
  if (!strRefId || strRefId.length === 0) {
270
271
  strRefId = 'refId';
271
272
  }
272
- const refId = obj[strRefId];
273
+ const refId = o[strRefId];
273
274
  const r: Limit = {fields, refId};
274
275
 
275
276
  let strLimit = config.limit;
276
277
  if (!strLimit || strLimit.length === 0) {
277
278
  strLimit = 'limit';
278
279
  }
279
- const pageSize = obj[strLimit];
280
+ const pageSize = o[strLimit];
280
281
  const arr = [config.page, config.limit, config.skip, config.refId, config.firstLimit];
281
282
  if (pageSize && !isNaN(pageSize)) {
282
283
  const ipageSize = Math.floor(parseFloat(pageSize));
@@ -286,13 +287,13 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
286
287
  if (!strSkip || strSkip.length === 0) {
287
288
  strSkip = 'skip';
288
289
  }
289
- const skip = obj[strSkip];
290
+ const skip = o[strSkip];
290
291
  if (skip && !isNaN(skip)) {
291
292
  const iskip = Math.floor(parseFloat(skip));
292
293
  if (iskip >= 0) {
293
294
  r.skip = iskip;
294
295
  r.skipOrRefId = r.skip;
295
- deletePageInfo(obj, arr);
296
+ deletePageInfo(o, arr);
296
297
  return r;
297
298
  }
298
299
  }
@@ -300,7 +301,7 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
300
301
  if (!strPage || strPage.length === 0) {
301
302
  strPage = 'page';
302
303
  }
303
- const pageIndex = obj[strPage];
304
+ const pageIndex = o[strPage];
304
305
  if (pageIndex && !isNaN(pageIndex)) {
305
306
  let ipageIndex = Math.floor(parseFloat(pageIndex));
306
307
  if (ipageIndex < 1) {
@@ -310,37 +311,37 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
310
311
  if (!strFirstLimit || strFirstLimit.length === 0) {
311
312
  strFirstLimit = 'firstLimit';
312
313
  }
313
- const firstPageSize = obj[strFirstLimit];
314
+ const firstPageSize = o[strFirstLimit];
314
315
  if (firstPageSize && !isNaN(firstPageSize)) {
315
316
  const ifirstPageSize = Math.floor(parseFloat(firstPageSize));
316
317
  if (ifirstPageSize > 0) {
317
318
  r.skip = ipageSize * (ipageIndex - 2) + ifirstPageSize;
318
319
  r.skipOrRefId = r.skip;
319
- deletePageInfo(obj, arr);
320
+ deletePageInfo(o, arr);
320
321
  return r;
321
322
  }
322
323
  }
323
324
  r.skip = ipageSize * (ipageIndex - 1);
324
325
  r.skipOrRefId = r.skip;
325
- deletePageInfo(obj, arr);
326
+ deletePageInfo(o, arr);
326
327
  return r;
327
328
  }
328
329
  r.skip = 0;
329
330
  if (r.refId && r.refId.length > 0) {
330
331
  r.skipOrRefId = r.refId;
331
332
  }
332
- deletePageInfo(obj, arr);
333
+ deletePageInfo(o, arr);
333
334
  return r;
334
335
  }
335
336
  }
336
337
  if (r.refId && r.refId.length > 0) {
337
338
  r.skipOrRefId = r.refId;
338
339
  }
339
- deletePageInfo(obj, arr);
340
+ deletePageInfo(o, arr);
340
341
  return r;
341
342
  }
342
343
  }
343
- export function deletePageInfo(obj: any, arr?: string[]): void {
344
+ export function deletePageInfo(obj: any, arr?: (string | undefined)[]): void {
344
345
  if (!arr || arr.length === 0) {
345
346
  delete obj['limit'];
346
347
  delete obj['firstLimit'];
@@ -375,7 +376,7 @@ export function toCsv<T>(fields: string[], r: SearchResult<T>): string {
375
376
  for (const item of r.list) {
376
377
  const cols: string[] = [];
377
378
  for (const name of fields) {
378
- const v = item[name];
379
+ const v = (item as any)[name];
379
380
  if (!v) {
380
381
  cols.push(e);
381
382
  } else {
@@ -443,8 +444,8 @@ export function buildMetadata(attributes: Attributes, includeDate?: boolean): Me
443
444
 
444
445
  const _datereg = '/Date(';
445
446
  const _re = /-?\d+/;
446
- function toDate(v: any) {
447
- if (!v || v === '') {
447
+ function toDate(v: any): Date | null | undefined {
448
+ if (!v) {
448
449
  return null;
449
450
  }
450
451
  if (v instanceof Date) {
@@ -455,33 +456,39 @@ function toDate(v: any) {
455
456
  const i = v.indexOf(_datereg);
456
457
  if (i >= 0) {
457
458
  const m = _re.exec(v);
458
- const d = parseInt(m[0], null);
459
- return new Date(d);
459
+ if (m !== null) {
460
+ const d = parseInt(m[0], 10);
461
+ return new Date(d);
462
+ } else {
463
+ return null;
464
+ }
460
465
  } else {
461
466
  if (isNaN(v)) {
462
467
  return new Date(v);
463
468
  } else {
464
- const d = parseInt(v, null);
469
+ const d = parseInt(v, 10);
465
470
  return new Date(d);
466
471
  }
467
472
  }
468
473
  }
469
474
 
470
475
  export function format<T>(obj: T, dates?: string[], nums?: string[]): T {
476
+ const o: any = obj;
471
477
  if (dates && dates.length > 0) {
472
478
  for (const s of dates) {
473
- const v = obj[s];
479
+ const v = o[s];
474
480
  if (v) {
475
481
  if (v instanceof Date) {
476
482
  continue;
477
483
  }
478
484
  if (typeof v === 'string' || typeof v === 'number') {
479
485
  const d = toDate(v);
480
- const error = d.toString();
481
- if (!(d instanceof Date) || error === 'Invalid Date') {
482
- delete obj[s];
483
- } else {
484
- obj[s] = d;
486
+ if (d) {
487
+ if (!(d instanceof Date) || d.toString() === 'Invalid Date') {
488
+ delete o[s];
489
+ } else {
490
+ o[s] = d;
491
+ }
485
492
  }
486
493
  } else if (typeof v === 'object') {
487
494
  const keys = Object.keys(v);
@@ -492,11 +499,12 @@ export function format<T>(obj: T, dates?: string[], nums?: string[]): T {
492
499
  }
493
500
  if (typeof v2 === 'string' || typeof v2 === 'number') {
494
501
  const d2 = toDate(v2);
495
- const error2 = d2.toString();
496
- if (!(d2 instanceof Date) || error2 === 'Invalid Date') {
497
- delete v[key];
498
- } else {
499
- v[key] = d2;
502
+ if (d2) {
503
+ if (!(d2 instanceof Date) || d2.toString() === 'Invalid Date') {
504
+ delete v[key];
505
+ } else {
506
+ v[key] = d2;
507
+ }
500
508
  }
501
509
  }
502
510
  }
@@ -506,10 +514,10 @@ export function format<T>(obj: T, dates?: string[], nums?: string[]): T {
506
514
  }
507
515
  if (nums && nums.length > 0) {
508
516
  for (const s of nums) {
509
- const v = obj[s];
517
+ const v = o[s];
510
518
  if (v) {
511
519
  if (v instanceof Date) {
512
- delete obj[s];
520
+ delete o[s];
513
521
  continue;
514
522
  }
515
523
  if (typeof v === 'number') {
@@ -517,18 +525,18 @@ export function format<T>(obj: T, dates?: string[], nums?: string[]): T {
517
525
  }
518
526
  if (typeof v === 'string') {
519
527
  if (!isNaN(v as any)) {
520
- delete obj[s];
528
+ delete o[s];
521
529
  continue;
522
530
  } else {
523
531
  const i = parseFloat(v);
524
- obj[s] = i;
532
+ o[s] = i;
525
533
  }
526
534
  } else if (typeof v === 'object') {
527
535
  const keys = Object.keys(v);
528
536
  for (const key of keys) {
529
537
  const v2 = v[key];
530
538
  if (v2 instanceof Date) {
531
- delete obj[key];
539
+ delete o[key];
532
540
  continue;
533
541
  }
534
542
  if (typeof v2 === 'number') {
@@ -547,5 +555,5 @@ export function format<T>(obj: T, dates?: string[], nums?: string[]): T {
547
555
  }
548
556
  }
549
557
  }
550
- return obj;
558
+ return o;
551
559
  }
@@ -2,7 +2,7 @@ import {ViewService} from './LoadController';
2
2
  import {Attribute, Attributes} from './metadata';
3
3
  import {buildMetadata, Metadata} from './search';
4
4
 
5
- export function getMetadataFunc<T, ID>(viewService: ViewService<T, ID> | ((id: ID, ctx?: any) => Promise<T>), dates?: string[], numbers?: string[], keys?: Attributes|Attribute[]|string[]): Metadata {
5
+ export function getMetadataFunc<T, ID>(viewService: ViewService<T, ID> | ((id: ID, ctx?: any) => Promise<T>), dates?: string[], numbers?: string[], keys?: Attributes|Attribute[]|string[]): Metadata | undefined {
6
6
  const m: Metadata = { dates, numbers };
7
7
  if (m.dates && m.dates.length > 0 || m.numbers && m.numbers.length > 0) {
8
8
  return m;
package/src/view.ts CHANGED
@@ -1,29 +1,32 @@
1
1
  import {Request, Response} from 'express';
2
2
  import {Attribute, Attributes} from './metadata';
3
3
 
4
- export function buildAndCheckId<ID>(req: Request, res: Response, keys?: Attribute[]) {
4
+ export function buildAndCheckId<ID>(req: Request, res: Response, keys?: Attribute[]): ID | undefined {
5
5
  const id = buildId<ID>(req, keys);
6
6
  if (!id) {
7
7
  res.status(400).end('invalid parameters');
8
- return null;
8
+ return undefined;
9
9
  }
10
10
  return id;
11
11
  }
12
- export function buildId<T>(req: Request, attrs?: Attribute[]): T {
13
- if (!attrs) {
12
+ export function buildId<T>(req: Request, attrs?: Attribute[]): T | undefined {
13
+ if (!attrs || attrs.length === 0) {
14
14
  const id = req.params['id'];
15
15
  if (id && id.length > 0) {
16
16
  return id as any;
17
17
  }
18
- return null;
18
+ return undefined;
19
19
  }
20
20
  if (attrs && attrs.length === 1) {
21
- const key = (attrs[0].name ? attrs[0].name : 'id');
22
- const id = req.params[key];
21
+ let id = req.params['id'];
22
+ const n = attrs[0].name;
23
+ if ((!id || id.length === 0) && n && n.length > 0) {
24
+ id = req.params[n];
25
+ }
23
26
  if (id && id.length > 0) {
24
27
  if (attrs[0].type === 'integer' || attrs[0].type === 'number') {
25
28
  if (isNaN(id as any)) {
26
- return null;
29
+ return undefined;
27
30
  }
28
31
  const v = parseFloat(id);
29
32
  return v as any;
@@ -33,13 +36,16 @@ export function buildId<T>(req: Request, attrs?: Attribute[]): T {
33
36
  }
34
37
  const ids: any = {};
35
38
  for (const attr of attrs) {
39
+ if (!attr.name) {
40
+ return undefined;
41
+ }
36
42
  const v = req.params[attr.name];
37
43
  if (!v) {
38
- return null;
44
+ return undefined;
39
45
  }
40
46
  if (attr.type === 'integer' || attr.type === 'number') {
41
47
  if (isNaN(v as any)) {
42
- return null;
48
+ return undefined;
43
49
  }
44
50
  ids[attr.name] = parseFloat(v);
45
51
  } else {
@@ -48,7 +54,7 @@ export function buildId<T>(req: Request, attrs?: Attribute[]): T {
48
54
  return ids;
49
55
  }
50
56
  }
51
- export function buildKeys(attrs: Attributes): Attribute[] {
57
+ export function buildKeys(attrs: Attributes): Attribute[] | undefined {
52
58
  if (!attrs) {
53
59
  return undefined;
54
60
  }
package/tsconfig.json CHANGED
@@ -6,6 +6,7 @@
6
6
  "outDir": "./lib",
7
7
  "module": "commonjs",
8
8
  "moduleResolution": "node",
9
+ "strict": true,
9
10
  "pretty": true,
10
11
  "sourceMap": false,
11
12
  "declaration": false,