express-ext 0.1.36 → 0.2.0
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/README.md +1 -1
- package/lib/http.js +11 -0
- package/package.json +1 -1
- package/src/http.ts +30 -17
package/README.md
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
# express
|
|
1
|
+
# express-ext
|
package/lib/http.js
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
Object.defineProperty(Error.prototype, 'toJSON', {
|
|
4
|
+
value: function () {
|
|
5
|
+
var alt = {};
|
|
6
|
+
Object.getOwnPropertyNames(this).forEach(function (key) {
|
|
7
|
+
alt[key] = this[key];
|
|
8
|
+
}, this);
|
|
9
|
+
return alt;
|
|
10
|
+
},
|
|
11
|
+
configurable: true,
|
|
12
|
+
writable: true
|
|
13
|
+
});
|
|
3
14
|
function handleError(err, res, log) {
|
|
4
15
|
if (log) {
|
|
5
16
|
log(toString(err));
|
package/package.json
CHANGED
package/src/http.ts
CHANGED
|
@@ -1,8 +1,22 @@
|
|
|
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
4
|
export type Log = (msg: string) => void;
|
|
5
5
|
export type LogFunc = Log;
|
|
6
|
+
|
|
7
|
+
Object.defineProperty(Error.prototype, 'toJSON', {
|
|
8
|
+
value() {
|
|
9
|
+
const alt:any = {};
|
|
10
|
+
Object.getOwnPropertyNames(this).forEach(function (key) {
|
|
11
|
+
// @ts-ignore
|
|
12
|
+
alt[key] = this[key];
|
|
13
|
+
}, this);
|
|
14
|
+
|
|
15
|
+
return alt;
|
|
16
|
+
},
|
|
17
|
+
configurable: true,
|
|
18
|
+
writable: true
|
|
19
|
+
});
|
|
6
20
|
export function handleError(err: any, res: Response, log?: (msg: string) => void) {
|
|
7
21
|
if (log) {
|
|
8
22
|
log(toString(err));
|
|
@@ -20,12 +34,12 @@ export function toString(v: any): string {
|
|
|
20
34
|
}
|
|
21
35
|
}
|
|
22
36
|
export function attr(name: string): Attribute {
|
|
23
|
-
return {name, type: 'string'};
|
|
37
|
+
return { name, type: 'string' };
|
|
24
38
|
}
|
|
25
39
|
export function attrs(keys: string[]): Attribute[] {
|
|
26
40
|
const atts: Attribute[] = [];
|
|
27
41
|
for (const str of keys) {
|
|
28
|
-
const at: Attribute = {name: str as string, type: 'string'};
|
|
42
|
+
const at: Attribute = { name: str as string, type: 'string' };
|
|
29
43
|
atts.push(at);
|
|
30
44
|
}
|
|
31
45
|
return atts;
|
|
@@ -53,7 +67,7 @@ export function queryRequiredParams(req: Request, res: Response, name: string, s
|
|
|
53
67
|
}
|
|
54
68
|
return v2.split(split);
|
|
55
69
|
}
|
|
56
|
-
export function queryParams(req: Request, name: string, d?: string[], split?: string): string[]|undefined {
|
|
70
|
+
export function queryParams(req: Request, name: string, d?: string[], split?: string): string[] | undefined {
|
|
57
71
|
const q = req.query[name];
|
|
58
72
|
if (!q) {
|
|
59
73
|
return d;
|
|
@@ -67,7 +81,7 @@ export function queryParams(req: Request, name: string, d?: string[], split?: st
|
|
|
67
81
|
}
|
|
68
82
|
return v.split(split);
|
|
69
83
|
}
|
|
70
|
-
export function queryParam(req: Request, res: Response, name: string): string|undefined {
|
|
84
|
+
export function queryParam(req: Request, res: Response, name: string): string | undefined {
|
|
71
85
|
const v = req.query[name];
|
|
72
86
|
if (!v) {
|
|
73
87
|
res.status(400).end(`'${name}' cannot be empty`);
|
|
@@ -82,14 +96,14 @@ export function queryParam(req: Request, res: Response, name: string): string|un
|
|
|
82
96
|
}
|
|
83
97
|
}
|
|
84
98
|
}
|
|
85
|
-
export function query(req: Request, name: string, d?: string): string|undefined {
|
|
99
|
+
export function query(req: Request, name: string, d?: string): string | undefined {
|
|
86
100
|
const p = req.query[name];
|
|
87
101
|
if (!p || p.toString().length === 0) {
|
|
88
102
|
return d;
|
|
89
103
|
}
|
|
90
104
|
return p.toString();
|
|
91
105
|
}
|
|
92
|
-
export function queryRequiredNumber(req: Request, res: Response, name: string): number|undefined {
|
|
106
|
+
export function queryRequiredNumber(req: Request, res: Response, name: string): number | undefined {
|
|
93
107
|
const field = req.query[name];
|
|
94
108
|
if (!field) {
|
|
95
109
|
return undefined;
|
|
@@ -106,7 +120,7 @@ export function queryRequiredNumber(req: Request, res: Response, name: string):
|
|
|
106
120
|
const n = parseFloat(v);
|
|
107
121
|
return n;
|
|
108
122
|
}
|
|
109
|
-
export function queryNumber(req: Request, name: string, d?: number): number|undefined {
|
|
123
|
+
export function queryNumber(req: Request, name: string, d?: number): number | undefined {
|
|
110
124
|
const field = req.query[name];
|
|
111
125
|
const v = field ? field.toString() : undefined;
|
|
112
126
|
if (!v || v.length === 0) {
|
|
@@ -118,7 +132,7 @@ export function queryNumber(req: Request, name: string, d?: number): number|unde
|
|
|
118
132
|
const n = parseFloat(v);
|
|
119
133
|
return n;
|
|
120
134
|
}
|
|
121
|
-
export function queryRequiredDate(req: Request, res: Response, name: string): Date|undefined {
|
|
135
|
+
export function queryRequiredDate(req: Request, res: Response, name: string): Date | undefined {
|
|
122
136
|
const field = req.query[name];
|
|
123
137
|
if (!field) {
|
|
124
138
|
return undefined;
|
|
@@ -135,7 +149,7 @@ export function queryRequiredDate(req: Request, res: Response, name: string): Da
|
|
|
135
149
|
}
|
|
136
150
|
return date;
|
|
137
151
|
}
|
|
138
|
-
export function queryDate(req: Request, name: string, d?: Date): Date|undefined {
|
|
152
|
+
export function queryDate(req: Request, name: string, d?: Date): Date | undefined {
|
|
139
153
|
const field = req.query[name];
|
|
140
154
|
if (field) {
|
|
141
155
|
const v = field.toString();
|
|
@@ -150,8 +164,7 @@ export function queryDate(req: Request, name: string, d?: Date): Date|undefined
|
|
|
150
164
|
}
|
|
151
165
|
return undefined;
|
|
152
166
|
}
|
|
153
|
-
|
|
154
|
-
export function param(req: Request, res: Response, name: string): string|undefined {
|
|
167
|
+
export function param(req: Request, res: Response, name: string): string | undefined {
|
|
155
168
|
const v = req.params[name];
|
|
156
169
|
if (!v || v.length === 0) {
|
|
157
170
|
res.status(400).end(`'${name}' cannot be empty`);
|
|
@@ -160,7 +173,7 @@ export function param(req: Request, res: Response, name: string): string|undefin
|
|
|
160
173
|
return v;
|
|
161
174
|
}
|
|
162
175
|
export const getParam = param;
|
|
163
|
-
export function params(req: Request, name: string, d?: string[], split?: string): string[]|undefined {
|
|
176
|
+
export function params(req: Request, name: string, d?: string[], split?: string): string[] | undefined {
|
|
164
177
|
const v = req.params[name];
|
|
165
178
|
if (!v || v.length === 0) {
|
|
166
179
|
return d;
|
|
@@ -171,7 +184,7 @@ export function params(req: Request, name: string, d?: string[], split?: string)
|
|
|
171
184
|
return v.split(split);
|
|
172
185
|
}
|
|
173
186
|
export const getParams = params;
|
|
174
|
-
export function getRequiredParameters(req: Request, res: Response, name: string, split?: string): string[]|undefined {
|
|
187
|
+
export function getRequiredParameters(req: Request, res: Response, name: string, split?: string): string[] | undefined {
|
|
175
188
|
const v = req.params[name];
|
|
176
189
|
if (!v || v.length === 0) {
|
|
177
190
|
res.status(400).end(`'${name}' cannot be empty`);
|
|
@@ -182,7 +195,7 @@ export function getRequiredParameters(req: Request, res: Response, name: string,
|
|
|
182
195
|
}
|
|
183
196
|
return v.split(split);
|
|
184
197
|
}
|
|
185
|
-
export function getRequiredNumber(req: Request, res: Response, name: string): number|undefined {
|
|
198
|
+
export function getRequiredNumber(req: Request, res: Response, name: string): number | undefined {
|
|
186
199
|
const v = req.params[name];
|
|
187
200
|
if (!v || v.length === 0) {
|
|
188
201
|
res.status(400).end(`'${name}' cannot be empty`);
|
|
@@ -195,7 +208,7 @@ export function getRequiredNumber(req: Request, res: Response, name: string): nu
|
|
|
195
208
|
const n = parseFloat(v);
|
|
196
209
|
return n;
|
|
197
210
|
}
|
|
198
|
-
export function getNumber(req: Request, name: string, d?: number): number|undefined {
|
|
211
|
+
export function getNumber(req: Request, name: string, d?: number): number | undefined {
|
|
199
212
|
const v = req.params[name];
|
|
200
213
|
if (!v || v.length === 0) {
|
|
201
214
|
return d;
|