Skip to main content
Version: 0.5.0

Container API

bind(type, options)

Binds a type. The container takes any binding associated to that type and process it. Optionally, it receives an options parameter:

PropertyDescriptionType
scopeBinding scope to applyBindingScope

bindToValue(options)

Binds a value. The options parameter is described as follows:

PropertyDescriptionType
serviceIdService id to associatenumber | string | symbol
tagsTags to associatenumber | string | symbol | (number | string | symbol)[]
valueValue to bindunknown
note

Values are always bound in singleton scope

get(serviceId)

Returns a service by its id. If necessary, the service is instantiated.

Consider the binding page as reference to know more about instance details.

getByTag(tag)

Returns all the services associated to a tag. If necessary, services are instantiated.

getAllBindinds()

Returns all the bindings registered in the container.

load(containerModule)

Loads a container module.

import { ContainerModule, ContainerModuleBindingService } from '@cuaklabs/iocuak';

const container: Container = Container.build();

const containerModule: ContainerModule = {
load: (containerModuleBindingService: ContainerModuleBindingService): void => {
containerModuleBindingService.bind(Dummy);
},
};

container.load(containerModule);

loadMetadata(containerModuleMetadata)

Loads container module metadata

import {
Container,
ContainerModuleBindingService,
ContainerModuleMetadata,
} from '@cuaklabs/iocuak';

import { someContainerModuleMetadataDependency } from 'some/path';

async function entryPoint(): Promise<void> {
const container: Container = Container.build();

const containerModuleMetadata: ContainerModuleMetadata = {
factory: (someServiceDependency: Dependency) => ({
load: (containerModuleBindingService: ContainerModuleBindingService) => {
containerModuleBindingService.bindToValue({
serviceId: 'service-id',
value: someServiceDependency.someValue,
});
}
}),
imports: [someContainerModuleMetadataDependency],
injects: [Dependency],
}

await container.loadMetadata(containerModuleMetadata);
}

unbind(serviceId)

Unbinds a bound service by its id.