express-ext 0.5.7 → 0.5.9

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
@@ -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) {
package/lib/view.js CHANGED
@@ -1,78 +1,77 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", { value: true })
3
3
  function buildAndCheckId(req, res, keys) {
4
- var id = buildId(req, keys);
4
+ var id = buildId(req, keys)
5
5
  if (!id) {
6
- res.status(400).end('invalid parameters');
7
- return undefined;
6
+ res.status(400).end("invalid parameters")
7
+ return undefined
8
8
  }
9
- return id;
9
+ return id
10
10
  }
11
- exports.buildAndCheckId = buildAndCheckId;
11
+ exports.buildAndCheckId = buildAndCheckId
12
12
  function buildId(req, attrs) {
13
13
  if (!attrs || attrs.length === 0) {
14
- var id = req.params['id'];
14
+ var id = req.params["id"]
15
15
  if (id && id.length > 0) {
16
- return id;
16
+ return id
17
17
  }
18
- return undefined;
18
+ return undefined
19
19
  }
20
20
  if (attrs && attrs.length === 1) {
21
- var id = req.params['id'];
22
- var n = attrs[0].name;
21
+ var id = req.params["id"]
22
+ var n = attrs[0].name
23
23
  if ((!id || id.length === 0) && n && n.length > 0) {
24
- id = req.params[n];
24
+ id = req.params[n]
25
25
  }
26
26
  if (id && id.length > 0) {
27
- if (attrs[0].type === 'integer' || attrs[0].type === 'number') {
27
+ if (attrs[0].type === "integer" || attrs[0].type === "number") {
28
28
  if (isNaN(id)) {
29
- return undefined;
29
+ return undefined
30
30
  }
31
- var v = parseFloat(id);
32
- return v;
31
+ var v = parseFloat(id)
32
+ return v
33
33
  }
34
- return id;
34
+ return id
35
35
  }
36
36
  }
37
- var ids = {};
37
+ var ids = {}
38
38
  for (var _i = 0, attrs_1 = attrs; _i < attrs_1.length; _i++) {
39
- var attr = attrs_1[_i];
39
+ var attr = attrs_1[_i]
40
40
  if (!attr.name) {
41
- return undefined;
41
+ return undefined
42
42
  }
43
- var v = req.params[attr.name];
43
+ var v = req.params[attr.name]
44
44
  if (!v) {
45
- return undefined;
45
+ return undefined
46
46
  }
47
- if (attr.type === 'integer' || attr.type === 'number') {
47
+ if (attr.type === "integer" || attr.type === "number") {
48
48
  if (isNaN(v)) {
49
- return undefined;
49
+ return undefined
50
50
  }
51
- ids[attr.name] = parseFloat(v);
51
+ ids[attr.name] = parseFloat(v)
52
+ } else {
53
+ ids[attr.name] = v
52
54
  }
53
- else {
54
- ids[attr.name] = v;
55
- }
56
- return ids;
55
+ return ids
57
56
  }
58
57
  }
59
- exports.buildId = buildId;
58
+ exports.buildId = buildId
60
59
  function buildKeys(attrs) {
61
60
  if (!attrs) {
62
- return undefined;
61
+ return undefined
63
62
  }
64
- var keys = Object.keys(attrs);
65
- var ats = [];
63
+ var keys = Object.keys(attrs)
64
+ var ats = []
66
65
  for (var _i = 0, keys_1 = keys; _i < keys_1.length; _i++) {
67
- var key = keys_1[_i];
68
- var attr = attrs[key];
66
+ var key = keys_1[_i]
67
+ var attr = attrs[key]
69
68
  if (attr) {
70
69
  if (attr.key === true) {
71
- var at = { name: key, type: attr.type };
72
- ats.push(at);
70
+ var at = { name: key, type: attr.type }
71
+ ats.push(at)
73
72
  }
74
73
  }
75
74
  }
76
- return ats;
75
+ return ats
77
76
  }
78
- exports.buildKeys = buildKeys;
77
+ exports.buildKeys = buildKeys
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.5.7",
3
+ "version": "0.5.9",
4
4
  "description": "express-ext",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
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
@@ -160,9 +160,8 @@ export function queryPage<F extends Filter>(req: Request, filter?: F): number {
160
160
  ;(filter as any)[resources.page] = n
161
161
  return n
162
162
  }
163
- export function getOffset(limit: number, page: number): number {
164
- const offset = limit * (page - 1)
165
- 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)
166
165
  }
167
166
 
168
167
  export function getPageTotal(pageSize?: number, total?: number): number {
package/src/view.ts CHANGED
@@ -11,14 +11,14 @@ export function buildAndCheckId<ID>(req: Request, res: Response, keys?: Attribut
11
11
  }
12
12
  export function buildId<T>(req: Request, attrs?: Attribute[]): T | undefined {
13
13
  if (!attrs || attrs.length === 0) {
14
- const id = req.params["id"]
14
+ const id = req.params["id"] as string
15
15
  if (id && id.length > 0) {
16
16
  return id as any
17
17
  }
18
18
  return undefined
19
19
  }
20
20
  if (attrs && attrs.length === 1) {
21
- let id = req.params["id"]
21
+ let id = req.params["id"] as string
22
22
  const n = attrs[0].name
23
23
  if ((!id || id.length === 0) && n && n.length > 0) {
24
24
  id = req.params[n]