@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hvedinich/utils",
3
- "version": "0.0.64",
3
+ "version": "0.0.66",
4
4
  "description": "utils module",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -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, addContext(handler));
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((key) => {
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 = (ctx) => {
20
- const result = Object
21
- .entries(constKeys)
22
- .reduce((acc, [key, value]) => {
23
- if (ctx[value] === undefined) return acc;
24
- acc[key] = ctx[value];
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
  }
@@ -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 = (user, token = {}) => {
7
- const accessToken = jwt.sign({
8
- uid: user.id,
9
- name: user.username,
10
- permission: user.permission,
11
- }, token.accessSecret, { expiresIn: token.accessTokenTimeout });
12
- const refreshToken = jwt.sign({
13
- uid: user.id,
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