Providers

A provider injects dependencies establishing relationships within objects. Most of the classes inside a project can be handled as providers, like: services, factories, repositories, and like.

HTTP Service
Cacheable Service
External Service
Logger Service
Page Service
Controller /page

Dependency injection

The framework leverages the dependency injection pattern. It makes classes injectable by using the decorator @Injectable() and injects them with the decorator @Inject() or directly in the constructor without the @Inject() decorator, but just the Provider class typing.

@Injectable()
export class ExternalTestService {
  constructor(private readonly httpService: HttpService) {
  }

  async getExternalCall() {
    const httpTest = await this.httpService.get({
      query: {
        param1: 'externaTest',
        param2: 12345
      },
      url: 'https://httpbin.org/get',
    });
    return { test: 'hello world', result: httpTest };
  }
}

The providers are finally resolved by the framework and returned as singletons, means returning the existing instance (if it has already been requested elsewhere) in the property that has been decorated with @Inject().

@Controller('/test')
export class TestController {
  constructor(private readonly externalService: ExternalTestService) {
  }
  
  @Get('/hello-world')
  async helloWorld() {
    return { test: 'hello world' };
  }
  
  @Get('/external')
  async external() {
    return { external: this.externalService.getExternalCall() };
  }
}

Custom providers

The providers can also be custom in the case we want to cover it with an abstract class and inject it with different implementations. The providers metadata in @Module() can either be a specific provider or a custom mapping.

@Module({
  modules: [RandomModule],
  controllers: [TestController],
  providers: [
    { provider: CacheManager, useClass: RedisService }
  ]
})
export class TestRootModule { }

You can inject custom providers also via the @Injectable({ interface: CustomProvider }) decorator.

@Injectable({ interface: CacheManager })
class RedisService implements CacheManager {}