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.
@@ -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.check(this.checkers).then(function (heath) {
11
- if (heath.status === 'UP') {
12
- return res.status(200).json(heath).end();
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(heath).end();
15
+ return res.status(500).json(r).end();
16
16
  }
17
17
  });
18
18
  };
@@ -49,7 +49,13 @@ var LogController = (function () {
49
49
  }
50
50
  }
51
51
  if (obj.constants !== undefined && typeof obj.constants === 'object') {
52
- this.logger.constants = obj.constants;
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).end('true');
74
+ return res.status(200).json(true).end();
69
75
  }
70
76
  else {
71
- return res.status(204).end('false');
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 check(checkers) {
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.check = check;
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).end('true');
193
+ return res.status(200).json(true).end();
194
194
  }
195
195
  else {
196
- return res.status(204).end('false');
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";Object.defineProperty(exports,"__esModule",{value:!0});var resources=(function(){function resources(){}
2
- return resources}());exports.resources=resources
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "description": "express-ext",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -1,16 +1,16 @@
1
1
  import {Request, Response} from 'express';
2
- import {check, HealthChecker} from './health';
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
- check(this.checkers).then(heath => {
10
- if (heath.status === 'UP') {
11
- return res.status(200).json(heath).end();
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(heath).end();
13
+ return res.status(500).json(r).end();
14
14
  }
15
15
  });
16
16
  }
@@ -99,7 +99,12 @@ export class LogController {
99
99
  }
100
100
  }
101
101
  if (obj.constants !== undefined && typeof obj.constants === 'object') {
102
- this.logger.constants = obj.constants;
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).end('true');
123
+ return res.status(200).json(true).end();
119
124
  } else {
120
- return res.status(204).end('false');
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 check(checkers: HealthChecker[]): Promise<Health> {
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).end('true');
203
+ return res.status(200).json(true).end();
204
204
  } else {
205
- return res.status(204).end('false');
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 DataType = 'ObjectId' | 'date' | 'datetime' | 'time'
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 FormatType = 'currency' | 'percentage' | 'email' | 'url' | 'phone' | 'fax' | 'ipv4' | 'ipv6';
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?: DataType;
21
- format?: FormatType;
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 {Attributes, ErrorMessage} from './metadata';
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
+ }