Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@tsed/di

Package Overview
Dependencies
Maintainers
5
Versions
1030
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@tsed/di - npm Package Compare versions

Comparing version 5.14.0 to 5.14.1

125

lib/services/InjectorService.d.ts

@@ -35,2 +35,24 @@ import { Provider } from "../class/Provider";

/**
* The getProvider() method returns a specified element from a Map object.
* @returns {T} Returns the element associated with the specified key or undefined if the key can't be found in the Map object.
* @param token
*/
getProvider(token: TokenProvider): Provider<any> | undefined;
/**
* Clone a provider from GlobalProviders and the given token. forkProvider method build automatically the provider if the instance parameter ins't given.
* @param token
* @param instance
*/
forkProvider(token: TokenProvider, instance?: any): Provider<any>;
/**
*
* @param {ProviderType} type
* @returns {[RegistryKey , Provider<any>][]}
*/
getProviders(type?: ProviderType | string): Provider<any>[];
/**
* Return a list of instance build by the injector.
*/
toArray(): any[];
/**
* Get a service or factory already constructed from his symbol or class.

@@ -51,3 +73,3 @@ *

*
* @param target The class or symbol registered in InjectorService.
* @param token The class or symbol registered in InjectorService.
* @returns {boolean}

@@ -60,22 +82,5 @@ */

* @returns {boolean}
* @param token
*/
has(token: TokenProvider): boolean;
/**
* The getProvider() method returns a specified element from a Map object.
* @param key Required. The key of the element to return from the Map object.
* @returns {T} Returns the element associated with the specified key or undefined if the key can't be found in the Map object.
*/
getProvider(token: TokenProvider): Provider<any> | undefined;
/**
*
* @param {RegistryKey} key
* @param instance
*/
forkProvider(token: TokenProvider, instance?: any): Provider<any>;
/**
*
* @param {ProviderType} type
* @returns {[RegistryKey , Provider<any>][]}
*/
getProviders(type?: ProviderType | string): Provider<any>[];
destroy(): Promise<void>;

@@ -104,48 +109,10 @@ /**

*/
invoke<T>(target: TokenProvider, locals?: Map<string | Function, any>, designParamTypes?: any[], requiredScope?: boolean): T;
invoke<T>(token: TokenProvider, locals?: Map<string | Function, any>, designParamTypes?: any[], requiredScope?: boolean): T;
/**
* Invoke a class method and inject service.
* Build all providers from GlobalProviders or from given providers parameters and emit `$onInit` event.
*
* #### 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
*
* ```typescript
* import {InjectorService} from "@tsed/common";
*
* class MyService {
* constructor(injectorService: InjectorService) {
* injectorService.invokeMethod(this.method, {
* this,
* methodName: 'method'
* });
* }
*
* method(otherService: OtherService) {}
* }
* ```
*
* @returns {any}
* @param handler The injectable method to invoke. Method parameters are injected according method signature.
* @param options Object to configure the invocation.
* @deprecated
* @param container
*/
invokeMethod(handler: any, options: IInjectableMethod<any>): any;
/**
* Initialize injectorService and load all services/factories.
*/
load(): Promise<any>;
/**
* Emit an event to all service. See service [lifecycle hooks](/docs/services.md#lifecycle-hooks).
* @param eventName The event name to emit at all services.
* @param args List of the parameters to give to each services.
* @returns {Promise<any[]>} A list of promises.
*/
emit(eventName: string, ...args: any[]): Promise<void>;
/**
*

@@ -195,6 +162,42 @@ * @param instance

/**
* 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
*
* ```typescript
* import {InjectorService} from "@tsed/common";
*
* class MyService {
* constructor(injectorService: InjectorService) {
* injectorService.invokeMethod(this.method, {
* this,
* methodName: 'method'
* });
* }
*
* method(otherService: OtherService) {}
* }
* ```
*
* @returns {any}
* @param handler The injectable method to invoke. Method parameters are injected according method signature.
* @param options Object to configure the invocation.
* @deprecated
*/
private initInjector;
invokeMethod(handler: any, options: IInjectableMethod<any>): any;
/**
* Emit an event to all service. See service [lifecycle hooks](/docs/services.md#lifecycle-hooks).
* @param eventName The event name to emit at all services.
* @param args List of the parameters to give to each services.
* @returns {Promise<any[]>} A list of promises.
*/
emit(eventName: string, ...args: any[]): Promise<void>;
/**
*

@@ -201,0 +204,0 @@ * @returns {any}

@@ -39,3 +39,3 @@ "use strict";

this.scopes = {};
this.initInjector();
this.forkProvider(InjectorService, this);
}

@@ -46,2 +46,37 @@ scopeOf(providerType) {

/**
* The getProvider() method returns a specified element from a Map object.
* @returns {T} Returns the element associated with the specified key or undefined if the key can't be found in the Map object.
* @param token
*/
getProvider(token) {
return super.get(core_1.getClassOrSymbol(token));
}
/**
* Clone a provider from GlobalProviders and the given token. forkProvider method build automatically the provider if the instance parameter ins't given.
* @param token
* @param instance
*/
forkProvider(token, instance) {
const provider = GlobalProviders_1.GlobalProviders.get(token).clone();
this.set(token, provider);
provider.instance = instance;
return provider;
}
/**
*
* @param {ProviderType} type
* @returns {[RegistryKey , Provider<any>][]}
*/
getProviders(type) {
return Array.from(this)
.filter(([key, provider]) => (type ? provider.type === type : true))
.map(([key, provider]) => provider);
}
/**
* Return a list of instance build by the injector.
*/
toArray() {
return Array.from(this.values()).map(provider => provider.instance);
}
/**
* Get a service or factory already constructed from his symbol or class.

@@ -62,3 +97,3 @@ *

*
* @param target The class or symbol registered in InjectorService.
* @param token The class or symbol registered in InjectorService.
* @returns {boolean}

@@ -73,2 +108,3 @@ */

* @returns {boolean}
* @param token
*/

@@ -78,31 +114,2 @@ has(token) {

}
/**
* The getProvider() method returns a specified element from a Map object.
* @param key Required. The key of the element to return from the Map object.
* @returns {T} Returns the element associated with the specified key or undefined if the key can't be found in the Map object.
*/
getProvider(token) {
return super.get(core_1.getClassOrSymbol(token));
}
/**
*
* @param {RegistryKey} key
* @param instance
*/
forkProvider(token, instance) {
const provider = GlobalProviders_1.GlobalProviders.get(token).clone();
this.set(token, provider);
provider.instance = instance;
return provider;
}
/**
*
* @param {ProviderType} type
* @returns {[RegistryKey , Provider<any>][]}
*/
getProviders(type) {
return Array.from(this)
.filter(([key, provider]) => (type ? provider.type === type : true))
.map(([key, provider]) => provider);
}
destroy() {

@@ -136,8 +143,8 @@ return tslib_1.__awaiter(this, void 0, void 0, function* () {

*/
invoke(target, locals = new Map(), designParamTypes, requiredScope = false) {
const { onInvoke } = GlobalProviders_1.GlobalProviders.getRegistrySettings(target);
const provider = this.getProvider(target);
const parentScope = core_1.Store.from(target).get("scope");
invoke(token, locals = new Map(), designParamTypes, requiredScope = false) {
const { onInvoke } = GlobalProviders_1.GlobalProviders.getRegistrySettings(token);
const provider = this.getProvider(token);
const parentScope = core_1.Store.from(token).get("scope");
if (!designParamTypes) {
designParamTypes = core_1.Metadata.getParamTypes(target);
designParamTypes = core_1.Metadata.getParamTypes(token);
}

@@ -149,3 +156,3 @@ if (provider && onInvoke) {

serviceType,
target,
target: token,
locals,

@@ -155,3 +162,3 @@ requiredScope,

}));
const instance = new target(...services);
const instance = new token(...services);
this.bindInjectableProperties(instance);

@@ -161,54 +168,6 @@ return instance;

/**
* Invoke a class method and inject service.
* Build all providers from GlobalProviders or from given providers parameters and emit `$onInit` event.
*
* #### 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
*
* ```typescript
* import {InjectorService} from "@tsed/common";
*
* class MyService {
* constructor(injectorService: InjectorService) {
* injectorService.invokeMethod(this.method, {
* this,
* methodName: 'method'
* });
* }
*
* method(otherService: OtherService) {}
* }
* ```
*
* @returns {any}
* @param handler The injectable method to invoke. Method parameters are injected according method signature.
* @param options Object to configure the invocation.
* @deprecated
* @param container
*/
invokeMethod(handler, options) {
let { designParamTypes } = options;
const { locals = new Map(), target, methodName } = options;
if (handler.$injected) {
return handler.call(target, locals);
}
if (!designParamTypes) {
designParamTypes = core_1.Metadata.getParamTypes(core_1.prototypeOf(target), methodName);
}
const services = designParamTypes.map((serviceType) => this.mapServices({
serviceType,
target,
locals,
requiredScope: false,
parentScope: false
}));
return handler(...services);
}
/**
* Initialize injectorService and load all services/factories.
*/
load() {

@@ -227,23 +186,2 @@ return tslib_1.__awaiter(this, void 0, void 0, function* () {

/**
* Emit an event to all service. See service [lifecycle hooks](/docs/services.md#lifecycle-hooks).
* @param eventName The event name to emit at all services.
* @param args List of the parameters to give to each services.
* @returns {Promise<any[]>} A list of promises.
*/
emit(eventName, ...args) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
this.logger.debug("\x1B[1mCall hook", eventName, "\x1B[22m");
const providers = this.getProviders();
for (const provider of providers) {
const service = provider.instance;
if (service && eventName in service) {
const startTime = new Date().getTime();
this.logger.debug(`Call ${core_1.nameOf(provider.provide)}.${eventName}()`);
yield service[eventName](...args);
this.logger.debug(`Run ${core_1.nameOf(provider.provide)}.${eventName}() in ${new Date().getTime() - startTime} ms`);
}
}
});
}
/**
*

@@ -369,8 +307,73 @@ * @param instance

/**
* 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
*
* ```typescript
* import {InjectorService} from "@tsed/common";
*
* class MyService {
* constructor(injectorService: InjectorService) {
* injectorService.invokeMethod(this.method, {
* this,
* methodName: 'method'
* });
* }
*
* method(otherService: OtherService) {}
* }
* ```
*
* @returns {any}
* @param handler The injectable method to invoke. Method parameters are injected according method signature.
* @param options Object to configure the invocation.
* @deprecated
*/
initInjector() {
this.forkProvider(InjectorService, this);
invokeMethod(handler, options) {
let { designParamTypes } = options;
const { locals = new Map(), target, methodName } = options;
if (handler.$injected) {
return handler.call(target, locals);
}
if (!designParamTypes) {
designParamTypes = core_1.Metadata.getParamTypes(core_1.prototypeOf(target), methodName);
}
const services = designParamTypes.map((serviceType) => this.mapServices({
serviceType,
target,
locals,
requiredScope: false,
parentScope: false
}));
return handler(...services);
}
/**
* Emit an event to all service. See service [lifecycle hooks](/docs/services.md#lifecycle-hooks).
* @param eventName The event name to emit at all services.
* @param args List of the parameters to give to each services.
* @returns {Promise<any[]>} A list of promises.
*/
emit(eventName, ...args) {
return tslib_1.__awaiter(this, void 0, void 0, function* () {
this.logger.debug("\x1B[1mCall hook", eventName, "\x1B[22m");
const providers = this.getProviders();
for (const provider of providers) {
const service = provider.instance;
if (service && eventName in service) {
const startTime = new Date().getTime();
this.logger.debug(`Call ${core_1.nameOf(provider.provide)}.${eventName}()`);
yield service[eventName](...args);
this.logger.debug(`Run ${core_1.nameOf(provider.provide)}.${eventName}() in ${new Date().getTime() - startTime} ms`);
}
}
});
}
/**
*

@@ -377,0 +380,0 @@ * @returns {any}

{
"name": "@tsed/di",
"version": "5.14.0",
"version": "5.14.1",
"description": "DI module for Ts.ED Framework",

@@ -11,3 +11,3 @@ "main": "lib/index.js",

"peerDependencies": {
"@tsed/core": "5.14.0"
"@tsed/core": "5.14.1"
},

@@ -28,3 +28,3 @@ "devDependencies": {},

"license": "MIT",
"gitHead": "5753bc75f5eb170ebc050672236f057f8733dfa7"
"gitHead": "967047aacbe11dcb61c1d73508ed8f0006847f12"
}

@@ -52,3 +52,3 @@ import {deepClone, getClass, getClassOrSymbol, Metadata, nameOf, prototypeOf, RegistryKey, Store, Type} from "@tsed/core";

super();
this.initInjector();
this.forkProvider(InjectorService, this);
}

@@ -61,2 +61,43 @@

/**
* The getProvider() method returns a specified element from a Map object.
* @returns {T} Returns the element associated with the specified key or undefined if the key can't be found in the Map object.
* @param token
*/
public getProvider(token: TokenProvider): Provider<any> | undefined {
return super.get(getClassOrSymbol(token));
}
/**
* Clone a provider from GlobalProviders and the given token. forkProvider method build automatically the provider if the instance parameter ins't given.
* @param token
* @param instance
*/
public forkProvider(token: TokenProvider, instance?: any): Provider<any> {
const provider = GlobalProviders.get(token)!.clone();
this.set(token, provider);
provider.instance = instance;
return provider;
}
/**
*
* @param {ProviderType} type
* @returns {[RegistryKey , Provider<any>][]}
*/
getProviders(type?: ProviderType | string): Provider<any>[] {
return Array.from(this)
.filter(([key, provider]) => (type ? provider.type === type : true))
.map(([key, provider]) => provider);
}
/**
* Return a list of instance build by the injector.
*/
public toArray(): any[] {
return Array.from(this.values()).map(provider => provider.instance);
}
/**
* Get a service or factory already constructed from his symbol or class.

@@ -77,3 +118,3 @@ *

*
* @param target The class or symbol registered in InjectorService.
* @param token The class or symbol registered in InjectorService.
* @returns {boolean}

@@ -89,2 +130,3 @@ */

* @returns {boolean}
* @param token
*/

@@ -95,36 +137,2 @@ has(token: TokenProvider): boolean {

/**
* The getProvider() method returns a specified element from a Map object.
* @param key Required. The key of the element to return from the Map object.
* @returns {T} Returns the element associated with the specified key or undefined if the key can't be found in the Map object.
*/
getProvider(token: TokenProvider): Provider<any> | undefined {
return super.get(getClassOrSymbol(token));
}
/**
*
* @param {RegistryKey} key
* @param instance
*/
forkProvider(token: TokenProvider, instance?: any): Provider<any> {
const provider = GlobalProviders.get(token)!.clone();
this.set(token, provider);
provider.instance = instance;
return provider;
}
/**
*
* @param {ProviderType} type
* @returns {[RegistryKey , Provider<any>][]}
*/
getProviders(type?: ProviderType | string): Provider<any>[] {
return Array.from(this)
.filter(([key, provider]) => (type ? provider.type === type : true))
.map(([key, provider]) => provider);
}
async destroy() {

@@ -158,3 +166,3 @@ await this.emit("$onDestroy");

invoke<T>(
target: TokenProvider,
token: TokenProvider,
locals: Map<string | Function, any> = new Map(),

@@ -164,8 +172,8 @@ designParamTypes?: any[],

): T {
const {onInvoke} = GlobalProviders.getRegistrySettings(target);
const provider = this.getProvider(target);
const parentScope = Store.from(target).get("scope");
const {onInvoke} = GlobalProviders.getRegistrySettings(token);
const provider = this.getProvider(token);
const parentScope = Store.from(token).get("scope");
if (!designParamTypes) {
designParamTypes = Metadata.getParamTypes(target);
designParamTypes = Metadata.getParamTypes(token);
}

@@ -180,3 +188,3 @@

serviceType,
target,
target: token,
locals,

@@ -188,3 +196,3 @@ requiredScope,

const instance = new target(...services);
const instance = new token(...services);

@@ -197,61 +205,6 @@ this.bindInjectableProperties(instance);

/**
* Invoke a class method and inject service.
* Build all providers from GlobalProviders or from given providers parameters and emit `$onInit` event.
*
* #### 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
*
* ```typescript
* import {InjectorService} from "@tsed/common";
*
* class MyService {
* constructor(injectorService: InjectorService) {
* injectorService.invokeMethod(this.method, {
* this,
* methodName: 'method'
* });
* }
*
* method(otherService: OtherService) {}
* }
* ```
*
* @returns {any}
* @param handler The injectable method to invoke. Method parameters are injected according method signature.
* @param options Object to configure the invocation.
* @deprecated
* @param container
*/
public invokeMethod(handler: any, options: IInjectableMethod<any>): any {
let {designParamTypes} = options;
const {locals = new Map<any, any>(), target, methodName} = options;
if (handler.$injected) {
return handler.call(target, locals);
}
if (!designParamTypes) {
designParamTypes = Metadata.getParamTypes(prototypeOf(target), methodName);
}
const services = designParamTypes.map((serviceType: any) =>
this.mapServices({
serviceType,
target,
locals,
requiredScope: false,
parentScope: false
})
);
return handler(...services);
}
/**
* Initialize injectorService and load all services/factories.
*/
async load(): Promise<any> {

@@ -271,27 +224,2 @@ // TODO copy all provider from GlobalProvider registry. In future this action will be performed from Bootstrap class

/**
* Emit an event to all service. See service [lifecycle hooks](/docs/services.md#lifecycle-hooks).
* @param eventName The event name to emit at all services.
* @param args List of the parameters to give to each services.
* @returns {Promise<any[]>} A list of promises.
*/
public async emit(eventName: string, ...args: any[]) {
this.logger.debug("\x1B[1mCall hook", eventName, "\x1B[22m");
const providers = this.getProviders();
for (const provider of providers) {
const service = provider.instance;
if (service && eventName in service) {
const startTime = new Date().getTime();
this.logger.debug(`Call ${nameOf(provider.provide)}.${eventName}()`);
await service[eventName](...args);
this.logger.debug(`Run ${nameOf(provider.provide)}.${eventName}() in ${new Date().getTime() - startTime} ms`);
}
}
}
/**
*

@@ -434,9 +362,84 @@ * @param instance

/**
* 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
*
* ```typescript
* import {InjectorService} from "@tsed/common";
*
* class MyService {
* constructor(injectorService: InjectorService) {
* injectorService.invokeMethod(this.method, {
* this,
* methodName: 'method'
* });
* }
*
* method(otherService: OtherService) {}
* }
* ```
*
* @returns {any}
* @param handler The injectable method to invoke. Method parameters are injected according method signature.
* @param options Object to configure the invocation.
* @deprecated
*/
private initInjector() {
this.forkProvider(InjectorService, this);
public invokeMethod(handler: any, options: IInjectableMethod<any>): any {
let {designParamTypes} = options;
const {locals = new Map<any, any>(), target, methodName} = options;
if (handler.$injected) {
return handler.call(target, locals);
}
if (!designParamTypes) {
designParamTypes = Metadata.getParamTypes(prototypeOf(target), methodName);
}
const services = designParamTypes.map((serviceType: any) =>
this.mapServices({
serviceType,
target,
locals,
requiredScope: false,
parentScope: false
})
);
return handler(...services);
}
/**
* Emit an event to all service. See service [lifecycle hooks](/docs/services.md#lifecycle-hooks).
* @param eventName The event name to emit at all services.
* @param args List of the parameters to give to each services.
* @returns {Promise<any[]>} A list of promises.
*/
public async emit(eventName: string, ...args: any[]) {
this.logger.debug("\x1B[1mCall hook", eventName, "\x1B[22m");
const providers = this.getProviders();
for (const provider of providers) {
const service = provider.instance;
if (service && eventName in service) {
const startTime = new Date().getTime();
this.logger.debug(`Call ${nameOf(provider.provide)}.${eventName}()`);
await service[eventName](...args);
this.logger.debug(`Run ${nameOf(provider.provide)}.${eventName}() in ${new Date().getTime() - startTime} ms`);
}
}
}
/**
*

@@ -443,0 +446,0 @@ * @returns {any}

@@ -55,7 +55,7 @@ import {GlobalProviders, Inject, Provider, ProviderScope, ProviderType} from "@tsed/common";

it("should return true", () => {
expect(this.injector.has(InjectorService)).to.be.true;
return expect(new InjectorService().has(InjectorService)).to.be.true;
});
it("should return false", () => {
expect(this.injector.has(Test)).to.be.false;
return expect(new InjectorService().has(Test)).to.be.false;
});

@@ -65,18 +65,14 @@ });

describe("get()", () => {
before(
inject([InjectorService], (injector: InjectorService) => {
this.injector2 = injector;
})
);
it("should return element", () => {
expect(this.injector.get(InjectorService)).to.be.instanceOf(InjectorService);
expect(new InjectorService().get(InjectorService)).to.be.instanceOf(InjectorService);
});
it("should return undefined", () => {
expect(this.injector.get(Test)).to.be.undefined;
return expect(new InjectorService().get(Test)).to.be.undefined;
});
});
it("should get a service", () => {
expect(this.injector2.get(InjectorService)).to.be.an.instanceof(InjectorService);
describe("toArray()", () => {
it("should return instances", () => {
expect(new InjectorService().toArray()).to.be.instanceOf(Array);
});

@@ -83,0 +79,0 @@ });

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc