UNPKG

@mooncake/container

Version:

DI(dependency injection) container for JavaScript and TypeScript.

93 lines (92 loc) 3.31 kB
import { BindOption, Constructor, ContainerBinder, ContainerResolver, Factory as TypeFactory, ID, InjectOption } from './types'; export declare const SYMBOL_INJECT_META: unique symbol; export declare const SYMBOL_BIND_META: unique symbol; export declare type ResolveMethod = (container: ContainerResolver, scope?: string) => any; export interface InjectMeta { required: boolean; index?: number; property?: string; scope?: string; resolver?: Function; id?: any; } export declare function getInjectMetas(target: Function): InjectMeta[]; export declare type BindMethod<T = any> = (clz: Constructor<T>, container: ContainerBinder) => void; export declare function getBindActions(target: Function): BindMethod<any>[]; export declare type OptionalInjectOption = Pick<InjectOption, Exclude<keyof InjectOption, "required">>; /** * Inject optional, undefined will be inject when can not resolve dependency * * @export * @param {ID<any>} [id] * @returns */ export declare function InjectOptional(option?: ID<any> | (OptionalInjectOption & { id?: ID<any>; })): (target: any, prop?: any, index?: any) => void; /** * Inject with container * * @export * @param {ID<any>} [id] * @returns */ export declare function Inject(option?: ID<any> | (InjectOption & { id?: ID<any>; })): (target: any, prop?: any, index?: any) => void; /** * Inject with custom rules * * @export * @param {(container: ContainerResolver) => any} resolve * @returns */ export declare function InjectRaw(resolveMethod: ResolveMethod, opt?: InjectOption): (target: any, prop?: string | undefined, index?: any) => void; /** * customize bind Action * * @export * @template T * @param {BindMethod<T>} action * @returns */ export declare function BindAction<T extends Function>(action: BindMethod<T>): (target: T) => void; export declare function Alias(id: symbol | string | Function, fromId?: symbol | string | Function, opt?: BindOption): (target: any) => void; export declare function Factory<T>(factory: Constructor<TypeFactory<T>> | TypeFactory<T>, opt?: BindOption): (target: Constructor<T>) => void; /** * bind a implement class for this class * * @export * @template T * @param {() => Constructor<T>} implementClassGetter * @param {BindOption} [opt] * @returns */ export declare function Implement<T>(implementClassGetter: () => Constructor<T>, opt?: BindOption): (target: Constructor<T>) => void; /** * mark class as a Service * default opt: {singleton: true, scope: 'root'} * * @export * @param {BindOption} [opt] * @returns {(cls: Function) => void} */ export declare function Service(opt?: BindOption): (cls: Function) => void; /** * mark class as a Service with a id * default opt: {singleton: true, scope: 'root'} * * @export * @param {(symbol | string | Function)} id * @param {BindOption} [opt] * @returns {(cls: Function) => void} */ export declare function Service(id: symbol | string | Function, opt?: BindOption): (cls: Function) => void; /** * alias of Service. singleton alwas be true * * @export * @param {Pick<BindOption, 'scope'>} [opt={ scope: 'root' }] * @returns */ export declare function Singleton(opt?: Pick<BindOption, 'scope'>): (cls: Function) => void;