nestjs-slack icon indicating copy to clipboard operation
nestjs-slack copied to clipboard

How to instantiate the SlackService class in main.ts

Open pedroriverove opened this issue 1 year ago • 1 comments

Hi.

I hope you are well.

In the main.ts I want to pass the SlackService configuration to the global exception filter which receives that class in the constructor, but I don't understand the way to achieve this.

public async bootstrap(): Promise<INestApplication> {
  this.app = await NestFactory.create(AppModule, { cors: true });
  const configService = await this.app.resolve(ConfigService);
  this.setGlobalFilters(configService);
  await this.listen(configService.get('app.port'));
  return this.app;
}

public setGlobalFilters(configService: ConfigService) {
  const httpAdapter: HttpAdapterHost =
    this.app.get<HttpAdapterHost>(HttpAdapterHost);
  const slackService = new SlackService({
    type: 'webhook',
    webhookOptions: {
      url: configService.get('SLACK_WEBHOOK_URL'),
    },
  });

  this.app.useGlobalFilters(
    new AllExceptionsFilter(httpAdapter, slackService),
  );
}

pedroriverove avatar Sep 20 '22 10:09 pedroriverove

Hello 👋

As far as I can see, this should work. I do the same in my applications, but I don't use the useGlobalFilters.

{
provide: APP_FILTER,
useClass: AllExceptionsFilter,
},

Maybe something for you too?

simenandre avatar Oct 13 '22 04:10 simenandre

Closing this as stale, but feel free to leave me a note if you wanna pick this up :)

simenandre avatar Nov 07 '22 18:11 simenandre