bootpress 7.1.2 → 8.0.0

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
@@ -219,6 +219,10 @@ There must be only one element in an array schema which defines ````ArrayOf<Sche
219
219
  ### **asStrict(any, string | object | array)**
220
220
  Same as 'as' method but doesn't try to parse different types instead throws error.
221
221
 
222
+ ## v8.0.0 Release Notes:
223
+ - Added support for async service functions. (You don't need to await if you wrapped your service with Bootpress functions)
224
+ - Bugfix for falsy response values
225
+ - Simplified implementation
222
226
 
223
227
  ## v7.1.0 Release Notes:
224
228
  - getOrThrow: Throws specified error when value is an empty array too.
package/index.js CHANGED
@@ -28,48 +28,57 @@ function RestService(service) {
28
28
  ...Object.getOwnPropertyDescriptors(service),
29
29
  ...Object.getOwnPropertyDescriptors(service.__proto__ || {})
30
30
  };
31
- const alteredDescriptors = Object.fromEntries(Object.entries(descriptors).filter(keyvalue => !protectedProperties.includes(keyvalue[0])).map(keyvalue => {
31
+ const newService = {};
32
+ Object.entries(descriptors).filter(keyvalue => !protectedProperties.includes(keyvalue[0])).forEach(keyvalue => {
32
33
  const propertyName = keyvalue[0];
33
34
  const value = keyvalue[1].value;
34
35
  if (typeof value == "function" && !propertyName.startsWith("#")) {
35
- return [
36
- propertyName,
37
- {
38
- value: ((...args) =>
39
- (req, res) => {
40
- try {
41
- const result = value.bind(service)(...args);
42
- if (result === undefined) {
43
- throw new HttpError(200, "Your method is executed but it returned undefined. Please avoid using 'void' methods as service methods.");
44
- } else if (result === null) {
45
- throw new HttpError(200, "Your method is executed but it returned null. At least a value is expected to be returned.");
46
- }
47
- reply(res, result.status || 200, result.data || result)
48
- } catch (e) {
49
- reply(res, e.status || 500, e.message || e);
50
- }
51
- }),
52
- configurable: keyvalue[1].configurable,
53
- writable: keyvalue[1].writable,
54
- enumerable: false
55
- }
56
- ]
36
+ newService[propertyName] = ((...args) =>
37
+ (req, res) => {
38
+ try {
39
+ const result = service[propertyName](...args);
40
+ if (result == undefined) {
41
+ reply(res, 204, null);
42
+ }
43
+ else if (result instanceof Promise) {
44
+ result.then(r => {
45
+ reply(res, r.status ?? 200, r.data ?? r);
46
+ }).catch(e => {
47
+ reply(res, e.status ?? 500, e.message ?? e);
48
+ })
49
+ }
50
+ else {
51
+ reply(res, result.status ?? 200, result.data ?? result)
52
+ }
53
+ } catch (e) {
54
+ reply(res, e.status ?? 500, e.message ?? e);
55
+ }
56
+ });
57
57
  } else {
58
- return keyvalue;
58
+ newService[propertyName] = service[propertyName];
59
59
  }
60
- }));
61
- Object.defineProperties(service, alteredDescriptors);
62
- return service;
60
+ })
61
+ return newService;
63
62
  }
64
63
 
65
64
  function RestMethod(callback) {
66
65
  return (req, res) => {
67
66
  try {
68
67
  const result = callback();
69
- reply(res, result.status || 200, result.data || result);
70
- return result;
68
+ if (result == undefined) {
69
+ reply(res, 204, null);
70
+ }
71
+ else if (result instanceof Promise) {
72
+ result.then(r => {
73
+ reply(res, r.status ?? 200, r.data ?? r);
74
+ }).catch(e => {
75
+ reply(res, e.status ?? 500, e.message ?? e);
76
+ })
77
+ } else {
78
+ reply(res, result.status ?? 200, result.data ?? result);
79
+ }
71
80
  } catch (e) {
72
- reply(res, e.status || 500, e.message || e)
81
+ reply(res, e.status ?? 500, e.message ?? e)
73
82
  }
74
83
  }
75
84
  }
@@ -82,10 +91,20 @@ function Restify(target, key, desc) {
82
91
  return (req, res) => {
83
92
  try {
84
93
  const result = oldFunc(...args);
85
- reply(res, result.status || 200, result.data || result);
86
- return result;
94
+ if (result == undefined) {
95
+ reply(res, 204, null);
96
+ }
97
+ else if (result instanceof Promise) {
98
+ result.then(r => {
99
+ reply(res, r.status ?? 200, r.data ?? r);
100
+ }).catch(e => {
101
+ reply(res, e.status ?? 500, e.message ?? e);
102
+ })
103
+ } else {
104
+ reply(res, result.status ?? 200, result.data ?? result);
105
+ }
87
106
  } catch (e) {
88
- reply(res, e.status || 500, e.message || e);
107
+ reply(res, e.status ?? 500, e.message ?? e);
89
108
  }
90
109
  }
91
110
  }).bind(target)
@@ -204,25 +223,25 @@ function PassBodyAs(type) {
204
223
  }
205
224
 
206
225
  function PassBody(actualHandler) {
207
- return (...args) => {
208
- if (isRequstHandlerArgs(args)) {
209
- const req = args.at(-3); const res = args.at(-2);
226
+ return (...args) => {
227
+ if (isRequstHandlerArgs(args)) {
228
+ const req = args.at(-3); const res = args.at(-2);
229
+ try {
230
+ return actualHandler(req.body)(req, res);
231
+ } catch (e) {
232
+ reply(res, e.status || 500, e.message || e);
233
+ }
234
+
235
+ } else {
236
+ return (req, res) => {
210
237
  try {
211
- return actualHandler(req.body)(req, res);
238
+ return actualHandler(...args, req.body)(req, res);
212
239
  } catch (e) {
213
- reply(res, e.status || 500, e.message || e);
214
- }
215
-
216
- } else {
217
- return (req, res) => {
218
- try {
219
- return actualHandler(...args, req.body)(req, res);
220
- } catch (e) {
221
- reply(res, e.status || 500, e.message || e)
222
- }
240
+ reply(res, e.status || 500, e.message || e)
223
241
  }
224
242
  }
225
243
  }
244
+ }
226
245
  }
227
246
 
228
247
  function PassRequest(actualHandler) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bootpress",
3
- "version": "7.1.2",
3
+ "version": "8.0.0",
4
4
  "description": "REST service methods for express",
5
5
  "main": "index.js",
6
6
  "repository": {
File without changes