express-ext 0.4.0 → 0.4.2

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/http.ts CHANGED
@@ -1,289 +1,289 @@
1
- import { Request, Response } from 'express';
2
- import { Attribute } from './metadata';
1
+ import { Request, Response } from "express"
2
+ import { Attribute } from "./metadata"
3
3
 
4
- export type Log = (msg: string) => void;
5
- export type LogFunc = Log;
4
+ export type Log = (msg: string) => void
5
+ export type LogFunc = Log
6
6
 
7
- Object.defineProperty(Error.prototype, 'toJSON', {
7
+ Object.defineProperty(Error.prototype, "toJSON", {
8
8
  value() {
9
- const alt: any = {};
9
+ const alt: any = {}
10
10
  Object.getOwnPropertyNames(this).forEach(function (key) {
11
11
  // @ts-ignore
12
- alt[key] = this[key];
13
- }, this);
12
+ alt[key] = this[key]
13
+ }, this)
14
14
 
15
- return alt;
15
+ return alt
16
16
  },
17
17
  configurable: true,
18
18
  writable: true,
19
- });
19
+ })
20
20
  export function handleError(err: any, res: Response, log?: (msg: string) => void) {
21
21
  if (log) {
22
- log(toString(err));
23
- res.status(500).end('Internal Server Error');
22
+ log(toString(err))
23
+ res.status(500).end("Internal Server Error")
24
24
  } else {
25
- res.status(500).end(toString(err));
25
+ res.status(500).end(toString(err))
26
26
  }
27
27
  }
28
- export const error = handleError;
28
+ export const error = handleError
29
29
  export function toString(v: any): string {
30
- if (typeof v === 'string') {
31
- return v;
30
+ if (typeof v === "string") {
31
+ return v
32
32
  } else {
33
- return JSON.stringify(v);
33
+ return JSON.stringify(v)
34
34
  }
35
35
  }
36
36
  export function attr(name: string): Attribute {
37
- return { name, type: 'string' };
37
+ return { name, type: "string" }
38
38
  }
39
39
  export function attrs(keys: string[]): Attribute[] {
40
- const atts: Attribute[] = [];
40
+ const atts: Attribute[] = []
41
41
  for (const str of keys) {
42
- const at: Attribute = { name: str as string, type: 'string' };
43
- atts.push(at);
42
+ const at: Attribute = { name: str as string, type: "string" }
43
+ atts.push(at)
44
44
  }
45
- return atts;
45
+ return atts
46
46
  }
47
47
  export function respondModel<T>(obj: T, res: Response): void {
48
48
  if (obj) {
49
- res.status(200).json(obj).end();
49
+ res.status(200).json(obj).end()
50
50
  } else {
51
- res.status(404).json(null).end();
51
+ res.status(404).json(null).end()
52
52
  }
53
53
  }
54
54
  export function queryRequiredParams(req: Request, res: Response, name: string, split?: string): string[] | undefined {
55
- const v = req.query[name];
55
+ const v = req.query[name]
56
56
  if (!v) {
57
- res.status(400).end(`'${name}' cannot be empty`);
58
- return undefined;
57
+ res.status(400).end(`'${name}' cannot be empty`)
58
+ return undefined
59
59
  }
60
- const v2 = v.toString();
60
+ const v2 = v.toString()
61
61
  if (v2.length === 0) {
62
- res.status(400).end(`'${name}' cannot be empty`);
63
- return undefined;
62
+ res.status(400).end(`'${name}' cannot be empty`)
63
+ return undefined
64
64
  }
65
65
  if (!split) {
66
- split = ',';
66
+ split = ","
67
67
  }
68
- return v2.split(split);
68
+ return v2.split(split)
69
69
  }
70
70
  export function queryParams(req: Request, name: string, d?: string[], split?: string): string[] | undefined {
71
- const q = req.query[name];
71
+ const q = req.query[name]
72
72
  if (!q) {
73
- return d;
73
+ return d
74
74
  }
75
- const v = q.toString();
75
+ const v = q.toString()
76
76
  if (!v || v.length === 0) {
77
- return d;
77
+ return d
78
78
  }
79
79
  if (!split) {
80
- split = ',';
80
+ split = ","
81
81
  }
82
- return v.split(split);
82
+ return v.split(split)
83
83
  }
84
84
  export function queryParam(req: Request, res: Response, name: string): string | undefined {
85
- const v = req.query[name];
85
+ const v = req.query[name]
86
86
  if (!v) {
87
- res.status(400).end(`'${name}' cannot be empty`);
88
- return undefined;
87
+ res.status(400).end(`'${name}' cannot be empty`)
88
+ return undefined
89
89
  } else {
90
- const v1 = v.toString();
90
+ const v1 = v.toString()
91
91
  if (v1.length === 0) {
92
- res.status(400).end(`'${name}' cannot be empty`);
93
- return undefined;
92
+ res.status(400).end(`'${name}' cannot be empty`)
93
+ return undefined
94
94
  } else {
95
- return v1;
95
+ return v1
96
96
  }
97
97
  }
98
98
  }
99
99
  export function query(req: Request, name: string, d?: string): string | undefined {
100
- const p = req.query[name];
100
+ const p = req.query[name]
101
101
  if (!p || p.toString().length === 0) {
102
- return d;
102
+ return d
103
103
  }
104
- return p.toString();
104
+ return p.toString()
105
105
  }
106
106
  export function queryRequiredNumber(req: Request, res: Response, name: string): number | undefined {
107
- const field = req.query[name];
107
+ const field = req.query[name]
108
108
  if (!field) {
109
- return undefined;
109
+ return undefined
110
110
  }
111
- const v = field.toString();
111
+ const v = field.toString()
112
112
  if (!v || v.length === 0) {
113
- res.status(400).end(`'${name}' cannot be empty`);
114
- return undefined;
113
+ res.status(400).end(`'${name}' cannot be empty`)
114
+ return undefined
115
115
  }
116
116
  if (isNaN(v as any)) {
117
- res.status(400).end(`'${name}' is not a valid number`);
118
- return undefined;
117
+ res.status(400).end(`'${name}' is not a valid number`)
118
+ return undefined
119
119
  }
120
- const n = parseFloat(v);
121
- return n;
120
+ const n = parseFloat(v)
121
+ return n
122
122
  }
123
123
  export function queryNumber(req: Request, name: string, d: number): number {
124
- const field = req.query[name];
125
- const v = field ? field.toString() : undefined;
124
+ const field = req.query[name]
125
+ const v = field ? field.toString() : undefined
126
126
  if (!v || v.length === 0) {
127
- return d;
127
+ return d
128
128
  }
129
129
  if (isNaN(v as any)) {
130
- return d;
130
+ return d
131
131
  }
132
- const n = parseFloat(v);
133
- return n;
132
+ const n = parseFloat(v)
133
+ return n
134
134
  }
135
135
  export function queryRequiredDate(req: Request, res: Response, name: string): Date | undefined {
136
- const field = req.query[name];
136
+ const field = req.query[name]
137
137
  if (!field) {
138
- return undefined;
138
+ return undefined
139
139
  }
140
- const v = field.toString();
140
+ const v = field.toString()
141
141
  if (!v || v.length === 0) {
142
- res.status(400).end(`'${name}' cannot be empty`);
143
- return undefined;
142
+ res.status(400).end(`'${name}' cannot be empty`)
143
+ return undefined
144
144
  }
145
- const date = new Date(v);
146
- if (date.toString() === 'Invalid Date') {
147
- res.status(400).end(`'${name}' is not a valid date`);
148
- return undefined;
145
+ const date = new Date(v)
146
+ if (date.toString() === "Invalid Date") {
147
+ res.status(400).end(`'${name}' is not a valid date`)
148
+ return undefined
149
149
  }
150
- return date;
150
+ return date
151
151
  }
152
152
  export function queryDate(req: Request, name: string, d?: Date): Date | undefined {
153
- const field = req.query[name];
153
+ const field = req.query[name]
154
154
  if (field) {
155
- const v = field.toString();
155
+ const v = field.toString()
156
156
  if (!v || v.length === 0) {
157
- return d;
157
+ return d
158
158
  }
159
- const date = new Date(v);
160
- if (date.toString() === 'Invalid Date') {
161
- return d;
159
+ const date = new Date(v)
160
+ if (date.toString() === "Invalid Date") {
161
+ return d
162
162
  }
163
- return date;
163
+ return date
164
164
  }
165
- return undefined;
165
+ return undefined
166
166
  }
167
167
  export function param(req: Request, res: Response, name: string): string | undefined {
168
- const v = req.params[name];
168
+ const v = req.params[name]
169
169
  if (!v || v.length === 0) {
170
- res.status(400).end(`'${name}' cannot be empty`);
171
- return undefined;
170
+ res.status(400).end(`'${name}' cannot be empty`)
171
+ return undefined
172
172
  }
173
- return v;
173
+ return v
174
174
  }
175
- export const getParam = param;
175
+ export const getParam = param
176
176
  export function params(req: Request, name: string, d?: string[], split?: string): string[] | undefined {
177
- const v = req.params[name];
177
+ const v = req.params[name]
178
178
  if (!v || v.length === 0) {
179
- return d;
179
+ return d
180
180
  }
181
181
  if (!split) {
182
- split = ',';
182
+ split = ","
183
183
  }
184
- return v.split(split);
184
+ return v.split(split)
185
185
  }
186
- export const getParams = params;
186
+ export const getParams = params
187
187
  export function getRequiredParameters(req: Request, res: Response, name: string, split?: string): string[] | undefined {
188
- const v = req.params[name];
188
+ const v = req.params[name]
189
189
  if (!v || v.length === 0) {
190
- res.status(400).end(`'${name}' cannot be empty`);
191
- return undefined;
190
+ res.status(400).end(`'${name}' cannot be empty`)
191
+ return undefined
192
192
  }
193
193
  if (!split) {
194
- split = ',';
194
+ split = ","
195
195
  }
196
- return v.split(split);
196
+ return v.split(split)
197
197
  }
198
198
  export function getRequiredNumber(req: Request, res: Response, name: string): number | undefined {
199
- const v = req.params[name];
199
+ const v = req.params[name]
200
200
  if (!v || v.length === 0) {
201
- res.status(400).end(`'${name}' cannot be empty`);
202
- return undefined;
201
+ res.status(400).end(`'${name}' cannot be empty`)
202
+ return undefined
203
203
  }
204
204
  if (isNaN(v as any)) {
205
- res.status(400).end(`'${name}' must be a number`);
206
- return undefined;
205
+ res.status(400).end(`'${name}' must be a number`)
206
+ return undefined
207
207
  }
208
- const n = parseFloat(v);
209
- return n;
208
+ const n = parseFloat(v)
209
+ return n
210
210
  }
211
211
  export function getNumber(req: Request, name: string, d?: number): number | undefined {
212
- const v = req.params[name];
212
+ const v = req.params[name]
213
213
  if (!v || v.length === 0) {
214
- return d;
214
+ return d
215
215
  }
216
216
  if (isNaN(v as any)) {
217
- return d;
217
+ return d
218
218
  }
219
- const n = parseFloat(v);
220
- return n;
219
+ const n = parseFloat(v)
220
+ return n
221
221
  }
222
222
  export function getInteger(req: Request, name: string, d?: number): number | undefined {
223
- const v = req.params[name];
223
+ const v = req.params[name]
224
224
  if (!v || v.length === 0) {
225
- return d;
225
+ return d
226
226
  }
227
227
  if (isNaN(v as any)) {
228
- return d;
228
+ return d
229
229
  }
230
- const n = parseFloat(v);
231
- const s = n.toFixed(0);
232
- return parseFloat(s);
230
+ const n = parseFloat(v)
231
+ const s = n.toFixed(0)
232
+ return parseFloat(s)
233
233
  }
234
234
  export function getRequiredDate(req: Request, res: Response, name: string): Date | undefined {
235
- const v = req.params[name];
235
+ const v = req.params[name]
236
236
  if (!v || v.length === 0) {
237
- res.status(400).end(`'${name}' cannot be empty`);
238
- return undefined;
237
+ res.status(400).end(`'${name}' cannot be empty`)
238
+ return undefined
239
239
  }
240
- const date = new Date(v);
241
- if (date.toString() === 'Invalid Date') {
242
- res.status(400).end(`'${name}' must be a date`);
243
- return undefined;
240
+ const date = new Date(v)
241
+ if (date.toString() === "Invalid Date") {
242
+ res.status(400).end(`'${name}' must be a date`)
243
+ return undefined
244
244
  }
245
- return date;
245
+ return date
246
246
  }
247
247
  export function getDate(req: Request, name: string, d?: Date): Date | undefined {
248
- const v = req.params[name];
248
+ const v = req.params[name]
249
249
  if (!v || v.length === 0) {
250
- return d;
250
+ return d
251
251
  }
252
- const date = new Date(v);
253
- if (date.toString() === 'Invalid Date') {
254
- return d;
252
+ const date = new Date(v)
253
+ if (date.toString() === "Invalid Date") {
254
+ return d
255
255
  }
256
- return date;
256
+ return date
257
257
  }
258
- const o = 'object';
258
+ const o = "object"
259
259
  export function minimize(obj: any): any {
260
260
  if (!obj || typeof obj !== o) {
261
- return obj;
261
+ return obj
262
262
  }
263
- const keys = Object.keys(obj);
263
+ const keys = Object.keys(obj)
264
264
  for (const key of keys) {
265
- const v = obj[key];
265
+ const v = obj[key]
266
266
  if (v == null) {
267
- delete obj[key];
267
+ delete obj[key]
268
268
  } else if (Array.isArray(v) && v.length > 0) {
269
- const v1 = v[0];
269
+ const v1 = v[0]
270
270
  if (typeof v1 === o && !(v1 instanceof Date)) {
271
271
  for (const item of v) {
272
- minimize(item);
272
+ minimize(item)
273
273
  }
274
274
  }
275
275
  }
276
276
  }
277
- return obj;
277
+ return obj
278
278
  }
279
279
  export function minimizeArray<T>(arrs: T[]): T[] {
280
280
  if (!arrs) {
281
- return arrs;
281
+ return arrs
282
282
  }
283
283
  if (arrs.length > 0) {
284
284
  for (const obj of arrs) {
285
- minimize(obj);
285
+ minimize(obj)
286
286
  }
287
287
  }
288
- return arrs;
288
+ return arrs
289
289
  }