express-ext 0.5.18 → 0.6.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.
@@ -30,8 +30,8 @@ var resources_1 = require("./resources")
30
30
  var view_1 = require("./view")
31
31
  var GenericController = (function (_super) {
32
32
  __extends(GenericController, _super)
33
- function GenericController(log, service, build, validate, returnNumber) {
34
- var _this = _super.call(this, log, service) || this
33
+ function GenericController(service, build, validate, returnNumber) {
34
+ var _this = _super.call(this, service) || this
35
35
  _this.service = service
36
36
  _this.build = build
37
37
  _this.validate = validate
@@ -53,22 +53,21 @@ var GenericController = (function (_super) {
53
53
  return _this
54
54
  }
55
55
  GenericController.prototype.create = function (req, res) {
56
- validateAndCreate(req, res, this.service.create, this.log, this.validate, this.build)
56
+ validateAndCreate(req, res, this.service.create, this.validate, this.build)
57
57
  }
58
58
  GenericController.prototype.update = function (req, res) {
59
59
  var id = buildAndCheckIdWithBody(req, res, this.keys, this.service.update)
60
60
  if (id) {
61
- validateAndUpdate(res, req.body, false, this.service.update, this.log, this.validate, undefined, this.build)
61
+ validateAndUpdate(res, req.body, false, this.service.update, this.validate, undefined, this.build)
62
62
  }
63
63
  }
64
64
  GenericController.prototype.patch = function (req, res) {
65
65
  var id = buildAndCheckIdWithBody(req, res, this.keys, this.service.patch)
66
66
  if (id && this.service.patch) {
67
- validateAndUpdate(res, req.body, true, this.service.patch, this.log, this.validate, undefined, this.build)
67
+ validateAndUpdate(res, req.body, true, this.service.patch, this.validate, undefined, this.build)
68
68
  }
69
69
  }
70
70
  GenericController.prototype.delete = function (req, res) {
71
- var _this = this
72
71
  var id = view_1.buildAndCheckId(req, res, this.keys)
73
72
  if (id) {
74
73
  if (!this.service.delete) {
@@ -80,7 +79,7 @@ var GenericController = (function (_super) {
80
79
  res.status(getDeleteStatus(count)).json(count).end()
81
80
  })
82
81
  .catch(function (err) {
83
- return http_1.handleError(err, res, _this.log)
82
+ return http_1.handleError(err, res)
84
83
  })
85
84
  }
86
85
  }
@@ -88,7 +87,7 @@ var GenericController = (function (_super) {
88
87
  return GenericController
89
88
  })(LoadController_1.LoadController)
90
89
  exports.GenericController = GenericController
91
- function validateAndCreate(req, res, save, log, validate, build, returnNumber) {
90
+ function validateAndCreate(req, res, save, validate, build, returnNumber) {
92
91
  var obj = req.body
93
92
  if (!obj || obj === "") {
94
93
  res.status(400).end("The request body cannot be empty.")
@@ -102,19 +101,19 @@ function validateAndCreate(req, res, save, log, validate, build, returnNumber) {
102
101
  if (build) {
103
102
  build(res, obj, true)
104
103
  }
105
- edit_1.create(res, obj, save, log, returnNumber)
104
+ edit_1.create(res, obj, save, returnNumber)
106
105
  }
107
106
  })
108
107
  .catch(function (err) {
109
- return http_1.handleError(err, res, log)
108
+ return http_1.handleError(err, res)
110
109
  })
111
110
  } else {
112
- edit_1.create(res, obj, save, log, returnNumber)
111
+ edit_1.create(res, obj, save, returnNumber)
113
112
  }
114
113
  }
115
114
  }
116
115
  exports.validateAndCreate = validateAndCreate
117
- function validateAndUpdate(res, obj, isPatch, save, log, validate, resource, build, returnNumber) {
116
+ function validateAndUpdate(res, obj, isPatch, save, validate, resource, build, returnNumber) {
118
117
  if (validate) {
119
118
  validate(obj, resource, isPatch)
120
119
  .then(function (errors) {
@@ -124,14 +123,14 @@ function validateAndUpdate(res, obj, isPatch, save, log, validate, resource, bui
124
123
  if (build) {
125
124
  build(res, obj, false, isPatch)
126
125
  }
127
- edit_1.update(res, obj, save, log, returnNumber)
126
+ edit_1.update(res, obj, save, returnNumber)
128
127
  }
129
128
  })
130
129
  .catch(function (err) {
131
- return http_1.handleError(err, res, log)
130
+ return http_1.handleError(err, res)
132
131
  })
133
132
  } else {
134
- edit_1.update(res, obj, save, log, returnNumber)
133
+ edit_1.update(res, obj, save, returnNumber)
135
134
  }
136
135
  }
137
136
  exports.validateAndUpdate = validateAndUpdate
@@ -29,8 +29,8 @@ var resources_1 = require("./resources")
29
29
  var search_1 = require("./search")
30
30
  var GenericSearchController = (function (_super) {
31
31
  __extends(GenericSearchController, _super)
32
- function GenericSearchController(log, find, service, config, build, validate, dates, numbers) {
33
- var _this = _super.call(this, log, service, build, validate) || this
32
+ function GenericSearchController(find, service, config, build, validate, dates, numbers) {
33
+ var _this = _super.call(this, service, build, validate) || this
34
34
  _this.find = find
35
35
  _this.search = _this.search.bind(_this)
36
36
  _this.config = search_1.initializeConfig(config)
@@ -55,7 +55,7 @@ var GenericSearchController = (function (_super) {
55
55
  return search_1.jsonResult(res, result, _this.csv, l.fields, _this.config)
56
56
  })
57
57
  .catch(function (err) {
58
- return http_1.handleError(err, res, _this.log)
58
+ return http_1.handleError(err, res)
59
59
  })
60
60
  }
61
61
  return GenericSearchController
@@ -1,91 +1,91 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var http_1 = require("./http");
4
- var view_1 = require("./view");
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", { value: true })
3
+ var http_1 = require("./http")
4
+ var view_1 = require("./view")
5
5
  function getViewFunc(viewService) {
6
- if (typeof viewService === 'function') {
7
- return viewService;
6
+ if (typeof viewService === "function") {
7
+ return viewService
8
8
  }
9
- return viewService.load;
9
+ return viewService.load
10
10
  }
11
11
  function getKeysFunc(viewService, keys) {
12
12
  if (keys) {
13
13
  if (Array.isArray(keys)) {
14
14
  if (keys.length > 0) {
15
- if (typeof keys[0] === 'string') {
16
- return http_1.attrs(keys);
17
- }
18
- else {
19
- return keys;
15
+ if (typeof keys[0] === "string") {
16
+ return http_1.attrs(keys)
17
+ } else {
18
+ return keys
20
19
  }
21
20
  }
22
- return undefined;
23
- }
24
- else {
25
- return view_1.buildKeys(keys);
21
+ return undefined
22
+ } else {
23
+ return view_1.buildKeys(keys)
26
24
  }
27
25
  }
28
- if (typeof viewService !== 'function' && viewService.metadata) {
29
- var metadata = viewService.metadata();
26
+ if (typeof viewService !== "function" && viewService.metadata) {
27
+ var metadata = viewService.metadata()
30
28
  if (metadata) {
31
- return view_1.buildKeys(metadata);
29
+ return view_1.buildKeys(metadata)
32
30
  }
33
31
  }
34
- return undefined;
32
+ return undefined
35
33
  }
36
34
  var LoadController = (function () {
37
- function LoadController(log, viewService, keys) {
38
- this.log = log;
39
- this.load = this.load.bind(this);
40
- this.view = getViewFunc(viewService);
41
- this.keys = getKeysFunc(viewService, keys);
35
+ function LoadController(viewService, keys) {
36
+ this.load = this.load.bind(this)
37
+ this.view = getViewFunc(viewService)
38
+ this.keys = getKeysFunc(viewService, keys)
42
39
  }
43
40
  LoadController.prototype.load = function (req, res) {
44
- var _this = this;
45
- var id = view_1.buildAndCheckId(req, res, this.keys);
41
+ var id = view_1.buildAndCheckId(req, res, this.keys)
46
42
  if (id) {
47
43
  this.view(id)
48
- .then(function (obj) { return http_1.respondModel(http_1.minimize(obj), res); })
49
- .catch(function (err) { return http_1.handleError(err, res, _this.log); });
44
+ .then(function (obj) {
45
+ return http_1.respondModel(http_1.minimize(obj), res)
46
+ })
47
+ .catch(function (err) {
48
+ return http_1.handleError(err, res)
49
+ })
50
50
  }
51
- };
52
- return LoadController;
53
- }());
54
- exports.LoadController = LoadController;
51
+ }
52
+ return LoadController
53
+ })()
54
+ exports.LoadController = LoadController
55
55
  var ItemController = (function () {
56
- function ItemController(log, loadData, name, param, max, maxName) {
57
- this.log = log;
58
- this.loadData = loadData;
59
- this.param = param;
60
- this.name = (name && name.length > 0 ? name : 'keyword');
61
- this.max = (max && max > 0 ? max : 20);
62
- this.maxName = (maxName && maxName.length > 0 ? maxName : 'max');
63
- this.load = this.load.bind(this);
64
- this.query = this.query.bind(this);
56
+ function ItemController(loadData, name, param, max, maxName) {
57
+ this.loadData = loadData
58
+ this.param = param
59
+ this.name = name && name.length > 0 ? name : "keyword"
60
+ this.max = max && max > 0 ? max : 20
61
+ this.maxName = maxName && maxName.length > 0 ? maxName : "max"
62
+ this.load = this.load.bind(this)
63
+ this.query = this.query.bind(this)
65
64
  }
66
65
  ItemController.prototype.query = function (req, res) {
67
- return this.load(req, res);
68
- };
66
+ return this.load(req, res)
67
+ }
69
68
  ItemController.prototype.load = function (req, res) {
70
- var _this = this;
71
- var v = this.param ? req.params[this.name] : req.query[this.name];
69
+ var v = this.param ? req.params[this.name] : req.query[this.name]
72
70
  if (!v) {
73
- res.status(400).end("'" + this.name + "' cannot be empty");
74
- }
75
- else {
76
- var s = v.toString();
71
+ res.status(400).end("'" + this.name + "' cannot be empty")
72
+ } else {
73
+ var s = v.toString()
77
74
  if (s.length === 0) {
78
- res.status(400).end("'" + this.name + "' cannot be empty");
79
- }
80
- else {
81
- var max = http_1.queryNumber(req, this.maxName, this.max);
75
+ res.status(400).end("'" + this.name + "' cannot be empty")
76
+ } else {
77
+ var max = http_1.queryNumber(req, this.maxName, this.max)
82
78
  this.loadData(s, max)
83
- .then(function (result) { return http_1.respondModel(http_1.minimize(result), res); })
84
- .catch(function (err) { return http_1.handleError(err, res, _this.log); });
79
+ .then(function (result) {
80
+ return http_1.respondModel(http_1.minimize(result), res)
81
+ })
82
+ .catch(function (err) {
83
+ return http_1.handleError(err, res)
84
+ })
85
85
  }
86
86
  }
87
- };
88
- return ItemController;
89
- }());
90
- exports.ItemController = ItemController;
91
- exports.ItemHandler = ItemController;
87
+ }
88
+ return ItemController
89
+ })()
90
+ exports.ItemController = ItemController
91
+ exports.ItemHandler = ItemController
@@ -27,8 +27,8 @@ var http_1 = require("./http")
27
27
  var LoadController_1 = require("./LoadController")
28
28
  var resources_1 = require("./resources")
29
29
  var search_1 = require("./search")
30
- function useSearchController(log, find, viewService, array, dates, numbers, keys, config) {
31
- var c = new LoadSearchController(log, find, viewService, keys, config, dates, numbers)
30
+ function useSearchController(find, viewService, array, dates, numbers, keys, config) {
31
+ var c = new LoadSearchController(find, viewService, keys, config, dates, numbers)
32
32
  c.array = array
33
33
  return c
34
34
  }
@@ -38,8 +38,8 @@ exports.createSearchController = useSearchController
38
38
  exports.createSearchHandler = useSearchController
39
39
  var LoadSearchController = (function (_super) {
40
40
  __extends(LoadSearchController, _super)
41
- function LoadSearchController(log, find, viewService, keys, config, dates, numbers) {
42
- var _this = _super.call(this, log, viewService, keys) || this
41
+ function LoadSearchController(find, viewService, keys, config, dates, numbers) {
42
+ var _this = _super.call(this, viewService, keys) || this
43
43
  _this.find = find
44
44
  _this.search = _this.search.bind(_this)
45
45
  if (config) {
@@ -70,7 +70,7 @@ var LoadSearchController = (function (_super) {
70
70
  return search_1.jsonResult(res, result, _this.csv, l.fields, _this.config)
71
71
  })
72
72
  .catch(function (err) {
73
- return http_1.handleError(err, res, _this.log)
73
+ return http_1.handleError(err, res)
74
74
  })
75
75
  }
76
76
  return LoadSearchController
@@ -78,8 +78,8 @@ var LoadSearchController = (function (_super) {
78
78
  exports.LoadSearchController = LoadSearchController
79
79
  var QueryController = (function (_super) {
80
80
  __extends(QueryController, _super)
81
- function QueryController(log, query, config, dates, numbers, array) {
82
- var _this = _super.call(this, log, query) || this
81
+ function QueryController(query, config, dates, numbers, array) {
82
+ var _this = _super.call(this, query) || this
83
83
  _this.query = query
84
84
  _this.search = _this.search.bind(_this)
85
85
  _this.array = array
@@ -112,7 +112,7 @@ var QueryController = (function (_super) {
112
112
  return search_1.jsonResult(res, result, _this.csv, l.fields, _this.config)
113
113
  })
114
114
  .catch(function (err) {
115
- return http_1.handleError(err, res, _this.log)
115
+ return http_1.handleError(err, res)
116
116
  })
117
117
  }
118
118
  return QueryController
@@ -29,8 +29,8 @@ var resources_1 = require("./resources")
29
29
  var search_1 = require("./search")
30
30
  var LowcodeController = (function (_super) {
31
31
  __extends(LowcodeController, _super)
32
- function LowcodeController(log, lowCodeService, config, build, validate, dates, numbers) {
33
- var _this = _super.call(this, log, lowCodeService, build, validate) || this
32
+ function LowcodeController(lowCodeService, config, build, validate, dates, numbers) {
33
+ var _this = _super.call(this, lowCodeService, build, validate) || this
34
34
  _this.lowCodeService = lowCodeService
35
35
  _this.search = _this.search.bind(_this)
36
36
  _this.config = search_1.initializeConfig(config)
@@ -56,7 +56,7 @@ var LowcodeController = (function (_super) {
56
56
  return search_1.jsonResult(res, result, _this.csv, l.fields, _this.config)
57
57
  })
58
58
  .catch(function (err) {
59
- return http_1.handleError(err, res, _this.log)
59
+ return http_1.handleError(err, res)
60
60
  })
61
61
  }
62
62
  return LowcodeController
@@ -66,7 +66,7 @@ exports.LowcodeHandler = LowcodeController
66
66
  var Controller = (function (_super) {
67
67
  __extends(Controller, _super)
68
68
  function Controller(log, lowCodeService, build, validate, config, dates, numbers) {
69
- var _this = _super.call(this, log, lowCodeService, build, validate) || this
69
+ var _this = _super.call(this, lowCodeService, build, validate) || this
70
70
  _this.lowCodeService = lowCodeService
71
71
  _this.search = _this.search.bind(_this)
72
72
  _this.config = search_1.initializeConfig(config)
@@ -92,7 +92,7 @@ var Controller = (function (_super) {
92
92
  return search_1.jsonResult(res, result, _this.csv, l.fields, _this.config)
93
93
  })
94
94
  .catch(function (err) {
95
- return http_1.handleError(err, res, _this.log)
95
+ return http_1.handleError(err, res)
96
96
  })
97
97
  }
98
98
  return Controller
@@ -32,7 +32,7 @@ var SearchController = (function () {
32
32
  return search_1.jsonResult(res, result, _this.csv, l.fields, _this.config)
33
33
  })
34
34
  .catch(function (err) {
35
- return http_1.handleError(err, res, _this.log)
35
+ return http_1.handleError(err, res)
36
36
  })
37
37
  }
38
38
  return SearchController
package/lib/client.js CHANGED
@@ -1,90 +1,80 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- var http_1 = require("./http");
4
- function checkId(obj, id, keys) {
5
- var n = (keys && keys.length === 1 && keys[0].name ? keys[0].name : 'id');
6
- var o = obj;
7
- var i = id;
8
- if (!keys || keys.length === 1) {
9
- var v = o[n];
10
- if (!v) {
11
- o[n] = i;
12
- return true;
13
- }
14
- if (v != i) {
15
- return false;
16
- }
17
- return true;
18
- }
19
- var ks = Object.keys(i);
20
- for (var _i = 0, ks_1 = ks; _i < ks_1.length; _i++) {
21
- var k = ks_1[_i];
22
- var v = o[k];
23
- if (!v) {
24
- o[k] = i[k];
25
- }
26
- else {
27
- if (v != i[k]) {
28
- return false;
29
- }
30
- }
31
- o[k] = i[k];
32
- }
33
- return true;
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", { value: true })
3
+ var http = require("http")
4
+ var https = require("https")
5
+ function getHealthSecure(url, timeout) {
6
+ return new Promise(function (resolve) {
7
+ https
8
+ .get(url, { rejectUnauthorized: false }, function (res) {
9
+ var data = ""
10
+ res.on("data", function (d) {
11
+ data += d
12
+ })
13
+ res.on("end", function () {
14
+ resolve({ statusCode: res.statusCode, data: data, statusMessage: res.statusMessage })
15
+ })
16
+ })
17
+ .on("error", function (e) {
18
+ return { statusCode: 500, statusMessage: e }
19
+ })
20
+ setTimeout(function () {
21
+ return resolve({ statusCode: 408, statusMessage: "Time out" })
22
+ }, timeout)
23
+ })
34
24
  }
35
- exports.checkId = checkId;
36
- function create(res, obj, insert, log, returnNumber) {
37
- insert(obj).then(function (result) {
38
- if (typeof result === 'number') {
39
- if (result >= 1) {
40
- res.status(201).json(returnNumber ? result : obj).end();
41
- }
42
- else {
43
- res.status(409).json(result).end();
44
- }
45
- }
46
- else if (Array.isArray(result)) {
47
- res.status(422).json(result).end();
48
- }
49
- else {
50
- res.status(201).json(result).end();
51
- }
52
- }).catch(function (err) { return http_1.handleError(err, res, log); });
25
+ function getHealth(url, timeout) {
26
+ return new Promise(function (resolve) {
27
+ http
28
+ .get(url, function (res) {
29
+ var data = ""
30
+ res.on("data", function (d) {
31
+ data += d
32
+ })
33
+ res.on("end", function () {
34
+ resolve({ statusCode: res.statusCode, data: data, statusMessage: res.statusMessage })
35
+ })
36
+ })
37
+ .on("error", function (e) {
38
+ return { statusCode: 500, statusMessage: e }
39
+ })
40
+ setTimeout(function () {
41
+ return resolve({ statusCode: 408, statusMessage: "Time out" })
42
+ }, timeout)
43
+ })
53
44
  }
54
- exports.create = create;
55
- function update(res, obj, save, log, returnNumber) {
56
- save(obj).then(function (result) {
57
- if (typeof result === 'number') {
58
- if (result >= 1) {
59
- res.status(200).json(returnNumber ? result : obj).end();
60
- }
61
- else if (result === 0) {
62
- res.status(404).json(result).end();
63
- }
64
- else {
65
- res.status(409).json(result).end();
66
- }
67
- }
68
- else if (Array.isArray(result)) {
69
- res.status(422).json(result).end();
70
- }
71
- else {
72
- res.status(200).json(result).end();
45
+ var ClientChecker = (function () {
46
+ function ClientChecker(service, url, timeout) {
47
+ this.service = service
48
+ this.url = url
49
+ this.timeout = timeout ? timeout : 4200
50
+ this.check = this.check.bind(this)
51
+ this.name = this.name.bind(this)
52
+ this.build = this.build.bind(this)
53
+ }
54
+ ClientChecker.prototype.check = function () {
55
+ var obj = {}
56
+ if (this.url.startsWith("https://")) {
57
+ return getHealthSecure(this.url, this.timeout).then(function (r) {
58
+ return (obj = r)
59
+ })
60
+ } else {
61
+ return getHealth(this.url, this.timeout).then(function (r) {
62
+ return (obj = r)
63
+ })
73
64
  }
74
- }).catch(function (err) { return http_1.handleError(err, res, log); });
75
- }
76
- exports.update = update;
77
- function isTypeError(errs) {
78
- if (!errs) {
79
- return false;
80
65
  }
81
- for (var _i = 0, errs_1 = errs; _i < errs_1.length; _i++) {
82
- var err = errs_1[_i];
83
- var c = err.code;
84
- if (c === 'string' || c === 'number' || c === 'date' || c === 'boolean') {
85
- return true;
66
+ ClientChecker.prototype.name = function () {
67
+ return this.service
68
+ }
69
+ ClientChecker.prototype.build = function (data, err) {
70
+ if (err) {
71
+ if (!data) {
72
+ data = {}
73
+ }
74
+ data["error"] = err
86
75
  }
76
+ return data
87
77
  }
88
- return false;
89
- }
90
- exports.isTypeError = isTypeError;
78
+ return ClientChecker
79
+ })()
80
+ exports.ClientChecker = ClientChecker
package/lib/edit.js CHANGED
@@ -32,7 +32,7 @@ function checkId(obj, id, keys) {
32
32
  return true
33
33
  }
34
34
  exports.checkId = checkId
35
- function create(res, obj, insert, log, returnNumber) {
35
+ function create(res, obj, insert, returnNumber) {
36
36
  insert(obj)
37
37
  .then(function (result) {
38
38
  if (typeof result === "number") {
@@ -54,11 +54,11 @@ function create(res, obj, insert, log, returnNumber) {
54
54
  }
55
55
  })
56
56
  .catch(function (err) {
57
- return http_1.handleError(err, res, log)
57
+ return http_1.handleError(err, res)
58
58
  })
59
59
  }
60
60
  exports.create = create
61
- function update(res, obj, save, log, returnNumber) {
61
+ function update(res, obj, save, returnNumber) {
62
62
  save(obj)
63
63
  .then(function (result) {
64
64
  if (typeof result === "number") {
@@ -82,7 +82,7 @@ function update(res, obj, save, log, returnNumber) {
82
82
  }
83
83
  })
84
84
  .catch(function (err) {
85
- return http_1.handleError(err, res, log)
85
+ return http_1.handleError(err, res)
86
86
  })
87
87
  }
88
88
  exports.update = update