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

tsyringe

Package Overview
Dependencies
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsyringe - npm Package Compare versions

Comparing version 1.0.1 to 2.0.0

dist/decorators/decorators.js

11

dist/dependency-container.js

@@ -71,6 +71,15 @@ "use strict";

construct(ctor) {
return this.parent ? new ctor(this) : new ctor();
if (ctor.length === 0) {
return new ctor();
}
const paramInfo = exports.typeInfo.get(ctor);
if (!paramInfo) {
throw `TypeInfo not known for ${ctor}`;
}
const params = paramInfo.map(param => this.resolve(param));
return new ctor(...params);
}
}
exports.DependencyContainer = DependencyContainer;
exports.typeInfo = new Map();
exports.instance = new DependencyContainer();

7

package.json
{
"name": "tsyringe",
"version": "1.0.1",
"version": "2.0.0",
"description": "Lightweight dependency injection container for JavaScript/TypeScript",

@@ -8,4 +8,5 @@ "main": "dist/index.js",

"build": "rimraf ./dist ./types && tsc -p ./src",
"test": "jest",
"test": "npm run lint && jest",
"test:coverage": "jest --coverage",
"lint": "tslint -p src && tslint -p tests",
"prepush": "npm test"

@@ -71,4 +72,6 @@ },

"ts-jest": "^22.0.4",
"tslint": "^5.9.1",
"tslint-eslint-rules": "^5.0.0",
"typescript": "^2.6.1"
}
}

@@ -5,5 +5,13 @@ [![Travis](https://img.shields.io/travis/Microsoft/tsyringe.svg)](https://travis-ci.org/Microsoft/tsyringe/)

A lightweight dependency injection container for TypeScript/TypeScript for
A lightweight dependency injection container for TypeScript/JavaScript for
constructor injection.
* [Installation](#installation)
* [API](#api)
* [injectable()](#injectable)
* [autoInjectable()](#autoInjectable)
* [inject()](#inject)
* [Full Examples](#full-examples)
* [Contributing](#contributing)
## Installation

@@ -25,7 +33,72 @@

## Usage
## API
### injectable()
Class decorator factory that allows the class' dependencies to be injected at
runtime.
#### Usage
```TypeScript
import {decorators} from "tsyringe";
const {injectable} = decorators;
@injectable()
class Foo {
constructor(private database: Database) {}
}
// some other file
import {container} from "tsyringe";
import {Foo} from "./foo";
const instance = container.resolve(Foo);
```
### autoInjectable()
Class decorator factory that replaces the decorated class' constructor with
a parameterless constructor that has dependencies auto-resolved.
**Note** Resolution is performed using the global container
#### Usage
```TypeScript
import {decorators} from "tsyringe";
const {autoInjectable} = decorators;
@autoInjectable()
class Foo {
constructor(private database?: Database) {}
}
// some other file
import {Foo} from "./foo";
const instance = new Foo();
```
Notice how in order to allow the use of the empty constructor `new Foo()`, we
need to make the parameters optional, e.g. `database?: Database`
### inject()
Parameter decorator factory that allows for interface and other non-class
information to be stored in the constructor's metadata
#### Usage
```TypeScript
import {decorators} from "tsyringe";
const {injectable, inject} = decorators;
interface Database {
// ...
}
@injectable()
class Foo {
constructor(@inject("Database") private database?: Database) {}
}
```
## Full examples
### Example without interfaces
Since classes have type information at runtime, we can resolve them without any
extra information. If a particular class isn't registered with the container then
a new instance will be resolved each time.
extra information.

@@ -32,0 +105,0 @@ ```TypeScript

@@ -15,2 +15,3 @@ import * as Types from "./types";

}
export declare const typeInfo: Map<Types.constructor<any>, any[]>;
export declare const instance: Types.DependencyContainer;

@@ -5,6 +5,13 @@ import * as decorators from "./decorators";

import * as Types from "./types";
export { DependencyContainer } from "./types";
export { factories };
export { providers };
export { decorators };
export declare const container: Types.DependencyContainer;
declare module "tsyringe" {
export { DependencyContainer } from "./types";
export { factories };
export { providers };
export { decorators };
export const container: Types.DependencyContainer;
}
declare module "tsyringe/decorators" {
export { decorators };
}
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