
Research
/Security News
Malicious npm Packages Target WhatsApp Developers with Remote Kill Switch
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.
inversify-binding-decorators
Advanced tools
An utility that allows developers to declare InversifyJS bindings using ES2016 decorators
The inversify-binding-decorators package provides decorators for InversifyJS, a powerful and flexible inversion of control (IoC) container for JavaScript and TypeScript applications. It simplifies the process of binding classes and interfaces to the IoC container using decorators, making the code more readable and maintainable.
Automatic Binding
The automatic binding feature allows you to use the @provide decorator to automatically bind a class to the InversifyJS container. This eliminates the need for manual binding in the container configuration, making the code cleaner and easier to manage.
```typescript
import { Container } from 'inversify';
import { provide } from 'inversify-binding-decorators';
@provide(Foo)
class Foo {
public sayHello() {
return 'Hello from Foo!';
}
}
const container = new Container();
const foo = container.get(Foo);
console.log(foo.sayHello()); // Output: Hello from Foo!
```
Named Bindings
Named bindings allow you to bind multiple implementations of the same interface or class under different names. The @provideNamed decorator is used to specify the name of the binding, and the @named decorator is used to inject the specific named binding into a class.
```typescript
import { Container, inject } from 'inversify';
import { provideNamed } from 'inversify-binding-decorators';
@provideNamed('MyService', 'ServiceA')
class ServiceA {
public getName() {
return 'ServiceA';
}
}
@provideNamed('MyService', 'ServiceB')
class ServiceB {
public getName() {
return 'ServiceB';
}
}
class Consumer {
constructor(@inject('MyService') @named('ServiceA') private service: ServiceA) {}
public useService() {
return this.service.getName();
}
}
const container = new Container();
const consumer = container.get(Consumer);
console.log(consumer.useService()); // Output: ServiceA
```
typescript-ioc is another IoC container for TypeScript that uses decorators to simplify dependency injection. It provides a similar feature set to inversify-binding-decorators, focusing on ease of use and integration with TypeScript's decorators. However, it is generally considered less flexible and feature-rich compared to InversifyJS and its decorators.
typedi is a dependency injection tool for TypeScript and JavaScript that also uses decorators to manage dependencies. It offers a simpler API compared to InversifyJS and is often chosen for smaller projects or when a lightweight solution is preferred. While it lacks some of the advanced features of InversifyJS, it provides a straightforward approach to dependency injection.
An utility that allows developers to declare InversifyJS bindings using ES2016 decorators:
You can install inversify-binding-decorators
using npm:
$ npm install inversify inversify-binding-decorators reflect-metadata --save
The inversify-binding-decorators
type definitions are included in the npm module and require TypeScript 2.0.
Please refer to the InversifyJS documentation to learn more about the installation process.
The InversifyJS API allows us to delcare bindings using a fluent API:
import { injectable, Container } from "inversify";
import "reflect-metadata";
@injectable()
class Katana implements Weapon {
public hit() {
return "cut!";
}
}
@injectable()
class Shuriken implements ThrowableWeapon {
public throw() {
return "hit!";
}
}
var container = new Container();
container.bind<Katana>("Katana").to(Katana);
container.bind<Shuriken>("Shuriken").to(Shuriken);
This small utility allows you to declare bindings using decorators:
import { injectable, Container } from "inversify";
import { provide, buildProviderModule } from "inversify-binding-decorators";
import "reflect-metadata";
@provide(Katana)
class Katana implements Weapon {
public hit() {
return "cut!";
}
}
@provide(Shuriken)
class Shuriken implements ThrowableWeapon {
public throw() {
return "hit!";
}
}
var container = new Container();
// Reflects all decorators provided by this package and packages them into
// a module to be loaded by the container
container.load(buildProviderModule());
If you try to apply @provide
multiple times:
@provide("Ninja")
@provide("SilentNinja")
class Ninja {
// ...
}
The library will throw an exception:
Cannot apply @injectable decorator multiple times. Please use @provide(ID, true) if you are trying to declare multiple bindings!
We throw an exception to ensure that you are are not trying to apply @provide
multiple times by mistake.
You can overcome this by passing the force
argument to @provide
:
@provide("Ninja", true)
@provide("SilentNinja", true)
class Ninja {
// ...
}
When you invoke @provide
using classes:
@provide(Katana)
class Katana {
public hit() {
return "cut!";
}
}
@provide(Ninja)
class Ninja {
private _katana: Weapon;
public constructor(
katana: Weapon
) {
this._katana = katana;
}
public fight() { return this._katana.hit(); };
}
A new binding is created under the hood:
container.bind<Katana>(Katana).to(Katana);
container.bind<Ninja>(Ninja).to(Ninja);
These bindings use classes as identidiers but you can also use string literals as identifiers:
let TYPE = {
IKatana: "Katana",
INinja: "Ninja"
};
@provide(TYPE.Katana)
class Katana implements Weapon {
public hit() {
return "cut!";
}
}
@provide(TYPE.Ninja)
class Ninja implements Ninja {
private _katana: Weapon;
public constructor(
@inject(TYPE.Katana) katana: Weapon
) {
this._katana = katana;
}
public fight() { return this._katana.hit(); };
}
You can also use symbols as identifiers:
let TYPE = {
Katana: Symbol("Katana"),
Ninja: Symbol("Ninja")
};
@provide(TYPE.Katana)
class Katana implements Weapon {
public hit() {
return "cut!";
}
}
@provide(TYPE.Ninja)
class Ninja implements Ninja {
private _katana: Weapon;
public constructor(
@inject(TYPE.Katana) katana: Weapon
) {
this._katana = katana;
}
public fight() { return this._katana.hit(); };
}
The basic @provide
decorator doesn't allow you to declare contextual constraints,
scope and other advanced binding features. However, inversify-binding-decorators
includes a second decorator that allows you to achieve access the full potential
of the fluent binding syntax:
import { injectable, Container } from "inversify";
import { fluentProvide, buildProviderModule } from "inversify-binding-decorators";
let TYPE = {
Weapon : "Weapon",
Ninja: "Ninja"
};
@fluentProvide(TYPE.Weapon).whenTargetTagged("throwable", true).done();
class Katana implements Weapon {
public hit() {
return "cut!";
}
}
@fluentProvide(TYPE.Weapon).whenTargetTagged("throwable", false).done();
class Shuriken implements Weapon {
public hit() {
return "hit!";
}
}
@fluentProvide(TYPE.Ninja).done();
class Ninja implements Ninja {
private _katana: Weapon;
private _shuriken: Weapon;
public constructor(
@inject(TYPE.Weapon) @tagged("throwable", false) katana: Weapon,
@inject(TYPE.Weapon) @tagged("throwable", true) shuriken: ThrowableWeapon
) {
this._katana = katana;
this._shuriken = shuriken;
}
public fight() { return this._katana.hit(); };
public sneak() { return this._shuriken.throw(); };
}
var container = new Container();
container.load(buildProviderModule());
One of the best things about the fluent decorator is that you can create aliases to fit your needs:
let provideThrowable = function(identifier, isThrowable) {
return provide(identifier)
.whenTargetTagged("throwable", isThrowable)
.done();
};
@provideThrowable(TYPE.Weapon, true)
class Katana implements Weapon {
public hit() {
return "cut!";
}
}
@provideThrowable(TYPE.Weapon, false)
class Shuriken implements Weapon {
public hit() {
return "hit!";
}
}
Another example:
let provideSingleton = function(identifier) {
return provide(identifier)
.inSingletonScope()
.done();
};
@provideSingleton(TYPE.Weapon)
class Shuriken implements Weapon {
public hit() {
return "hit!";
}
}
If you try to apply @provideFluent
multiple times:
let container = new Container();
let provideFluent = fluentProvide(container);
const provideSingleton = (identifier: any) => {
return provideFluent(identifier)
.inSingletonScope()
.done();
};
function shouldThrow() {
@provideSingleton("Ninja")
@provideSingleton("SilentNinja")
class Ninja {}
return Ninja;
}
The library will throw an exception:
Cannot apply @provideFluent decorator multiple times but is has been used multiple times in Ninja Please use done(true) if you are trying to declare multiple bindings!
We throw an exception to ensure that you are are not trying to apply @fluentProvide
multiple times by mistake.
You can overcome this by passing the force
argument to done()
:
const provideSingleton = (identifier: any) => {
return provideFluent(identifier)
.inSingletonScope()
.done(true); // IMPORTANT!
};
function shouldThrow() {
@provideSingleton("Ninja")
@provideSingleton("SilentNinja")
class Ninja {}
return Ninja;
}
let container = new Container();
container.load(buildProviderModule());
This library includes a small utility apply to add the default @provide
decorator to all
the public properties of a module:
Consider the following example:
import * as entites from "../entities";
let container = new Container();
autoProvide(container, entites);
let warrior = container.get(entites.Warrior);
expect(warrior.fight()).eql("Using Katana...");
The contents of the entities.ts file are the following:
export { default as Warrior } from "./warrior";
export { default as Katana } from "./katana";
The contents of the katana.ts file are the following:
class Katana {
public use() {
return "Using Katana...";
}
}
export default Katana;
The contents of the warrior.ts file are the following:
import Katana from "./katana";
import { inject } from "inversify";
class Warrior {
private _weapon: Weapon;
public constructor(
// we need to declare binding because auto-provide uses
// @injectbale decorator at runtime not compilation time
// in the future maybe this limitation will desapear
// thanks to design-time decorators or some other TS feature
@inject(Katana) weapon: Weapon
) {
this._weapon = weapon;
}
public fight() {
return this._weapon.use();
}
}
export default Warrior;
If you are experience any kind of issues we will be happy to help. You can report an issue using the
issues page or the
chat. You can also ask questions at
Stack overflow using the inversifyjs
tag.
If you want to share your thoughts with the development team or join us you will be able to do so using the official the mailing list. You can check out the wiki and browse the documented source code to learn more about InversifyJS internals.
Thanks a lot to all the contributors, all the developers out there using InversifyJS and all those that help us to spread the word by sharing content about InversifyJS online. Without your feedback and support this project would not be possible.
License under the MIT License (MIT)
Copyright © 2016 Remo H. Jansen
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
An utility that allows developers to declare InversifyJS bindings using ES2016 decorators
The npm package inversify-binding-decorators receives a total of 123,233 weekly downloads. As such, inversify-binding-decorators popularity was classified as popular.
We found that inversify-binding-decorators demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
/Security News
Two npm packages masquerading as WhatsApp developer libraries include a kill switch that deletes all files if the phone number isn’t whitelisted.
Research
/Security News
Socket uncovered 11 malicious Go packages using obfuscated loaders to fetch and execute second-stage payloads via C2 domains.
Security News
TC39 advances 11 JavaScript proposals, with two moving to Stage 4, bringing better math, binary APIs, and more features one step closer to the ECMAScript spec.