angular-style-injector
Version:
Dependency injection container inspired by Angular's Injector.
82 lines (81 loc) • 4.33 kB
TypeScript
import { ExtractOutputValue, InjectOptions, ProviderConfig, ProviderToken } from './injector.interface';
export declare class Injector {
private readonly providers;
private readonly resolvers;
private parent?;
private name?;
/**
* @description Creates a new instance of the `Injector` class.
*
* @param config Configuration object used to initialize the injector.
* - `providers`: An array of provider definitions used to resolve dependencies.
* - `parent` (optional): An optional parent injector to fall back to when a provider is not found locally.
* - `name` (optional): A developer-defined name used for debugging and error reporting.
* @returns A new `Injector` instance configured with the given providers and options.
*
* @remarks If no providers are passed, a warning will be logged to the console.
**/
static create(config: {
providers: ProviderConfig[];
parent?: Injector;
name?: string;
}): Injector;
/**
* @description Retrieves an instance from the injector based on the provided token.
*
* @param token The provider token used to retrieve the instance.
* @param notFoundValue A fallback value to return if the token is not provided by this injector or its parents.
* @param options Configuration object that influences how a dependency is resolved by the injector.
* - `optional` (optional): If true, the injector returns `null` instead of throwing an error when the token was not found.
* - `skipSelf` (optional): If true, the injector skips checking itself and instead delegates resolution to its parent (if presents).
* - `self` (optional): If true, the injector only looks for the dependency in itself and does not check the parent injector (if presents).
* @returns The resolved instance associated with the token.
* @throws Error If the provider cannot be found in the current or parent injectors, and neither a `notFoundValue`
* nor the `optional` flag was provided in the options.
*
* @remarks If the token is not found in the current injector,
* the method delegates resolution to the parent injector (if present).
**/
get<T extends ProviderToken, Output extends ExtractOutputValue<T>>(token: T, notFoundValue?: Output, options?: InjectOptions): Output;
/**
* @description This method was extracted as a separate method to preserve
* the original injector's name `originName` and `notFoundValue` during
* recursive resolution through the parent injector chain.
**/
private internalGet;
/**
* @description Registers a provider in the injector.
* Handles both single and multi-provider configurations.
* If a multi provider is added for an existing token, it merges the configurations into an array.
* Clears any previously resolved instance for the given token to allow proper re resolution.
*
* @param providerConfig The provider configuration to register.
**/
private provide;
/**
* @description Resolves a provider by its token and stores the result in the internal cache (`resolvers`).
*
* @param token The token used to look up the provider.
* @param provider The actual provider configuration(s) associated with the token.
* @param originName The name of the injector that initiated the resolution.
**/
private resolve;
/**
* @description Resolves a single provider configuration into its actual value or instance.
*
* @param providerConfig The configuration object or class constructor to resolve.
* @param originName The name of the injector that initiated the resolution.
* @returns The resolved instance or value for the provider.
**/
private getResolvedSingleProvider;
/**
* @description Creates an instance of a dependency by resolving its constructor dependencies.
* Uses `Reflect.getMetadata` to retrieve the list of dependencies defined in the constructor
* and recursively resolves each dependency.
*
* @param constructor A class constructor
* @param originName The name of the injector that initiated the resolution.
* @return The class instance with resolved dependencies.
**/
private createClassInstance;
}