Skip to content

InjectorService

@tsed/di

Usage

typescript
import { InjectorService } from "@tsed/di";

See /packages/di/src/types/common/services/InjectorService.ts.

Overview

ts
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:

typescript
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

ts
logger: DILogger;

get settings

ts
get settings(): DIConfiguration;

set settings

ts
set settings(settings: DIConfiguration);

isLoaded

ts
isLoaded(): boolean;

toArray

ts
toArray(): any[];

Return a list of instance build by the injector.

get

ts
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

typescript
import {InjectorService} from "@tsed/di";
import MyService from "./services.js";

class OtherService {
     constructor(injectorService: InjectorService) {
         const myService = injectorService.get<MyService>(MyService);
     }
}

getMany

ts
getMany<Type = any>(type: any, options?: Partial<InvokeOptions>): Type[];

Return all instance of the same provider type

has

ts
has(token: TokenProvider): boolean;

The has() method returns a boolean indicating whether an element with the specified key exists or not.

alias

ts
alias(token: TokenProvider, alias: TokenProvider): this;

Declare an alias for a given token.

resolve

ts
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

typescript
import {InjectorService} from "@tsed/di";
import MyService from "./services.js";

class OtherService {
    constructor(injectorService: InjectorService) {
         const myService = injectorService.invoke<MyService>(MyService);
     }
 }

invoke

ts
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

typescript
import {InjectorService} from "@tsed/di";
import MyService from "./services.js";

class OtherService {
    constructor(injectorService: InjectorService) {
         const myService = injectorService.invoke<MyService>(MyService);
     }
 }

loadAsync

ts
loadAsync(): Promise<void>;

Build only providers which are asynchronous.

loadSync

ts
loadSync(): void;

Build only providers which are synchronous.

load

ts
load(container?: Container): Promise<void>;

Build all providers from given container (or GlobalProviders) and emit $onInit event.

resolveConfiguration

ts
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

ts
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

ts
alter<T = any>(eventName: string, value: any, ...args: any[]): T;

Alter value attached to an event.

alterAsync

ts
alterAsync<T = any>(eventName: string, value: any, ...args: any[]): Promise<T>;

Alter value attached to an event asynchronously.

destroy

ts
destroy(): Promise<void>;

Destroy the injector and all services.

protected bootstrap

ts
protected bootstrap(container?: Container): this;

Bootstrap injector from container, resolve configuration and providers.

protected ensureProvider

ts
protected ensureProvider(token: TokenProvider, force: true): Provider;

Ensure that a provider is added to the container.

protected ensureProvider

ts
protected ensureProvider(token: TokenProvider, force: false): Provider | undefined;

protected ensureProvider

ts
protected ensureProvider(token: TokenProvider): Provider | undefined;

protected invokeToken

ts
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 the InjectorService is consulted.

Example

Released under the MIT License.