Skip to main content

API

1. Decorators#

/** * Make class's dependencies become injectable.   * *Technically, typescript require using a decorator to emit type.* */@Injectable()
/** * Allow to inject dependency with specific token.   * *For now, it only allow to inject dependency via constructor.* */@Inject(token: Token)
/** * Config module meta data. */@Module(opts: ModuleOpts)

2. Container methods#

/** * Convenient method for adding multiple providers into this container. */addProviders(providers: GenericProvider[]): Promise<void>;
/** * Add a provider into this container for resolving dependency later. */addProvider(provider: GenericProvider): Promise<void>;
/** * Convenient method for adding multiple modules into this container. */addModules(modules: (ImportableCnf | ExportableCnf)[]): Promise<void>;
/** * Add a module into this container. */addModule(module: ImportableCnf | ExportableCnf): Promise<void>;
/** * Check if a provider with given token exists in this container. */has(token: Token): boolean;
/** * Remove provider from this container by token. */remove(token: Token): void;
/** * Resolve value by token. */resolve<T>(token: Token, options?: ResolveOptions): Promise<T>;

3. Types#

3.1. GenericProvider#

GenericProvider<T = any> = UseModuleProvider | UseClassProvider<T> | ClassType<T> | UseFuncProvider<T> | UseValueProvider<T>:

NameTypeRequiredDefaultCacheableDescription
tokenTokenTrueUnique identifier for a provider inside a container.
useValueValueTypeDependingYesn't :vUse a value as dependency value. Actually, you will get same value every time resolve() is invoked.
useFuncFuncTypeDependingYesUse a function as factory to assemble dependency value.
useClassClassTypeDependingYesUse a class to resolve dependency value.
useModuleClassTypeDependingUse a module as reference to resolve dependency value without import that module into current container.
cycleCycleTypeFalse'transient'Resolved value life cycle. It is only used for useClass, useFunc provider.
- 'permanent': resolved value will be cached.
- 'transient': resolved value will not be cached.
depsany[]FalseDependencies for useFunc provider.
*Notice:
- Dependencies's order must be same as useFunc parameter order.
- Class will be used as a token for UseClassProvider provider, so you need to define its provider.

3.2. ModuleOpts#

NameTypeRequired
providersGenericProvider[]False
importsImportableCnf[]False
exportsExportableCnf[]False

3.3. ExtModuleOpts#

ExtModuleOpts extends ModuleOpts

NameTypeRequiredDescription
extModuleClassTypeTrueModule class that you want to extend

3.4. ResolveOptions#

NameTypeRequiredDescription
extModuleContainerFalse

3.5. Others#

ClassType<T> = { new(...args: any[]): T };

FuncType<T> = (...args: any[]) => T;

ValueType<T> = T;

Token = any;

ImportableCnf = ClassType<any> | ExtModuleOpts;

ExportableCnf = ClassType<any> | ExtModuleOpts;

CycleType = 'permanent' | 'transient';