FluentConfiguration.ts
72 lines
| 3.0 KiB
| video/mp2t
|
TypeScriptLexer
|
|
r15 | import { ConfigurationMap, ConfigurationMapConstraint, ContainerServices, ContainerServicesConstraint, ExtractRequiredKeys, IContainerBuilder } from "../typings/interfaces"; | ||
|
|
r1 | import { argumentNotNull, each, isKey } from "./traits"; | ||
|
|
r0 | |||
|
|
r9 | type ContainerExtensionConstraint<X, S> = ContainerServicesConstraint<Pick<S, keyof X & keyof S>>; | ||
|
|
r0 | |||
|
|
r9 | export class FluentConfiguration<S extends ContainerServicesConstraint<S>, Y extends keyof S = keyof S> { | ||
| private _builders: Partial<ConfigurationMap<ContainerServices<S>, keyof S, keyof S>> = {}; | ||||
|
|
r0 | |||
|
|
r2 | /** Adds a declaration of the services to the current config. | ||
| * | ||||
| * @template D The map of the services | ||||
| * @returns self | ||||
| */ | ||||
|
|
r9 | declare<D extends ContainerExtensionConstraint<D,S>>(): FluentConfiguration<S & D, Y | keyof D> { | ||
| return this as unknown as FluentConfiguration<S & D, Y | keyof D>; | ||||
|
|
r0 | } | ||
|
|
r2 | /** Adds compile-time information about the already provided services | ||
| * | ||||
| * @template P The map of the provided services | ||||
| * @returns self | ||||
| */ | ||||
|
|
r1 | provided<P extends Pick<S, keyof P & keyof S>>(): FluentConfiguration<S & P, Exclude<Y, keyof P>> { | ||
|
|
r9 | return this as unknown as FluentConfiguration<S & P, Exclude<Y, keyof P>>; | ||
|
|
r1 | } | ||
|
|
r2 | /** Register the service. | ||
| * | ||||
| * @param name The name of the service | ||||
| * @param builder The service builder | ||||
| * @returns self | ||||
| */ | ||||
|
|
r9 | register<K extends Y>(name: K, builder: ConfigurationMap<ContainerServices<S>,Y, keyof S>[K]): FluentConfiguration<S, Exclude<Y, K>>; | ||
|
|
r2 | /** Registers the collection of services | ||
| * @param config The collection of services to register. | ||||
| * @returns self | ||||
| */ | ||||
|
|
r9 | register<X extends ConfigurationMapConstraint<ContainerServices<S>, Y, keyof X>>(config: X): FluentConfiguration<S, Exclude<Y, keyof X>>; | ||
| register<K extends Y>(nameOrConfig: K | ConfigurationMap<ContainerServices<S>, K, keyof S>, builder?: ConfigurationMap<ContainerServices<S>, Y, keyof S>[K]) { | ||||
|
|
r1 | if (isKey(nameOrConfig)) { | ||
|
|
r0 | argumentNotNull(builder, "builder"); | ||
| this._builders[nameOrConfig] = builder; | ||||
| } else { | ||||
| each(nameOrConfig, (v, k) => this.register(k, v)); | ||||
| } | ||||
|
|
r9 | return this; // as FluentConfiguration<S, Exclude<Y, K>>; | ||
|
|
r1 | } | ||
|
|
r2 | /** | ||
| * 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 | ||||
|
|
r9 | done(...args: ExtractRequiredKeys<S, Y> extends never ? [] : [services: {[ k in ExtractRequiredKeys<S, Y>]: "required"}]) { | ||
| //done() { | ||||
|
|
r0 | return this; | ||
| } | ||||
|
|
r9 | configure<C extends IContainerBuilder<ContainerServices<S>, keyof S>>(builder: C) { | ||
|
|
r1 | each(this._builders, (v, k) => { | ||
|
|
r5 | v(builder.createServiceBuilder(k)); | ||
|
|
r0 | }); | ||
|
|
r9 | return builder.build(); | ||
|
|
r0 | } | ||
| } | ||||
