@leonid-shutov/uncommonjs 1.0.0-alpha.4 → 1.0.0-alpha.5

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/lib/rest.js CHANGED
@@ -42,6 +42,13 @@ const bodyFromDefinition = (response, definition) => {
42
42
  return definition.response ?? null;
43
43
  };
44
44
 
45
+ const statusFromDefinition = (response, definition) => {
46
+ if (typeof definition.status === 'function') {
47
+ return definition.status(response);
48
+ }
49
+ return definition.status ?? 200;
50
+ };
51
+
45
52
  const createHandler = (definition) => {
46
53
  const schema = { query: definition.query, body: definition.body };
47
54
  return async ({ path, query, body, ...rest }) => {
@@ -49,7 +56,7 @@ const createHandler = (definition) => {
49
56
  validateRequest(schema, { query, body });
50
57
  const response = await definition.handler({ path, query, body, ...rest });
51
58
  return {
52
- status: definition.status ?? 200,
59
+ status: statusFromDefinition(response, definition),
53
60
  body: bodyFromDefinition(response, definition),
54
61
  };
55
62
  } catch (error) {
package/lib/service.js CHANGED
@@ -4,15 +4,15 @@ const isService = (definition, context) =>
4
4
  definition?.method !== undefined && context.PASS !== undefined;
5
5
 
6
6
  const loadService = (definition, context) => {
7
- const { description, method, expectedErrors } = definition;
7
+ const { description, method } = definition;
8
8
  const logger = context.logger ?? context.console ?? { info: () => {} };
9
- return async (...args) => {
9
+ return (...args) => {
10
10
  const interpolatedDesc =
11
11
  typeof description === 'function' ? description(...args) : description;
12
- try {
13
- logger.info(interpolatedDesc);
14
- return await method(...args);
15
- } catch (error) {
12
+ logger.info(interpolatedDesc);
13
+
14
+ const handleError = (error) => {
15
+ const { description, expectedErrors } = definition;
16
16
  let domainError = expectedErrors?.[error.code];
17
17
  if (domainError === context.PASS) throw error;
18
18
  if (domainError === undefined) {
@@ -23,7 +23,21 @@ const loadService = (definition, context) => {
23
23
  }
24
24
  domainError.cause = error;
25
25
  throw domainError;
26
+ };
27
+
28
+ let result;
29
+
30
+ try {
31
+ result = method(...args);
32
+ } catch (error) {
33
+ handleError(error);
26
34
  }
35
+
36
+ if (typeof result?.then === 'function') {
37
+ return result.catch((error) => handleError(error));
38
+ }
39
+
40
+ return result;
27
41
  };
28
42
  };
29
43
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leonid-shutov/uncommonjs",
3
- "version": "1.0.0-alpha.4",
3
+ "version": "1.0.0-alpha.5",
4
4
  "author": "Leonid Shutov <leonid.shutov.main@gmail.com>",
5
5
  "license": "MIT",
6
6
  "homepage": "https://github.com/leonid-shutov/uncommonjs#readme",