InjectorService Service

Module
import { InjectorService } from "@tsed/common"
Source/packages/common/src/di/services/InjectorService.ts

Overview

class InjectorService extends Map<;RegistryKey, Provider<;any>;>; {
    constructor();
    readonly settings: any;
    get<;T>;(target: Type<;T>; | symbol | any): T | undefined;
    has(key: RegistryKey): boolean;
    getProvider(key: RegistryKey): Provider<;any>; | undefined;
    forkProvider(key: RegistryKey, instance?: any): Provider<;any>;;
    getProviders(type?: ProviderType | string): Provider<;any>;[];
    invoke<;T>;(target: any, locals?: Map<;string | Function, any>;, designParamTypes?: any[], requiredScope?: boolean): T;
    invokeMethod(handler: any, options: IInjectableMethod<;any>;): any;
    load(): Promise<;any>;;
    emit(eventName: string, ...args: any[]): Promise<;void>;;
    static invoke<;T>;(target: any, locals?: Map<;string | Function, any>;, designParamTypes?: any[], requiredScope?: boolean): T;
    static construct<;T>;(target: Type<;any>; | symbol): T;
    static emit(eventName: string, ...args: any[]): Promise<;any>;;
    static get<;T>;(target: RegistryKey): T;
    static invokeMethod(handler: any, options: IInjectableMethod<;any>; | any[]): any;
    static set(provider: IProvider<;any>; | any, instance?: any): typeof InjectorService;
    static has(target: any): boolean;
    static load(): Promise<;any>;;
    static service(target: any): void;
    static factory(target: any, instance: any): void;
}

Description

This service contain all services collected by @Service or services declared manually with InjectorService.factory() or InjectorService.service().

Example:

import {InjectorService} from "@tsed/common";

// Import the services (all services are decorated with @Service()";
import MyService1 from "./services/service1";
import MyService2 from "./services/service2";
import MyService3 from "./services/service3";

// When all services is imported you can load InjectorService.
const injector = new InjectorService()
injector.load();

const myService1 = injector.get<MyService1>(MyServcice1);

Note: ServerLoader make this automatically when you use ServerLoader.mount() method (or settings attributes) and load services and controllers during the starting server.

Members

readonly settings: any

get<;T>;(target: Type<;T>; | symbol | any): T | undefined
Param Type Description
target <a href="/api/core/interfaces/Type.html"><span class="token">Type</span></a><T> | symbol | any The class or symbol registered in InjectorService.

Get a service or factory already constructed from his symbol or class.

Example

import {InjectorService} from "@tsed/common";
import MyService from "./services";

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

has(key: RegistryKey): boolean

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


getProvider(key: RegistryKey): Provider<;any>; | undefined
Param Type Description
key <a href="/api/core/class/RegistryKey.html"><span class="token">RegistryKey</span></a> Required. The key of the element to return from the Map object.

The getProvider() method returns a specified element from a Map object.


forkProvider(key: RegistryKey, instance?: any): Provider<;any>;

getProviders(type?: ProviderType | string): Provider<;any>;[]

invoke<;T>;(target: any, locals?: Map<;string | Function, any>;, designParamTypes?: any[], requiredScope?: boolean): T
Param Type Description
target any The injectable class to invoke. Class parameters are injected according constructor signature. locals

Invoke the class and inject all services that required by the class constructor.

Example

import {InjectorService} from "@tsed/common";
import MyService from "./services";

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

invokeMethod(handler: any, options: IInjectableMethod<;any>;): any
Param Type Description
handler any The injectable method to invoke. Method parameters are injected according method signature. options

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

import {InjectorService} from "@tsed/common";

class MyService {
     constructor(injectorService: InjectorService) {
         injectorService.invokeMethod(this.method, {
             this,
             methodName: 'method'
         });
     }

  method(otherService: OtherService) {}
}

load(): Promise<;any>;

Initialize injectorService and load all services/factories.


emit(eventName: string, ...args: any[]): Promise<;void>;
Param Type Description
eventName string The event name to emit at all services. args

Emit an event to all service. See service lifecycle hooks.


static invoke<;T>;(target: any, locals?: Map<;string | Function, any>;, designParamTypes?: any[], requiredScope?: boolean): T
Param Type Description
target any The injectable class to invoke. Class parameters are injected according constructor signature. locals

Invoke the class and inject all services that required by the class constructor.

Example

import {InjectorService} from "@tsed/common";
import MyService from "./services";

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

static construct<;T>;(target: Type<;any>; | symbol): T
Param Type Description
target <a href="/api/core/interfaces/Type.html"><span class="token">Type</span></a><any> | symbol The service to be built.

Construct the service with his dependencies.


static emit(eventName: string, ...args: any[]): Promise<;any>;
Param Type Description
eventName string The event name to emit at all services. args

Emit an event to all service. See service lifecycle hooks.


static get<;T>;(target: RegistryKey): T
Param Type Description
target <a href="/api/core/class/RegistryKey.html"><span class="token">RegistryKey</span></a> The class or symbol registered in InjectorService.

Get a service or factory already constructed from his symbol or class.

Example

import {InjectorService} from "@tsed/common";
import MyService from "./services";

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

static invokeMethod(handler: any, options: IInjectableMethod<;any>; | any[]): any
Param Type Description
handler any The injectable method to invoke. Method parameters are injected according method signature. options

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

import {InjectorService} from "@tsed/common";

class MyService {
     constructor(injectorService: InjectorService) {
         injectorService.invokeMethod(this.method.bind(this), {
             target: this,
             methodName: 'method'
         });
     }

  method(otherService: OtherService) {}
}

static set(provider: IProvider<;any>; | any, instance?: any): typeof InjectorService
Param Type Description
provider <a href="/api/common/di/interfaces/IProvider.html"><span class="token">IProvider</span></a><any> | any provide token. instance

Set a new provider from providerSetting.


static has(target: any): boolean
Param Type Description
target any The service class

Check if the service of factory exists in InjectorService.

Example

import {InjectorService} from "@tsed/common";
import MyService from "./services";

class OtherService {
   constructor(injectorService: InjectorService) {
       const exists = injectorService.has(MyService); // true or false
   }
}

static load(): Promise<;any>;

Initialize injectorService and load all services/factories.


static service(target: any): void
Param Type Description
target any The class to add in registry.

Add a new service in the registry. This service will be constructed when InjectorServicewill loaded.

Example

import {InjectorService} from "@tsed/common";

export default class MyFooService {
    constructor(){}
    getFoo() {
        return "test";
    }
}

InjectorService.service(MyFooService);
const injector = new InjectorService();
injector.load();

const myFooService = injector.get<MyFooService>(MyFooService);
myFooService.getFoo(); // test

static factory(target: any, instance: any): void

Add a new factory in InjectorService registry.

Example with symbol definition

import {InjectorService} from "@tsed/common";

export interface IMyFooFactory {
   getFoo(): string;
}

export type MyFooFactory = IMyFooFactory;
export const MyFooFactory = Symbol("MyFooFactory");

InjectorService.factory(MyFooFactory, {
     getFoo:  () => "test"
});

@Service()
export class OtherService {
     constructor(@Inject(MyFooFactory) myFooFactory: MyFooFactory){
         console.log(myFooFactory.getFoo()); /// "test"
     }
}

Note: When you use the factory method with Symbol definition, you must use the @Inject() decorator to retrieve your factory in another Service. Advice: By convention all factory class name will be prefixed by Factory.

Example with class

import {InjectorService} from "@tsed/common";

export class MyFooService {
 constructor(){}
     getFoo() {
         return "test";
     }
}

InjectorService.factory(MyFooService, new MyFooService());

@Service()
export class OtherService {
     constructor(myFooService: MyFooService){
         console.log(myFooFactory.getFoo()); /// "test"
     }
}