@leonid-shutov/uncommonjs 1.0.0-alpha.3 → 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/loader.js +1 -1
- package/lib/rest.js +8 -1
- package/lib/service.js +20 -6
- package/package.json +1 -1
package/lib/loader.js
CHANGED
|
@@ -26,7 +26,7 @@ const loadFile = async (context, container, filePath, key) => {
|
|
|
26
26
|
};
|
|
27
27
|
|
|
28
28
|
const loadDir = async (context, container, dirPath, key) => {
|
|
29
|
-
key ??= dirPath.split('/').at(-1);
|
|
29
|
+
key ??= removeIndex(dirPath.split('/').at(-1));
|
|
30
30
|
const nextContainer = container[key] ?? {};
|
|
31
31
|
container[key] = nextContainer;
|
|
32
32
|
const files = [];
|
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
|
|
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
|
|
7
|
+
const { description, method } = definition;
|
|
8
8
|
const logger = context.logger ?? context.console ?? { info: () => {} };
|
|
9
|
-
return
|
|
9
|
+
return (...args) => {
|
|
10
10
|
const interpolatedDesc =
|
|
11
11
|
typeof description === 'function' ? description(...args) : description;
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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