InjectorService
Usage
import { InjectorService } from "@tsed/di";
See /packages/di/src/types/common/services/InjectorService.ts.
Overview
class InjectorService extends Container {
logger: DILogger;
constructor();
get settings(): DIConfiguration;
set settings(settings: DIConfiguration);
isLoaded(): boolean;
toArray(): any[];
get<T = any>(token: TokenProvider<T>, options?: Partial<InvokeOptions>): T;
getMany<Type = any>(type: any, options?: Partial<InvokeOptions>): Type[];
has(token: TokenProvider): boolean;
alias(token: TokenProvider, alias: TokenProvider): this;
resolve<Type = any>(token: TokenProvider<Type>, options?: Partial<InvokeOptions>): Type;
invoke<Type = any>(token: TokenProvider<Type>, options?: Partial<InvokeOptions>): Type;
loadAsync(): Promise<void>;
loadSync(): void;
load(container?: Container): Promise<void>;
resolveConfiguration(): void;
emit(eventName: string, ...args: unknown[]): Promise<void>;
alter<T = any>(eventName: string, value: any, ...args: any[]): T;
alterAsync<T = any>(eventName: string, value: any, ...args: any[]): Promise<T>;
destroy(): Promise<void>;
protected bootstrap(container?: Container): this;
protected ensureProvider(token: TokenProvider, force: true): Provider;
protected ensureProvider(token: TokenProvider, force: false): Provider | undefined;
protected ensureProvider(token: TokenProvider): Provider | undefined;
protected invokeToken<T>(target: TokenProvider, options?: Partial<InvokeOptions>): T | Promise<T>;
}
Description
This service contain all services collected by @Service
or services declared manually with InjectorService.factory()
or InjectorService.service()
.
Example:
import {InjectorService} from "@tsed/di";
// Import the services (all services are decorated with @Service()";
import MyService1 from "./services/service1.js";
import MyService2 from "./services/service2.js";
import MyService3 from "./services/service3.js";
// When all services are imported, you can load InjectorService.
await injector().load();
const myService1 = injector.get<MyService1>(MyService1);
logger
logger: DILogger;
get settings
get settings(): DIConfiguration;
set settings
set settings(settings: DIConfiguration);
isLoaded
isLoaded(): boolean;
toArray
toArray(): any[];
Return a list of instance build by the injector.
get
get<T = any>(token: TokenProvider<T>, options?: Partial<InvokeOptions>): T;
- token (
TokenProvider<T>
): The class or symbol registered in InjectorService.
Get a service or factory already constructed from his symbol or class.
Example
import {InjectorService} from "@tsed/di";
import MyService from "./services.js";
class OtherService {
constructor(injectorService: InjectorService) {
const myService = injectorService.get<MyService>(MyService);
}
}
getMany
getMany<Type = any>(type: any, options?: Partial<InvokeOptions>): Type[];
Return all instance of the same provider type
has
has(token: TokenProvider): boolean;
The has() method returns a boolean indicating whether an element with the specified key exists or not.
alias
alias(token: TokenProvider, alias: TokenProvider): this;
Declare an alias for a given token.
resolve
resolve<Type = any>(token: TokenProvider<Type>, options?: Partial<InvokeOptions>): Type;
token (
TokenProvider<Type>
): The injectable class to invoke. Class parameters are injected according constructor signature.options (
Partial<InvokeOptions>
): Optional. options to invoke the class.
Resolve the token depending on his provider configuration.
If the token isn't cached, the injector will invoke the provider and cache the result.
Example
import {InjectorService} from "@tsed/di";
import MyService from "./services.js";
class OtherService {
constructor(injectorService: InjectorService) {
const myService = injectorService.invoke<MyService>(MyService);
}
}
invoke
invoke<Type = any>(token: TokenProvider<Type>, options?: Partial<InvokeOptions>): Type;
token (
TokenProvider<Type>
): The injectable class to invoke. Class parameters are injected according constructor signature.options (
Partial<InvokeOptions>
): Optional. options to invoke the class.
Resolve the token depending on his provider configuration.
If the token isn't cached, the injector will invoke the provider and cache the result.
Example
import {InjectorService} from "@tsed/di";
import MyService from "./services.js";
class OtherService {
constructor(injectorService: InjectorService) {
const myService = injectorService.invoke<MyService>(MyService);
}
}
loadAsync
loadAsync(): Promise<void>;
Build only providers which are asynchronous.
loadSync
loadSync(): void;
Build only providers which are synchronous.
load
load(container?: Container): Promise<void>;
Build all providers from given container (or GlobalProviders) and emit $onInit
event.
resolveConfiguration
resolveConfiguration(): void;
Load all configurations registered on providers via @Configuration decorator. It inspects for each provider some fields like imports, mount, etc. to resolve the configuration.
emit
emit(eventName: string, ...args: unknown[]): Promise<void>;
eventName (
string
): The event name to emit at all services.args (
unknown[]
): List of the parameters to give to each service.
Emit an event to all service. See service lifecycle hooks.
alter
alter<T = any>(eventName: string, value: any, ...args: any[]): T;
Alter value attached to an event.
alterAsync
alterAsync<T = any>(eventName: string, value: any, ...args: any[]): Promise<T>;
Alter value attached to an event asynchronously.
destroy
destroy(): Promise<void>;
Destroy the injector and all services.
protected bootstrap
protected bootstrap(container?: Container): this;
Bootstrap injector from container, resolve configuration and providers.
protected ensureProvider
protected ensureProvider(token: TokenProvider, force: true): Provider;
Ensure that a provider is added to the container.
protected ensureProvider
protected ensureProvider(token: TokenProvider, force: false): Provider | undefined;
protected ensureProvider
protected ensureProvider(token: TokenProvider): Provider | undefined;
protected invokeToken
protected invokeToken<T>(target: TokenProvider, options?: Partial<InvokeOptions>): T | Promise<T>;
Invoke a class method and inject service.
IInjectableMethod options
- target: Optional. The class instance.
- methodName:
string
Optional. The method name. - designParamTypes:
any[]
Optional. List of injectable types. - locals:
Map<Function, any>
Optional. If preset then any argument Class are read from this object first, before theInjectorService
is consulted.