express-ext 0.1.36 → 0.2.1

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 CHANGED
@@ -1 +1 @@
1
- # express
1
+ # express-ext
@@ -20,12 +20,12 @@ var resources_1 = require("./resources");
20
20
  var view_1 = require("./view");
21
21
  var GenericController = (function (_super) {
22
22
  __extends(GenericController, _super);
23
- function GenericController(log, service, status, build, validate) {
23
+ function GenericController(log, service, build, validate, returnNumber) {
24
24
  var _this = _super.call(this, log, service) || this;
25
25
  _this.service = service;
26
26
  _this.build = build;
27
27
  _this.validate = validate;
28
- _this.status = edit_1.initializeStatus(status);
28
+ _this.returnNumber = returnNumber;
29
29
  if (service.metadata) {
30
30
  var m = service.metadata();
31
31
  if (m) {
@@ -47,18 +47,18 @@ var GenericController = (function (_super) {
47
47
  return this.insert(req, res);
48
48
  };
49
49
  GenericController.prototype.insert = function (req, res) {
50
- validateAndCreate(req, res, this.status, this.service.insert, this.log, this.validate, this.build);
50
+ validateAndCreate(req, res, this.service.insert, this.log, this.validate, this.build);
51
51
  };
52
52
  GenericController.prototype.update = function (req, res) {
53
53
  var id = buildAndCheckIdWithBody(req, res, this.keys, this.service.update);
54
54
  if (id) {
55
- validateAndUpdate(res, this.status, req.body, false, this.service.update, this.log, this.validate, this.build);
55
+ validateAndUpdate(res, req.body, false, this.service.update, this.log, this.validate, this.build);
56
56
  }
57
57
  };
58
58
  GenericController.prototype.patch = function (req, res) {
59
59
  var id = buildAndCheckIdWithBody(req, res, this.keys, this.service.patch);
60
60
  if (id && this.service.patch) {
61
- validateAndUpdate(res, this.status, req.body, true, this.service.patch, this.log, this.validate, this.build);
61
+ validateAndUpdate(res, req.body, true, this.service.patch, this.log, this.validate, this.build);
62
62
  }
63
63
  };
64
64
  GenericController.prototype.delete = function (req, res) {
@@ -78,7 +78,7 @@ var GenericController = (function (_super) {
78
78
  return GenericController;
79
79
  }(LoadController_1.LoadController));
80
80
  exports.GenericController = GenericController;
81
- function validateAndCreate(req, res, status, save, log, validate, build) {
81
+ function validateAndCreate(req, res, save, log, validate, build, returnNumber) {
82
82
  var obj = req.body;
83
83
  if (!obj || obj === '') {
84
84
  res.status(400).end('The request body cannot be empty.');
@@ -87,40 +87,38 @@ function validateAndCreate(req, res, status, save, log, validate, build) {
87
87
  if (validate) {
88
88
  validate(obj).then(function (errors) {
89
89
  if (errors && errors.length > 0) {
90
- var r = { status: status.validation_error, errors: errors };
91
- res.status(getStatusCode(errors)).json(r).end();
90
+ res.status(getStatusCode(errors)).json(errors).end();
92
91
  }
93
92
  else {
94
93
  if (build) {
95
94
  build(res, obj, true);
96
95
  }
97
- edit_1.create(res, status, obj, save, log);
96
+ edit_1.create(res, obj, save, log, returnNumber);
98
97
  }
99
98
  }).catch(function (err) { return http_1.handleError(err, res, log); });
100
99
  }
101
100
  else {
102
- edit_1.create(res, status, obj, save, log);
101
+ edit_1.create(res, obj, save, log, returnNumber);
103
102
  }
104
103
  }
105
104
  }
106
105
  exports.validateAndCreate = validateAndCreate;
107
- function validateAndUpdate(res, status, obj, isPatch, save, log, validate, build) {
106
+ function validateAndUpdate(res, obj, isPatch, save, log, validate, build, returnNumber) {
108
107
  if (validate) {
109
108
  validate(obj, isPatch).then(function (errors) {
110
109
  if (errors && errors.length > 0) {
111
- var r = { status: status.validation_error, errors: errors };
112
- res.status(getStatusCode(errors)).json(r).end();
110
+ res.status(getStatusCode(errors)).json(errors).end();
113
111
  }
114
112
  else {
115
113
  if (build) {
116
114
  build(res, obj, false, isPatch);
117
115
  }
118
- edit_1.update(res, status, obj, save, log);
116
+ edit_1.update(res, obj, save, log, returnNumber);
119
117
  }
120
118
  }).catch(function (err) { return http_1.handleError(err, res, log); });
121
119
  }
122
120
  else {
123
- edit_1.update(res, status, obj, save, log);
121
+ edit_1.update(res, obj, save, log, returnNumber);
124
122
  }
125
123
  }
126
124
  exports.validateAndUpdate = validateAndUpdate;
@@ -20,7 +20,7 @@ var search_func_1 = require("./search_func");
20
20
  var GenericSearchController = (function (_super) {
21
21
  __extends(GenericSearchController, _super);
22
22
  function GenericSearchController(log, find, service, config, build, validate, dates, numbers) {
23
- var _this = _super.call(this, log, service, config, build, validate) || this;
23
+ var _this = _super.call(this, log, service, build, validate) || this;
24
24
  _this.find = find;
25
25
  _this.search = _this.search.bind(_this);
26
26
  _this.config = search_1.initializeConfig(config);
@@ -20,7 +20,7 @@ var search_func_1 = require("./search_func");
20
20
  var LowcodeController = (function (_super) {
21
21
  __extends(LowcodeController, _super);
22
22
  function LowcodeController(log, lowCodeService, config, build, validate, dates, numbers) {
23
- var _this = _super.call(this, log, lowCodeService, config, build, validate) || this;
23
+ var _this = _super.call(this, log, lowCodeService, build, validate) || this;
24
24
  _this.lowCodeService = lowCodeService;
25
25
  _this.search = _this.search.bind(_this);
26
26
  _this.config = search_1.initializeConfig(config);
@@ -55,7 +55,7 @@ exports.LowcodeHandler = LowcodeController;
55
55
  var Controller = (function (_super) {
56
56
  __extends(Controller, _super);
57
57
  function Controller(log, lowCodeService, build, validate, config, dates, numbers) {
58
- var _this = _super.call(this, log, lowCodeService, config, build, validate) || this;
58
+ var _this = _super.call(this, log, lowCodeService, build, validate) || this;
59
59
  _this.lowCodeService = lowCodeService;
60
60
  _this.search = _this.search.bind(_this);
61
61
  _this.config = search_1.initializeConfig(config);
package/lib/edit.js CHANGED
@@ -1,21 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  var http_1 = require("./http");
4
- function initializeStatus(s) {
5
- if (s) {
6
- return s;
7
- }
8
- var s1 = {
9
- duplicate_key: 0,
10
- not_found: 0,
11
- success: 1,
12
- version_error: 2,
13
- validation_error: 4,
14
- error: 4
15
- };
16
- return s1;
17
- }
18
- exports.initializeStatus = initializeStatus;
19
4
  function checkId(obj, id, keys) {
20
5
  var n = (keys && keys.length === 1 && keys[0].name ? keys[0].name : 'id');
21
6
  var o = obj;
@@ -48,45 +33,37 @@ function checkId(obj, id, keys) {
48
33
  return true;
49
34
  }
50
35
  exports.checkId = checkId;
51
- function create(res, status, obj, insert, log) {
36
+ function create(res, obj, insert, log, returnNumber) {
52
37
  insert(obj).then(function (result) {
53
38
  if (typeof result === 'number') {
54
39
  if (result >= 1) {
55
- var r = { status: status.success, value: obj };
56
- res.status(201).json(r).end();
57
- }
58
- else if (result === 0) {
59
- var r = { status: status.duplicate_key };
60
- res.status(201).json(r).end();
40
+ res.status(201).json(returnNumber ? result : obj).end();
61
41
  }
62
42
  else {
63
- res.status(500).end('Internal Server Error');
43
+ res.status(409).json(result).end();
64
44
  }
65
45
  }
66
46
  else {
67
- res.status(200).json(result).end();
47
+ res.status(422).json(result).end();
68
48
  }
69
49
  }).catch(function (err) { return http_1.handleError(err, res, log); });
70
50
  }
71
51
  exports.create = create;
72
- function update(res, status, obj, save, log) {
52
+ function update(res, obj, save, log, returnNumber) {
73
53
  save(obj).then(function (result) {
74
54
  if (typeof result === 'number') {
75
55
  if (result >= 1) {
76
- var r = { status: status.success, value: obj };
77
- res.status(201).json(r).end();
56
+ res.status(200).json(returnNumber ? result : obj).end();
78
57
  }
79
58
  else if (result === 0) {
80
- var r = { status: status.not_found };
81
- res.status(404).json(r).end();
59
+ res.status(404).json(result).end();
82
60
  }
83
61
  else {
84
- var r = { status: status.not_found };
85
- res.status(409).json(r).end();
62
+ res.status(409).json(result).end();
86
63
  }
87
64
  }
88
65
  else {
89
- res.status(200).json(result).end();
66
+ res.status(422).json(result).end();
90
67
  }
91
68
  }).catch(function (err) { return http_1.handleError(err, res, log); });
92
69
  }
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
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "express-ext",
3
- "version": "0.1.36",
3
+ "version": "0.2.1",
4
4
  "description": "express-ext",
5
5
  "main": "./lib/index.js",
6
6
  "types": "./src/index.ts",
@@ -1,5 +1,5 @@
1
1
  import {Request, Response} from 'express';
2
- import {checkId, create, initializeStatus, isTypeError, ResultInfo, StatusConfig, update} from './edit';
2
+ import {checkId, create, isTypeError, update} from './edit';
3
3
  import {handleError, Log} from './http';
4
4
  import {LoadController} from './LoadController';
5
5
  import {Attribute, Attributes, ErrorMessage} from './metadata';
@@ -8,7 +8,7 @@ import {buildAndCheckId, buildId} from './view';
8
8
 
9
9
  export type Build<T> = (res: Response, obj: T, isCreate?: boolean, isPatch?: boolean) => void;
10
10
  export type Validate<T> = (obj: T, patch?: boolean) => Promise<ErrorMessage[]>;
11
- export type Save<T> = (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>;
11
+ export type Save<T> = (obj: T, ctx?: any) => Promise<number|ErrorMessage[]>;
12
12
  export interface GenericService<T, ID, R> {
13
13
  metadata?(): Attributes|undefined;
14
14
  load(id: ID, ctx?: any): Promise<T|null>;
@@ -18,11 +18,11 @@ export interface GenericService<T, ID, R> {
18
18
  delete?(id: ID, ctx?: any): Promise<number>;
19
19
  }
20
20
  export class GenericController<T, ID> extends LoadController<T, ID> {
21
- status: StatusConfig;
22
21
  metadata?: Attributes;
23
- constructor(log: Log, public service: GenericService<T, ID, number|ResultInfo<T>>, status?: StatusConfig, public build?: Build<T>, public validate?: Validate<T>) {
22
+ returnNumber?: boolean;
23
+ constructor(log: Log, public service: GenericService<T, ID, number|ErrorMessage[]>, public build?: Build<T>, public validate?: Validate<T>, returnNumber?: boolean) {
24
24
  super(log, service);
25
- this.status = initializeStatus(status);
25
+ this.returnNumber = returnNumber;
26
26
  if (service.metadata) {
27
27
  const m = service.metadata();
28
28
  if (m) {
@@ -43,18 +43,18 @@ export class GenericController<T, ID> extends LoadController<T, ID> {
43
43
  return this.insert(req, res);
44
44
  }
45
45
  insert(req: Request, res: Response): void {
46
- validateAndCreate(req, res, this.status, this.service.insert, this.log, this.validate, this.build);
46
+ validateAndCreate(req, res, this.service.insert, this.log, this.validate, this.build);
47
47
  }
48
48
  update(req: Request, res: Response): void {
49
49
  const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.update);
50
50
  if (id) {
51
- validateAndUpdate(res, this.status, req.body, false, this.service.update, this.log, this.validate, this.build);
51
+ validateAndUpdate(res, req.body, false, this.service.update, this.log, this.validate, this.build);
52
52
  }
53
53
  }
54
54
  patch(req: Request, res: Response): void {
55
55
  const id = buildAndCheckIdWithBody<T, ID, any>(req, res, this.keys, this.service.patch);
56
56
  if (id && this.service.patch) {
57
- validateAndUpdate(res, this.status, req.body, true, this.service.patch, this.log, this.validate, this.build);
57
+ validateAndUpdate(res, req.body, true, this.service.patch, this.log, this.validate, this.build);
58
58
  }
59
59
  }
60
60
  delete(req: Request, res: Response): void {
@@ -70,7 +70,7 @@ export class GenericController<T, ID> extends LoadController<T, ID> {
70
70
  }
71
71
  }
72
72
  }
73
- export function validateAndCreate<T>(req: Request, res: Response, status: StatusConfig, save: Save<T>, log: Log, validate?: Validate<T>, build?: Build<T>): void {
73
+ export function validateAndCreate<T>(req: Request, res: Response, save: Save<T>, log: Log, validate?: Validate<T>, build?: Build<T>, returnNumber?: boolean): void {
74
74
  const obj = req.body;
75
75
  if (!obj || obj === '') {
76
76
  res.status(400).end('The request body cannot be empty.');
@@ -78,35 +78,33 @@ export function validateAndCreate<T>(req: Request, res: Response, status: Status
78
78
  if (validate) {
79
79
  validate(obj).then(errors => {
80
80
  if (errors && errors.length > 0) {
81
- const r: ResultInfo<T> = {status: status.validation_error, errors};
82
- res.status(getStatusCode(errors)).json(r).end();
81
+ res.status(getStatusCode(errors)).json(errors).end();
83
82
  } else {
84
83
  if (build) {
85
84
  build(res, obj, true);
86
85
  }
87
- create(res, status, obj, save, log);
86
+ create(res, obj, save, log, returnNumber);
88
87
  }
89
88
  }).catch(err => handleError(err, res, log));
90
89
  } else {
91
- create(res, status, obj, save, log);
90
+ create(res, obj, save, log, returnNumber);
92
91
  }
93
92
  }
94
93
  }
95
- export function validateAndUpdate<T>(res: Response, status: StatusConfig, obj: T, isPatch: boolean, save: Save<T>, log: Log, validate?: Validate<T>, build?: Build<T>): void {
94
+ export function validateAndUpdate<T>(res: Response, obj: T, isPatch: boolean, save: Save<T>, log: Log, validate?: Validate<T>, build?: Build<T>, returnNumber?: boolean): void {
96
95
  if (validate) {
97
96
  validate(obj, isPatch).then(errors => {
98
97
  if (errors && errors.length > 0) {
99
- const r: ResultInfo<T> = {status: status.validation_error, errors};
100
- res.status(getStatusCode(errors)).json(r).end();
98
+ res.status(getStatusCode(errors)).json(errors).end();
101
99
  } else {
102
100
  if (build) {
103
101
  build(res, obj, false, isPatch);
104
102
  }
105
- update(res, status, obj, save, log);
103
+ update(res, obj, save, log, returnNumber);
106
104
  }
107
105
  }).catch(err => handleError(err, res, log));
108
106
  } else {
109
- update(res, status, obj, save, log);
107
+ update(res, obj, save, log, returnNumber);
110
108
  }
111
109
  }
112
110
  export function buildAndCheckIdWithBody<T, ID, R>(req: Request, res: Response, keys?: Attribute[], patch?: (obj: T, ctx?: any) => Promise<R>): ID | undefined {
@@ -1,13 +1,10 @@
1
1
  import {Request, Response} from 'express';
2
- import {ResultInfo, StatusConfig} from './edit';
3
2
  import {Build, GenericController, GenericService} from './GenericController';
4
3
  import {handleError, Log} from './http';
5
4
  import {ErrorMessage} from './metadata';
6
5
  import {buildArray, Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
7
6
  import {getMetadataFunc} from './search_func';
8
7
 
9
- export interface Config extends StatusConfig, SearchConfig {
10
- }
11
8
  export class GenericSearchController<T, ID, S extends Filter> extends GenericController<T, ID> {
12
9
  config?: SearchConfig;
13
10
  csv?: boolean;
@@ -16,8 +13,8 @@ export class GenericSearchController<T, ID, S extends Filter> extends GenericCon
16
13
  fields?: string;
17
14
  excluding?: string;
18
15
  array?: string[];
19
- constructor(log: Log, public find: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>, service: GenericService<T, ID, number|ResultInfo<T>>, config?: Config, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
20
- super(log, service, config, build, validate);
16
+ constructor(log: Log, public find: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>, service: GenericService<T, ID, number|ErrorMessage[]>, config?: SearchConfig, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
17
+ super(log, service, build, validate);
21
18
  this.search = this.search.bind(this);
22
19
  this.config = initializeConfig(config);
23
20
  if (this.config) {
@@ -1,13 +1,10 @@
1
1
  import {Request, Response} from 'express';
2
- import {ResultInfo, StatusConfig} from './edit';
3
2
  import {Build, GenericController, GenericService} from './GenericController';
4
3
  import {handleError, Log} from './http';
5
4
  import {ErrorMessage} from './metadata';
6
5
  import {buildArray, Filter, format, fromRequest, getParameters, initializeConfig, jsonResult, SearchConfig, SearchResult} from './search';
7
6
  import {getMetadataFunc} from './search_func';
8
7
 
9
- export interface LowCodeConfig extends StatusConfig, SearchConfig {
10
- }
11
8
  export interface Service<T, ID, R, S extends Filter> extends GenericService<T, ID, R> {
12
9
  search: (s: S, limit?: number, skip?: number|string, fields?: string[]) => Promise<SearchResult<T>>;
13
10
  }
@@ -19,8 +16,8 @@ export class LowcodeController<T, ID, S extends Filter> extends GenericControlle
19
16
  fields?: string;
20
17
  excluding?: string;
21
18
  array?: string[];
22
- constructor(log: Log, public lowCodeService: Service<T, ID, number|ResultInfo<T>, S>, config?: LowCodeConfig, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
23
- super(log, lowCodeService, config, build, validate);
19
+ constructor(log: Log, public lowCodeService: Service<T, ID, number|ErrorMessage[], S>, config?: SearchConfig, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, dates?: string[], numbers?: string[]) {
20
+ super(log, lowCodeService, build, validate);
24
21
  this.search = this.search.bind(this);
25
22
  this.config = initializeConfig(config);
26
23
  if (this.config) {
@@ -55,8 +52,8 @@ export class Controller<T, ID, S extends Filter> extends GenericController<T, ID
55
52
  fields?: string;
56
53
  excluding?: string;
57
54
  array?: string[];
58
- constructor(log: Log, public lowCodeService: Service<T, ID, number|ResultInfo<T>, S>, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, config?: LowCodeConfig, dates?: string[], numbers?: string[]) {
59
- super(log, lowCodeService, config, build, validate);
55
+ constructor(log: Log, public lowCodeService: Service<T, ID, number|ErrorMessage[], S>, build?: Build<T>, validate?: (obj: T, patch?: boolean) => Promise<ErrorMessage[]>, config?: SearchConfig, dates?: string[], numbers?: string[]) {
56
+ super(log, lowCodeService, build, validate);
60
57
  this.search = this.search.bind(this);
61
58
  this.config = initializeConfig(config);
62
59
  if (this.config) {
package/src/edit.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import {Response} from 'express';
2
2
  import {handleError} from './http';
3
3
  import {Attribute, ErrorMessage} from './metadata';
4
-
4
+ /*
5
5
  export interface StatusConfig {
6
6
  duplicate_key: number|string;
7
7
  not_found: number|string;
@@ -30,6 +30,7 @@ export function initializeStatus(s?: StatusConfig): StatusConfig {
30
30
  };
31
31
  return s1;
32
32
  }
33
+ */
33
34
  export function checkId<T, ID>(obj: T, id: ID, keys?: Attribute[]): boolean {
34
35
  const n: string = (keys && keys.length === 1 && keys[0].name ? keys[0].name : 'id');
35
36
  const o: any = obj;
@@ -61,38 +62,31 @@ export function checkId<T, ID>(obj: T, id: ID, keys?: Attribute[]): boolean {
61
62
  }
62
63
  return true;
63
64
  }
64
- export function create<T>(res: Response, status: StatusConfig, obj: T, insert: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log: (msg: string, ctx?: any) => void): void {
65
+ export function create<T>(res: Response, obj: T, insert: (obj: T, ctx?: any) => Promise<number|ErrorMessage[]>, log: (msg: string, ctx?: any) => void, returnNumber?: boolean): void {
65
66
  insert(obj).then(result => {
66
67
  if (typeof result === 'number') {
67
68
  if (result >= 1) {
68
- const r: ResultInfo<T> = {status: status.success, value: obj};
69
- res.status(201).json(r).end();
70
- } else if (result === 0) {
71
- const r: ResultInfo<T> = {status: status.duplicate_key};
72
- res.status(201).json(r).end();
69
+ res.status(201).json(returnNumber ? result : obj).end();
73
70
  } else {
74
- res.status(500).end('Internal Server Error');
71
+ res.status(409).json(result).end();
75
72
  }
76
73
  } else {
77
- res.status(200).json(result).end();
74
+ res.status(422).json(result).end();
78
75
  }
79
76
  }).catch(err => handleError(err, res, log));
80
77
  }
81
- export function update<T>(res: Response, status: StatusConfig, obj: T, save: (obj: T, ctx?: any) => Promise<number|ResultInfo<T>>, log: (msg: string, ctx?: any) => void): void {
78
+ export function update<T>(res: Response, obj: T, save: (obj: T, ctx?: any) => Promise<number|ErrorMessage[]>, log: (msg: string, ctx?: any) => void, returnNumber?: boolean): void {
82
79
  save(obj).then(result => {
83
80
  if (typeof result === 'number') {
84
81
  if (result >= 1) {
85
- const r: ResultInfo<T> = {status: status.success, value: obj};
86
- res.status(201).json(r).end();
82
+ res.status(200).json(returnNumber ? result : obj).end();
87
83
  } else if (result === 0) {
88
- const r: ResultInfo<T> = {status: status.not_found};
89
- res.status(404).json(r).end();
84
+ res.status(404).json(result).end();
90
85
  } else {
91
- const r: ResultInfo<T> = {status: status.not_found};
92
- res.status(409).json(r).end();
86
+ res.status(409).json(result).end();
93
87
  }
94
88
  } else {
95
- res.status(200).json(result).end();
89
+ res.status(422).json(result).end();
96
90
  }
97
91
  }).catch(err => handleError(err, res, log));
98
92
  }
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;