express-ext 0.5.5 → 0.5.8

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
@@ -132,8 +132,7 @@ function queryPage(req, filter) {
132
132
  }
133
133
  exports.queryPage = queryPage
134
134
  function getOffset(limit, page) {
135
- var offset = limit * (page - 1)
136
- return offset < 0 ? 0 : offset
135
+ return !page || page <= 1 || limit <= 0 ? 0 : limit * (page - 1)
137
136
  }
138
137
  exports.getOffset = getOffset
139
138
  function getPageTotal(pageSize, total) {
@@ -398,10 +397,6 @@ function buildResult(r, conf) {
398
397
  var t = conf.token ? conf.token : "token"
399
398
  x[t] = r.nextPageToken
400
399
  }
401
- if (r.last) {
402
- var l = conf.last ? conf.last : "last"
403
- x[l] = r.last
404
- }
405
400
  return x
406
401
  }
407
402
  exports.buildResult = buildResult
@@ -414,7 +409,6 @@ function initializeConfig(conf) {
414
409
  list: conf.list,
415
410
  total: conf.total,
416
411
  token: conf.token,
417
- last: conf.last,
418
412
  csv: conf.csv,
419
413
  }
420
414
  if (!c.excluding || c.excluding.length === 0) {
@@ -426,9 +420,6 @@ function initializeConfig(conf) {
426
420
  if (!c.total || c.total.length === 0) {
427
421
  c.total = "total"
428
422
  }
429
- if (!c.last || c.last.length === 0) {
430
- c.last = "last"
431
- }
432
423
  if (!c.token || c.token.length === 0) {
433
424
  c.token = "nextPageToken"
434
425
  }
@@ -453,14 +444,14 @@ function fromRequest(req, arr) {
453
444
  }
454
445
  var limit = s[resources_1.resources.limit]
455
446
  if (limit) {
456
- if (isNaN(page)) {
447
+ if (isNaN(limit)) {
457
448
  s[resources_1.resources.limit] = resources_1.resources.defaultLimit
458
449
  } else {
459
450
  var n = parseFloat(limit)
460
451
  if (n < 1) {
461
452
  n = resources_1.resources.defaultLimit
462
453
  }
463
- s[resources_1.resources.page] = n
454
+ s[resources_1.resources.limit] = n
464
455
  }
465
456
  } else {
466
457
  s[resources_1.resources.limit] = resources_1.resources.defaultLimit
@@ -552,7 +543,7 @@ var setKey = function (_object, _isArrayKey, _key, _nextValue) {
552
543
  }
553
544
  return _object
554
545
  }
555
- function getParameters(obj, config) {
546
+ function getParameters(obj) {
556
547
  var o = obj
557
548
  var fields
558
549
  var fs = o[resources_1.resources.fields]
@@ -570,17 +561,17 @@ function getParameters(obj, config) {
570
561
  }
571
562
  }
572
563
  }
573
- var pageSize = resources_1.resources.defaultLimit
574
- var spageSize = o[resources_1.resources.limit]
575
- if (spageSize && typeof spageSize === "string") {
576
- if (!isNaN(spageSize)) {
577
- var ipageSize = Math.floor(parseFloat(spageSize))
578
- if (ipageSize > 0) {
579
- pageSize = ipageSize
564
+ var limit = resources_1.resources.defaultLimit
565
+ var slimit = o[resources_1.resources.limit]
566
+ if (slimit && typeof slimit === "string") {
567
+ if (!isNaN(slimit)) {
568
+ var ilimit = Math.floor(parseFloat(slimit))
569
+ if (ilimit > 0) {
570
+ limit = ilimit
580
571
  }
581
572
  }
582
573
  }
583
- var r = { limit: pageSize, fields: fields, page: page, nextPageToken: nextPageToken, pageOrNextPageToken: page }
574
+ var r = { limit: limit, fields: fields, page: page, nextPageToken: nextPageToken, pageOrNextPageToken: page }
584
575
  if (r.nextPageToken && r.nextPageToken.length > 0) {
585
576
  r.pageOrNextPageToken = r.nextPageToken
586
577
  }
@@ -619,7 +610,7 @@ function toCsv(fields, r) {
619
610
  var n = "number"
620
611
  var b = '""'
621
612
  var rows = []
622
- rows.push("" + (r.total ? r.total : "") + "," + (r.nextPageToken ? r.nextPageToken : "") + "," + (r.last ? "1" : ""))
613
+ rows.push("" + (r.total ? r.total : "") + "," + (r.nextPageToken ? r.nextPageToken : ""))
623
614
  for (var _i = 0, _a = r.list; _i < _a.length; _i++) {
624
615
  var item = _a[_i]
625
616
  var cols = []
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.5.5",
3
+ "version": "0.5.8",
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/log.ts CHANGED
@@ -1,6 +1,4 @@
1
- import { NextFunction } from "express"
2
- import { ParamsDictionary, Request, Response } from "express-serve-static-core"
3
- import { ParsedQs } from "qs"
1
+ import { NextFunction, Request, Response } from "express"
4
2
  import { PassThrough } from "stream"
5
3
  import { resources } from "./resources"
6
4
 
@@ -64,16 +62,12 @@ export interface Middleware {
64
62
  }
65
63
  const o = "OPTIONS"
66
64
  export class MiddlewareLogger {
67
- constructor(
68
- public write: (msg: string, m?: SimpleMap) => void,
69
- conf?: LogConf,
70
- public build?: (req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, m: SimpleMap) => SimpleMap,
71
- ) {
65
+ constructor(public write: (msg: string, m?: SimpleMap) => void, conf?: LogConf, public build?: (req: Request, m: SimpleMap) => SimpleMap) {
72
66
  this.log = this.log.bind(this)
73
67
  this.conf = createConfig(conf)
74
68
  }
75
69
  conf: MiddleLog
76
- log(req: Request<ParamsDictionary, any, any, ParsedQs, Record<string, any>>, res: Response<any, Record<string, any>, number>, next: NextFunction) {
70
+ log(req: Request, res: Response, next: NextFunction) {
77
71
  const m = req.method
78
72
  if (m !== o && this.conf.log && !skip(this.conf.skips, req.originalUrl)) {
79
73
  const start = process.hrtime()
@@ -134,18 +128,16 @@ export class MiddlewareLogger {
134
128
  }
135
129
  }
136
130
  }
137
- const mapResponseBody = (res: Response<any, Record<string, any>, number>, chunks: Uint8Array[]) => {
131
+ const mapResponseBody = (res: Response, chunks: Uint8Array[]) => {
138
132
  const defaultWrite = res.write.bind(res)
139
133
  const defaultEnd = res.end.bind(res)
140
134
  const ps = new PassThrough()
141
135
 
142
136
  ps.on("data", (data: any) => chunks.push(data))
143
-
144
137
  ;(res as any).write = (...args: any) => {
145
138
  ;(ps as any).write(...args)
146
139
  ;(defaultWrite as any)(...args)
147
140
  }
148
-
149
141
  ;(res as any).end = (...args: any) => {
150
142
  ps.end(...args)
151
143
  defaultEnd(...args)
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 {
@@ -161,9 +160,8 @@ export function queryPage<F extends Filter>(req: Request, filter?: F): number {
161
160
  ;(filter as any)[resources.page] = n
162
161
  return n
163
162
  }
164
- export function getOffset(limit: number, page: number): number {
165
- const offset = limit * (page - 1)
166
- return offset < 0 ? 0 : offset
163
+ export function getOffset(limit: number, page?: number): number {
164
+ return !page || page <= 1 || limit <= 0 ? 0 : limit * (page - 1)
167
165
  }
168
166
 
169
167
  export function getPageTotal(pageSize?: number, total?: number): number {
@@ -413,10 +411,6 @@ export function buildResult<T>(r: SearchResult<T>, conf?: SearchConfig): any {
413
411
  const t = conf.token ? conf.token : "token"
414
412
  x[t] = r.nextPageToken
415
413
  }
416
- if (r.last) {
417
- const l = conf.last ? conf.last : "last"
418
- x[l] = r.last
419
- }
420
414
  return x
421
415
  }
422
416
  export function initializeConfig(conf?: SearchConfig): SearchConfig | undefined {
@@ -428,7 +422,6 @@ export function initializeConfig(conf?: SearchConfig): SearchConfig | undefined
428
422
  list: conf.list,
429
423
  total: conf.total,
430
424
  token: conf.token,
431
- last: conf.last,
432
425
  csv: conf.csv,
433
426
  }
434
427
  if (!c.excluding || c.excluding.length === 0) {
@@ -440,9 +433,6 @@ export function initializeConfig(conf?: SearchConfig): SearchConfig | undefined
440
433
  if (!c.total || c.total.length === 0) {
441
434
  c.total = "total"
442
435
  }
443
- if (!c.last || c.last.length === 0) {
444
- c.last = "last"
445
- }
446
436
  if (!c.token || c.token.length === 0) {
447
437
  c.token = "nextPageToken"
448
438
  }
@@ -467,14 +457,14 @@ export function fromRequest<S>(req: Request, arr?: string[]): S {
467
457
 
468
458
  const limit = s[resources.limit]
469
459
  if (limit) {
470
- if (isNaN(page as any)) {
460
+ if (isNaN(limit as any)) {
471
461
  s[resources.limit] = resources.defaultLimit
472
462
  } else {
473
463
  let n = parseFloat(limit)
474
464
  if (n < 1) {
475
465
  n = resources.defaultLimit
476
466
  }
477
- s[resources.page] = n
467
+ s[resources.limit] = n
478
468
  }
479
469
  } else {
480
470
  s[resources.limit] = resources.defaultLimit
@@ -589,7 +579,7 @@ export interface Limit {
589
579
  fields?: string[]
590
580
  pageOrNextPageToken?: string | number
591
581
  }
592
- export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
582
+ export function getParameters<T>(obj: T): Limit {
593
583
  const o: any = obj
594
584
  let fields: string[] | undefined
595
585
  const fs = o[resources.fields]
@@ -607,17 +597,17 @@ export function getParameters<T>(obj: T, config?: SearchConfig): Limit {
607
597
  }
608
598
  }
609
599
  }
610
- let pageSize = resources.defaultLimit
611
- let spageSize = o[resources.limit]
612
- if (spageSize && typeof spageSize === "string") {
613
- if (!isNaN(spageSize as any)) {
614
- const ipageSize = Math.floor(parseFloat(spageSize))
615
- if (ipageSize > 0) {
616
- pageSize = ipageSize
600
+ let limit = resources.defaultLimit
601
+ let slimit = o[resources.limit]
602
+ if (slimit && typeof slimit === "string") {
603
+ if (!isNaN(slimit as any)) {
604
+ const ilimit = Math.floor(parseFloat(slimit))
605
+ if (ilimit > 0) {
606
+ limit = ilimit
617
607
  }
618
608
  }
619
609
  }
620
- const r: Limit = { limit: pageSize, fields, page, nextPageToken, pageOrNextPageToken: page }
610
+ const r: Limit = { limit: limit, fields, page, nextPageToken, pageOrNextPageToken: page }
621
611
  if (r.nextPageToken && r.nextPageToken.length > 0) {
622
612
  r.pageOrNextPageToken = r.nextPageToken
623
613
  }
@@ -654,7 +644,7 @@ export function toCsv<T>(fields: string[], r: SearchResult<T>): string {
654
644
  const n = "number"
655
645
  const b = '""'
656
646
  const rows: string[] = []
657
- rows.push("" + (r.total ? r.total : "") + "," + (r.nextPageToken ? r.nextPageToken : "") + "," + (r.last ? "1" : ""))
647
+ rows.push("" + (r.total ? r.total : "") + "," + (r.nextPageToken ? r.nextPageToken : ""))
658
648
  for (const item of r.list) {
659
649
  const cols: string[] = []
660
650
  for (const name of fields) {