如何创建全球动态提供者

时间:2019-07-02 16:28:51

标签: nestjs

我正在尝试使用单例服务创建一个模块来获取伪造的浏览器实例

我尝试不成功地遵循文档,并且在没有成功的情况下挖掘typeorm模块

import { DynamicModule, Global, Module, Provider } from '@nestjs/common';
import puppeteer from 'puppeteer';
import { PuppeteerService } from './puppeteer.service';

@Global()
@Module({})
export class PuppeteerModule {
  static forRoot(options?): DynamicModule {
    const providers = [this.createPuppeteerService(options)];

    return {
      module: PuppeteerModule,
      providers,
      exports: providers,
    };
  }

  private static createPuppeteerService(options: puppeteer.LaunchOptions): Provider {
    return {
      provide: 'PUPPETEER_SERVICE',
      useFactory: async () => {
        const browser = await puppeteer.launch(options);
        const puppeteerService = new PuppeteerService(browser);
        return puppeteerService;
      },
    };
  }
}

@Module({
  imports: [
    PuppeteerModule.forRoot(config.puppeter.launchOptions),
]
  controllers: [AppController],
  providers: [],
  exports: [],
})
export class AppModule {}

我要实现的是在根目录下注册PuppeteerModule并在另一个模块中进行以下操作:

@Module({
  imports: [
    PuppeteerModule,
  ],
  providers: [
  ],
  controllers: [],
  exports: [],
})
export class AnotherModule {}

并有权访问PupperterService

0 个答案:

没有答案