//#region src/tokens.d.ts /** * @ignore * Binding token for mandatory value */ type RequiredToken = { symbol: symbol; type?: T; isOptional?: false; }; /** * @ignore * Binding token for optional value */ type OptionalToken = { symbol: symbol; type?: T; isOptional: true; optionalValue: T; }; /** * Binding token */ type Token = RequiredToken | OptionalToken; /** * Token options */ type TokenOptions = { /** * Key for token's symbol. It allows to create shareable tokens. */ key: string; /** @ignore */ description?: undefined; } | { /** Description for better error messages */description?: string; /** @ignore */ key?: undefined; }; /** * Creates a new binding token. * @param description - Token description for better error messages. */ declare function token(description?: string): Token; /** * Creates a new binding token. * @param options - Token description for better error messages. */ declare function token(options?: TokenOptions): Token; /** * Decorate a token with an optional value. * This value is be used as default value in case a container does not have registered token. * @param token - Existed token. * @param optionalValue - Default value for the resolver. */ declare function optional(token: Token, optionalValue: T): OptionalToken; declare function optional(token: Token): OptionalToken; //#endregion //#region src/container.d.ts /** * ResolverError is thrown by the resolver when a token is not found in a container. */ declare class ResolverError extends Error { constructor(message: string); } /** * @see https://github.com/mnasyrov/ditox#factory-lifetimes */ type FactoryScope = 'scoped' | 'singleton' | 'transient'; /** * Options for factory binding. * * `scope` types: * - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function. * - `scoped` - The value is created and cached by the nearest container which owns the factory function. * - `transient` - The value is created every time it is resolved. * * `scoped` and `singleton` scopes can have `onRemoved` callback. It is called when a token is removed from the container. */ type FactoryOptions = { scope?: 'scoped' | 'singleton'; onRemoved?: (value: T) => void; } | { scope: 'transient'; }; /** * Dependency container. */ type Container = { /** * Binds a value for the token */ bindValue(token: Token, value: T): void; /** * Binds a factory for the token. */ bindFactory(token: Token, factory: (container: Container) => T, options?: FactoryOptions): void; /** * Checks if the token is registered in the container hierarchy. */ hasToken(token: Token): boolean; /** * Returns a resolved value by the token, or returns `undefined` in case the token is not found. */ get(token: Token): T | undefined; /** * Returns a resolved value by the token or throws `ResolverError` in case the token is not found. */ resolve(token: Token): T; /** * Removes a binding for the token. */ remove(token: Token): void; /** * Removes all bindings in the container. */ removeAll(): void; }; /** * A subset of Container interface that provides read-only access to dependency resolution. * This type is used for parent containers to allow token resolution without exposing mutation methods. */ type ContainerResolver = Pick; /** * Creates a new dependency container. * * Container can have an optional parent to chain token resolution. The parent is used in case the current container does not have a registered token. * * @param parentArg - Optional parent container or an array of containers. */ declare function createContainer(parentArg?: ContainerResolver | ReadonlyArray): Container; //#endregion //#region src/modules.d.ts type AnyObject = Record; type EmptyObject = Record; type ModuleController = { /** Dispose the module and clean its resources */destroy?: () => void; }; /** * Dependency module * * @example * ```ts * type LoggerModule = Module<{ * logger: Logger; * }>; * ``` */ type Module = ModuleController & ModuleProps; type GetModuleProps = T extends Module ? Props : never; /** * Description how to bind the module in declarative way. * * @example * ```ts * const LOGGER_MODULE: ModuleDeclaration = { * token: LOGGER_MODULE_TOKEN, * factory: (container) => { * const transport = container.resolve(TRANSPORT_TOKEN).open(); * return { * logger: { log: (message) => transport.write(message) }, * destroy: () => transport.close(), * } * }, * exports: { * logger: LOGGER_TOKEN, * }, * }; * ``` */ type ModuleDeclaration> = { /** Token for the module */token: Token; /** Modules for binding */ imports?: ReadonlyArray; /** Factory of the module */ factory: (container: Container) => T; /** Dictionary of module properties which are bound to tokens. */ exports?: { [K in keyof GetModuleProps]?: Token[K]> }; /** Callback could be used to prepare an environment. It is called before binding the module. */ beforeBinding?: (container: Container) => void; /** Callback could be used to export complex dependencies from the module. It is called after binding the module. */ afterBinding?: (container: Container) => void; /** * Strategy for executing the factory: * - `lazy` - **This is the default**. The factory is called when the module is resolved. * - `eager` - The factory is called immediately after the module is bound to the container. */ strategy?: 'eager' | 'lazy'; }; type AnyModuleDeclaration = ModuleDeclaration>; /** * Options for module binding. * * `scope` types: * - `singleton` - **This is the default**. The value is created and cached by the most distant parent container which owns the factory function. * - `scoped` - The value is created and cached by the nearest container which owns the factory function. */ type BindModuleOptions = { scope?: 'scoped' | 'singleton'; }; type ModuleDeclarationWithOptions = { module: ModuleDeclaration; options: BindModuleOptions; }; type ModuleBindingEntry = ModuleDeclaration | ModuleDeclarationWithOptions; /** * Binds the dependency module to the container * @param container - Dependency container. * @param moduleDeclaration - Declaration of the dependency module. * @param options - Options for module binding. * * @example * ```ts * bindModule(container, LOGGER_MODULE); * ``` */ declare function bindModule>(container: Container, moduleDeclaration: ModuleDeclaration, options?: BindModuleOptions): void; /** * Binds dependency modules to the container * * @param container - Dependency container for binding * @param modules - Array of module binding entries: module declaration or `{module: ModuleDeclaration, options: BindModuleOptions}` objects. */ declare function bindModules(container: Container, modules: ReadonlyArray): void; /** * Declares a module binding * * @param declaration - a module declaration * @param declaration.token - optional field * * @example * ```ts * const LOGGER_MODULE = declareModule({ * factory: (container) => { * const transport = container.resolve(TRANSPORT_TOKEN).open(); * return { * logger: { log: (message) => transport.write(message) }, * destroy: () => transport.close(), * } * }, * exports: { * logger: LOGGER_TOKEN, * }, * }); * ``` */ declare function declareModule>(declaration: Omit, 'token'> & Partial, 'token'>>): ModuleDeclaration; /** * @deprecated Use `declareModule` instead * * Declares bindings of several modules * * @param modules - module declaration entries */ declare function declareModuleBindings(modules: ReadonlyArray): ModuleDeclaration; //#endregion //#region src/utils.d.ts type ValuesProps = { [key: string]: unknown; }; type TokenProps = { [K in keyof Props]: Token }; /** * Checks if a value is the token */ declare function isToken(value: unknown): value is Token; /** * Rebinds the array by the token with added new value. * @param container - Dependency container. * @param token - Token for an array of values. * @param value - New value which is added to the end of the array. */ declare function bindMultiValue(container: Container, token: Token>, value: T): void; /** * Tries to resolve a value by the provided token. * * If an argument is an object which has tokens as its properties, * then returns an object containing resolved values as properties. * If a token is not found, then `undefined` value is used. * * @example * ```ts * const value = tryResolveValue(container, tokenA); * console.log(value); // 1 * * const props = tryResolveValue(container, {a: tokenA, b: tokenB}); * console.log(props); // {a: 1, b: 2} * ``` */ declare function tryResolveValue | { [key: string]: Token; }, Values extends (Tokens extends Token ? V | undefined : Tokens extends TokenProps ? Partial : never)>(container: Container, token: Tokens): Values; /** * Returns an array of resolved values or objects with resolved values. * * If an item of the array is an object which has tokens as its properties, * then returns an object containing resolved values as properties. * If a token is not found, then `undefined` value is used. * * @example * ```ts * const items1 = tryResolveValues(container, tokenA); * console.log(items1); // [1] * * const items2 = tryResolveValues(container, tokenA, {a: tokenA, b: tokenB}); * console.log(items2); // [1, {a: 1, b: 2}] * ``` */ declare function tryResolveValues | { [key: string]: Token; })[], Values extends { [K in keyof Tokens]: Tokens[K] extends Token ? V | undefined : Tokens[K] extends TokenProps ? Partial : never }>(container: Container, ...tokens: Tokens): Values; /** * Resolves a value by the provided token. * * If an argument is an object which has tokens as its properties, * then returns an object containing resolved values as properties. * If a value is not found by the token, then `ResolverError` is thrown. * * @example * ```ts * const value = resolveValue(container, tokenA); * console.log(value); // 1 * * const props = resolveValue(container, {a: tokenA, b: tokenB}); * console.log(props); // {a: 1, b: 2} * ``` */ declare function resolveValue | { [key: string]: Token; }, Values extends (Tokens extends Token ? V : Tokens extends TokenProps ? Props : never)>(container: Container, token: Tokens): Values; /** * Returns an array of resolved values or objects with resolved values. * * If an item of the array is an object which has tokens as its properties, * then returns an object containing resolved values as properties. * If a token is not found, then `ResolverError` is thrown. * * @example * ```ts * const items1 = resolveValues(container, tokenA); * console.log(items1); // [1] * * const items2 = resolveValues(container, tokenA, {a: tokenA, b: tokenB}); * console.log(items2); // [1, {a: 1, b: 2}] * ``` */ declare function resolveValues | { [key: string]: Token; })[], Values extends { [K in keyof Tokens]: Tokens[K] extends Token ? V : Tokens[K] extends TokenProps ? Props : never }>(container: Container, ...tokens: Tokens): Values; /** * Decorates a factory by passing resolved values as factory arguments. * * If an argument is an object which has tokens as its properties, * then returns an object containing resolved values as properties. * * @param factory - A factory. * @param tokens - Tokens which correspond to factory arguments. * * @return Decorated factory which takes a dependency container as a single argument. */ declare function injectable | { [key: string]: Token; })[], Values extends { [K in keyof Tokens]: Tokens[K] extends Token ? V : Tokens[K] extends TokenProps ? Props : never }, Result>(this: unknown, factory: (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result; /** * Decorates a class by passing resolved values as arguments to its constructor. * * If an argument is an object which has tokens as its properties, * then returns an object containing resolved values as properties. * * @param constructor - Constructor of a class * @param tokens - Tokens which correspond to constructor arguments * * @return A factory function which takes a dependency container as a single argument * and returns a new created class. */ declare function injectableClass | { [key: string]: Token; })[], Values extends { [K in keyof Tokens]: Tokens[K] extends Token ? V : Tokens[K] extends TokenProps ? Props : never }, Result>(this: unknown, constructor: new (...params: Values) => Result, ...tokens: Tokens): (container: Container) => Result; //#endregion export { type AnyModuleDeclaration, type BindModuleOptions, type Container, type ContainerResolver, type FactoryOptions, type FactoryScope, type Module, type ModuleBindingEntry, type ModuleDeclaration, type OptionalToken, type RequiredToken, ResolverError, type Token, bindModule, bindModules, bindMultiValue, createContainer, declareModule, declareModuleBindings, injectable, injectableClass, isToken, optional, resolveValue, resolveValues, token, tryResolveValue, tryResolveValues }; //# sourceMappingURL=index.d.ts.map