@clipboard-health/execution-context
Advanced tools
Comparing version 0.6.3 to 0.7.0
{ | ||
"name": "@clipboard-health/execution-context", | ||
"description": "A lightweight Node.js utility for managing execution contexts and metadata aggregation using AsyncLocalStorage.", | ||
"version": "0.6.3", | ||
"version": "0.7.0", | ||
"bugs": "https://github.com/ClipboardHealth/core-utils/issues", | ||
@@ -6,0 +6,0 @@ "dependencies": { |
@@ -15,2 +15,14 @@ import "../types/global"; | ||
export declare function addMetadataToLocalContext(metadata: Record<string, unknown>): void; | ||
/** | ||
* A utility function that will add metadata to the list under current context's key | ||
* @param key - the list's key (string) in the context | ||
* @param metadata - the metadata (key-value pair), to be added to the context | ||
*/ | ||
export declare function addToMetadataList(key: string, metadata: Record<string, unknown>): void; | ||
/** | ||
* A utility function that will add metadata to the record under current context's key, | ||
* supporting nested records. | ||
* @param key - the record's key (string) in the context | ||
* @param metadata - the metadata (key-value pair), to be added to the context | ||
*/ | ||
export declare function addToMetadataRecord(key: string, metadata: Record<string, unknown>): void; |
@@ -8,4 +8,6 @@ "use strict"; | ||
exports.addToMetadataList = addToMetadataList; | ||
exports.addToMetadataRecord = addToMetadataRecord; | ||
require("../types/global"); | ||
const node_async_hooks_1 = require("node:async_hooks"); | ||
const util_1 = require("./util"); | ||
globalThis.threadLocalStorage ||= new node_async_hooks_1.AsyncLocalStorage(); | ||
@@ -60,2 +62,7 @@ function getAsyncLocalStorage() { | ||
} | ||
/** | ||
* A utility function that will add metadata to the list under current context's key | ||
* @param key - the list's key (string) in the context | ||
* @param metadata - the metadata (key-value pair), to be added to the context | ||
*/ | ||
function addToMetadataList(key, metadata) { | ||
@@ -65,2 +72,22 @@ const metadataList = [...getMetadataListByKey(key), metadata]; | ||
} | ||
function getMetadataRecordByKey(key) { | ||
const context = getExecutionContext(); | ||
if (context?.metadata && key in context.metadata) { | ||
const metadataForKey = context.metadata[key]; | ||
if ((0, util_1.isRecord)(metadataForKey)) { | ||
return metadataForKey; | ||
} | ||
} | ||
return {}; | ||
} | ||
/** | ||
* A utility function that will add metadata to the record under current context's key, | ||
* supporting nested records. | ||
* @param key - the record's key (string) in the context | ||
* @param metadata - the metadata (key-value pair), to be added to the context | ||
*/ | ||
function addToMetadataRecord(key, metadata) { | ||
const metadataRecord = { ...getMetadataRecordByKey(key), ...metadata }; | ||
addMetadataToLocalContext({ [key]: metadataRecord }); | ||
} | ||
//# sourceMappingURL=contextStore.js.map |
Sorry, the diff of this file is not supported yet
12986
17
152