| @@ -1,21 +1,41 | |||
|
|
1 | 1 | import { DescriptorBuilder } from "./DescriptorBuilder"; |
|
|
2 |
import { ConfigurableKeys, ContainerServices, ConfigurableServices, RegistrationBuildersMap, Required |
|
|
|
2 | import { ConfigurableKeys, ContainerServices, ConfigurableServices, RegistrationBuildersMap, ExtractRequired } from "./interfaces"; | |
|
|
3 | 3 | import { ServiceContainer } from "./interfaces"; |
|
|
4 | 4 | import { argumentNotNull, each, isKey } from "./traits"; |
|
|
5 | 5 | |
|
|
6 | 6 | export class FluentConfiguration<S extends object, Y extends ConfigurableKeys<S> = ConfigurableKeys<S>> { |
|
|
7 | 7 | |
|
|
8 | _builders: Partial<RegistrationBuildersMap<S>> = {}; | |
|
|
8 | private _builders: Partial<RegistrationBuildersMap<S>> = {}; | |
|
|
9 | 9 | |
|
|
10 | /** Adds a declaration of the services to the current config. | |
|
|
11 | * | |
|
|
12 | * @template D The map of the services | |
|
|
13 | * @returns self | |
|
|
14 | */ | |
|
|
10 | 15 | declare<D extends Partial<Pick<S, keyof D & keyof S>>>(): FluentConfiguration<S & D, Y | ConfigurableKeys<D>> { |
|
|
11 | 16 | return this as FluentConfiguration<S & D, Y | ConfigurableKeys<D>>; |
|
|
12 | 17 | } |
|
|
13 | 18 | |
|
|
19 | /** Adds compile-time information about the already provided services | |
|
|
20 | * | |
|
|
21 | * @template P The map of the provided services | |
|
|
22 | * @returns self | |
|
|
23 | */ | |
|
|
14 | 24 | provided<P extends Pick<S, keyof P & keyof S>>(): FluentConfiguration<S & P, Exclude<Y, keyof P>> { |
|
|
15 | 25 | return this as FluentConfiguration<S & P, Exclude<Y, keyof P>>; |
|
|
16 | 26 | } |
|
|
17 | 27 | |
|
|
28 | /** Register the service. | |
|
|
29 | * | |
|
|
30 | * @param name The name of the service | |
|
|
31 | * @param builder The service builder | |
|
|
32 | * @returns self | |
|
|
33 | */ | |
|
|
18 | 34 | register<K extends Y>(name: K, builder: RegistrationBuildersMap<S>[K]): FluentConfiguration<S, Exclude<Y, K>>; |
|
|
35 | /** Registers the collection of services | |
|
|
36 | * @param config The collection of services to register. | |
|
|
37 | * @returns self | |
|
|
38 | */ | |
|
|
19 | 39 | register<K extends Y>(config: RegistrationBuildersMap<S, K>): FluentConfiguration<S, Exclude<Y, K>>; |
|
|
20 | 40 | register<K extends Y>(nameOrConfig: K | RegistrationBuildersMap<S, K>, builder?: RegistrationBuildersMap<S>[K]) { |
|
|
21 | 41 | if (isKey(nameOrConfig)) { |
| @@ -28,9 +48,16 export class FluentConfiguration<S exten | |||
|
|
28 | 48 | return this as FluentConfiguration<S, Exclude<Y, K>>; |
|
|
29 | 49 | } |
|
|
30 | 50 | |
|
|
31 | done(missing: RequiredKeys<S, Y> extends never ? void : RequiredKeys<S, Y>) { | |
|
|
32 | if (missing !== undefined) | |
|
|
33 | throw new Error("The configuration isn't complete"); | |
|
|
51 | /** | |
|
|
52 | * This method is used to enable a compile time check of the configuration. | |
|
|
53 | * If there are not configured services in the configuration the compiler | |
|
|
54 | * will trigger the error. | |
|
|
55 | * | |
|
|
56 | * @param missing Empty object literal should always be specified. | |
|
|
57 | * @returns self | |
|
|
58 | */ | |
|
|
59 | // eslint-disable-next-line @typescript-eslint/no-unused-vars | |
|
|
60 | done<M extends ExtractRequired<S,Y>>(missing: M) { | |
|
|
34 | 61 | return this; |
|
|
35 | 62 | } |
|
|
36 | 63 | |
| @@ -117,5 +117,5 export interface ILifetime<T> { | |||
|
|
117 | 117 | store(item: T, cleanup?: (item: T) => void): void; |
|
|
118 | 118 | } |
|
|
119 | 119 | |
|
|
120 |
export type Required |
|
|
|
120 | export type ExtractRequired<T, K extends keyof T = keyof T> = { [p in K as (undefined extends T[p] ? never : p)]-?: T[p] }; | |
|
|
121 | 121 | |
General Comments 0
You need to be logged in to leave comments.
Login now
