#Inject a CustomTransportStrategy from DI?

2 messages · Page 1 of 1 (latest)

placid portal
#

I'm creating a custom transport strategy that relies on other services. Is it possible to inject the strategy in the bootstrap function?


class MyTransportStrategy extends Server implements CustomTransportStrategy {
  constructor(private service: SomeService) {}
}

...

async function bootstrap() {
  const app = await NestFactory.createMicroservice<MicroserviceOptions>(
    AppModule,
    {
      strategy: new MyTransportStrategy(?), // <- Inject instead of construct
    },
  );

  await app.listen();
}
torn marlin
#

Yes, but not via NestFactory.createMicroservice

You'll first need to create an standalone Nest app (or a regular Nest http app) and then use app.connectMicroservice(xxx). You can pull dependencies via app.get before attaching the transport