import { DynamicModule , Global , Module , Provider } from "@nestjs/common" ; import { ClassProvider } from "@nestjs/common/interfaces" ; import { IncomingWebhook } from "@slack/webhook" ; import { createSlackProvider } from "./createSlackProvider" ; import { getSlackClient } from "./getSlackClient" ; import { SLACK_MODULE , SLACK_TOKEN } from "./slackConstants" ; import { SlackAsyncOptions , SlackOptions , SlackOptionsFactory , } from "./slackOptions" ; @ Global ( ) @ Module ( { } ) export class SlackCoreModule { public static forRoot ( options ) { const provider = createSlackProvider ( options ) ; return { exports : [ provider ] , module : SlackCoreModule , providers : [ provider ] , } ; } static forRootAsync ( options ) { const slackProvider = { inject : [ SLACK_MODULE ] , provide : SLACK_TOKEN , useFactory : ( slackOptions ) => getSlackClient ( slackOptions ) , } ; return { exports : [ slackProvider ] , imports : options . imports , module : SlackCoreModule , providers : [ ... this . createAsyncProviders ( options ) , slackProvider ] , } ; } private static createAsyncProviders ( options ) { if ( options . useExisting || options . useFactory ) { return [ this . createAsyncOptionsProvider ( options ) ] ; } return [ this . createAsyncOptionsProvider ( options ) , { provide : options . useClass , useClass : options . useClass , inject : [ options . inject ?? [ ] ] , } as ClassProvider , ] ; } private static createAsyncOptionsProvider ( options ) { if ( options . useFactory ) { return { inject : options . inject ?? [ ] , provide : SLACK_MODULE , useFactory : options . useFactory , } ; } return { inject : options . useExisting ? [ options . useExisting ] : options . useClass ? [ options . useClass ] : [ ] , provide : SLACK_MODULE , useFactory : ( optionsFactory ) => optionsFactory . createSlackOptions ( ) , } ; } }