问题
been looking into the dynamic modules using the Advanced NestJS: How to build completely dynamic NestJS modules. (https://dev.to/nestjs/advanced-nestjs-how-to-build-completely-dynamic-nestjs-modules-1370)
From what I've seen, most people use this guide to build a sync/async dynamic module.
But my question is, that if I use the registerAsync method, and my dynamic module needs to import HttpModule, and HttpModule's register-options are provided by my dynamic module.
How do you import a module within a dynamic module, where the options are provided by the dynamic module? Or is it the wrong way to handle this issue? if so, how would you structure it?
Here's the code. Which is practically a carbon copy of the tutorial. As you can see in the register method, it's simple - I just pass in the options. registerAsync however, I'm having trouble figuring out what to do.
Any help is much appreciated :)
import { Module, DynamicModule, Provider, HttpModule } from "@nestjs/common";
import { InvoicesHealth } from "./invoices/invoices.health";
import { InvoicesResolver, InvoicesService } from "./invoices";
import {
CustomerInvoicesOptions,
CustomerInvoicesAsyncOptions,
CustomerInvoicesOptionsFactory,
} from "./interfaces";
import { CUSTOMER_INVOICES_OPTIONS } from "./constants";
import { createCustomerInvoicesProviders } from "./providers/customer-invoices.providers";
@Module({
imports: [],
controllers: [],
providers: [InvoicesHealth, InvoicesResolver, InvoicesService],
exports: [InvoicesHealth],
})
export class CustomerInvoicesModule {
/**
* Registers a configured customer-invoices Module for import into the current module
*/
public static register(options: CustomerInvoicesOptions): DynamicModule {
return {
imports: [
HttpModule.register({
url: options.url,
auth: {
username: options.username,
password: options.password,
},
}),
],
module: CustomerInvoicesModule,
providers: createCustomerInvoicesProviders(options),
};
}
/**
* Registers a configured customer-invoices Module for import into the current module
* using dynamic options (factory, etc)
*/
public static registerAsync(
options: CustomerInvoicesAsyncOptions,
): DynamicModule {
return {
module: CustomerInvoicesModule,
imports: options.imports || [],
providers: [...this.createProviders(options)],
};
}
private static createProviders(
options: CustomerInvoicesAsyncOptions,
): Provider[] {
if (options.useExisting || options.useFactory) {
return [this.createOptionsProvider(options)];
}
return [
this.createOptionsProvider(options),
{
provide: options.useClass,
useClass: options.useClass,
},
];
}
private static createOptionsProvider(
options: CustomerInvoicesAsyncOptions,
): Provider {
if (options.useFactory) {
return {
provide: CUSTOMER_INVOICES_OPTIONS,
useFactory: options.useFactory,
inject: options.inject || [],
};
}
// For useExisting...
return {
provide: CUSTOMER_INVOICES_OPTIONS,
useFactory: async (optionsFactory: CustomerInvoicesOptionsFactory) =>
await optionsFactory.createFtNestCustomerInvoicesOptions(),
inject: [options.useExisting || options.useClass],
};
}
}
来源:https://stackoverflow.com/questions/63356440/how-to-import-a-registerasync-in-a-dynamic-nestjs-module