Skip to content

Commit

Permalink
feat: allow registering Proxy providers globally
Browse files Browse the repository at this point in the history
  • Loading branch information
Papooch committed Oct 18, 2023
1 parent bb755f9 commit 92d00f7
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
19 changes: 19 additions & 0 deletions packages/core/src/lib/cls.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ export class ClsModule implements NestModule {
}
}

/**
* Configures the CLS module in the root.
*
* Provides the `ClsService` for injection.
*/
static forRoot(options?: ClsModuleOptions): DynamicModule {
options = { ...new ClsModuleOptions(), ...options };
const { providers, exports } = this.getProviders();
Expand All @@ -107,6 +112,11 @@ export class ClsModule implements NestModule {
};
}

/**
* Configures the CLS module in the root with asynchronously provided configuration.
*
* Provides the `ClsService` for injection.
*/
static forRootAsync(asyncOptions: ClsModuleAsyncOptions): DynamicModule {
const { providers, exports } = this.getProviders();
const proxyProviders = this.createProxyClassProviders(
Expand Down Expand Up @@ -134,6 +144,9 @@ export class ClsModule implements NestModule {
* Registers the `ClsService` provider in the module
*/
static forFeature(): DynamicModule;
/**
* Registers the given Class proxy providers in the module along with `ClsService`.
*/
static forFeature(...proxyProviderClasses: Array<Type>): DynamicModule;
static forFeature(...proxyProviderClasses: Array<Type>): DynamicModule {
const proxyProviders =
Expand All @@ -146,6 +159,11 @@ export class ClsModule implements NestModule {
};
}

/**
* Registers the given Class or Factory proxy providers in the module along with `ClsService`.
*
* If used with `global: true`, makes the proxy provider available globally.
*/
static forFeatureAsync(
options: ClsModuleProxyProviderOptions,
): DynamicModule {
Expand All @@ -159,6 +177,7 @@ export class ClsModule implements NestModule {
imports: options.imports ?? [],
providers: [...providers, proxyProvider],
exports: [...commonProviders, proxyProvider.provide],
global: options.global,
};
}

Expand Down
34 changes: 16 additions & 18 deletions packages/core/src/lib/proxy-provider/proxy-provider.interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { Provider, Type } from '@nestjs/common';
import { ModuleRef } from '@nestjs/core';

export interface ClsModuleProxyClassProviderOptions {
/**
* Custom injection token to use for the provider. In case of a class provider,
* this parameter is optional, as the class reference passed to `useClass` will
* be used by default.
*/
provide?: any;

interface ClsModuleProxyProviderCommonOptions {
/**
* Optional list of imported modules that export the providers which are required for the provider.
*/
Expand All @@ -21,28 +14,33 @@ export interface ClsModuleProxyClassProviderOptions {
extraProviders?: Provider[];

/**
* The target class that will be used by this Proxy Provider. Make sure it is decorated with `@InjectableProxy`.
* Whether to make this proxy provider available globally or just in this module.
*/
useClass: Type;
global?: boolean;
}
export interface ClsModuleProxyFactoryProviderOptions {

export interface ClsModuleProxyClassProviderOptions
extends ClsModuleProxyProviderCommonOptions {
/**
* Custom injection token to use for the provider. In case of a class provider,
* this parameter is optional, as the class reference passed to `useClass` will
* be used by default.
*/
provide: any;
provide?: any;

/**
* Optional list of imported modules that export the providers which are required for the provider.
* The target class that will be used by this Proxy Provider. Make sure it is decorated with `@InjectableProxy`.
*/
imports?: any[];

useClass: Type;
}
export interface ClsModuleProxyFactoryProviderOptions
extends ClsModuleProxyProviderCommonOptions {
/**
* Optional list of additional providers that should be available to the Proxy.
* Useful for passing configuration from a parent dynamic module.
* Custom injection token to use for the provider. In case of a class provider,
* this parameter is optional, as the class reference passed to `useClass` will
* be used by default.
*/
extraProviders?: Provider[];
provide: any;

/**
* An array of injection tokens for providers used in the `useFactory`.
Expand Down

0 comments on commit 92d00f7

Please sign in to comment.