@devmoods/express-extras 0.19.0 → 0.20.0

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/README.md CHANGED
@@ -77,7 +77,7 @@ app.get(
77
77
  route(async () => {
78
78
  const posts = await postgres.query('SELECT * FROM posts LIMIT 10');
79
79
  return posts;
80
- })
80
+ }),
81
81
  );
82
82
 
83
83
  app.post(
@@ -100,13 +100,13 @@ app.post(
100
100
  const post = { title, content };
101
101
 
102
102
  await postgres.insert(
103
- sql`INSERT INTO posts (title, content) VALUES (${title}, ${content})`
103
+ sql`INSERT INTO posts (title, content) VALUES (${title}, ${content})`,
104
104
  );
105
105
 
106
106
  slack.postMessage(`New post created with title ${title}`);
107
107
 
108
108
  return [201, post];
109
- })
109
+ }),
110
110
  );
111
111
 
112
112
  app.use((req, res, next) => {
@@ -118,7 +118,7 @@ app.use(
118
118
  shouldHandleError(error) {
119
119
  return true;
120
120
  },
121
- })
121
+ }),
122
122
  );
123
123
 
124
124
  app.use(
@@ -130,7 +130,7 @@ app.use(
130
130
  logger.error(error);
131
131
  }
132
132
  },
133
- })
133
+ }),
134
134
  );
135
135
 
136
136
  listen(server, {
package/dist/auth.js CHANGED
@@ -118,6 +118,7 @@ export function createAuth({ getUserByToken, methods: { emailAndPassword }, filt
118
118
  }));
119
119
  router.get('/token/refresh', middleware(false), route(async (req, res) => {
120
120
  const user = useCurrentUser();
121
+ logger.info('refreshed token', { user });
121
122
  if (!user) {
122
123
  throw Unauthorized();
123
124
  }
package/dist/context.d.ts CHANGED
@@ -10,16 +10,8 @@ export declare function withRequestContext({ getRequestId, }?: RequestContextOpt
10
10
  * Return a value from the request context
11
11
  */
12
12
  export declare function useContext<T>(key: string): T | undefined;
13
- /**
14
- * @deprecated
15
- */
16
- export declare const getContext: typeof useContext;
17
13
  /**
18
14
  * Set a value on the request context
19
15
  */
20
- export declare function setContext<T>(key: string, value: T): any;
16
+ export declare function setContext<T>(key: string, value: T): Map<string, any> | undefined;
21
17
  export declare function useRequestId(): string | undefined;
22
- /**
23
- * @deprecated
24
- */
25
- export declare const getRequestId: typeof useRequestId;
package/dist/context.js CHANGED
@@ -1,14 +1,18 @@
1
- import { createNamespace } from 'cls-hooked';
1
+ import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import { v4 as uuid } from 'uuid';
3
- const requestContext = createNamespace('@requestContext');
3
+ const requestContext = new AsyncLocalStorage();
4
+ const REQUEST_ID = 'requestId';
4
5
  /**
5
6
  * Middleware for setting a `requestId` on the request context.
6
7
  */
7
8
  export function withRequestContext({ getRequestId = (req) => req.headers['X-Request-ID'], } = {}) {
8
9
  return (req, res, next) => {
9
10
  const requestId = getRequestId(req) || uuid();
10
- requestContext.run(() => {
11
- requestContext.set('requestId', requestId);
11
+ requestContext.run(new Map(), () => {
12
+ const store = requestContext.getStore();
13
+ if (store) {
14
+ store.set(REQUEST_ID, requestId);
15
+ }
12
16
  next();
13
17
  });
14
18
  };
@@ -17,26 +21,20 @@ export function withRequestContext({ getRequestId = (req) => req.headers['X-Requ
17
21
  * Return a value from the request context
18
22
  */
19
23
  export function useContext(key) {
20
- if (requestContext && requestContext.active) {
21
- return requestContext.get(key);
24
+ const store = requestContext.getStore();
25
+ if (store) {
26
+ return store.get(key);
22
27
  }
23
28
  }
24
- /**
25
- * @deprecated
26
- */
27
- export const getContext = useContext;
28
29
  /**
29
30
  * Set a value on the request context
30
31
  */
31
32
  export function setContext(key, value) {
32
- if (requestContext && requestContext.active) {
33
- return requestContext.set(key, value);
33
+ const store = requestContext.getStore();
34
+ if (store) {
35
+ return store.set(key, value);
34
36
  }
35
37
  }
36
38
  export function useRequestId() {
37
- return useContext('requestId');
39
+ return useContext(REQUEST_ID);
38
40
  }
39
- /**
40
- * @deprecated
41
- */
42
- export const getRequestId = useRequestId;
package/dist/errors.js CHANGED
@@ -44,8 +44,7 @@ function convertToAPIError(error) {
44
44
  return error;
45
45
  }
46
46
  function _errorHandler({ onError }) {
47
- return (err, req, res, next // eslint-disable-line
48
- ) => {
47
+ return (err, req, res, next) => {
49
48
  if (!err) {
50
49
  err = makeError(500);
51
50
  }
package/dist/listen.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createTerminus } from '@godaddy/terminus';
2
2
  import { getLogger } from './logger.js';
3
- const logger = getLogger();
3
+ const logger = getLogger('core');
4
4
  const READINESS_PERIOD_MS = parseInt(process.env.DM_READINESS_PERIOD_MS || '', 10) || 0;
5
5
  const FORCE_EXIT_TIMEOUT = parseInt(process.env.DM_FORCE_EXIT_TIMEOUT || '', 10) || 1000;
6
6
  /**
package/dist/logger.js CHANGED
@@ -27,41 +27,40 @@ export function createLogger({ name, getContext, silent = () => process.env.NODE
27
27
  if (silent()) {
28
28
  logger.level(bunyan.FATAL + 1);
29
29
  }
30
- function getFinalContext() {
30
+ function getFinalContext(context) {
31
31
  return {
32
32
  ...defaultGetContext(),
33
33
  ...getContext?.(),
34
+ ...context,
34
35
  };
35
36
  }
36
37
  return {
37
38
  debug(message, context = {}) {
38
- return logger.child(getFinalContext(), true).debug(context, message);
39
+ return logger.debug(getFinalContext(context), message);
39
40
  },
40
41
  info(message, context = {}) {
41
- return logger.child(getFinalContext(), true).info(context, message);
42
+ return logger.info(getFinalContext(context), message);
42
43
  },
43
44
  warn(message, context = {}) {
44
- return logger.child(getFinalContext(), true).warn(context, message);
45
+ return logger.warn(getFinalContext(context), message);
45
46
  },
46
47
  error(error, message) {
47
- const context = getFinalContext();
48
+ const context = getFinalContext({ err: error });
48
49
  if (_sentryClient != null) {
49
50
  _sentryClient.captureException(error, {
50
51
  tags: context,
51
52
  });
52
53
  }
53
- return logger
54
- .child(context, true)
55
- .error(error, message || error?.message);
54
+ return logger.error(context, message || error?.message);
56
55
  },
57
56
  fatal(error, message) {
58
- const context = getFinalContext();
57
+ const context = getFinalContext({ err: error });
59
58
  if (_sentryClient != null) {
60
59
  _sentryClient.captureException(error, {
61
60
  tags: context,
62
61
  });
63
62
  }
64
- return logger.child(context, true).fatal(error, message);
63
+ return logger.fatal(context, message);
65
64
  },
66
65
  };
67
66
  }
@@ -48,9 +48,9 @@ export function before({ cookies = false, helmet: useHelmet = true, json: useJso
48
48
  function logRequestDuration() {
49
49
  const getDurationInMilliseconds = (start) => {
50
50
  const NS_PER_SEC = 1e9;
51
- const NS_TO_MS = 1e6;
51
+ const NS_TO_S = 1e9;
52
52
  const diff = process.hrtime(start);
53
- return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_MS;
53
+ return (diff[0] * NS_PER_SEC + diff[1]) / NS_TO_S;
54
54
  };
55
55
  return (req, res, next) => {
56
56
  const start = process.hrtime();
@@ -58,6 +58,7 @@ function logRequestDuration() {
58
58
  method: req.method,
59
59
  url: req.originalUrl,
60
60
  path: req.path,
61
+ ip: req.ip,
61
62
  });
62
63
  res.on('finish', () => {
63
64
  const duration = getDurationInMilliseconds(start);
@@ -66,6 +67,7 @@ function logRequestDuration() {
66
67
  url: req.originalUrl,
67
68
  path: req.path,
68
69
  duration,
70
+ status: res.statusCode,
69
71
  });
70
72
  });
71
73
  next();
package/dist/slack.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { createFetch } from '@devmoods/fetch';
2
2
  import { getLogger } from './logger.js';
3
- const logger = getLogger();
3
+ const logger = getLogger('slack');
4
4
  /**
5
5
  * Send messages to Slack using Incoming Webhooks
6
6
  *
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@devmoods/express-extras",
3
- "version": "0.19.0",
3
+ "version": "0.20.0",
4
4
  "description": "Kubernetes-ready utilities for rapid Node.js application development",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -52,7 +52,6 @@
52
52
  "@types/bcrypt": "^5.0.0",
53
53
  "@types/body-parser": "^1.19.2",
54
54
  "@types/bunyan": "^1.8.8",
55
- "@types/cls-hooked": "^4.3.4",
56
55
  "@types/cookie-parser": "^1.4.3",
57
56
  "@types/express": "^4.17.17",
58
57
  "@types/jsonwebtoken": "^9.0.2",
@@ -63,7 +62,6 @@
63
62
  "bcrypt": "^5.1.0",
64
63
  "body-parser": "^1.20.2",
65
64
  "bunyan": "^1.8.15",
66
- "cls-hooked": "^4.2.2",
67
65
  "compose-middleware": "^5.0.1",
68
66
  "cookie-parser": "^1.4.6",
69
67
  "dotenv": "^16.3.1",