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 badges1 Answer
Reset to default 5It 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
版权声明:本文标题:javascript - With nest.js, how can I inject a Provider that has a constructor? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744698742a2620438.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论