ts-ioc-container
Version:
Fast, lightweight TypeScript dependency injection container with a clean API, scoped lifecycles, decorators, tokens, hooks, lazy injection, customizable providers, and no global container objects.
27 lines (26 loc) • 1.14 kB
TypeScript
import { IContainer, Tagged } from '../container/IContainer';
import { InjectOptions } from '../injector/IInjector';
export type WithLazy = {
lazy: boolean;
};
export type ProviderOptions = InjectOptions & Partial<WithLazy>;
export type ResolveDependency<T = unknown> = (container: IContainer, options: ProviderOptions) => T;
export type ScopeAccessOptions = {
invocationScope: Tagged;
providerScope: Tagged;
args: unknown[];
};
export type ScopeAccessRule = (options: ScopeAccessOptions, prev: boolean) => boolean;
export type ArgsFn = (l: IContainer, options?: InjectOptions) => unknown[];
export type GetCacheKey = (...args: unknown[]) => string | symbol;
export type DecorateFn<Instance = any> = (dep: Instance, scope: IContainer) => Instance;
export interface IProvider<T = any> {
resolve(container: IContainer, options: ProviderOptions): T;
hasAccess(options: ScopeAccessOptions): boolean;
map(...mappers: DecorateFn<T>[]): this;
addAccessRule(...rules: ScopeAccessRule[]): this;
addArgsFn(argsFn: ArgsFn): this;
lazy(): this;
singleton(getCacheKey?: GetCacheKey): this;
dispose(): void;
}