|
|
import { DescriptorBuilder } from "./DescriptorBuilder";
|
|
|
import { ConfigurableKeys, ContainerServices, RegistrationBuildersMap, ExtractRequired, IContainerBuilder } from "./interfaces";
|
|
|
import { ServiceContainer } from "./interfaces";
|
|
|
import { argumentNotNull, each, isKey } from "./traits";
|
|
|
|
|
|
export class FluentConfiguration<S, Y extends ConfigurableKeys<S> = ConfigurableKeys<S>> {
|
|
|
|
|
|
private _builders: Partial<RegistrationBuildersMap<S>> = {};
|
|
|
|
|
|
/** Adds a declaration of the services to the current config.
|
|
|
*
|
|
|
* @template D The map of the services
|
|
|
* @returns self
|
|
|
*/
|
|
|
declare<D extends Pick<S, keyof D & keyof S>>(): FluentConfiguration<S & D, Y | ConfigurableKeys<D>> {
|
|
|
return this as FluentConfiguration<S & D, Y | ConfigurableKeys<D>>;
|
|
|
}
|
|
|
|
|
|
/** Adds compile-time information about the already provided services
|
|
|
*
|
|
|
* @template P The map of the provided services
|
|
|
* @returns self
|
|
|
*/
|
|
|
provided<P extends Pick<S, keyof P & keyof S>>(): FluentConfiguration<S & P, Exclude<Y, keyof P>> {
|
|
|
return this as FluentConfiguration<S & P, Exclude<Y, keyof P>>;
|
|
|
}
|
|
|
|
|
|
/** Register the service.
|
|
|
*
|
|
|
* @param name The name of the service
|
|
|
* @param builder The service builder
|
|
|
* @returns self
|
|
|
*/
|
|
|
register<K extends Y>(name: K, builder: RegistrationBuildersMap<S>[K]): FluentConfiguration<S, Exclude<Y, K>>;
|
|
|
/** Registers the collection of services
|
|
|
* @param config The collection of services to register.
|
|
|
* @returns self
|
|
|
*/
|
|
|
register<K extends Y>(config: RegistrationBuildersMap<S, K>): FluentConfiguration<S, Exclude<Y, K>>;
|
|
|
register<K extends Y>(nameOrConfig: K | RegistrationBuildersMap<S, K>, builder?: RegistrationBuildersMap<S>[K]) {
|
|
|
if (isKey(nameOrConfig)) {
|
|
|
argumentNotNull(builder, "builder");
|
|
|
this._builders[nameOrConfig] = builder;
|
|
|
} else {
|
|
|
each(nameOrConfig, (v, k) => this.register(k, v));
|
|
|
}
|
|
|
|
|
|
return this as FluentConfiguration<S, Exclude<Y, K>>;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* This method is used to enable a compile time check of the configuration.
|
|
|
* If there are not configured services in the configuration the compiler
|
|
|
* will trigger the error.
|
|
|
*
|
|
|
* @param missing Empty object literal should always be specified.
|
|
|
* @returns self
|
|
|
*/
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
|
done<M extends ExtractRequired<S,Y>>(missing: M) {
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
configure<T extends IContainerBuilder<S>>(builder: T) {
|
|
|
each(this._builders, (v, k) => {
|
|
|
v(builder.createServiceBuilder(k));
|
|
|
});
|
|
|
builder.build() as T & ServiceContainer<S>;
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|