Skip to main content

Providers helper

This class simplifies the addition of providers to DI while simultaneously controlling their types. Since this class implements the so-called Iteration protocols, it facilitates the conversion of itself into an array (note the spread operator):

import { featureModule, Providers } from '@ditsmod/core';
// ...
@featureModule({
// ...
providersPerRou: [
Provider1,
Provider2,
...new Providers().useValue<CorsOpts>(CorsOpts, { origin: 'https://example.com' }),
// ...
],
// ...
})
export class SomeModule {}

Starting from v2.55, Ditsmod allows passing an instance of Providers directly into the providersPer* properties of the module or controller metadata:

import { featureModule, Providers } from '@ditsmod/core';
// ...
@featureModule({
// ...
providersPerRou: new Providers()
.passThrough(Provider1)
.passThrough(Provider2)
.useValue<CorsOpts>(CorsOpts, { origin: 'https://example.com' }),
// ...
})
export class SomeModule {}

The providers.passThrough() method allows providers to be passed without type checking; it is intended for passing classes as providers.

In addition, Providers has a special method $if(), which allows providers to be passed only if it receives a truthy value:

import { featureModule, Providers } from '@ditsmod/core';
// ...
@featureModule({
// ...
providersPerRou: new Providers()
.passThrough(Provider1)
.$if(false)
.passThrough(Provider2)
.passThrough(Provider3),
// ...
})
export class SomeModule {}

In this case, Provider2 will not be passed to DI, while Provider1 and Provider3 will be passed. That is, $if() applies only to the first expression immediately following it.

The providers.$use() method allows creating plugins (or middlewares) to extend the functionality of Providers:

class Plugin1 extends Providers {
method1() {
if (this.true) {
// ...
}
return this.self;
}
}

class Plugin2 extends Providers {
method2() {
if (this.true) {
// ...
}
return this.self;
}
}

const providers = [...new Providers()
.use(Plugin1)
.use(Plugin2)
.method1()
.method2()
.useLogConfig({ level: 'trace' })
.useClass(SomeService, ExtendedService)];