UNPKG

5.13 kBTypeScriptView Raw
1import { DynamicModule } from '../interfaces';
2import { Logger } from '../services/logger.service';
3import { DEFAULT_FACTORY_CLASS_METHOD_KEY, DEFAULT_METHOD_KEY } from './constants';
4import { ConfigurableModuleHost } from './interfaces';
5/**
6 * @publicApi
7 */
8export interface ConfigurableModuleBuilderOptions {
9 /**
10 * Specified what injection token should be used for the module options provider.
11 * By default, an auto-generated UUID will be used.
12 */
13 optionsInjectionToken?: string | symbol;
14 /**
15 * By default, an UUID will be used as a module options provider token.
16 * Explicitly specifying the "moduleName" will instruct the "ConfigurableModuleBuilder"
17 * to use a more descriptive provider token.
18 *
19 * For example, if `moduleName: "Cache"` then auto-generated provider token will be "CACHE_MODULE_OPTIONS".
20 */
21 moduleName?: string;
22 /**
23 * Indicates whether module should always be "transient", meaning,
24 * every time you call the static method to construct a dynamic module,
25 * regardless of what arguments you pass in, a new "unique" module will be created.
26 *
27 * @default false
28 */
29 alwaysTransient?: boolean;
30}
31/**
32 * Factory that lets you create configurable modules and
33 * provides a way to reduce the majority of dynamic module boilerplate.
34 *
35 * @publicApi
36 */
37export declare class ConfigurableModuleBuilder<ModuleOptions, StaticMethodKey extends string = typeof DEFAULT_METHOD_KEY, FactoryClassMethodKey extends string = typeof DEFAULT_FACTORY_CLASS_METHOD_KEY, ExtraModuleDefinitionOptions = {}> {
38 protected readonly options: ConfigurableModuleBuilderOptions;
39 protected staticMethodKey: StaticMethodKey;
40 protected factoryClassMethodKey: FactoryClassMethodKey;
41 protected extras: ExtraModuleDefinitionOptions;
42 protected transformModuleDefinition: (definition: DynamicModule, extraOptions: ExtraModuleDefinitionOptions) => DynamicModule;
43 protected readonly logger: Logger;
44 constructor(options?: ConfigurableModuleBuilderOptions, parentBuilder?: ConfigurableModuleBuilder<ModuleOptions>);
45 /**
46 * Registers the "extras" object (a set of extra options that can be used to modify the dynamic module definition).
47 * Values you specify within the "extras" object will be used as default values (that can be overriden by module consumers).
48 *
49 * This method also applies the so-called "module definition transform function" that takes the auto-generated
50 * dynamic module object ("DynamicModule") and the actual consumer "extras" object as input parameters.
51 * The "extras" object consists of values explicitly specified by module consumers and default values.
52 *
53 * @example
54 * ```typescript
55 * .setExtras<{ isGlobal?: boolean }>({ isGlobal: false }, (definition, extras) =>
56 * ({ ...definition, global: extras.isGlobal })
57 * )
58 * ```
59 */
60 setExtras<ExtraModuleDefinitionOptions>(extras: ExtraModuleDefinitionOptions, transformDefinition?: (definition: DynamicModule, extras: ExtraModuleDefinitionOptions) => DynamicModule): ConfigurableModuleBuilder<ModuleOptions, StaticMethodKey, FactoryClassMethodKey, ExtraModuleDefinitionOptions>;
61 /**
62 * Dynamic modules must expose public static methods that let you pass in
63 * configuration parameters (control the module's behavior from the outside).
64 * Some frequently used names that you may have seen in other modules are:
65 * "forRoot", "forFeature", "register", "configure".
66 *
67 * This method "setClassMethodName" lets you specify the name of the
68 * method that will be auto-generated.
69 *
70 * @param key name of the method
71 */
72 setClassMethodName<StaticMethodKey extends string>(key: StaticMethodKey): ConfigurableModuleBuilder<ModuleOptions, StaticMethodKey, FactoryClassMethodKey, ExtraModuleDefinitionOptions>;
73 /**
74 * Asynchronously configured modules (that rely on other modules, i.e. "ConfigModule")
75 * let you pass the configuration factory class that will be registered and instantiated as a provider.
76 * This provider then will be used to retrieve the module's configuration. To provide the configuration,
77 * the corresponding factory method must be implemented.
78 *
79 * This method ("setFactoryMethodName") lets you control what method name will have to be
80 * implemented by the config factory (default is "create").
81 *
82 * @param key name of the method
83 */
84 setFactoryMethodName<FactoryClassMethodKey extends string>(key: FactoryClassMethodKey): ConfigurableModuleBuilder<ModuleOptions, StaticMethodKey, FactoryClassMethodKey, ExtraModuleDefinitionOptions>;
85 /**
86 * Returns an object consisting of multiple properties that lets you
87 * easily construct dynamic configurable modules. See "ConfigurableModuleHost" interface for more details.
88 */
89 build(): ConfigurableModuleHost<ModuleOptions, StaticMethodKey, FactoryClassMethodKey, ExtraModuleDefinitionOptions>;
90 private constructInjectionTokenString;
91 private createConfigurableModuleCls;
92 private createTypeProxy;
93}
94
\No newline at end of file