express-ext 0.1.14 → 0.1.15
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/HealthController.js +4 -4
- package/lib/LogController.js +9 -3
- package/lib/health.js +2 -2
- package/lib/log.js +2 -2
- package/lib/resources.js +37 -2
- package/package.json +1 -1
- package/src/HealthController.ts +5 -5
- package/src/LogController.ts +8 -3
- package/src/health.ts +1 -1
- package/src/log.ts +2 -2
- package/src/metadata.ts +6 -4
- package/src/resources.ts +27 -1
package/lib/HealthController.js
CHANGED
|
@@ -7,12 +7,12 @@ var HealthController = (function () {
|
|
|
7
7
|
this.check = this.check.bind(this);
|
|
8
8
|
}
|
|
9
9
|
HealthController.prototype.check = function (req, res) {
|
|
10
|
-
health_1.
|
|
11
|
-
if (
|
|
12
|
-
return res.status(200).json(
|
|
10
|
+
health_1.health(this.checkers).then(function (r) {
|
|
11
|
+
if (r.status === 'UP') {
|
|
12
|
+
return res.status(200).json(r).end();
|
|
13
13
|
}
|
|
14
14
|
else {
|
|
15
|
-
return res.status(500).json(
|
|
15
|
+
return res.status(500).json(r).end();
|
|
16
16
|
}
|
|
17
17
|
});
|
|
18
18
|
};
|
package/lib/LogController.js
CHANGED
|
@@ -49,7 +49,13 @@ var LogController = (function () {
|
|
|
49
49
|
}
|
|
50
50
|
}
|
|
51
51
|
if (obj.constants !== undefined && typeof obj.constants === 'object') {
|
|
52
|
-
|
|
52
|
+
var ks = Object.keys(obj.constants);
|
|
53
|
+
if (ks.length > 0) {
|
|
54
|
+
this.logger.constants = obj.constants;
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
this.logger.constants = undefined;
|
|
58
|
+
}
|
|
53
59
|
changed = true;
|
|
54
60
|
}
|
|
55
61
|
if (obj.name) {
|
|
@@ -65,10 +71,10 @@ var LogController = (function () {
|
|
|
65
71
|
}
|
|
66
72
|
}
|
|
67
73
|
if (changed) {
|
|
68
|
-
return res.status(200).
|
|
74
|
+
return res.status(200).json(true).end();
|
|
69
75
|
}
|
|
70
76
|
else {
|
|
71
|
-
return res.status(204).
|
|
77
|
+
return res.status(204).json(false).end();
|
|
72
78
|
}
|
|
73
79
|
};
|
|
74
80
|
return LogController;
|
package/lib/health.js
CHANGED
|
@@ -36,7 +36,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
36
36
|
}
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
|
-
function
|
|
39
|
+
function health(checkers) {
|
|
40
40
|
return __awaiter(this, void 0, void 0, function () {
|
|
41
41
|
var p, total, count, _i, checkers_1, checker, sub, r, err_1;
|
|
42
42
|
return __generator(this, function (_a) {
|
|
@@ -90,4 +90,4 @@ function check(checkers) {
|
|
|
90
90
|
});
|
|
91
91
|
});
|
|
92
92
|
}
|
|
93
|
-
exports.
|
|
93
|
+
exports.health = health;
|
package/lib/log.js
CHANGED
|
@@ -190,10 +190,10 @@ var MiddlewareController = (function () {
|
|
|
190
190
|
changed = true;
|
|
191
191
|
}
|
|
192
192
|
if (changed) {
|
|
193
|
-
return res.status(200).
|
|
193
|
+
return res.status(200).json(true).end();
|
|
194
194
|
}
|
|
195
195
|
else {
|
|
196
|
-
return res.status(204).
|
|
196
|
+
return res.status(204).json(false).end();
|
|
197
197
|
}
|
|
198
198
|
};
|
|
199
199
|
return MiddlewareController;
|
package/lib/resources.js
CHANGED
|
@@ -1,2 +1,37 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
var resources = (function () {
|
|
4
|
+
function resources() {
|
|
5
|
+
}
|
|
6
|
+
return resources;
|
|
7
|
+
}());
|
|
8
|
+
exports.resources = resources;
|
|
9
|
+
var TypeChecker = (function () {
|
|
10
|
+
function TypeChecker(attributes, allowUndefined) {
|
|
11
|
+
this.attributes = attributes;
|
|
12
|
+
this.allowUndefined = allowUndefined;
|
|
13
|
+
this.check = this.check.bind(this);
|
|
14
|
+
}
|
|
15
|
+
TypeChecker.prototype.check = function (req, res, next) {
|
|
16
|
+
var obj = req.body;
|
|
17
|
+
if (!obj || obj === '') {
|
|
18
|
+
return res.status(400).end('The request body cannot be empty');
|
|
19
|
+
}
|
|
20
|
+
else {
|
|
21
|
+
var errors = resources.check(obj, this.attributes, this.allowUndefined);
|
|
22
|
+
if (errors.length > 0) {
|
|
23
|
+
res.status(400).json(errors).end();
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
next();
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
};
|
|
30
|
+
return TypeChecker;
|
|
31
|
+
}());
|
|
32
|
+
exports.TypeChecker = TypeChecker;
|
|
33
|
+
function check(attributes, allowUndefined) {
|
|
34
|
+
var x = new TypeChecker(attributes, allowUndefined);
|
|
35
|
+
return x.check;
|
|
36
|
+
}
|
|
37
|
+
exports.check = check;
|
package/package.json
CHANGED
package/src/HealthController.ts
CHANGED
|
@@ -1,16 +1,16 @@
|
|
|
1
1
|
import {Request, Response} from 'express';
|
|
2
|
-
import {
|
|
2
|
+
import {health, HealthChecker} from './health';
|
|
3
3
|
|
|
4
4
|
export class HealthController {
|
|
5
5
|
constructor(protected checkers: HealthChecker[]) {
|
|
6
6
|
this.check = this.check.bind(this);
|
|
7
7
|
}
|
|
8
8
|
check(req: Request, res: Response) {
|
|
9
|
-
|
|
10
|
-
if (
|
|
11
|
-
return res.status(200).json(
|
|
9
|
+
health(this.checkers).then(r => {
|
|
10
|
+
if (r.status === 'UP') {
|
|
11
|
+
return res.status(200).json(r).end();
|
|
12
12
|
} else {
|
|
13
|
-
return res.status(500).json(
|
|
13
|
+
return res.status(500).json(r).end();
|
|
14
14
|
}
|
|
15
15
|
});
|
|
16
16
|
}
|
package/src/LogController.ts
CHANGED
|
@@ -99,7 +99,12 @@ export class LogController {
|
|
|
99
99
|
}
|
|
100
100
|
}
|
|
101
101
|
if (obj.constants !== undefined && typeof obj.constants === 'object') {
|
|
102
|
-
|
|
102
|
+
const ks = Object.keys(obj.constants);
|
|
103
|
+
if (ks.length > 0) {
|
|
104
|
+
this.logger.constants = obj.constants;
|
|
105
|
+
} else {
|
|
106
|
+
this.logger.constants = undefined;
|
|
107
|
+
}
|
|
103
108
|
changed = true;
|
|
104
109
|
}
|
|
105
110
|
if (obj.name) {
|
|
@@ -115,9 +120,9 @@ export class LogController {
|
|
|
115
120
|
}
|
|
116
121
|
}
|
|
117
122
|
if (changed) {
|
|
118
|
-
return res.status(200).
|
|
123
|
+
return res.status(200).json(true).end();
|
|
119
124
|
} else {
|
|
120
|
-
return res.status(204).
|
|
125
|
+
return res.status(204).json(false).end();
|
|
121
126
|
}
|
|
122
127
|
}
|
|
123
128
|
}
|
package/src/health.ts
CHANGED
|
@@ -16,7 +16,7 @@ export interface HealthChecker {
|
|
|
16
16
|
check(): Promise<AnyMap>;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export async function
|
|
19
|
+
export async function health(checkers: HealthChecker[]): Promise<Health> {
|
|
20
20
|
const p: Health = { status: 'UP' };
|
|
21
21
|
const total = checkers.length - 1;
|
|
22
22
|
let count = 0;
|
package/src/log.ts
CHANGED
|
@@ -200,9 +200,9 @@ export class MiddlewareController {
|
|
|
200
200
|
changed = true;
|
|
201
201
|
}
|
|
202
202
|
if (changed) {
|
|
203
|
-
return res.status(200).
|
|
203
|
+
return res.status(200).json(true).end();
|
|
204
204
|
} else {
|
|
205
|
-
return res.status(204).
|
|
205
|
+
return res.status(204).json(false).end();
|
|
206
206
|
}
|
|
207
207
|
}
|
|
208
208
|
}
|
package/src/metadata.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
|
-
export type
|
|
1
|
+
export type Type = 'ObjectId' | 'date' | 'datetime' | 'time'
|
|
2
2
|
| 'boolean' | 'number' | 'integer' | 'string' | 'text'
|
|
3
3
|
| 'object' | 'array' | 'binary'
|
|
4
4
|
| 'primitives' | 'booleans' | 'numbers' | 'integers' | 'strings' | 'dates' | 'datetimes' | 'times';
|
|
5
|
-
export type
|
|
5
|
+
export type Format = 'currency' | 'percentage' | 'email' | 'url' | 'phone' | 'fax' | 'ipv4' | 'ipv6';
|
|
6
6
|
|
|
7
|
+
export type DataType = Type;
|
|
8
|
+
export type FormatType = Format;
|
|
7
9
|
export interface ErrorMessage {
|
|
8
10
|
field: string;
|
|
9
11
|
code: string;
|
|
@@ -17,8 +19,8 @@ export interface Model {
|
|
|
17
19
|
|
|
18
20
|
export interface Attribute {
|
|
19
21
|
name?: string;
|
|
20
|
-
type?:
|
|
21
|
-
format?:
|
|
22
|
+
type?: Type;
|
|
23
|
+
format?: Format;
|
|
22
24
|
required?: boolean;
|
|
23
25
|
key?: boolean;
|
|
24
26
|
length?: number;
|
package/src/resources.ts
CHANGED
|
@@ -1,10 +1,36 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { NextFunction, Request, Response } from 'express';
|
|
2
|
+
import { Attributes, ErrorMessage } from './metadata';
|
|
2
3
|
|
|
3
4
|
// tslint:disable-next-line:class-name
|
|
4
5
|
export class resources {
|
|
5
6
|
static createValidator?: <T>(attributes: Attributes, allowUndefined?: boolean, max?: number) => Validator<T>;
|
|
7
|
+
static check: (obj: any, attributes: Attributes, allowUndefined?: boolean, patch?: boolean) => ErrorMessage[];
|
|
6
8
|
}
|
|
7
9
|
|
|
8
10
|
export interface Validator<T> {
|
|
9
11
|
validate(obj: T, patch?: boolean): Promise<ErrorMessage[]>;
|
|
10
12
|
}
|
|
13
|
+
|
|
14
|
+
export class TypeChecker {
|
|
15
|
+
constructor(public attributes: Attributes, public allowUndefined?: boolean) {
|
|
16
|
+
this.check = this.check.bind(this);
|
|
17
|
+
}
|
|
18
|
+
check(req: Request, res: Response, next: NextFunction): void {
|
|
19
|
+
const obj = req.body;
|
|
20
|
+
if (!obj || (obj as any) === '') {
|
|
21
|
+
return res.status(400).end('The request body cannot be empty');
|
|
22
|
+
} else {
|
|
23
|
+
const errors = resources.check(obj, this.attributes, this.allowUndefined);
|
|
24
|
+
if (errors.length > 0) {
|
|
25
|
+
res.status(400).json(errors).end();
|
|
26
|
+
} else {
|
|
27
|
+
next();
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
export type Handler = (req: Request, res: Response, next: NextFunction) => void;
|
|
33
|
+
export function check(attributes: Attributes, allowUndefined?: boolean): Handler {
|
|
34
|
+
const x = new TypeChecker(attributes, allowUndefined);
|
|
35
|
+
return x.check;
|
|
36
|
+
}
|