@skillswaveca/nova-shared-libraries 4.17.1 → 4.18.1

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.
@@ -1,31 +1,31 @@
1
1
  export const packageInfo = [
2
2
  {
3
3
  "name": "@skillswaveca/nova-utils",
4
- "version": "4.17.0",
4
+ "version": "4.18.0",
5
5
  "description": "A collection of random utils used in nova repos",
6
6
  "docsPath": "./nova-utils/index.html"
7
7
  },
8
8
  {
9
9
  "name": "@skillswaveca/nova-router",
10
- "version": "4.17.0",
10
+ "version": "4.18.0",
11
11
  "description": "An extended Koa router that enables better validation",
12
12
  "docsPath": "./nova-router/index.html"
13
13
  },
14
14
  {
15
15
  "name": "@skillswaveca/nova-model",
16
- "version": "4.17.0",
16
+ "version": "4.18.0",
17
17
  "description": "Nova model stuff",
18
18
  "docsPath": "./nova-model/index.html"
19
19
  },
20
20
  {
21
21
  "name": "@skillswaveca/nova-middleware",
22
- "version": "4.17.0",
22
+ "version": "4.18.0",
23
23
  "description": "A collection of middleware used by nova projects",
24
24
  "docsPath": "./nova-middleware/index.html"
25
25
  },
26
26
  {
27
27
  "name": "@skillswaveca/nova-drivers",
28
- "version": "4.17.0",
28
+ "version": "4.18.0",
29
29
  "description": "Some helper drivers for AWS services",
30
30
  "docsPath": "./drivers/index.html"
31
31
  }
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "name": "@skillswaveca/nova-shared-libraries",
4
4
  "description": "A monorepo of shared libraries for Nova projects.",
5
5
  "repository": "https://github.com/SkillsWave/nova-shared-libraries",
6
- "version": "4.17.1",
6
+ "version": "4.18.1",
7
7
  "main": "index.js",
8
8
  "license": "MIT",
9
9
  "keywords": [],
@@ -3,7 +3,7 @@
3
3
  "name": "@skillswaveca/nova-drivers",
4
4
  "description": "Some helper drivers for AWS services",
5
5
  "repository": "https://github.com/SkillsWave/nova-shared-libraries",
6
- "version": "4.17.1",
6
+ "version": "4.18.1",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "pre-release": "pnpm run create-index",
@@ -3,7 +3,7 @@
3
3
  "name": "@skillswaveca/nova-middleware",
4
4
  "description": "A collection of middleware used by nova projects",
5
5
  "repository": "https://github.com/SkillsWave/nova-shared-libraries",
6
- "version": "4.17.1",
6
+ "version": "4.18.1",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "pre-release": "pnpm run create-index",
@@ -3,7 +3,7 @@
3
3
  "name": "@skillswaveca/nova-model",
4
4
  "description": "Nova model stuff",
5
5
  "repository": "https://github.com/SkillsWave/nova-shared-libraries",
6
- "version": "4.17.1",
6
+ "version": "4.18.1",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "pre-release": "pnpm run create-index",
@@ -26,12 +26,12 @@ export class NovaStreamTaskRouter {
26
26
  handle(newRecord, oldRecord) {
27
27
  const { recordType, operation } = this.getRecordStatus(oldRecord, newRecord);
28
28
  if (!recordType) {
29
- log.warn({ recordType, operation }, 'No record type found');
29
+ log.trace({ recordType, operation }, 'No record type found');
30
30
  return null;
31
31
  }
32
32
  const handler = this._recordHandlers[recordType]?.[operation];
33
33
  if (!handler) {
34
- log.warn({ recordType, operation }, 'No handler found for record');
34
+ log.trace({ recordType, operation }, 'No handler found for record');
35
35
  return null;
36
36
  }
37
37
  return handler(newRecord, oldRecord);
@@ -3,7 +3,7 @@
3
3
  "name": "@skillswaveca/nova-router",
4
4
  "description": "An extended Koa router that enables better validation",
5
5
  "repository": "https://github.com/SkillsWave/nova-shared-libraries",
6
- "version": "4.17.1",
6
+ "version": "4.18.1",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "pre-release": "pnpm run create-index",
@@ -3,7 +3,7 @@
3
3
  "name": "@skillswaveca/nova-utils",
4
4
  "description": "A collection of random utils used in nova repos",
5
5
  "repository": "https://github.com/SkillsWave/nova-shared-libraries",
6
- "version": "4.17.1",
6
+ "version": "4.18.1",
7
7
  "main": "index.js",
8
8
  "scripts": {
9
9
  "pre-release": "pnpm run create-index",
@@ -2,13 +2,28 @@ import { AsyncLocalStorage } from 'node:async_hooks';
2
2
  import pino from 'pino';
3
3
  import { v4 as uuidv4 } from 'uuid';
4
4
 
5
+ /**
6
+ * AsyncLocalStorage instance for maintaining request-specific context.
7
+ * @type {AsyncLocalStorage<Map<string, any>>}
8
+ */
5
9
  export const asyncContext = new AsyncLocalStorage();
6
- // Create a base logger instance
10
+
11
+ /**
12
+ * Base logger instance created using pino.
13
+ * The log level is 'info' in production and 'debug' otherwise.
14
+ * @type {pino.Logger}
15
+ */
7
16
  const baseLogger = pino({
8
- level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
17
+ level: 'info',
9
18
  });
10
19
 
11
- // addToContext function: adds a key/value pair to the current request's logger context.
20
+ /**
21
+ * Adds a key/value pair to the current request's logger context.
22
+ * This function creates a child logger with the new field and updates the async context.
23
+ *
24
+ * @param {string} key - The key to add to the context.
25
+ * @param {*} value - The value to associate with the key.
26
+ */
12
27
  export const addToContext = (key, value) => {
13
28
  const store = asyncContext.getStore();
14
29
  if (store) {
@@ -18,9 +33,32 @@ export const addToContext = (key, value) => {
18
33
  const updatedLogger = currentLogger.child({ [key]: value });
19
34
  // Update the store so that all further logging in this async context will include the new field.
20
35
  store.set('logger', updatedLogger);
36
+ store.set(key, value);
37
+ }
38
+ };
39
+
40
+ /**
41
+ * Retrieves a value from the current async context by key.
42
+ *
43
+ * @param {string} key - The key whose value should be retrieved.
44
+ * @returns {*} The value associated with the key, or undefined if not found.
45
+ */
46
+ export const getFromContext = key => {
47
+ const store = asyncContext.getStore();
48
+ if (store) {
49
+ return store.get(key);
21
50
  }
22
51
  };
23
52
 
53
+ /**
54
+ * Middleware to set up a new async context for each request.
55
+ * It generates a unique request ID, creates a child logger for that request,
56
+ * and stores both in the async context.
57
+ *
58
+ * @param {Object} ctx - The context object (e.g., from a web framework).
59
+ * @param {Function} next - The next middleware function.
60
+ * @returns {Promise<void>}
61
+ */
24
62
  export const contextMiddleware = async(ctx, next) => {
25
63
  const requestId = uuidv4();
26
64
  const child = baseLogger.child({ requestId });
@@ -31,7 +69,12 @@ export const contextMiddleware = async(ctx, next) => {
31
69
  });
32
70
  };
33
71
 
34
- // Wrap the base logger with a Proxy that uses the child logger from AsyncLocalStorage if available.
72
+ /**
73
+ * A Proxy-wrapped logger that uses the child logger from the current async context if available.
74
+ * This allows you to log with context-specific information without manually passing the child logger.
75
+ *
76
+ * @type {pino.Logger}
77
+ */
35
78
  export const log = new Proxy(baseLogger, {
36
79
  get(target, property, receiver) {
37
80
  const store = asyncContext.getStore();
@@ -42,6 +85,11 @@ export const log = new Proxy(baseLogger, {
42
85
  },
43
86
  });
44
87
 
88
+ /**
89
+ * Alias for the proxied logger.
90
+ *
91
+ * @type {pino.Logger}
92
+ */
45
93
  export const logger = log;
46
94
 
47
95
  export default log;