# InjectorService Class

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

# Overview

class InjectorService extends Container {
    settings: TsED.Configuration & DIConfiguration;
    logger: IDILogger;
<span class="token keyword">constructor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
get <span class="token function">resolvers</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">import</span><span class="token punctuation">(</span>"../interfaces"<span class="token punctuation">)</span>.<a href="/api/di/interfaces/IDIResolver.html"><span class="token">IDIResolver</span></a><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
get <span class="token function">scopes</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token punctuation">{</span>
    <span class="token punctuation">[</span>key<span class="token punctuation">:</span> <span class="token keyword">string</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/ProviderScope.html"><span class="token">ProviderScope</span></a><span class="token punctuation">;</span>
<span class="token punctuation">}</span> & <span class="token punctuation">{</span>
    <span class="token punctuation">[</span>key<span class="token punctuation">:</span> <span class="token keyword">string</span><span class="token punctuation">]</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/ProviderScope.html"><span class="token">ProviderScope</span></a><span class="token punctuation">;</span>
<span class="token punctuation">}</span><span class="token punctuation">;</span>
/**
 * Retrieve default scope for a given provider.
 * @param provider
 */
<span class="token function">scopeOf</span><span class="token punctuation">(</span>provider<span class="token punctuation">:</span> <a href="/api/di/class/Provider.html"><span class="token">Provider</span></a>&lt;<span class="token keyword">any</span>&gt;<span class="token punctuation">)</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/ProviderScope.html"><span class="token">ProviderScope</span></a><span class="token punctuation">;</span>
/**
 * Clone a provider <span class="token keyword">from</span> <a href="/api/di/registries/GlobalProviders.html"><span class="token">GlobalProviders</span></a> and the given token. forkProvider method build automatically the provider if the instance parameter ins't given.
 * @param token
 * @param instance
 */
<span class="token function">forkProvider</span><span class="token punctuation">(</span>token<span class="token punctuation">:</span> <a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">,</span> instance?<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <a href="/api/di/class/Provider.html"><span class="token">Provider</span></a><span class="token punctuation">;</span>
/**
 * Return a list of instance build by the injector.
 */
<span class="token function">toArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
/**
 * <a href="/api/common/mvc/decorators/method/Get.html"><span class="token">Get</span></a> a service or factory already constructed <span class="token keyword">from</span> his symbol or <span class="token keyword">class</span>.
 *
 * #### <a href="/api/swagger/decorators/Example.html"><span class="token">Example</span></a>
 *
 * ```typescript
 * <span class="token keyword">import</span> <span class="token punctuation">{</span>InjectorService<span class="token punctuation">}</span> <span class="token keyword">from</span> "@tsed/common"<span class="token punctuation">;</span>
 * <span class="token keyword">import</span> MyService <span class="token keyword">from</span> "./services"<span class="token punctuation">;</span>
 *
 * <span class="token keyword">class</span> OtherService <span class="token punctuation">{</span>
 *      <span class="token keyword">constructor</span><span class="token punctuation">(</span>injectorService<span class="token punctuation">:</span> InjectorService<span class="token punctuation">)</span> <span class="token punctuation">{</span>
 *          <span class="token keyword">const</span> myService<span class="token punctuation"> = </span>injectorService.get&lt;MyService&gt;<span class="token punctuation">(</span>MyService<span class="token punctuation">)</span><span class="token punctuation">;</span>
 *      <span class="token punctuation">}</span>
 * <span class="token punctuation">}</span>
 * ```
 *
 * @param token The <span class="token keyword">class</span> or symbol registered in InjectorService.
 * @param options
 * @returns <span class="token punctuation">{</span><span class="token keyword">boolean</span><span class="token punctuation">}</span>
 */
get&lt;T<span class="token punctuation"> = </span><span class="token keyword">any</span>&gt;<span class="token punctuation">(</span>token<span class="token punctuation">:</span> <a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">,</span> options?<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">)</span><span class="token punctuation">:</span> T | undefined<span class="token punctuation">;</span>
/**
 * The <span class="token function">has</span><span class="token punctuation">(</span><span class="token punctuation">)</span> method returns a <span class="token keyword">boolean</span> indicating whether an element with the specified key exists or not.
 * @returns <span class="token punctuation">{</span><span class="token keyword">boolean</span><span class="token punctuation">}</span>
 * @param token
 */
<span class="token function">has</span><span class="token punctuation">(</span>token<span class="token punctuation">:</span> <a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">boolean</span><span class="token punctuation">;</span>
/**
 * Invoke the <span class="token keyword">class</span> and inject all services that required by the <span class="token keyword">class</span> <span class="token keyword">constructor</span>.
 *
 * #### <a href="/api/swagger/decorators/Example.html"><span class="token">Example</span></a>
 *
 * ```typescript
 * <span class="token keyword">import</span> <span class="token punctuation">{</span>InjectorService<span class="token punctuation">}</span> <span class="token keyword">from</span> "@tsed/common"<span class="token punctuation">;</span>
 * <span class="token keyword">import</span> MyService <span class="token keyword">from</span> "./services"<span class="token punctuation">;</span>
 *
 * <span class="token keyword">class</span> OtherService <span class="token punctuation">{</span>
 *     <span class="token keyword">constructor</span><span class="token punctuation">(</span>injectorService<span class="token punctuation">:</span> InjectorService<span class="token punctuation">)</span> <span class="token punctuation">{</span>
 *          <span class="token keyword">const</span> myService<span class="token punctuation"> = </span>injectorService.invoke&lt;MyService&gt;<span class="token punctuation">(</span>MyService<span class="token punctuation">)</span><span class="token punctuation">;</span>
 *      <span class="token punctuation">}</span>
 *  <span class="token punctuation">}</span>
 * ```
 *
 * @param token The injectable <span class="token keyword">class</span> to invoke. Class parameters are injected according <span class="token keyword">constructor</span> signature.
 * @param locals  Optional object. If preset then <span class="token keyword">any</span> argument Class are read <span class="token keyword">from</span> this object first<span class="token punctuation">,</span> before the `InjectorService` is consulted.
 * @param options
 * @returns <span class="token punctuation">{</span>T<span class="token punctuation">}</span> The <span class="token keyword">class</span> constructed.
 */
invoke&lt;T&gt;<span class="token punctuation">(</span>token<span class="token punctuation">:</span> <a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">,</span> locals?<span class="token punctuation">:</span> Map&lt;<a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">,</span> <span class="token keyword">any</span>&gt;<span class="token punctuation">,</span> options?<span class="token punctuation">:</span> Partial&lt;<a href="/api/testing/IInvokeOptions.html"><span class="token">IInvokeOptions</span></a>&lt;T&gt;&gt;<span class="token punctuation">)</span><span class="token punctuation">:</span> T<span class="token punctuation">;</span>
/**
 * Build only providers which are asynchronous.
 */
<span class="token function">loadAsync</span><span class="token punctuation">(</span>locals?<span class="token punctuation">:</span> <a href="/api/di/class/LocalsContainer.html"><span class="token">LocalsContainer</span></a>&lt;<span class="token keyword">any</span>&gt;<span class="token punctuation">)</span><span class="token punctuation">:</span> Promise&lt;<a href="/api/di/class/LocalsContainer.html"><span class="token">LocalsContainer</span></a>&lt;<span class="token keyword">any</span>&gt;&gt;<span class="token punctuation">;</span>
<span class="token function">loadSync</span><span class="token punctuation">(</span>locals?<span class="token punctuation">:</span> <a href="/api/di/class/LocalsContainer.html"><span class="token">LocalsContainer</span></a>&lt;<span class="token keyword">any</span>&gt;<span class="token punctuation">)</span><span class="token punctuation">:</span> <a href="/api/di/class/LocalsContainer.html"><span class="token">LocalsContainer</span></a>&lt;<span class="token keyword">any</span>&gt;<span class="token punctuation">;</span>
/**
 * Build all providers <span class="token keyword">from</span> given container <span class="token punctuation">(</span>or <a href="/api/di/registries/GlobalProviders.html"><span class="token">GlobalProviders</span></a><span class="token punctuation">)</span> and emit `$onInit` event.
 *
 * @param container
 */
<span class="token function">load</span><span class="token punctuation">(</span>container?<span class="token punctuation">:</span> Map&lt;<a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">,</span> <a href="/api/di/class/Provider.html"><span class="token">Provider</span></a>&lt;<span class="token keyword">any</span>&gt;&gt;<span class="token punctuation">)</span><span class="token punctuation">:</span> Promise&lt;<a href="/api/di/class/LocalsContainer.html"><span class="token">LocalsContainer</span></a>&lt;<span class="token keyword">any</span>&gt;&gt;<span class="token punctuation">;</span>
/**
 * Load all configurations registered on providers
 */
<span class="token function">resolveConfiguration</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
/**
 *
 * @param instance
 * @param locals
 * @param options
 */
<span class="token function">bindInjectableProperties</span><span class="token punctuation">(</span>instance<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">,</span> locals<span class="token punctuation">:</span> Map&lt;<a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">,</span> <span class="token keyword">any</span>&gt;<span class="token punctuation">,</span> options<span class="token punctuation">:</span> Partial&lt;<a href="/api/testing/IInvokeOptions.html"><span class="token">IInvokeOptions</span></a>&gt;<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
/**
 *
 * @param instance
 * @param <span class="token punctuation">{</span><span class="token keyword">string</span><span class="token punctuation">}</span> propertyKey
 */
<span class="token function">bindMethod</span><span class="token punctuation">(</span>instance<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> propertyKey <span class="token punctuation">}</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/IInjectablePropertyService.html"><span class="token">IInjectablePropertyService</span></a><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
/**
 *
 * @param instance
 * @param <span class="token punctuation">{</span><span class="token keyword">string</span><span class="token punctuation">}</span> propertyKey
 * @param <span class="token punctuation">{</span><span class="token keyword">any</span><span class="token punctuation">}</span> useType
 * @param locals
 * @param invokeOptions
 */
<span class="token function">bindProperty</span><span class="token punctuation">(</span>instance<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> propertyKey<span class="token punctuation">,</span> useType<span class="token punctuation">,</span> options <span class="token punctuation">}</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/IInjectablePropertyService.html"><span class="token">IInjectablePropertyService</span></a><span class="token punctuation">,</span> locals<span class="token punctuation">:</span> Map&lt;<a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">,</span> <span class="token keyword">any</span>&gt;<span class="token punctuation">,</span> invokeOptions<span class="token punctuation">:</span> Partial&lt;<a href="/api/testing/IInvokeOptions.html"><span class="token">IInvokeOptions</span></a>&gt;<span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
/**
 *
 * @param instance
 * @param <span class="token punctuation">{</span><span class="token keyword">string</span><span class="token punctuation">}</span> propertyKey
 * @param <span class="token punctuation">{</span><span class="token keyword">any</span><span class="token punctuation">}</span> useType
 */
<span class="token function">bindValue</span><span class="token punctuation">(</span>instance<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> propertyKey<span class="token punctuation">,</span> expression<span class="token punctuation">,</span> defaultValue <span class="token punctuation">}</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/IInjectablePropertyValue.html"><span class="token">IInjectablePropertyValue</span></a><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
/**
 *
 * @param instance
 * @param <span class="token punctuation">{</span><span class="token keyword">string</span><span class="token punctuation">}</span> propertyKey
 * @param <span class="token punctuation">{</span><span class="token keyword">any</span><span class="token punctuation">}</span> useType
 */
<span class="token function">bindConstant</span><span class="token punctuation">(</span>instance<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> propertyKey<span class="token punctuation">,</span> expression<span class="token punctuation">,</span> defaultValue <span class="token punctuation">}</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/IInjectablePropertyValue.html"><span class="token">IInjectablePropertyValue</span></a><span class="token punctuation">)</span><span class="token punctuation">:</span> PropertyDescriptor<span class="token punctuation">;</span>
/**
 *
 * @param instance
 * @param propertyKey
 * @param useType
 * @param options
 */
<span class="token function">bindInterceptor</span><span class="token punctuation">(</span>instance<span class="token punctuation">:</span> <span class="token keyword">any</span><span class="token punctuation">,</span> <span class="token punctuation">{</span> propertyKey<span class="token punctuation">,</span> useType<span class="token punctuation">,</span> options <span class="token punctuation">}</span><span class="token punctuation">:</span> <a href="/api/di/interfaces/IInjectablePropertyService.html"><span class="token">IInjectablePropertyService</span></a><span class="token punctuation">)</span><span class="token punctuation">:</span> <span class="token keyword">void</span><span class="token punctuation">;</span>
<span class="token keyword">protected</span> <span class="token function">ensureProvider</span><span class="token punctuation">(</span>token<span class="token punctuation">:</span> <a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a><span class="token punctuation">)</span><span class="token punctuation">:</span> <a href="/api/di/class/Provider.html"><span class="token">Provider</span></a> | undefined<span class="token punctuation">;</span>
/**
 * Invoke a <span class="token keyword">class</span> method and inject service.
 *
 * #### IInjectableMethod options
 *
 * * **target**<span class="token punctuation">:</span> Optional. The <span class="token keyword">class</span> instance.
 * * **methodName**<span class="token punctuation">:</span> `<span class="token keyword">string</span>` Optional. The method name.
 * * **designParamTypes**<span class="token punctuation">:</span> `<span class="token keyword">any</span><span class="token punctuation">[</span><span class="token punctuation">]</span>` Optional. List of injectable types.
 * * **locals**<span class="token punctuation">:</span> `Map&lt;Function<span class="token punctuation">,</span> <span class="token keyword">any</span>&gt;` Optional. If preset then <span class="token keyword">any</span> argument Class are read <span class="token keyword">from</span> this object first<span class="token punctuation">,</span> before the `InjectorService` is consulted.
 *
 * #### <a href="/api/swagger/decorators/Example.html"><span class="token">Example</span></a>
 *
 * @param target
 * @param locals
 * @param options
 * @<span class="token keyword">private</span>
 */

/**
 * Create options to invoke a provider or <span class="token keyword">class</span>.
 * @param token
 * @param locals
 * @param options
 */

}

# 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()

await injector.load();

const myService1 = injector.get<MyService1>(MyServcice1);
1
2
3
4
5
6
7
8
9
10
11
12
13

# Members

settings: TsED.Configuration & DIConfiguration;

logger: IDILogger;

get resolvers(): import("../interfaces").IDIResolver[];

get scopes(): {
     [key: string]: ProviderScope;
 } & {
     [key: string]: ProviderScope;
 };

scopeOf(provider: Provider<any>): ProviderScope;

Retrieve default scope for a given provider.


forkProvider(token: TokenProvider, instance?: any): Provider;

Clone a provider from GlobalProviders and the given token. forkProvider method build automatically the provider if the instance parameter ins't given.


toArray(): any[];

Return a list of instance build by the injector.


get<T = any>(token: TokenProvider, options?: any): T | undefined;
Param Type Description
token <a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</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);
     }
}
1
2
3
4
5
6
7
8

has(token: TokenProvider): boolean;

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


invoke<T>(token: TokenProvider, locals?: Map<TokenProvider, any>, options?: Partial<IInvokeOptions<T>>): T;
Param Type Description
token <a href="/api/di/interfaces/TokenProvider.html"><span class="token">TokenProvider</span></a> 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);
     }
 }
1
2
3
4
5
6
7
8

loadAsync(locals?: LocalsContainer<any>): Promise<LocalsContainer<any>>;

Build only providers which are asynchronous.


loadSync(locals?: LocalsContainer<any>): LocalsContainer<any>;

load(container?: Map<TokenProvider, Provider<any>>): Promise<LocalsContainer<any>>;

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


resolveConfiguration(): void;

Load all configurations registered on providers


bindInjectableProperties(instance: any, locals: Map<TokenProvider, any>, options: Partial<IInvokeOptions>): void;

bindMethod(instance: any, { propertyKey }: IInjectablePropertyService): void;

bindProperty(instance: any, { propertyKey, useType, options }: IInjectablePropertyService, locals: Map<TokenProvider, any>, invokeOptions: Partial<IInvokeOptions>): void;

bindValue(instance: any, { propertyKey, expression, defaultValue }: IInjectablePropertyValue): void;

bindConstant(instance: any, { propertyKey, expression, defaultValue }: IInjectablePropertyValue): PropertyDescriptor;

bindInterceptor(instance: any, { propertyKey, useType, options }: IInjectablePropertyService): void;

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