core-services-sdk 1.3.56 → 1.3.59

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,55 +1,124 @@
1
- export namespace Context {
1
+ /**
2
+ * Represents the data stored in the async context for a single execution flow.
3
+ *
4
+ * This object is propagated automatically across async boundaries
5
+ * using Node.js AsyncLocalStorage.
6
+ *
7
+ * It defines the contract between producers (who set values)
8
+ * and consumers (who read values) of the context.
9
+ *
10
+ * @typedef {Object} ContextStore
11
+ *
12
+ * @property {string} [correlationId] - Unique identifier for request or operation tracing.
13
+ * @property {string} [ip] - Client IP address.
14
+ * @property {string} [userAgent] - Client user agent string.
15
+ * @property {string} [tenantId] - Active tenant identifier.
16
+ * @property {string} [userId] - Authenticated user identifier.
17
+ */
18
+ /**
19
+ * Async execution context manager built on top of Node.js AsyncLocalStorage.
20
+ *
21
+ * This class provides a thin, static API for storing and accessing
22
+ * request-scoped (or async-chain-scoped) metadata such as correlation IDs,
23
+ * user information, tenant identifiers, and similar data.
24
+ *
25
+ * The context is bound to the current async execution chain using
26
+ * AsyncLocalStorage and is automatically propagated across `await` boundaries.
27
+ *
28
+ * This class is intentionally static and acts as a singleton wrapper
29
+ * around AsyncLocalStorage.
30
+ */
31
+ export class Context {
2
32
  /**
3
- * Run a callback within a given context store.
4
- * Everything `await`ed or invoked inside this callback will have access
5
- * to the provided store via {@link Context.get}, {@link Context.set}, or {@link Context.all}.
33
+ * Run a callback within a given async context store.
34
+ *
35
+ * All asynchronous operations spawned inside the callback
36
+ * will have access to the provided store via {@link Context.get},
37
+ * {@link Context.set}, or {@link Context.all}.
6
38
  *
7
39
  * @template T
8
- * @param {Record<string, any>} store
40
+ * @param {ContextStore} store - Initial context store for this execution.
9
41
  * @param {() => T} callback - Function to execute inside the context.
10
42
  * @returns {T} The return value of the callback (sync or async).
11
43
  *
12
44
  * @example
13
45
  * Context.run(
14
- * { correlationId: 'abc123' },
46
+ * { correlationId: 'abc123', userId: 'usr_1' },
15
47
  * async () => {
16
48
  * console.log(Context.get('correlationId')) // "abc123"
17
49
  * }
18
50
  * )
19
51
  */
20
- function run<T>(store: Record<string, any>, callback: () => T): T
52
+ static run<T>(store: ContextStore, callback: () => T): T
21
53
  /**
22
54
  * Retrieve a single value from the current async context store.
23
55
  *
24
- * @template T
25
- * @param {string} key - The key of the value to retrieve.
26
- * @returns {T|undefined} The stored value, or `undefined` if no store exists or key not found.
56
+ * If called outside of an active {@link Context.run},
57
+ * this method returns `undefined`.
58
+ *
59
+ * @template {keyof ContextStore} K
60
+ * @param {K} key - Context property name.
61
+ * @returns {ContextStore[K] | undefined} The stored value, if present.
27
62
  *
28
63
  * @example
29
- * const userId = Context.get('userId')
64
+ * const tenantId = Context.get('tenantId')
30
65
  */
31
- function get<T>(key: string): T | undefined
66
+ static get<K extends keyof ContextStore>(key: K): ContextStore[K] | undefined
32
67
  /**
33
- * Set a single key-value pair in the current async context store.
34
- * If there is no active store (i.e. outside of a {@link Context.run}),
35
- * this function does nothing.
68
+ * Set a single key-value pair on the current async context store.
69
+ *
70
+ * If called outside of an active {@link Context.run},
71
+ * this method is a no-op.
36
72
  *
37
- * @param {string} key - The key under which to store the value.
38
- * @param {any} value - The value to store.
73
+ * @template {keyof ContextStore} K
74
+ * @param {K} key - Context property name.
75
+ * @param {ContextStore[K]} value - Value to store.
39
76
  *
40
77
  * @example
41
78
  * Context.set('tenantId', 'tnt_1234')
42
79
  */
43
- function set(key: string, value: any): void
80
+ static set<K extends keyof ContextStore>(key: K, value: ContextStore[K]): void
44
81
  /**
45
- * Get the entire store object for the current async context.
82
+ * Get the full context store for the current async execution.
46
83
  *
47
- * @returns {Record<string, any>} The current store object,
48
- * or an empty object if no store exists.
84
+ * If no context is active, an empty object is returned.
85
+ *
86
+ * @returns {ContextStore}
49
87
  *
50
88
  * @example
51
- * const all = Context.all()
52
- * console.log(all) // { correlationId: 'abc123', userId: 'usr_789' }
89
+ * const ctx = Context.all()
90
+ * console.log(ctx.correlationId)
91
+ */
92
+ static all(): ContextStore
93
+ }
94
+ /**
95
+ * Represents the data stored in the async context for a single execution flow.
96
+ *
97
+ * This object is propagated automatically across async boundaries
98
+ * using Node.js AsyncLocalStorage.
99
+ *
100
+ * It defines the contract between producers (who set values)
101
+ * and consumers (who read values) of the context.
102
+ */
103
+ export type ContextStore = {
104
+ /**
105
+ * - Unique identifier for request or operation tracing.
106
+ */
107
+ correlationId?: string
108
+ /**
109
+ * - Client IP address.
110
+ */
111
+ ip?: string
112
+ /**
113
+ * - Client user agent string.
114
+ */
115
+ userAgent?: string
116
+ /**
117
+ * - Active tenant identifier.
118
+ */
119
+ tenantId?: string
120
+ /**
121
+ * - Authenticated user identifier.
53
122
  */
54
- function all(): Record<string, any>
123
+ userId?: string
55
124
  }