UNPKG

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.

32 lines (31 loc) 2.04 kB
import type { DependencyKey, IContainer, IContainerModule } from '../container/IContainer'; import type { ArgsFn, DecorateFn, GetCacheKey, IProvider, ScopeAccessRule } from '../provider/IProvider'; import { BindToken } from '../token/BindToken'; import { MapFn } from '../utils/fp'; import { type constructor } from '../utils/basic'; export type ScopeMatchRule = (s: IContainer, prev: boolean) => boolean; export interface ProviderPipe<T = unknown> { mapProvider(p: IProvider<T>): IProvider<T>; mapRegistration(r: IRegistration<T>): IRegistration<T>; } export declare const isProviderPipe: <T>(obj: unknown) => obj is ProviderPipe<T>; export declare const registerPipe: <T>(mapProvider: (p: IProvider<T>) => IProvider<T>) => ProviderPipe<T>; export interface IRegistration<T = any> extends IContainerModule { getKeyOrFail(): DependencyKey; when(...predicates: ScopeMatchRule[]): this; bindToKey(key: DependencyKey): this; bindTo(key: DependencyKey | BindToken): this; pipe(...mappers: (MapFn<IProvider<T>> | ProviderPipe<T>)[]): this; bindToAlias(alias: DependencyKey): this; } export type ReturnTypeOfRegistration<T> = T extends IRegistration<infer R> ? R : never; export declare const getTransformers: (Target: constructor<unknown>) => MapFn<IRegistration<any>>[]; export declare const register: (...mappers: Array<MapFn<IRegistration> | ProviderPipe>) => ClassDecorator; export declare const bindTo: (...tokens: (DependencyKey | BindToken)[]) => MapFn<IRegistration>; export declare const scope: (...rules: ScopeMatchRule[]) => MapFn<IRegistration>; export declare const appendArgs: <T>(...extraArgs: unknown[]) => ProviderPipe<T>; export declare const appendArgsFn: <T>(fn: ArgsFn) => ProviderPipe<T>; export declare const scopeAccess: <T>(rule: ScopeAccessRule) => ProviderPipe<T>; export declare const lazy: <T>() => ProviderPipe<T>; export declare const decorate: (...fns: DecorateFn[]) => ProviderPipe<unknown>; export declare const singleton: <T = unknown>(getCacheKey?: GetCacheKey) => ProviderPipe<T>;