0

With nestjs framework in mind, I have a couple of services exposed as API. Each service has its own module and specific httpmodule config.

ServiceA needs to make a call to ServiceB. In this case I have two options: 1 - Perform an http request. 2 - Consume serviceB within service A.

For option 1 there is a problem. ServiceA has its own HTTPModule config and therefore to perform an http request I need to override HttpModule configurations. Also, it puts an unnecessary load on my service sending http requests.

Option 2 seems to have an issue where ServiceA HttpModule configurations are being used for ServiceB.

Question: IS there a way to consume ServiceB in ServiceA but maintain the module context integrity of ServiceB?

For clarity here is the structure:

-src
  --serviceA
    -- serviceA.module.ts
    -- serviceA.service.ts
  --serviceB
    -- serviceB.module.ts
    -- serviceB.service.ts

Thanks

  • Possible duplicate of [Inject nestjs service from another module](https://stackoverflow.com/questions/51819504/inject-nestjs-service-from-another-module) – Kim Kern Jan 04 '19 at 22:27

1 Answers1

0

I think I figured it out.

In serviceA.module:

@Module({
    imports: [
        ServiceBModule
    ],
})
export class ServiceA {}

in ServiceB module I had to add the exports like so:

@Module({
    exports: [ServiceBService]
})
export class ServiceBModule {}

in serviceA.service

import { ServiceBService } from 'src/serviceB/serviceB.service';

constructor(private readonly serviceB: ServiceBService) { }

Use serviceB as you normally would in your code.