nestjs-cls
Advanced tools
Comparing version 2.1.0 to 2.1.1
@@ -0,0 +0,0 @@ export * from './lib/cls-service-manager'; |
@@ -0,0 +0,0 @@ "use strict"; |
import { ClassProvider, ValueProvider } from '@nestjs/common'; | ||
import { ClsService } from './cls.service'; | ||
export declare const getClsServiceToken: (namespace: string) => string; | ||
export declare function getClsServiceToken(): string; | ||
export declare function getClsServiceToken(namespace: string): string; | ||
export declare class ClsServiceManager { | ||
@@ -5,0 +6,0 @@ private static namespaces; |
@@ -8,3 +8,5 @@ "use strict"; | ||
const async_hooks_1 = require("async_hooks"); | ||
const getClsServiceToken = (namespace) => `ClsService-${namespace}`; | ||
function getClsServiceToken(namespace = cls_constants_1.CLS_DEFAULT_NAMESPACE) { | ||
return `ClsService-${namespace}`; | ||
} | ||
exports.getClsServiceToken = getClsServiceToken; | ||
@@ -21,7 +23,7 @@ class ClsServiceManager { | ||
const service = new cls_service_1.ClsService(this.resolveNamespace(name)); | ||
this.clsServices.set((0, exports.getClsServiceToken)(name), new cls_service_1.ClsService(this.resolveNamespace(name))); | ||
this.clsServices.set(getClsServiceToken(name), new cls_service_1.ClsService(this.resolveNamespace(name))); | ||
return service; | ||
} | ||
static getClsService(name) { | ||
const cls = this.clsServices.get(name ? (0, exports.getClsServiceToken)(name) : cls_service_1.ClsService); | ||
const cls = this.clsServices.get(name ? getClsServiceToken(name) : cls_service_1.ClsService); | ||
if (!cls) | ||
@@ -28,0 +30,0 @@ throw new Error(`ClsService with namespace ${name} does not exist`); |
@@ -0,0 +0,0 @@ export declare const CLS_REQ: unique symbol; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -1,1 +0,2 @@ | ||
export declare const InjectCls: (namespace: string) => (target: object, key: string | symbol, index?: number) => void; | ||
export declare function InjectCls(): (target: any, key: string | symbol, index?: number) => void; | ||
export declare function InjectCls(namespace: string): (target: any, key: string | symbol, index?: number) => void; |
@@ -6,4 +6,7 @@ "use strict"; | ||
const cls_service_manager_1 = require("./cls-service-manager"); | ||
const InjectCls = (namespace) => (0, common_1.Inject)((0, cls_service_manager_1.getClsServiceToken)(namespace)); | ||
const cls_constants_1 = require("./cls.constants"); | ||
function InjectCls(namespace = cls_constants_1.CLS_DEFAULT_NAMESPACE) { | ||
return (0, common_1.Inject)((0, cls_service_manager_1.getClsServiceToken)(namespace)); | ||
} | ||
exports.InjectCls = InjectCls; | ||
//# sourceMappingURL=cls.decorators.js.map |
@@ -0,0 +0,0 @@ import { CanActivate, ExecutionContext } from '@nestjs/common'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { ExecutionContext, ModuleMetadata } from '@nestjs/common'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -0,0 +0,0 @@ import { NestMiddleware } from '@nestjs/common'; |
@@ -0,0 +0,0 @@ "use strict"; |
@@ -10,3 +10,4 @@ import { DynamicModule, MiddlewareConsumer, NestModule } from '@nestjs/common'; | ||
configure(consumer: MiddlewareConsumer): void; | ||
static forFeature(namespaceName?: string): DynamicModule; | ||
static forFeature(): DynamicModule; | ||
static forFeature(namespaceName: string): DynamicModule; | ||
private static clsMiddlewareOptionsFactory; | ||
@@ -13,0 +14,0 @@ private static clsGuardOptionsFactory; |
@@ -16,3 +16,2 @@ "use strict"; | ||
const core_1 = require("@nestjs/core"); | ||
const __1 = require(".."); | ||
const cls_service_manager_1 = require("./cls-service-manager"); | ||
@@ -93,3 +92,3 @@ const cls_constants_1 = require("./cls.constants"); | ||
provide: cls_constants_1.CLS_MIDDLEWARE_OPTIONS, | ||
inject: [__1.CLS_MODULE_OPTIONS], | ||
inject: [cls_constants_1.CLS_MODULE_OPTIONS], | ||
useFactory: this.clsMiddlewareOptionsFactory, | ||
@@ -99,3 +98,3 @@ }, | ||
provide: cls_constants_1.CLS_GUARD_OPTIONS, | ||
inject: [__1.CLS_MODULE_OPTIONS], | ||
inject: [cls_constants_1.CLS_MODULE_OPTIONS], | ||
useFactory: this.clsGuardOptionsFactory, | ||
@@ -105,3 +104,3 @@ }, | ||
provide: cls_constants_1.CLS_INTERCEPTOR_OPTIONS, | ||
inject: [__1.CLS_MODULE_OPTIONS], | ||
inject: [cls_constants_1.CLS_MODULE_OPTIONS], | ||
useFactory: this.clsInterceptorOptionsFactory, | ||
@@ -134,3 +133,3 @@ }, | ||
{ | ||
provide: __1.CLS_MODULE_OPTIONS, | ||
provide: cls_constants_1.CLS_MODULE_OPTIONS, | ||
useValue: options, | ||
@@ -151,3 +150,3 @@ }, | ||
{ | ||
provide: __1.CLS_MODULE_OPTIONS, | ||
provide: cls_constants_1.CLS_MODULE_OPTIONS, | ||
inject: asyncOptions.inject, | ||
@@ -154,0 +153,0 @@ useFactory: asyncOptions.useFactory, |
@@ -0,0 +0,0 @@ /// <reference types="node" /> |
@@ -0,0 +0,0 @@ "use strict"; |
export {}; |
@@ -124,2 +124,5 @@ "use strict"; | ||
typedService.set('b.d.e', false); | ||
typedService.set('b.d.f', ['x']); | ||
typedService.set('b.g', new Map()); | ||
typedService.set('b.h', { i: 'i', j: 1 }); | ||
const { a, b } = typedService.get(); | ||
@@ -132,2 +135,7 @@ a; | ||
typedService.get('b.d.e'); | ||
typedService.get('b.d.f')[0]; | ||
typedService.get('b.g').get('x'); | ||
const { i, j } = typedService.get('b.h'); | ||
i; | ||
j; | ||
}); | ||
@@ -134,0 +142,0 @@ }); |
@@ -1,3 +0,8 @@ | ||
declare type Terminal = string | number | bigint | boolean | null | undefined | Date | RegExp | ((...args: any) => any); | ||
export declare type RecursiveKeyOf<T, Prefix extends string = never> = T extends Terminal ? never : { | ||
declare type TerminalType = string | number | bigint | boolean | null | undefined | any[] | Map<any, any> | Set<any> | Date | RegExp | BrandedTerminal | ((...args: any) => any); | ||
declare const TERMINAL_BRAND: unique symbol; | ||
declare class BrandedTerminal { | ||
private [TERMINAL_BRAND]?; | ||
} | ||
export declare type Terminal<T> = T & BrandedTerminal; | ||
export declare type RecursiveKeyOf<T, Prefix extends string = never> = T extends TerminalType ? never : { | ||
[K in keyof T & string]: [Prefix] extends [never] ? K | RecursiveKeyOf<T[K], K> : `${Prefix}.${K}` | RecursiveKeyOf<T[K], `${Prefix}.${K}`>; | ||
@@ -4,0 +9,0 @@ }[keyof T & string]; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
const TERMINAL_BRAND = Symbol(); | ||
class BrandedTerminal { | ||
} | ||
//# sourceMappingURL=recursive-key-of.type.js.map |
@@ -0,0 +0,0 @@ export declare type TypeIfType<C, E, T, T2> = [C] extends [E] ? T : T2; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
//# sourceMappingURL=type-if-type.type.js.map |
import { RecursiveKeyOf, DeepPropertyType } from '../types/recursive-key-of.type'; | ||
export declare function getValueFromPath<T extends any, TP extends RecursiveKeyOf<T> & string>(obj: T, path?: TP): DeepPropertyType<T, TP>; | ||
export declare function setValueFromPath<T extends any, TP extends RecursiveKeyOf<T> & string, V extends DeepPropertyType<T, TP>>(obj: T, path: TP, value: V): T; |
@@ -0,0 +0,0 @@ "use strict"; |
export {}; |
@@ -0,0 +0,0 @@ "use strict"; |
{ | ||
"name": "nestjs-cls", | ||
"version": "2.1.0", | ||
"version": "2.1.1", | ||
"description": "A continuation-local storage module compatible with NestJS's dependency injection.", | ||
@@ -48,3 +48,3 @@ "author": "papooch", | ||
"@nestjs/core": "^8.0.0", | ||
"@nestjs/graphql": "^9.0.5", | ||
"@nestjs/graphql": "^8.0.0", | ||
"@nestjs/platform-express": "^8.0.0", | ||
@@ -61,3 +61,3 @@ "@nestjs/platform-fastify": "^8.0.6", | ||
"@typescript-eslint/parser": "^4.28.2", | ||
"apollo-server-express": "^3.3.0", | ||
"apollo-server-express": "^2.25.4", | ||
"class-validator": "^0.13.1", | ||
@@ -64,0 +64,0 @@ "cls-hooked": "^4.2.2", |
@@ -42,4 +42,6 @@ # NestJS CLS | ||
- [Others](#others) | ||
- [Namespaces](#namespaces-experimental) (experimental) | ||
- [~~Namespaces~~](#namespaces-deprecated) (deprecated) | ||
> **Notice**: I have deprecated [Namespaces](#namespaces-deprecated) since version `2.1.1` and will be removing them in `3.0` to make room for new features ([#31](https://github.com/Papooch/nestjs-cls/issues/31)). Namespace support was experimental from the begining, and I havent seen any justifiable use case to keep it around. | ||
# Install | ||
@@ -83,3 +85,3 @@ | ||
private readonly cls: ClsService, | ||
); | ||
) {} | ||
@@ -89,3 +91,3 @@ intercept(context: ExecutionContext, next: CallHandler): Observable<any> { | ||
const request = context.switchToHttp().getRequest(); | ||
const userIp = req.connection.remoteAddress; | ||
const userIp = request.connection.remoteAddress; | ||
// ...and store it to the cls context. | ||
@@ -157,3 +159,3 @@ this.cls.set('ip', userIp); | ||
configure(consumer: MiddlewareConsumer) { | ||
apply(ClsMiddleware).forRoutes(AppController); | ||
consumer.apply(ClsMiddleware).forRoutes(AppController); | ||
} | ||
@@ -450,3 +452,3 @@ } | ||
This will only generate the paths `tenantId | user` and won't allow directly accessing nested keys (like `cls.get('user.id')`, but you'll still get fully typing for things like `const { id } = cls.get('user')`). See issue #22 for more details. | ||
This will only generate the paths `tenantId | user` and won't allow directly accessing nested keys (like `cls.get('user.id')`, but you'll still get fully typing for things like `const { id } = cls.get('user')`). See issue [#22](https://github.com/Papooch/nestjs-cls/issues/22) for more details. | ||
@@ -575,5 +577,5 @@ # API | ||
# Namespaces (experimental) | ||
# ~~Namespaces~~ (deprecated) | ||
> Warning: Namespace support is currently experimental and has no tests. While the API is mostly stable now, it can still change any time. | ||
> **Warning**: Namespace support will be dropped in v3.0 | ||
@@ -593,3 +595,3 @@ The default CLS namespace that the `ClsService` provides should be enough for most application, but should you need it, this package provides a way to use multiple CLS namespaces simultaneously. | ||
This creates a namespaces `ClsService` provider that you can inject using `@InjectCls` | ||
This creates a namespaced `ClsService` provider that you can inject using `@InjectCls` | ||
@@ -631,2 +633,2 @@ ```ts | ||
> **Note**: `@InjectCls('x')` is equivalent to `@Inject(getNamespaceToken('x'))`. If you don't pass an argument to `@InjectCls()`, the default ClsService will be injected and is equivalent to omitting the decorator altogether. | ||
> **Note**: `@InjectCls('x')` is equivalent to `@Inject(getClsServiceToken('x'))`. If you don't pass an argument to `@InjectCls()`, the default ClsService will be injected and is equivalent to omitting the decorator altogether. |
@@ -0,0 +0,0 @@ export * from './lib/cls-service-manager'; |
@@ -6,4 +6,15 @@ import { ClassProvider, ValueProvider } from '@nestjs/common'; | ||
export const getClsServiceToken = (namespace: string) => | ||
`ClsService-${namespace}`; | ||
/** | ||
* Get ClsService injection token (as a string) | ||
*/ | ||
export function getClsServiceToken(): string; | ||
/** | ||
* Get namespaced ClsService injection token (as a string) | ||
* @param namespace name of the namespace | ||
* @deprecated Namespace support will be removed in v3.0 | ||
*/ | ||
export function getClsServiceToken(namespace: string): string; | ||
export function getClsServiceToken(namespace = CLS_DEFAULT_NAMESPACE) { | ||
return `ClsService-${namespace}`; | ||
} | ||
@@ -10,0 +21,0 @@ export class ClsServiceManager { |
@@ -0,0 +0,0 @@ export const CLS_REQ = Symbol('CLS_REQUEST'); |
import { Inject } from '@nestjs/common'; | ||
import { getClsServiceToken } from './cls-service-manager'; | ||
import { CLS_DEFAULT_NAMESPACE } from './cls.constants'; | ||
export const InjectCls = (namespace: string) => | ||
Inject(getClsServiceToken(namespace)); | ||
/** | ||
* Use to explicitly inject the ClsService | ||
*/ | ||
export function InjectCls(): (target: any, key: string | symbol, index?: number) => void; | ||
/** | ||
* Use to inject a namespaced CLS service | ||
* @param namespace name of the namespace | ||
* @deprecated Namespace support will be removed in v3.0 | ||
*/ | ||
export function InjectCls(namespace: string): (target: any, key: string | symbol, index?: number) => void; | ||
export function InjectCls(namespace = CLS_DEFAULT_NAMESPACE) { | ||
return Inject(getClsServiceToken(namespace)); | ||
} |
@@ -0,0 +0,0 @@ import { |
@@ -0,0 +0,0 @@ import { |
@@ -31,2 +31,3 @@ import { ExecutionContext, ModuleMetadata } from '@nestjs/common'; | ||
* (most of the time you will not need to touch this setting) | ||
* @deprecated Namespace support will be removed in v3.0 | ||
*/ | ||
@@ -54,2 +55,3 @@ namespaceName? = CLS_DEFAULT_NAMESPACE; | ||
* (most of the time you will not need to touch this setting) | ||
* @deprecated Namespace support will be removed in v3.0 | ||
*/ | ||
@@ -104,2 +106,5 @@ namespaceName?: string; | ||
/** | ||
* @deprecated Namespace support will be removed in v3.0 | ||
*/ | ||
readonly namespaceName?: string; | ||
@@ -134,2 +139,5 @@ } | ||
/** | ||
* @deprecated Namespace support will be removed in v3.0 | ||
*/ | ||
readonly namespaceName?: string; | ||
@@ -164,2 +172,5 @@ } | ||
/** | ||
* @deprecated Namespace support will be removed in v3.0 | ||
*/ | ||
readonly namespaceName?: string; | ||
@@ -166,0 +177,0 @@ } |
@@ -0,0 +0,0 @@ import { Inject, Injectable, NestMiddleware } from '@nestjs/common'; |
@@ -17,3 +17,2 @@ import { | ||
} from '@nestjs/core'; | ||
import { CLS_MODULE_OPTIONS } from '..'; | ||
import { ClsServiceManager, getClsServiceToken } from './cls-service-manager'; | ||
@@ -24,2 +23,3 @@ import { | ||
CLS_MIDDLEWARE_OPTIONS, | ||
CLS_MODULE_OPTIONS, | ||
} from './cls.constants'; | ||
@@ -74,2 +74,13 @@ import { ClsGuard } from './cls.guard'; | ||
/** | ||
* Registers the `ClsService` provider in the module | ||
*/ | ||
static forFeature(): DynamicModule; | ||
/** | ||
* @param namespaceName | ||
* @deprecated usage with namespaceName is deprecated and will be | ||
* removed with namespace support in v3.0 | ||
* @returns | ||
*/ | ||
static forFeature(namespaceName: string): DynamicModule; | ||
static forFeature(namespaceName?: string): DynamicModule { | ||
@@ -76,0 +87,0 @@ const providers = ClsServiceManager.getClsServicesAsProviders().filter( |
@@ -0,0 +0,0 @@ import { Test, TestingModule } from '@nestjs/testing'; |
@@ -0,0 +0,0 @@ import { AsyncLocalStorage } from 'async_hooks'; |
@@ -0,0 +0,0 @@ type TerminalType = |
@@ -0,0 +0,0 @@ /** |
@@ -0,0 +0,0 @@ import { getValueFromPath, setValueFromPath } from './value-from-path'; |
@@ -0,0 +0,0 @@ import { |
@@ -0,0 +0,0 @@ { |
@@ -0,0 +0,0 @@ { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
322392
1901
627