admin管理员组

文章数量:1390204

I have conversations.module.ts that has:

@Module({
  imports: [ImageModule, YoutubeModule],
  controllers: [ConversationsController],
  providers: [ConversationsService, ParticipantsService, StreamsService]
})
export class ConversationsModule { }

and within my conversations.controller.ts, I have:

@Controller('conversations')
export class ConversationsController {
    constructor(private conversationsService: ConversationsService, private imageService: ImageService, private youtubeService: YoutubeService, private participantsService: ParticipantsService, private streamsService: StreamsService) { }

But what I want to do is inject the AWS S3 module:

const secretsmanager = new S3({ region: 'us-east-1' })

that requires it to be instantiated. How can I acplish this?

I have conversations.module.ts that has:

@Module({
  imports: [ImageModule, YoutubeModule],
  controllers: [ConversationsController],
  providers: [ConversationsService, ParticipantsService, StreamsService]
})
export class ConversationsModule { }

and within my conversations.controller.ts, I have:

@Controller('conversations')
export class ConversationsController {
    constructor(private conversationsService: ConversationsService, private imageService: ImageService, private youtubeService: YoutubeService, private participantsService: ParticipantsService, private streamsService: StreamsService) { }

But what I want to do is inject the AWS S3 module:

const secretsmanager = new S3({ region: 'us-east-1' })

that requires it to be instantiated. How can I acplish this?

Share Improve this question asked May 8, 2020 at 17:54 ShamoonShamoon 43.7k101 gold badges332 silver badges628 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 5

It sounds like you are looking for a custom provider. You can define an injection token (a string) and give it to an object with the key provide and a key useClass, useValue, or useFactory that determines what value is to be injected. In your case, you can do something like

{
  provide: 'SECRETS_MANAGER',
  useValue: new S3({ region: 'us-east-1' }),
}

And now you can use the injection token with the @Inject() decorator in the constructor like so

constructor(@Inject('SECRETS_MANAGER') private readonly manager: S3) {}

Or whatever the type new S3() returns.

本文标签: javascriptWith nestjshow can I inject a Provider that has a constructorStack Overflow