@hvedinich/utils 0.0.65 → 0.0.67
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/package.json +1 -1
- package/src/constants/index.js +1 -1
- package/src/error/index.js +15 -0
- package/src/server/index.js +6 -3
- package/src/utils/contextUtils.js +7 -10
package/package.json
CHANGED
package/src/constants/index.js
CHANGED
package/src/error/index.js
CHANGED
|
@@ -59,6 +59,20 @@ class TokenExpiredError extends ServerError {
|
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
61
|
|
|
62
|
+
class MissingControllerError extends ServerError {
|
|
63
|
+
constructor({ name, slug, ctx }) {
|
|
64
|
+
super(
|
|
65
|
+
ctx,
|
|
66
|
+
'MissingControllerError',
|
|
67
|
+
404,
|
|
68
|
+
{},
|
|
69
|
+
`Method ${name} not found in ${slug}Controller`,
|
|
70
|
+
`Method ${name} not found in ${slug}Controller`,
|
|
71
|
+
'MISSING_INTEGRATION_CONTROLLER',
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
62
76
|
module.exports = {
|
|
63
77
|
InternalServerError,
|
|
64
78
|
Unauthorized,
|
|
@@ -67,4 +81,5 @@ module.exports = {
|
|
|
67
81
|
PaymentError,
|
|
68
82
|
TokenExpiredError,
|
|
69
83
|
Forbidden,
|
|
84
|
+
MissingControllerError,
|
|
70
85
|
};
|
package/src/server/index.js
CHANGED
|
@@ -6,7 +6,6 @@ const {
|
|
|
6
6
|
guestCtx,
|
|
7
7
|
token: { EXPIRED, UNAUTHORIZED },
|
|
8
8
|
} = require('../constants');
|
|
9
|
-
const { addContext } = require('../utils/contextUtils');
|
|
10
9
|
|
|
11
10
|
const logError = (err, logger = console) => {
|
|
12
11
|
const description = {};
|
|
@@ -48,11 +47,15 @@ const defaultContext = async ({ req }) => {
|
|
|
48
47
|
};
|
|
49
48
|
|
|
50
49
|
class Server {
|
|
51
|
-
constructor({ config, healthCheck }) {
|
|
50
|
+
constructor({ config, healthCheck, context }) {
|
|
52
51
|
this.config = config;
|
|
53
52
|
this.app = express();
|
|
54
53
|
this.router = null;
|
|
55
54
|
this.app.get('/_status', healthCheck || defaultCHealthCheck);
|
|
55
|
+
this.app.use((req, res, next) => {
|
|
56
|
+
req.ctx = context({ req, res });
|
|
57
|
+
next();
|
|
58
|
+
});
|
|
56
59
|
}
|
|
57
60
|
|
|
58
61
|
initGQL({ gateway, schema, context, options, logger, errorHandler }) {
|
|
@@ -110,7 +113,7 @@ class Server {
|
|
|
110
113
|
if (proxy) {
|
|
111
114
|
this.app.use(path, proxy);
|
|
112
115
|
} else {
|
|
113
|
-
this.app[method](path,
|
|
116
|
+
this.app[method](path, handler);
|
|
114
117
|
}
|
|
115
118
|
});
|
|
116
119
|
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
|
|
2
1
|
const constKeys = {
|
|
3
2
|
'x-yoda-uid': 'uid',
|
|
4
3
|
'x-yoda-city': 'city',
|
|
5
4
|
};
|
|
6
5
|
const defaultContext = ({ req }) => {
|
|
7
6
|
const res = {};
|
|
8
|
-
Object.keys(constKeys).forEach(
|
|
7
|
+
Object.keys(constKeys).forEach(key => {
|
|
9
8
|
if (req.headers[key]) {
|
|
10
9
|
res[constKeys[key]] = req.headers[key];
|
|
11
10
|
}
|
|
@@ -16,14 +15,12 @@ const defaultContext = ({ req }) => {
|
|
|
16
15
|
return res;
|
|
17
16
|
};
|
|
18
17
|
|
|
19
|
-
const ctxToHeaders =
|
|
20
|
-
const result = Object
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
return acc;
|
|
26
|
-
}, {});
|
|
18
|
+
const ctxToHeaders = ctx => {
|
|
19
|
+
const result = Object.entries(constKeys).reduce((acc, [key, value]) => {
|
|
20
|
+
if (ctx[value] === undefined) return acc;
|
|
21
|
+
acc[key] = ctx[value];
|
|
22
|
+
return acc;
|
|
23
|
+
}, {});
|
|
27
24
|
if (ctx.permission) {
|
|
28
25
|
result['x-yoda-permission'] = ctx.permission.join(',');
|
|
29
26
|
}
|