core-express 0.1.0 → 0.1.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-service
@@ -1,154 +1,247 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- extendStatics(d, b);
11
- function __() { this.constructor = d; }
12
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
- };
14
- })();
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- var edit_1 = require("./edit");
17
- var LoadController_1 = require("./LoadController");
18
- var resources_1 = require("./resources");
19
- var response_1 = require("./response");
20
- var view_1 = require("./view");
1
+ "use strict"
2
+ var __extends =
3
+ (this && this.__extends) ||
4
+ (function () {
5
+ var extendStatics = function (d, b) {
6
+ extendStatics =
7
+ Object.setPrototypeOf ||
8
+ ({ __proto__: [] } instanceof Array &&
9
+ function (d, b) {
10
+ d.__proto__ = b
11
+ }) ||
12
+ function (d, b) {
13
+ for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]
14
+ }
15
+ return extendStatics(d, b)
16
+ }
17
+ return function (d, b) {
18
+ extendStatics(d, b)
19
+ function __() {
20
+ this.constructor = d
21
+ }
22
+ d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __())
23
+ }
24
+ })()
25
+ Object.defineProperty(exports, "__esModule", { value: true })
26
+ var edit_1 = require("./edit")
27
+ var http_1 = require("./http")
28
+ var LoadController_1 = require("./LoadController")
29
+ var resources_1 = require("./resources")
30
+ var view_1 = require("./view")
21
31
  var GenericController = (function (_super) {
22
- __extends(GenericController, _super);
23
- function GenericController(log, service, status, validate) {
24
- var _this = _super.call(this, log, service) || this;
25
- _this.service = service;
26
- _this.validate = validate;
27
- _this.status = edit_1.initializeStatus(status);
32
+ __extends(GenericController, _super)
33
+ function GenericController(service, build, validate, returnNumber) {
34
+ var _this = _super.call(this, service) || this
35
+ _this.service = service
36
+ _this.build = build
37
+ _this.validate = validate
38
+ _this.returnNumber = returnNumber
28
39
  if (service.metadata) {
29
- var m = service.metadata();
40
+ var m = service.metadata()
30
41
  if (m) {
31
- _this.metadata = m.attributes;
42
+ _this.metadata = m
32
43
  }
33
44
  }
34
- _this.insert = _this.insert.bind(_this);
35
- _this.update = _this.update.bind(_this);
36
- _this.patch = _this.patch.bind(_this);
37
- _this.delete = _this.delete.bind(_this);
45
+ _this.create = _this.create.bind(_this)
46
+ _this.update = _this.update.bind(_this)
47
+ _this.patch = _this.patch.bind(_this)
48
+ _this.delete = _this.delete.bind(_this)
38
49
  if (!validate && resources_1.resources.createValidator && _this.metadata) {
39
- var v = resources_1.resources.createValidator(_this.metadata);
40
- _this.validate = v.validate;
50
+ var v = resources_1.resources.createValidator(_this.metadata)
51
+ _this.validate = v.validate
41
52
  }
42
- return _this;
53
+ return _this
54
+ }
55
+ GenericController.prototype.create = function (req, res) {
56
+ validateAndCreate(req, res, this.service.create, this.validate, this.build)
43
57
  }
44
- GenericController.prototype.insert = function (req, res) {
45
- var _this = this;
46
- var obj = req.body;
47
- if (!obj) {
48
- return res.status(400).end('The request body cannot be empty.');
49
- }
50
- if (this.validate) {
51
- var l_1 = this.log;
52
- this.validate(obj).then(function (errors) {
53
- if (errors && errors.length > 0) {
54
- var r = { status: _this.status.validation_error, errors: errors };
55
- res.status(getStatusCode(errors)).json(r);
56
- }
57
- else {
58
- edit_1.create(res, _this.status, obj, _this.service.insert, _this.log);
59
- }
60
- }).catch(function (err) { return response_1.handleError(err, res, l_1); });
61
- }
62
- else {
63
- edit_1.create(res, this.status, obj, this.service.insert, this.log);
64
- }
65
- };
66
58
  GenericController.prototype.update = function (req, res) {
67
- var _this = this;
68
- var obj = req.body;
69
- if (!obj) {
70
- return res.status(400).end('The request body cannot be empty.');
71
- }
72
- var id = view_1.buildId(req, this.keys);
73
- if (!id) {
74
- return res.status(400).end('Invalid parameters');
75
- }
76
- var ok = edit_1.checkId(obj, id, this.keys);
77
- if (!ok) {
78
- return res.status(400).end('Invalid parameters');
59
+ var id = buildAndCheckIdWithBody(req, res, this.keys, this.service.update)
60
+ if (id) {
61
+ validateAndUpdate(res, req.body, false, this.service.update, this.validate, undefined, this.build)
79
62
  }
80
- if (this.validate) {
81
- var l_2 = this.log;
82
- this.validate(obj).then(function (errors) {
83
- if (errors && errors.length > 0) {
84
- var r = { status: _this.status.validation_error, errors: errors };
85
- res.status(getStatusCode(errors)).json(r);
86
- }
87
- else {
88
- edit_1.update(res, _this.status, obj, _this.service.update, _this.log);
89
- }
90
- }).catch(function (err) { return response_1.handleError(err, res, l_2); });
91
- }
92
- else {
93
- edit_1.update(res, this.status, obj, this.service.update, this.log);
94
- }
95
- };
63
+ }
96
64
  GenericController.prototype.patch = function (req, res) {
97
- var _this = this;
98
- var obj = req.body;
99
- if (!obj) {
100
- return res.status(400).end('The request body cannot be empty.');
65
+ var id = buildAndCheckIdWithBody(req, res, this.keys, this.service.patch)
66
+ if (id && this.service.patch) {
67
+ validateAndUpdate(res, req.body, true, this.service.patch, this.validate, undefined, this.build)
101
68
  }
102
- var id = view_1.buildId(req, this.keys);
103
- if (!id) {
104
- return res.status(400).end('Invalid parameters');
69
+ }
70
+ GenericController.prototype.delete = function (req, res) {
71
+ var id = view_1.buildAndCheckId(req, res, this.keys)
72
+ if (id) {
73
+ if (!this.service.delete) {
74
+ res.status(405).end("Method Not Allowed")
75
+ } else {
76
+ this.service
77
+ .delete(id)
78
+ .then(function (count) {
79
+ res.status(getDeleteStatus(count)).json(count).end()
80
+ })
81
+ .catch(function (err) {
82
+ return http_1.handleError(err, res)
83
+ })
84
+ }
105
85
  }
106
- var ok = edit_1.checkId(obj, id, this.keys);
107
- if (!ok) {
108
- return res.status(400).end('Invalid parameters');
86
+ }
87
+ return GenericController
88
+ })(LoadController_1.LoadController)
89
+ exports.GenericController = GenericController
90
+ function validateAndCreate(req, res, save, validate, build, returnNumber) {
91
+ var obj = req.body
92
+ if (!obj || obj === "") {
93
+ res.status(400).end("The request body cannot be empty.")
94
+ } else {
95
+ if (validate) {
96
+ validate(obj)
97
+ .then(function (errors) {
98
+ if (errors && errors.length > 0) {
99
+ res.status(edit_1.getStatusCode(errors)).json(errors).end()
100
+ } else {
101
+ if (build) {
102
+ build(res, obj, true)
103
+ }
104
+ edit_1.create(res, obj, save, returnNumber)
105
+ }
106
+ })
107
+ .catch(function (err) {
108
+ return http_1.handleError(err, res)
109
+ })
110
+ } else {
111
+ edit_1.create(res, obj, save, returnNumber)
109
112
  }
110
- if (this.validate) {
111
- var l_3 = this.log;
112
- this.validate(obj, true).then(function (errors) {
113
+ }
114
+ }
115
+ exports.validateAndCreate = validateAndCreate
116
+ function validateAndUpdate(res, obj, isPatch, save, validate, resource, build, returnNumber) {
117
+ if (validate) {
118
+ validate(obj, resource, isPatch)
119
+ .then(function (errors) {
113
120
  if (errors && errors.length > 0) {
114
- var r = { status: _this.status.validation_error, errors: errors };
115
- res.status(getStatusCode(errors)).json(r);
121
+ res.status(edit_1.getStatusCode(errors)).json(errors).end()
122
+ } else {
123
+ if (build) {
124
+ build(res, obj, false, isPatch)
125
+ }
126
+ edit_1.update(res, obj, save, returnNumber)
116
127
  }
117
- else {
118
- edit_1.update(res, _this.status, obj, _this.service.patch, _this.log);
128
+ })
129
+ .catch(function (err) {
130
+ return http_1.handleError(err, res)
131
+ })
132
+ } else {
133
+ edit_1.update(res, obj, save, returnNumber)
134
+ }
135
+ }
136
+ exports.validateAndUpdate = validateAndUpdate
137
+ function buildAndCheckIdWithBody(req, res, keys, patch) {
138
+ var obj = req.body
139
+ if (!obj || obj === "") {
140
+ res.status(400).end("The request body cannot be empty.")
141
+ return undefined
142
+ }
143
+ if (!patch) {
144
+ res.status(405).end("Method Not Allowed")
145
+ return undefined
146
+ }
147
+ var id = view_1.buildId(req, keys)
148
+ if (!id) {
149
+ res.status(400).end("Invalid parameters")
150
+ return undefined
151
+ }
152
+ var ok = edit_1.checkId(obj, id, keys)
153
+ if (!ok) {
154
+ res.status(400).end("body and url are not matched")
155
+ return undefined
156
+ }
157
+ return id
158
+ }
159
+ exports.buildAndCheckIdWithBody = buildAndCheckIdWithBody
160
+ function getDeleteStatus(count) {
161
+ if (count > 0) {
162
+ return 200
163
+ } else if (count === 0) {
164
+ return 404
165
+ } else {
166
+ return 409
167
+ }
168
+ }
169
+ exports.getDeleteStatus = getDeleteStatus
170
+ function useBuild(c, generate) {
171
+ var b = new Builder(
172
+ generate,
173
+ c.id ? c.id : "",
174
+ c.payload ? c.payload : "",
175
+ c.user ? c.user : "",
176
+ c.updatedBy ? c.updatedBy : "",
177
+ c.updatedAt ? c.updatedAt : "",
178
+ c.createdBy ? c.createdBy : "",
179
+ c.createdAt ? c.createdAt : "",
180
+ c.version ? c.version : "",
181
+ )
182
+ return b.build
183
+ }
184
+ exports.useBuild = useBuild
185
+ var Builder = (function () {
186
+ function Builder(generate, id, payload, user, updatedBy, updatedAt, createdBy, createdAt, version) {
187
+ this.generate = generate
188
+ this.id = id
189
+ this.payload = payload
190
+ this.user = user
191
+ this.updatedBy = updatedBy
192
+ this.updatedAt = updatedAt
193
+ this.createdBy = createdBy
194
+ this.createdAt = createdAt
195
+ this.version = version
196
+ this.build = this.build.bind(this)
197
+ }
198
+ Builder.prototype.build = function (res, obj, isCreate, isPatch) {
199
+ var o = obj
200
+ var usr = ""
201
+ if (this.user.length > 0) {
202
+ if (this.payload.length > 0) {
203
+ var payload = res.locals[this.payload]
204
+ if (payload) {
205
+ usr = payload[this.user]
119
206
  }
120
- }).catch(function (err) { return response_1.handleError(err, res, l_3); });
121
- }
122
- else {
123
- edit_1.update(res, this.status, obj, this.service.update, this.log);
207
+ } else {
208
+ usr = res.locals[this.user]
209
+ }
124
210
  }
125
- };
126
- GenericController.prototype.delete = function (req, res) {
127
- var _this = this;
128
- var id = view_1.buildId(req, this.keys);
129
- if (!id) {
130
- return res.status(400).end('invalid parameters');
211
+ if (!usr) {
212
+ usr = ""
131
213
  }
132
- this.service.delete(id).then(function (count) {
133
- if (count > 0) {
134
- res.status(200).json(count);
214
+ var now = new Date()
215
+ if (isCreate) {
216
+ if (this.generate && this.id.length > 0) {
217
+ o[this.id] = this.generate()
135
218
  }
136
- else if (count === 0) {
137
- res.status(404).json(count);
219
+ if (usr.length > 0) {
220
+ if (this.createdAt.length > 0) {
221
+ o[this.createdAt] = now
222
+ }
223
+ if (this.createdBy.length > 0) {
224
+ o[this.createdBy] = usr
225
+ }
138
226
  }
139
- else {
140
- res.status(409).json(count);
227
+ if (this.version.length > 0) {
228
+ o[this.version] = 1
141
229
  }
142
- }).catch(function (err) {
143
- if (_this.log) {
144
- _this.log(err);
230
+ } else if (isPatch) {
231
+ var keys = Object.keys(o)
232
+ if (keys.length === 0) {
233
+ return
145
234
  }
146
- res.status(500).end('Internal Server Error');
147
- });
148
- };
149
- return GenericController;
150
- }(LoadController_1.LoadController));
151
- exports.GenericController = GenericController;
152
- function getStatusCode(errs) {
153
- return (edit_1.isTypeError(errs) ? 400 : 422);
154
- }
235
+ }
236
+ if (usr.length > 0) {
237
+ if (this.updatedAt.length > 0) {
238
+ o[this.updatedAt] = now
239
+ }
240
+ if (this.updatedBy.length > 0) {
241
+ o[this.updatedBy] = usr
242
+ }
243
+ }
244
+ }
245
+ return Builder
246
+ })()
247
+ exports.Builder = Builder
@@ -1,42 +1,63 @@
1
- "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- extendStatics(d, b);
11
- function __() { this.constructor = d; }
12
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
- };
14
- })();
15
- Object.defineProperty(exports, "__esModule", { value: true });
16
- var GenericController_1 = require("./GenericController");
17
- var response_1 = require("./response");
18
- var search_1 = require("./search");
1
+ "use strict"
2
+ var __extends =
3
+ (this && this.__extends) ||
4
+ (function () {
5
+ var extendStatics = function (d, b) {
6
+ extendStatics =
7
+ Object.setPrototypeOf ||
8
+ ({ __proto__: [] } instanceof Array &&
9
+ function (d, b) {
10
+ d.__proto__ = b
11
+ }) ||
12
+ function (d, b) {
13
+ for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]
14
+ }
15
+ return extendStatics(d, b)
16
+ }
17
+ return function (d, b) {
18
+ extendStatics(d, b)
19
+ function __() {
20
+ this.constructor = d
21
+ }
22
+ d.prototype = b === null ? Object.create(b) : ((__.prototype = b.prototype), new __())
23
+ }
24
+ })()
25
+ Object.defineProperty(exports, "__esModule", { value: true })
26
+ var GenericController_1 = require("./GenericController")
27
+ var http_1 = require("./http")
28
+ var resources_1 = require("./resources")
29
+ var search_1 = require("./search")
19
30
  var GenericSearchController = (function (_super) {
20
- __extends(GenericSearchController, _super);
21
- function GenericSearchController(log, find, service, config, validate, format) {
22
- var _this = _super.call(this, log, service, config, validate) || this;
23
- _this.find = find;
24
- _this.format = format;
25
- _this.search = _this.search.bind(_this);
26
- _this.config = search_1.initializeConfig(config);
31
+ __extends(GenericSearchController, _super)
32
+ function GenericSearchController(find, service, config, build, validate, dates, numbers) {
33
+ var _this = _super.call(this, service, build, validate) || this
34
+ _this.find = find
35
+ _this.search = _this.search.bind(_this)
36
+ _this.config = search_1.initializeConfig(config)
27
37
  if (_this.config) {
28
- _this.csv = _this.config.csv;
38
+ _this.csv = _this.config.csv
39
+ _this.excluding = _this.config.excluding
29
40
  }
30
- return _this;
41
+ var m = search_1.getMetadataFunc(service, dates, numbers)
42
+ if (m) {
43
+ _this.dates = m.dates
44
+ _this.numbers = m.numbers
45
+ }
46
+ return _this
31
47
  }
32
48
  GenericSearchController.prototype.search = function (req, res) {
33
- var _this = this;
34
- var s = search_1.fromRequest(req, this.format);
35
- var l = search_1.getLimit(s);
36
- this.find(s, l.limit, l.skip, l.refId)
37
- .then(function (result) { return search_1.jsonResult(res, result, _this.csv, s.fields, _this.config); })
38
- .catch(function (err) { return response_1.handleError(err, res, _this.log); });
39
- };
40
- return GenericSearchController;
41
- }(GenericController_1.GenericController));
42
- exports.GenericSearchController = GenericSearchController;
49
+ var _this = this
50
+ var s = search_1.fromRequest(req, search_1.buildArray(this.array, resources_1.resources.fields, this.excluding))
51
+ var l = search_1.getParameters(s)
52
+ var s2 = search_1.format(s, this.dates, this.numbers)
53
+ this.find(s2, l.limit, l.pageOrNextPageToken, l.fields)
54
+ .then(function (result) {
55
+ return search_1.jsonResult(res, result, _this.csv, l.fields, _this.config)
56
+ })
57
+ .catch(function (err) {
58
+ return http_1.handleError(err, res)
59
+ })
60
+ }
61
+ return GenericSearchController
62
+ })(GenericController_1.GenericController)
63
+ exports.GenericSearchController = GenericSearchController
@@ -1,62 +1,20 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- var health_1 = require("./health");
1
+ "use strict"
2
+ Object.defineProperty(exports, "__esModule", { value: true })
3
+ var health_1 = require("./health")
40
4
  var HealthController = (function () {
41
5
  function HealthController(checkers) {
42
- this.checkers = checkers;
43
- this.check = this.check.bind(this);
6
+ this.checkers = checkers
7
+ this.check = this.check.bind(this)
44
8
  }
45
9
  HealthController.prototype.check = function (req, res) {
46
- return __awaiter(this, void 0, void 0, function () {
47
- return __generator(this, function (_a) {
48
- health_1.check(this.checkers).then(function (heath) {
49
- if (heath.status === 'UP') {
50
- return res.status(200).json(heath);
51
- }
52
- else {
53
- return res.status(500).json(heath);
54
- }
55
- });
56
- return [2];
57
- });
58
- });
59
- };
60
- return HealthController;
61
- }());
62
- exports.HealthController = HealthController;
10
+ health_1.health(this.checkers).then(function (r) {
11
+ if (r.status === "UP") {
12
+ res.status(200).json(r).end()
13
+ } else {
14
+ res.status(500).json(r).end()
15
+ }
16
+ })
17
+ }
18
+ return HealthController
19
+ })()
20
+ exports.HealthController = HealthController