Skip to content

Instantly share code, notes, and snippets.

@alexbridge
Created November 6, 2023 19:36
Show Gist options
  • Save alexbridge/4d7397d60a184d5d2073410d11dc5e8d to your computer and use it in GitHub Desktop.
Save alexbridge/4d7397d60a184d5d2073410d11dc5e8d to your computer and use it in GitHub Desktop.
Nestjs useFactory example with inject and ConfigService
@Module({
providers: [
{
provide: MyApiService,
useFactory: (configService: ConfigService) => {
return new MyApiService(
configService.getOrThrow('MY_BASE_URL'),
configService.getOrThrow('MY_USER'),
configService.getOrThrow('MY_PASSWORD')
);
},
inject: [ConfigService],
},
],
exports: [MyApiService],
})
export class MyApiModule {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment