@bootloader/context 1.0.23 → 1.1.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/index.js +18 -6
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -15,8 +15,8 @@ class Context {
|
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
/** Starts a new context for the request */
|
|
18
|
-
run(fn) {
|
|
19
|
-
if (this.asyncLocalStorage.getStore()) {
|
|
18
|
+
run(fn,{ forceNew = false } = {}) {
|
|
19
|
+
if (this.asyncLocalStorage.getStore() && !forceNew) {
|
|
20
20
|
return fn(); // Continue using the existing context
|
|
21
21
|
}
|
|
22
22
|
let mp = new Map();
|
|
@@ -35,7 +35,13 @@ class Context {
|
|
|
35
35
|
return { tenant, traceId };
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
-
|
|
38
|
+
/**
|
|
39
|
+
* This is for HTTP Request.
|
|
40
|
+
*
|
|
41
|
+
* @param {...any} args
|
|
42
|
+
* @returns
|
|
43
|
+
*/
|
|
44
|
+
initHttpRequest(...args) {
|
|
39
45
|
let fun = args.find(arg => typeof arg == 'function') || (() => {});
|
|
40
46
|
return (req, res, next) => {
|
|
41
47
|
this.run(async () => {
|
|
@@ -45,12 +51,17 @@ class Context {
|
|
|
45
51
|
let traceId = requestContext.headerOrParam('x-trace-id');
|
|
46
52
|
await fun(this.useDefaults({ tenant, traceId }));
|
|
47
53
|
next();
|
|
48
|
-
});
|
|
54
|
+
},{forceNew: true});
|
|
49
55
|
};
|
|
50
56
|
}
|
|
51
|
-
|
|
57
|
+
/**
|
|
58
|
+
* For http request
|
|
59
|
+
*
|
|
60
|
+
* @param {*} callback
|
|
61
|
+
* @returns
|
|
62
|
+
*/
|
|
52
63
|
withRequest(callback) {
|
|
53
|
-
return this.
|
|
64
|
+
return this.initHttpRequest(callback);
|
|
54
65
|
}
|
|
55
66
|
|
|
56
67
|
init(...args) {
|
|
@@ -65,6 +76,7 @@ class Context {
|
|
|
65
76
|
set(key, value) {
|
|
66
77
|
const store = this.asyncLocalStorage.getStore();
|
|
67
78
|
if (store) store.set(key, value);
|
|
79
|
+
else console.log(`NOTFOUND. !!!!!!!!!! Context.set: Setting key "${key}" to value "${value}" in context store.`);
|
|
68
80
|
}
|
|
69
81
|
|
|
70
82
|
/** Get a value from the current request context */
|