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

typedi

Package Overview
Dependencies
Maintainers
1
Versions
26
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

typedi - npm Package Compare versions

Comparing version 0.7.3 to 0.8.0

15

Container.d.ts

@@ -1,7 +0,7 @@

import { ServiceMetadata } from "./types/ServiceMetadata";
import { ContainerInstance } from "./ContainerInstance";
import { Token } from "./Token";
import { Handler } from "./types/Handler";
import { ObjectType } from "./types/ObjectType";
import { Handler } from "./types/Handler";
import { Token } from "./Token";
import { ServiceIdentifier } from "./types/ServiceIdentifier";
import { ContainerInstance } from "./ContainerInstance";
import { ServiceMetadata } from "./types/ServiceMetadata";
/**

@@ -58,2 +58,9 @@ * Service container.

/**
* Retrieves the service with given name or type from the service container.
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
*/
static get<T>(service: {
service: T;
}): T;
/**
* Gets all instances registered in the container of the given service identifier.

@@ -60,0 +67,0 @@ * Used when service defined with multiple: true flag.

@@ -58,2 +58,3 @@ "use strict";

Container.remove = function () {
var _a;
var ids = [];

@@ -65,3 +66,2 @@ for (var _i = 0; _i < arguments.length; _i++) {

return this;
var _a;
};

@@ -68,0 +68,0 @@ /**

@@ -1,5 +0,5 @@

import { ServiceMetadata } from "./types/ServiceMetadata";
import { Token } from "./Token";
import { ObjectType } from "./types/ObjectType";
import { Token } from "./Token";
import { ServiceIdentifier } from "./types/ServiceIdentifier";
import { ServiceMetadata } from "./types/ServiceMetadata";
/**

@@ -50,2 +50,9 @@ * TypeDI can have multiple containers.

/**
* Retrieves the service with given name or type from the service container.
* Optionally, parameters can be passed in case if instance is initialized in the container for the first time.
*/
get<T>(id: {
service: T;
}): T;
/**
* Gets all instances registered in the container of the given service identifier.

@@ -95,23 +102,23 @@ * Used when service defined with multiple: true flag.

*/
private filterServices(identifier);
private filterServices;
/**
* Finds registered service in the with a given service identifier.
*/
private findService(identifier);
private findService;
/**
* Gets service value.
*/
private getServiceValue(identifier, service);
private getServiceValue;
/**
* Initializes all parameter types for a given target service class.
*/
private initializeParams(type, paramTypes);
private initializeParams;
/**
* Checks if given type is primitive (e.g. string, boolean, number, object).
*/
private isTypePrimitive(param);
private isTypePrimitive;
/**
* Applies all registered handlers on a given target class.
*/
private applyPropertyHandlers(target, instance);
private applyPropertyHandlers;
}
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var Container_1 = require("./Container");
var MissingProvidedServiceTypeError_1 = require("./error/MissingProvidedServiceTypeError");
var ServiceNotFoundError_1 = require("./error/ServiceNotFoundError");
var Token_1 = require("./Token");
var ServiceNotFoundError_1 = require("./error/ServiceNotFoundError");
var MissingProvidedServiceTypeError_1 = require("./error/MissingProvidedServiceTypeError");
var Container_1 = require("./Container");
/**

@@ -73,2 +73,5 @@ * TypeDI can have multiple containers.

}
if (typeof identifierOrServiceMetadata === "object" && identifierOrServiceMetadata.service) {
return this.set({ id: identifierOrServiceMetadata.service, value: value });
}
if (identifierOrServiceMetadata instanceof Function) {

@@ -131,4 +134,10 @@ return this.set({ type: identifierOrServiceMetadata, id: identifierOrServiceMetadata, value: value });

return this.services.find(function (service) {
if (service.id)
if (service.id) {
if (identifier instanceof Object &&
service.id instanceof Token_1.Token &&
identifier.service instanceof Token_1.Token) {
return service.id === identifier.service;
}
return service.id === identifier;
}
if (service.type && identifier instanceof Function)

@@ -143,4 +152,5 @@ return service.type === identifier; // todo: not sure why it was here || identifier.prototype instanceof service.type;

ContainerInstance.prototype.getServiceValue = function (identifier, service) {
var _a;
// find if instance of this object already initialized in the container and return it if it is
if (service && service.value !== null && service.value !== undefined)
if (service && service.value !== undefined)
return service.value;

@@ -163,2 +173,4 @@ // if named service was requested and its instance was not found plus there is not type to know what to initialize,

type = identifier;
// } else if (identifier instanceof Object && (identifier as { service: Token<any> }).service instanceof Token) {
// type = (identifier as { service: Token<any> }).service;
}

@@ -187,7 +199,7 @@ // if service was not found then create a new one and register it

}
else {
value = service.factory.apply(service, params);
else { // regular factory function
value = service.factory.apply(service, params.concat([this]));
}
}
else {
else { // otherwise simply create a new object instance
if (!type)

@@ -207,3 +219,2 @@ throw new MissingProvidedServiceTypeError_1.MissingProvidedServiceTypeError(identifier);

return value;
var _a;
};

@@ -210,0 +221,0 @@ /**

@@ -0,3 +1,41 @@

import { Token } from "../Token";
import { ServiceOptions } from "../types/ServiceOptions";
import { Token } from "../Token";
export declare type ObjectType<T1> = {
new (...args: any[]): T1;
} | {
service: T1;
};
export declare function Service<R>(factory: () => R): {
service: R;
};
export declare function Service<R, T1>(dependencies: [ObjectType<T1>], factory: (dependency1: T1) => R): {
service: R;
};
export declare function Service<R, T1, T2>(dependencies: [ObjectType<T1>, ObjectType<T2>], factory: (dependency1: T1, dependency2: T2) => R): {
service: R;
};
export declare function Service<R, T1, T2, T3>(dependencies: [ObjectType<T1>, ObjectType<T2>, ObjectType<T3>], factory: (dependency1: T1, dependency2: T2, dependency3: T3) => R): {
service: R;
};
export declare function Service<R, T1, T2, T3, T4>(dependencies: [ObjectType<T1>, ObjectType<T2>, ObjectType<T3>, ObjectType<T4>], factory: (dependency1: T1, dependency2: T2, dependency3: T3, dependency4: T4) => R): {
service: R;
};
export declare function Service<R, T1, T2, T3, T4, T5>(dependencies: [ObjectType<T1>, ObjectType<T2>, ObjectType<T3>, ObjectType<T4>, ObjectType<T5>], factory: (dependency1: T1, dependency2: T2, dependency3: T3, dependency4: T4, dependency5: T5) => R): {
service: R;
};
export declare function Service<R, T1, T2, T3, T4, T5, T6>(dependencies: [ObjectType<T1>, ObjectType<T2>, ObjectType<T3>, ObjectType<T4>, ObjectType<T5>, ObjectType<T6>], factory: (dependency1: T1, dependency2: T2, dependency3: T3, dependency4: T4, dependency5: T5, dependency6: T6) => R): {
service: R;
};
export declare function Service<R, T1, T2, T3, T4, T5, T6, T7>(dependencies: [ObjectType<T1>, ObjectType<T2>, ObjectType<T3>, ObjectType<T4>, ObjectType<T5>, ObjectType<T6>, ObjectType<T7>], factory: (dependency1: T1, dependency2: T2, dependency3: T3, dependency4: T4, dependency5: T5, dependency6: T6, dependency7: T7) => R): {
service: R;
};
export declare function Service<R, T1, T2, T3, T4, T5, T6, T7, T8>(dependencies: [ObjectType<T1>, ObjectType<T2>, ObjectType<T3>, ObjectType<T4>, ObjectType<T5>, ObjectType<T6>, ObjectType<T7>, ObjectType<T8>], factory: (dependency1: T1, dependency2: T2, dependency3: T3, dependency4: T4, dependency5: T5, dependency6: T6, dependency7: T7, dependency8: T8) => R): {
service: R;
};
export declare function Service<R, T1, T2, T3, T4, T5, T6, T7, T8, T9>(dependencies: [ObjectType<T1>, ObjectType<T2>, ObjectType<T3>, ObjectType<T4>, ObjectType<T5>, ObjectType<T6>, ObjectType<T7>, ObjectType<T8>, ObjectType<T9>], factory: (dependency1: T1, dependency2: T2, dependency3: T3, dependency4: T4, dependency5: T5, dependency6: T6, dependency7: T7, dependency8: T8, dependency9: T9) => R): {
service: R;
};
export declare function Service<R, T1, T2, T3, T4, T5, T6, T7, T8, T9, T10>(dependencies: [ObjectType<T1>, ObjectType<T2>, ObjectType<T3>, ObjectType<T4>, ObjectType<T5>, ObjectType<T6>, ObjectType<T7>, ObjectType<T8>, ObjectType<T9>, ObjectType<T10>], factory: (dependency1: T1, dependency2: T2, dependency3: T3, dependency4: T4, dependency5: T5, dependency6: T6, dependency7: T7, dependency8: T8, dependency9: T9, dependency10: T10) => R): {
service: R;
};
/**

@@ -4,0 +42,0 @@ * Marks class as a service that can be injected using Container.

@@ -8,22 +8,37 @@ "use strict";

*/
function Service(optionsOrServiceName) {
return function (target) {
var service = {
type: target
function Service(optionsOrServiceName, maybeFactory) {
if (arguments.length === 2 || (optionsOrServiceName instanceof Function)) {
var serviceId = { service: new Token_1.Token() };
var dependencies_1 = arguments.length === 2 ? optionsOrServiceName : [];
var factory_1 = arguments.length === 2 ? maybeFactory : optionsOrServiceName;
Container_1.Container.set({
id: serviceId.service,
factory: function (container) {
var params = dependencies_1.map(function (dependency) { return container.get(dependency); });
return factory_1.apply(void 0, params);
}
});
return serviceId;
}
else {
return function (target) {
var service = {
type: target
};
if (typeof optionsOrServiceName === "string" || optionsOrServiceName instanceof Token_1.Token) {
service.id = optionsOrServiceName;
service.multiple = optionsOrServiceName.multiple;
service.global = optionsOrServiceName.global || false;
service.transient = optionsOrServiceName.transient;
}
else if (optionsOrServiceName) { // ServiceOptions
service.id = optionsOrServiceName.id;
service.factory = optionsOrServiceName.factory;
service.multiple = optionsOrServiceName.multiple;
service.global = optionsOrServiceName.global || false;
service.transient = optionsOrServiceName.transient;
}
Container_1.Container.set(service);
};
if (typeof optionsOrServiceName === "string" || optionsOrServiceName instanceof Token_1.Token) {
service.id = optionsOrServiceName;
service.multiple = optionsOrServiceName.multiple;
service.global = optionsOrServiceName.global || false;
service.transient = optionsOrServiceName.transient;
}
else if (optionsOrServiceName) {
service.id = optionsOrServiceName.id;
service.factory = optionsOrServiceName.factory;
service.multiple = optionsOrServiceName.multiple;
service.global = optionsOrServiceName.global || false;
service.transient = optionsOrServiceName.transient;
}
Container_1.Container.set(service);
};
}
}

@@ -30,0 +45,0 @@ exports.Service = Service;

{
"name": "typedi",
"version": "0.7.3",
"version": "0.8.0",
"description": "Dependency injection for TypeScript",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -39,3 +39,3 @@ # TypeDI

In your class's constructor you always recieve as a last argument a container which you can use to get other dependencies.
In your class's constructor you always receive as a last argument a container which you can use to get other dependencies.

@@ -160,2 +160,43 @@ ```javascript

TypeDI also supports a function dependency injection. Here is how it looks like:
```javascript
var Service = require("typedi").Service;
var Container = require("typedi").Container;
var PostRepository = Service(() => ({
getName() {
return "hello from post repository";
}
}));
var PostManager = Service(() => ({
getId() {
return "some post id";
}
}));
class PostQueryBuilder {
build() {
return "SUPER * QUERY";
}
}
var PostController = Service([
PostManager,
PostRepository,
PostQueryBuilder
], (manager, repository, queryBuilder) => {
return {
id: manager.getId(),
name: repository.getName(),
query: queryBuilder.build()
};
});
var postController = Container.get(PostController);
console.log(postController);
```
## Usage with TypeScript

@@ -720,2 +761,40 @@

TypeDI also supports a function dependency injection. Here is how it looks like:
```javascript
export const PostRepository = Service(() => ({
getName() {
return "hello from post repository";
}
}));
export const PostManager = Service(() => ({
getId() {
return "some post id";
}
}));
export class PostQueryBuilder {
build() {
return "SUPER * QUERY";
}
}
export const PostController = Service([
PostManager,
PostRepository,
PostQueryBuilder
], (manager, repository, queryBuilder) => {
return {
id: manager.getId(),
name: repository.getName(),
query: queryBuilder.build()
};
});
const postController = Container.get(PostController);
console.log(postController);
```
### Remove registered services or reset container state

@@ -722,0 +801,0 @@

@@ -6,3 +6,3 @@ /**

export declare class Token<T> {
name: string;
name?: string;
/**

@@ -9,0 +9,0 @@ * @param name Token name, optional and only used for debugging purposes.

@@ -6,2 +6,4 @@ import { Token } from "../Token";

*/
export declare type ServiceIdentifier = Function | Token<any> | string;
export declare type ServiceIdentifier<T = any> = Function | Token<T> | string | {
service: T;
};
import { ObjectType } from "./ObjectType";
import { Token } from "../Token";
import { ServiceIdentifier } from "./ServiceIdentifier";
/**

@@ -29,3 +29,3 @@ * Service metadata is used to initialize service and store its state.

*/
id?: Token<any> | string | Function;
id?: ServiceIdentifier;
/**

@@ -32,0 +32,0 @@ * Factory function used to initialize this service.

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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