@mdzzohrabi/container
Version:
Dependency Injection Container
134 lines (133 loc) • 4.67 kB
TypeScript
export declare function isService(object: any): object is Service;
export declare function isIDefinition(object: any): object is IServiceDefinition;
export declare function isInjectable(object: any): object is Injectable;
export declare const Name: {
isService: (name: string) => boolean;
isParameter: (name: string) => boolean;
isTag: (name: string) => boolean;
};
/**
* Dependency Injection Container
* @author Masoud Zohrabi <mdzzohrabi@gmail.com>
*/
export declare class Container {
static VERSION: 1.0;
protected services: ServicesBag;
protected parameters: ParametersBag;
constructor(services?: IServices, parameters?: ParametersBag, global?: boolean);
private loaded;
/** Clean loaded services cache */
fresh(): this;
/** Reset container */
reset(): this;
setParams(parameters: ParametersBag): this;
/** Declare multiple services */
setAll(services: IServices): this;
/** Get service definition */
getDefinition(serviceName: string): IServiceDefinition;
/** Set a service */
set(serviceName: string, service: IServiceDefinition | Function): this;
/** Set a service factory */
setFactory(serviceName: string, factory: Function, shared?: boolean): this;
/** Set container parameter */
setParam(name: string, value: any): this;
/** Check if a service has factory */
isFactory(name: string): boolean;
/** Get a parameter */
getParam<T>(name: string): T;
/** Check if a service exists */
has(serviceName: string): boolean;
/** Build and get a service */
get<T extends any>(serviceName: string): T;
/**
* Resolve dependencies
* There are three type of dependencies :
* - Tag dependency [prefixed with $$]
* - Parameter dependency [prefixed with $]
* - Service dependency
* @example
* resolve([ '$dbName', '$$loggers', 'odm' ])
* @param deps Dependencies
*/
resolve(deps: string[] | string): any;
/**
* Invoke a function or class
* @example
* invoke(( $$loggers ) => { console.log($$loggers) })
* @param func Function or class
*/
invoke<T>(func: string): T;
invoke<T>(func: new (...args) => T): T;
invoke<T>(func: (...args) => T): T;
invoke<T>(func: Function): T;
protected prepareDefinition(service: IServiceDefinition | Function | string): IServiceDefinition;
/**
* Build a service
* @param service Service definition or function
*/
protected buildService(service: IServiceDefinition | Function | string): any;
invokeLater(func: Instancable | Function, method: string): any;
getDependencies(target: Function | Service): any[];
setAsMain(): this;
readonly isGlobal: boolean;
static readonly main: Container;
}
export declare function Service(name: string, options?: ServiceDecorator): ClassDecorator;
export declare function Call(...params: any[]): MethodDecorator;
export declare const Decorator: {
isClass: (args: IArguments) => boolean;
isProperty: (args: IArguments) => boolean;
isMethod: (args: IArguments) => boolean;
isParameter: (args: IArguments) => boolean;
};
export declare function Inject(injects: string): any;
export declare function Inject(injects: Arguments | InjectDefinition): any;
export interface ServiceDecorator {
container?: Container;
arguments?: Arguments;
properties?: PropertiesInjection;
calls?: ServiceCalls;
shared?: boolean;
tags?: string[];
}
export interface InjectDefinition {
arguments?: Arguments;
calls?: ServiceCalls;
properties?: PropertiesInjection;
container?: Container;
}
export interface Injectable {
$inject: InjectDefinition;
}
export declare type ParametersBag = {
[name: string]: any;
};
export declare type ServicesBag = {
[name: string]: IServiceDefinition;
};
export declare type Service = Function | Instancable;
export declare type Arguments = any[];
export declare type ServiceCalls = {
[method: string]: Arguments;
};
export declare type PropertiesInjection = {
[property: string]: string;
};
export interface Instancable {
new (...args: any[]): any;
}
export interface IServices {
[name: string]: Service | IServiceDefinition;
}
export interface IServiceFactory {
(...params: any[]): any;
}
export interface IServiceDefinition {
target?: Service;
tags?: string[];
calls?: ServiceCalls;
parameters?: Arguments;
properties?: PropertiesInjection;
shared?: boolean;
factory?: Function;
}