@hvedinich/utils 0.0.64 → 0.0.66
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/server/index.js +6 -3
- package/src/utils/contextUtils.js +7 -10
- package/src/utils/token/index.js +8 -10
package/package.json
CHANGED
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
|
}
|
package/src/utils/token/index.js
CHANGED
|
@@ -1,17 +1,15 @@
|
|
|
1
|
-
|
|
2
1
|
const jwt = require('jsonwebtoken');
|
|
3
2
|
const { ValidationError, TokenExpiredError } = require('../../error');
|
|
4
3
|
const { serverCtx, token: { REFRESH, ACCESS } } = require('../../constants');
|
|
5
4
|
|
|
6
|
-
const createToken = (
|
|
7
|
-
const accessToken = jwt.sign({
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}, token.refreshSecret, { expiresIn: token.refreshTokenTimeout });
|
|
5
|
+
const createToken = (data, options = {}) => {
|
|
6
|
+
const accessToken = jwt.sign(data, options.accessSecret, { expiresIn: options.accessTokenTimeout });
|
|
7
|
+
|
|
8
|
+
if (!options.refreshSecret) {
|
|
9
|
+
return { accessToken };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
const refreshToken = jwt.sign(data, options.refreshSecret, { expiresIn: options.refreshTokenTimeout });
|
|
15
13
|
return { accessToken, refreshToken };
|
|
16
14
|
};
|
|
17
15
|
|