express-ext 0.5.4 → 0.5.7

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.
@@ -48,7 +48,7 @@ var GenericSearchController = (function (_super) {
48
48
  GenericSearchController.prototype.search = function (req, res) {
49
49
  var _this = this
50
50
  var s = search_1.fromRequest(req, search_1.buildArray(this.array, resources_1.resources.fields, this.excluding))
51
- var l = search_1.getParameters(s, this.config)
51
+ var l = search_1.getParameters(s)
52
52
  var s2 = search_1.format(s, this.dates, this.numbers)
53
53
  this.find(s2, l.limit, l.pageOrNextPageToken, l.fields)
54
54
  .then(function (result) {
@@ -63,7 +63,7 @@ var LoadSearchController = (function (_super) {
63
63
  LoadSearchController.prototype.search = function (req, res) {
64
64
  var _this = this
65
65
  var s = search_1.fromRequest(req, search_1.buildArray(this.array, resources_1.resources.fields, this.excluding))
66
- var l = search_1.getParameters(s, this.config)
66
+ var l = search_1.getParameters(s)
67
67
  var s2 = search_1.format(s, this.dates, this.numbers)
68
68
  this.find(s2, l.limit, l.pageOrNextPageToken, l.fields)
69
69
  .then(function (result) {
@@ -104,7 +104,7 @@ var QueryController = (function (_super) {
104
104
  QueryController.prototype.search = function (req, res) {
105
105
  var _this = this
106
106
  var s = search_1.fromRequest(req, search_1.buildArray(this.array, resources_1.resources.fields, this.excluding))
107
- var l = search_1.getParameters(s, this.config)
107
+ var l = search_1.getParameters(s)
108
108
  var s2 = search_1.format(s, this.dates, this.numbers)
109
109
  this.query
110
110
  .search(s2, l.limit, l.pageOrNextPageToken, l.fields)
@@ -48,7 +48,7 @@ var LowcodeController = (function (_super) {
48
48
  LowcodeController.prototype.search = function (req, res) {
49
49
  var _this = this
50
50
  var s = search_1.fromRequest(req, search_1.buildArray(this.array, resources_1.resources.fields, this.excluding))
51
- var l = search_1.getParameters(s, this.config)
51
+ var l = search_1.getParameters(s)
52
52
  var s2 = search_1.format(s, this.dates, this.numbers)
53
53
  this.lowCodeService
54
54
  .search(s2, l.limit, l.pageOrNextPageToken, l.fields)
@@ -84,7 +84,7 @@ var Controller = (function (_super) {
84
84
  Controller.prototype.search = function (req, res) {
85
85
  var _this = this
86
86
  var s = search_1.fromRequest(req, search_1.buildArray(this.array, resources_1.resources.fields, this.excluding))
87
- var l = search_1.getParameters(s, this.config)
87
+ var l = search_1.getParameters(s)
88
88
  var s2 = search_1.format(s, this.dates, this.numbers)
89
89
  this.lowCodeService
90
90
  .search(s2, l.limit, l.pageOrNextPageToken, l.fields)
@@ -25,7 +25,7 @@ var SearchController = (function () {
25
25
  SearchController.prototype.search = function (req, res) {
26
26
  var _this = this
27
27
  var s = search_1.fromRequest(req, search_1.buildArray(this.array, resources_1.resources.fields, this.excluding))
28
- var l = search_1.getParameters(s, this.config)
28
+ var l = search_1.getParameters(s)
29
29
  var s2 = search_1.format(s, this.dates, this.numbers)
30
30
  this.find(s2, l.limit, l.pageOrNextPageToken, l.fields)
31
31
  .then(function (result) {
package/lib/search.js CHANGED
@@ -61,8 +61,8 @@ function getLimit(req, filter) {
61
61
  if (filter) {
62
62
  var v0 = filter[resources_1.resources.limit]
63
63
  if (v0 == undefined) {
64
- var field = req.query[resources_1.resources.limit]
65
- var v_2 = field ? field.toString() : undefined
64
+ var field_2 = req.query[resources_1.resources.limit]
65
+ var v_2 = field_2 ? field_2.toString() : undefined
66
66
  if (!v_2 || v_2.length === 0) {
67
67
  filter[resources_1.resources.limit] = resources_1.resources.defaultLimit
68
68
  return resources_1.resources.defaultLimit
@@ -87,8 +87,8 @@ function getLimit(req, filter) {
87
87
  }
88
88
  }
89
89
  }
90
- var fieldxxx = req.query[resources_1.resources.limit]
91
- var v = fieldxxx ? fieldxxx.toString() : undefined
90
+ var field = req.query[resources_1.resources.limit]
91
+ var v = field ? field.toString() : undefined
92
92
  if (!v || v.length === 0) {
93
93
  if (filter) {
94
94
  filter[resources_1.resources.limit] = resources_1.resources.defaultLimit
@@ -398,10 +398,6 @@ function buildResult(r, conf) {
398
398
  var t = conf.token ? conf.token : "token"
399
399
  x[t] = r.nextPageToken
400
400
  }
401
- if (r.last) {
402
- var l = conf.last ? conf.last : "last"
403
- x[l] = r.last
404
- }
405
401
  return x
406
402
  }
407
403
  exports.buildResult = buildResult
@@ -414,7 +410,6 @@ function initializeConfig(conf) {
414
410
  list: conf.list,
415
411
  total: conf.total,
416
412
  token: conf.token,
417
- last: conf.last,
418
413
  csv: conf.csv,
419
414
  }
420
415
  if (!c.excluding || c.excluding.length === 0) {
@@ -426,9 +421,6 @@ function initializeConfig(conf) {
426
421
  if (!c.total || c.total.length === 0) {
427
422
  c.total = "total"
428
423
  }
429
- if (!c.last || c.last.length === 0) {
430
- c.last = "last"
431
- }
432
424
  if (!c.token || c.token.length === 0) {
433
425
  c.token = "nextPageToken"
434
426
  }
@@ -437,6 +429,37 @@ function initializeConfig(conf) {
437
429
  exports.initializeConfig = initializeConfig
438
430
  function fromRequest(req, arr) {
439
431
  var s = req.method === "GET" ? fromUrl(req, arr) : req.body
432
+ var page = s[resources_1.resources.page]
433
+ if (page) {
434
+ if (isNaN(page)) {
435
+ s[resources_1.resources.page] = 1
436
+ } else {
437
+ var n = parseFloat(page)
438
+ if (n < 1) {
439
+ n = 1
440
+ }
441
+ s[resources_1.resources.page] = n
442
+ }
443
+ } else {
444
+ s[resources_1.resources.page] = 1
445
+ }
446
+ var limit = s[resources_1.resources.limit]
447
+ if (limit) {
448
+ if (isNaN(limit)) {
449
+ s[resources_1.resources.limit] = resources_1.resources.defaultLimit
450
+ } else {
451
+ var n = parseFloat(limit)
452
+ if (n < 1) {
453
+ n = resources_1.resources.defaultLimit
454
+ }
455
+ s[resources_1.resources.limit] = n
456
+ }
457
+ } else {
458
+ s[resources_1.resources.limit] = resources_1.resources.defaultLimit
459
+ }
460
+ if (resources_1.resources.partial.length > 0) {
461
+ delete s[resources_1.resources.partial]
462
+ }
440
463
  return s
441
464
  }
442
465
  exports.fromRequest = fromRequest
@@ -521,7 +544,7 @@ var setKey = function (_object, _isArrayKey, _key, _nextValue) {
521
544
  }
522
545
  return _object
523
546
  }
524
- function getParameters(obj, config) {
547
+ function getParameters(obj) {
525
548
  var o = obj
526
549
  var fields
527
550
  var fs = o[resources_1.resources.fields]
@@ -539,17 +562,17 @@ function getParameters(obj, config) {
539
562
  }
540
563
  }
541
564
  }
542
- var pageSize = resources_1.resources.defaultLimit
543
- var spageSize = o[resources_1.resources.limit]
544
- if (spageSize && typeof spageSize === "string") {
545
- if (!isNaN(spageSize)) {
546
- var ipageSize = Math.floor(parseFloat(spageSize))
547
- if (ipageSize > 0) {
548
- pageSize = ipageSize
565
+ var limit = resources_1.resources.defaultLimit
566
+ var slimit = o[resources_1.resources.limit]
567
+ if (slimit && typeof slimit === "string") {
568
+ if (!isNaN(slimit)) {
569
+ var ilimit = Math.floor(parseFloat(slimit))
570
+ if (ilimit > 0) {
571
+ limit = ilimit
549
572
  }
550
573
  }
551
574
  }
552
- var r = { limit: pageSize, fields: fields, page: page, nextPageToken: nextPageToken, pageOrNextPageToken: page }
575
+ var r = { limit: limit, fields: fields, page: page, nextPageToken: nextPageToken, pageOrNextPageToken: page }
553
576
  if (r.nextPageToken && r.nextPageToken.length > 0) {
554
577
  r.pageOrNextPageToken = r.nextPageToken
555
578
  }
@@ -588,7 +611,7 @@ function toCsv(fields, r) {
588
611
  var n = "number"
589
612
  var b = '""'
590
613
  var rows = []
591
- rows.push("" + (r.total ? r.total : "") + "," + (r.nextPageToken ? r.nextPageToken : "") + "," + (r.last ? "1" : ""))
614
+ rows.push("" + (r.total ? r.total : "") + "," + (r.nextPageToken ? r.nextPageToken : ""))
592
615
  for (var _i = 0, _a = r.list; _i < _a.length; _i++) {
593
616
  var item = _a[_i]
594
617
  var cols = []
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.5.4",
3
+ "version": "0.5.7",
4
4
  "description": "express-ext",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -37,7 +37,7 @@ export class GenericSearchController<T, ID, S extends Filter> extends GenericCon
37
37
  }
38
38
  search(req: Request, res: Response) {
39
39
  const s = fromRequest<S>(req, buildArray(this.array, resources.fields, this.excluding))
40
- const l = getParameters(s, this.config)
40
+ const l = getParameters(s)
41
41
  const s2 = format(s, this.dates, this.numbers)
42
42
  this.find(s2, l.limit, l.pageOrNextPageToken, l.fields)
43
43
  .then((result) => jsonResult(res, result, this.csv, l.fields, this.config))
@@ -72,7 +72,7 @@ export class LoadSearchController<T, ID, S extends Filter> extends LoadControlle
72
72
  }
73
73
  search(req: Request, res: Response): void {
74
74
  const s = fromRequest<S>(req, buildArray(this.array, resources.fields, this.excluding))
75
- const l = getParameters(s, this.config)
75
+ const l = getParameters(s)
76
76
  const s2 = format(s, this.dates, this.numbers)
77
77
  this.find(s2, l.limit, l.pageOrNextPageToken, l.fields)
78
78
  .then((result) => jsonResult(res, result, this.csv, l.fields, this.config))
@@ -109,7 +109,7 @@ export class QueryController<T, ID, S extends Filter> extends LoadController<T,
109
109
  }
110
110
  search(req: Request, res: Response): void {
111
111
  const s = fromRequest<S>(req, buildArray(this.array, resources.fields, this.excluding))
112
- const l = getParameters(s, this.config)
112
+ const l = getParameters(s)
113
113
  const s2 = format(s, this.dates, this.numbers)
114
114
  this.query
115
115
  .search(s2, l.limit, l.pageOrNextPageToken, l.fields)
@@ -39,7 +39,7 @@ export class LowcodeController<T, ID, S extends Filter> extends GenericControlle
39
39
  }
40
40
  search(req: Request, res: Response) {
41
41
  const s = fromRequest<S>(req, buildArray(this.array, resources.fields, this.excluding))
42
- const l = getParameters(s, this.config)
42
+ const l = getParameters(s)
43
43
  const s2 = format(s, this.dates, this.numbers)
44
44
  this.lowCodeService
45
45
  .search(s2, l.limit, l.pageOrNextPageToken, l.fields)
@@ -79,7 +79,7 @@ export class Controller<T, ID, S extends Filter> extends GenericController<T, ID
79
79
  }
80
80
  search(req: Request, res: Response) {
81
81
  const s = fromRequest<S>(req, buildArray(this.array, resources.fields, this.excluding))
82
- const l = getParameters(s, this.config)
82
+ const l = getParameters(s)
83
83
  const s2 = format(s, this.dates, this.numbers)
84
84
  this.lowCodeService
85
85
  .search(s2, l.limit, l.pageOrNextPageToken, l.fields)
@@ -30,7 +30,7 @@ export class SearchController<T, S extends Filter> {
30
30
  }
31
31
  search(req: Request, res: Response) {
32
32
  const s = fromRequest<S>(req, buildArray(this.array, resources.fields, this.excluding))
33
- const l = getParameters(s, this.config)
33
+ const l = getParameters(s)
34
34
  const s2 = format(s, this.dates, this.numbers)
35
35
  this.find(s2, l.limit, l.pageOrNextPageToken, l.fields)
36
36
  .then((result) => jsonResult(res, result, this.csv, l.fields, this.config))
package/src/search.ts CHANGED
@@ -21,7 +21,7 @@ export interface SearchConfig {
21
21
  list?: string
22
22
  total?: string
23
23
  token?: string
24
- last?: string
24
+ // last?: string
25
25
  csv?: boolean
26
26
  // page?: string
27
27
  // limit?: string
@@ -33,7 +33,6 @@ export interface SearchResult<T> {
33
33
  list: T[]
34
34
  total?: number
35
35
  nextPageToken?: string
36
- last?: boolean
37
36
  }
38
37
 
39
38
  export function getPage<F extends Filter>(req: Request, filter?: F): number {
@@ -120,8 +119,8 @@ export function getLimit<F extends Filter>(req: Request, filter?: F): number {
120
119
  }
121
120
  }
122
121
  }
123
- const fieldxxx = req.query[resources.limit]
124
- const v = fieldxxx ? fieldxxx.toString() : undefined
122
+ const field = req.query[resources.limit]
123
+ const v = field ? field.toString() : undefined
125
124
  if (!v || v.length === 0) {
126
125
  if (filter) {
127
126
  ;(filter as any)[resources.limit] = resources.defaultLimit
@@ -413,10 +412,6 @@ export function buildResult<T>(r: SearchResult<T>, conf?: SearchConfig): any {
413
412
  const t = conf.token ? conf.token : "token"
414
413
  x[t] = r.nextPageToken
415
414
  }
416
- if (r.last) {
417
- const l = conf.last ? conf.last : "last"
418
- x[l] = r.last
419
- }
420
415
  return x
421
416
  }
422
417
  export function initializeConfig(conf?: SearchConfig): SearchConfig | undefined {
@@ -428,7 +423,6 @@ export function initializeConfig(conf?: SearchConfig): SearchConfig | undefined
428
423
  list: conf.list,
429
424
  total: conf.total,
430
425
  token: conf.token,
431
- last: conf.last,
432
426
  csv: conf.csv,
433
427
  }
434
428
  if (!c.excluding || c.excluding.length === 0) {
@@ -440,9 +434,6 @@ export function initializeConfig(conf?: SearchConfig): SearchConfig | undefined
440
434
  if (!c.total || c.total.length === 0) {
441
435
  c.total = "total"
442
436
  }
443
- if (!c.last || c.last.length === 0) {
444
- c.last = "last"
445
- }
446
437
  if (!c.token || c.token.length === 0) {
447
438
  c.token = "nextPageToken"
448
439
  }
@@ -450,6 +441,38 @@ export function initializeConfig(conf?: SearchConfig): SearchConfig | undefined
450
441
  }
451
442
  export function fromRequest<S>(req: Request, arr?: string[]): S {
452
443
  const s: any = req.method === "GET" ? fromUrl(req, arr) : req.body
444
+ const page = s[resources.page]
445
+ if (page) {
446
+ if (isNaN(page as any)) {
447
+ s[resources.page] = 1
448
+ } else {
449
+ let n = parseFloat(page)
450
+ if (n < 1) {
451
+ n = 1
452
+ }
453
+ s[resources.page] = n
454
+ }
455
+ } else {
456
+ s[resources.page] = 1
457
+ }
458
+
459
+ const limit = s[resources.limit]
460
+ if (limit) {
461
+ if (isNaN(limit as any)) {
462
+ s[resources.limit] = resources.defaultLimit
463
+ } else {
464
+ let n = parseFloat(limit)
465
+ if (n < 1) {
466
+ n = resources.defaultLimit
467
+ }
468
+ s[resources.limit] = n
469
+ }
470
+ } else {
471
+ s[resources.limit] = resources.defaultLimit
472
+ }
473
+ if (resources.partial.length > 0) {
474
+ delete s[resources.partial]
475
+ }
453
476
  return s
454
477
  }
455
478
  export function buildArray(arr?: string[], s0?: string, s1?: string, s2?: string): string[] {
@@ -557,7 +580,7 @@ export interface Limit {
557
580
  fields?: string[]
558
581
  pageOrNextPageToken?: string | number
559
582
  }
560
- export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
583
+ export function getParameters<T>(obj: T): Limit {
561
584
  const o: any = obj
562
585
  let fields: string[] | undefined
563
586
  const fs = o[resources.fields]
@@ -575,17 +598,17 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
575
598
  }
576
599
  }
577
600
  }
578
- let pageSize = resources.defaultLimit
579
- let spageSize = o[resources.limit]
580
- if (spageSize && typeof spageSize === "string") {
581
- if (!isNaN(spageSize as any)) {
582
- const ipageSize = Math.floor(parseFloat(spageSize))
583
- if (ipageSize > 0) {
584
- pageSize = ipageSize
601
+ let limit = resources.defaultLimit
602
+ let slimit = o[resources.limit]
603
+ if (slimit && typeof slimit === "string") {
604
+ if (!isNaN(slimit as any)) {
605
+ const ilimit = Math.floor(parseFloat(slimit))
606
+ if (ilimit > 0) {
607
+ limit = ilimit
585
608
  }
586
609
  }
587
610
  }
588
- const r: Limit = { limit: pageSize, fields, page, nextPageToken, pageOrNextPageToken: page }
611
+ const r: Limit = { limit: limit, fields, page, nextPageToken, pageOrNextPageToken: page }
589
612
  if (r.nextPageToken && r.nextPageToken.length > 0) {
590
613
  r.pageOrNextPageToken = r.nextPageToken
591
614
  }
@@ -622,7 +645,7 @@ export function toCsv<T>(fields: string[], r: SearchResult<T>): string {
622
645
  const n = "number"
623
646
  const b = '""'
624
647
  const rows: string[] = []
625
- rows.push("" + (r.total ? r.total : "") + "," + (r.nextPageToken ? r.nextPageToken : "") + "," + (r.last ? "1" : ""))
648
+ rows.push("" + (r.total ? r.total : "") + "," + (r.nextPageToken ? r.nextPageToken : ""))
626
649
  for (const item of r.list) {
627
650
  const cols: string[] = []
628
651
  for (const name of fields) {