-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* chore: remove namespaces from code * docs: remove namespaces from docs
- Loading branch information
Showing
12 changed files
with
44 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ on: | |
pull_request: | ||
branches: | ||
- main | ||
- release/* | ||
|
||
concurrency: | ||
cancel-in-progress: true | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,75 +1,17 @@ | ||
import { ClassProvider, ValueProvider } from '@nestjs/common'; | ||
import { CLS_DEFAULT_NAMESPACE } from './cls.constants'; | ||
import { ClsService } from './cls.service'; | ||
import { AsyncLocalStorage } from 'async_hooks'; | ||
|
||
/** | ||
* 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}`; | ||
} | ||
import { ClsStore } from './cls.interfaces'; | ||
import { ClsService } from './cls.service'; | ||
|
||
export class ClsServiceManager { | ||
private static namespaces: Record< | ||
string, | ||
AsyncLocalStorage<any> & { name?: string } | ||
> = {}; | ||
|
||
private static clsServices: Map<string | typeof ClsService, ClsService> = | ||
new Map([ | ||
[ | ||
ClsService, | ||
new ClsService(this.resolveNamespace(CLS_DEFAULT_NAMESPACE)), | ||
], | ||
]); | ||
|
||
private static resolveNamespace(name: string) { | ||
if (!this.namespaces[name]) { | ||
this.namespaces[name] = new AsyncLocalStorage(); | ||
this.namespaces[name].name = name; | ||
} | ||
return this.namespaces[name]; | ||
} | ||
|
||
static addClsService(name: string = CLS_DEFAULT_NAMESPACE) { | ||
const service = new ClsService(this.resolveNamespace(name)); | ||
this.clsServices.set( | ||
getClsServiceToken(name), | ||
new ClsService(this.resolveNamespace(name)), | ||
); | ||
return service; | ||
} | ||
private static als = new AsyncLocalStorage(); | ||
private static clsService = new ClsService(this.als); | ||
|
||
/** | ||
* Retrieve a ClsService outside of Nest's DI. | ||
* @param name namespace name, omit for default | ||
* @returns the ClsService with the given namespace | ||
* @returns the ClsService | ||
*/ | ||
static getClsService(name?: string) { | ||
const cls = this.clsServices.get( | ||
name ? getClsServiceToken(name) : ClsService, | ||
); | ||
if (!cls) | ||
throw new Error(`ClsService with namespace ${name} does not exist`); | ||
static getClsService<T extends ClsStore = ClsStore>(): ClsService<T> { | ||
const cls = this.clsService as ClsService<T>; | ||
return cls; | ||
} | ||
|
||
static getClsServicesAsProviders(): Array< | ||
ClassProvider<ClsService> | ValueProvider<ClsService> | ||
> { | ||
return Array.from(this.clsServices.entries()).map( | ||
([provide, service]) => ({ | ||
provide, | ||
useValue: service, | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,9 @@ | ||
import { Inject } from '@nestjs/common'; | ||
import { getClsServiceToken } from './cls-service-manager'; | ||
import { CLS_DEFAULT_NAMESPACE } from './cls.constants'; | ||
import { ClsService } from './cls.service'; | ||
|
||
/** | ||
* 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)); | ||
export function InjectCls() { | ||
return Inject(ClsService); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.