nestjs-cls
Advanced tools
Comparing version 4.2.0 to 4.2.1
@@ -7,2 +7,3 @@ export * from './lib/cls-service-manager'; | ||
export * from './lib/cls-initializers/use-cls.decorator'; | ||
export * from './lib/cls-initializers/utils/context-cls-store-map'; | ||
export * from './lib/cls.module'; | ||
@@ -9,0 +10,0 @@ export * from './lib/cls.service'; |
@@ -23,2 +23,3 @@ "use strict"; | ||
__exportStar(require("./lib/cls-initializers/use-cls.decorator"), exports); | ||
__exportStar(require("./lib/cls-initializers/utils/context-cls-store-map"), exports); | ||
__exportStar(require("./lib/cls.module"), exports); | ||
@@ -25,0 +26,0 @@ __exportStar(require("./lib/cls.service"), exports); |
@@ -20,2 +20,3 @@ "use strict"; | ||
const cls_options_1 = require("../cls.options"); | ||
const context_cls_store_map_1 = require("./utils/context-cls-store-map"); | ||
let ClsGuard = class ClsGuard { | ||
@@ -28,3 +29,10 @@ constructor(options) { | ||
const cls = cls_service_manager_1.ClsServiceManager.getClsService(); | ||
cls.enter({ ifNested: 'reuse' }); | ||
const existingStore = context_cls_store_map_1.ContextClsStoreMap.get(context); | ||
if (existingStore) { | ||
cls.enter({ ifNested: 'reuse' }); | ||
} | ||
else { | ||
cls.enterWith({}); | ||
context_cls_store_map_1.ContextClsStoreMap.set(context, cls.get()); | ||
} | ||
if (this.options.generateId) { | ||
@@ -31,0 +39,0 @@ const id = await ((_b = (_a = this.options).idGenerator) === null || _b === void 0 ? void 0 : _b.call(_a, context)); |
import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { ClsInterceptorOptions } from '../cls.options'; | ||
import { ClsService } from '../cls.service'; | ||
export declare class ClsInterceptor implements NestInterceptor { | ||
@@ -9,3 +8,2 @@ private readonly options; | ||
intercept(context: ExecutionContext, next: CallHandler): Observable<any>; | ||
createOrReuseStore(context: ExecutionContext, cls: ClsService): import("../cls.options").ClsStore; | ||
} |
@@ -21,2 +21,3 @@ "use strict"; | ||
const cls_options_1 = require("../cls.options"); | ||
const context_cls_store_map_1 = require("./utils/context-cls-store-map"); | ||
let ClsInterceptor = class ClsInterceptor { | ||
@@ -27,4 +28,6 @@ constructor(options) { | ||
intercept(context, next) { | ||
var _a; | ||
const cls = cls_service_manager_1.ClsServiceManager.getClsService(); | ||
const clsStore = this.createOrReuseStore(context, cls); | ||
const clsStore = (_a = context_cls_store_map_1.ContextClsStoreMap.get(context)) !== null && _a !== void 0 ? _a : {}; | ||
context_cls_store_map_1.ContextClsStoreMap.set(context, clsStore); | ||
return new rxjs_1.Observable((subscriber) => { | ||
@@ -61,20 +64,2 @@ cls.runWith(clsStore, async () => { | ||
} | ||
createOrReuseStore(context, cls) { | ||
let store = (cls.isActive() && cls.get()) || {}; | ||
// NestJS triggers the interceptor for all queries within the same | ||
// call individually, so each query would be wrapped in a different | ||
// CLS context. | ||
// The solution is to store the CLS store in the GQL context and re-use | ||
// it each time the interceptor is triggered within the same request. | ||
if (context.getType() == 'graphql') { | ||
const gqlContext = context.getArgByIndex(2); | ||
if (!gqlContext.__CLS_STORE__) { | ||
gqlContext.__CLS_STORE__ = store; | ||
} | ||
else { | ||
store = gqlContext.__CLS_STORE__; | ||
} | ||
} | ||
return store; | ||
} | ||
}; | ||
@@ -81,0 +66,0 @@ ClsInterceptor = __decorate([ |
import { NestMiddleware } from '@nestjs/common'; | ||
import { ClsMiddlewareOptions } from '../cls.options'; | ||
export declare class ClsMiddleware implements NestMiddleware { | ||
private readonly cls; | ||
private readonly options; | ||
@@ -6,0 +5,0 @@ constructor(options?: Omit<ClsMiddlewareOptions, 'mount'>); |
@@ -20,2 +20,3 @@ "use strict"; | ||
const cls_options_1 = require("../cls.options"); | ||
const context_cls_store_map_1 = require("./utils/context-cls-store-map"); | ||
let ClsMiddleware = class ClsMiddleware { | ||
@@ -29,2 +30,3 @@ constructor(options) { | ||
this.options.useEnterWith && cls.enter(); | ||
context_cls_store_map_1.ContextClsStoreMap.setByRaw(req, cls.get()); | ||
if (this.options.generateId) { | ||
@@ -31,0 +33,0 @@ const id = await ((_b = (_a = this.options).idGenerator) === null || _b === void 0 ? void 0 : _b.call(_a, req)); |
@@ -14,3 +14,2 @@ "use strict"; | ||
const original = descriptor.value; | ||
// console.log('original is', original?.toString()); | ||
if (typeof original !== 'function') { | ||
@@ -17,0 +16,0 @@ throw new Error(`The @UseCls decorator can be only used on functions, but ${propertyKey.toString()} is not a function.`); |
@@ -16,3 +16,4 @@ "use strict"; | ||
}, obj !== null && obj !== void 0 ? obj : {}); | ||
leaf[pathSegments.pop()] = value; | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
leaf[pathSegments.at(-1)] = value; | ||
return obj; | ||
@@ -19,0 +20,0 @@ } |
{ | ||
"name": "nestjs-cls", | ||
"version": "4.2.0", | ||
"version": "4.2.1", | ||
"description": "A continuation-local storage module compatible with NestJS's dependency injection.", | ||
@@ -5,0 +5,0 @@ "author": "papooch", |
86777
59
1899