Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
inversify-binding-decorators
Advanced tools
An utility that allows developers to declare InversifyJS bindings using ES2016 decorators
An utility that allows developers to declare InversifyJS bindings using ES2016 decorators:
You can install inversify-binding-decorators
using npm:
$ npm install --save inversify inversify-binding-decorators reflect-metadata inversify-dts
If you are workiong with TypeScript you will need the following .d.ts
files:
/// <reference path="node_modules/reflect-metadata/reflect-metadata.d.ts" />
/// <reference path="node_modules/inversify-dts/inversify-binding-decorators/inversify-binding-decorators.d.ts" />
The InversifyJS API allows us to delcare bindings using a fluent API:
import { injectable, Kernel } from "inversify";
import "reflect-metadata";
@injectable()
class Katana implements IKatana {
public hit() {
return "cut!";
}
}
@injectable()
class Shuriken implements IShuriken {
public throw() {
return "hit!";
}
}
var kernel = new Kernel();
kernel.bind<IKatana>("IKatana").to(Katana);
kernel.bind<IShuriken>("IShuriken").to(Shuriken);
This small utility allows you to declare bindings using decorators:
import { injectable, Kernel } from "inversify";
import makeProvideDecorator from "inversify-binding-decorators";
import "reflect-metadata";
var kernel = new Kernel();
let provide = makeProvideDecorator(kernel);
@provide(Katana)
class Katana implements IKatana {
public hit() {
return "cut!";
}
}
@provide(Shuriken)
class Shuriken implements IShuriken {
public throw() {
return "hit!";
}
}
A basic example can be found at the inversify-code-samples repository.
When you invoke @provide
using classes:
@provide(Katana)
class Katana {
public hit() {
return "cut!";
}
}
@provide(Ninja)
class Ninja {
private _katana: Katana;
public constructor(
katana: Katana
) {
this._katana = katana;
}
public fight() { return this._katana.hit(); };
}
A new binding is created under the hood:
kernel.bind<Katana>(Katana).to(Katana);
kernel.bind<Ninja>(Ninja).to(Ninja);
These bindings use classes as identidiers but you can also use string literals as identifiers:
let TYPE = {
IKatana: "IKatana",
INinja: "INinja"
};
@provide(TYPE.IKatana)
class Katana implements IKatana {
public hit() {
return "cut!";
}
}
@provide(TYPE.INinja)
class Ninja implements INinja {
private _katana: IKatana;
public constructor(
@inject(TYPE.IKatana) katana: IKatana
) {
this._katana = katana;
}
public fight() { return this._katana.hit(); };
}
You can also use symbols as identifiers:
let TYPE = {
IKatana: Symbol("IKatana"),
INinja: Symbol("INinja")
};
@provide(TYPE.IKatana)
class Katana implements IKatana {
public hit() {
return "cut!";
}
}
@provide(TYPE.INinja)
class Ninja implements INinja {
private _katana: IKatana;
public constructor(
@inject(TYPE.IKatana) katana: IKatana
) {
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:
The decorator returned by makeProvideDecorator
is not fluent and is very limited when compared to makeFluentProvideDecorator
:
import { injectable, Kernel } from "inversify";
import makeFluentProvideDecoratorfrom "inversify-binding-decorators";
var kernel = new Kernel();
let provide = makeFluentProvideDecorator(kernel);
let TYPE = {
IWeapon : "IWeapon",
INinja: "INinja"
};
@provide(TYPE.IWeapon).whenTargetTagged("throwable", true).done();
class Katana implements IWeapon {
public hit() {
return "cut!";
}
}
@provide(TYPE.IWeapon).whenTargetTagged("throwable", false).done();
class Shuriken implements IWeapon {
public hit() {
return "hit!";
}
}
@provide(TYPE.INinja)
class Ninja implements INinja {
private _katana: IWeapon;
private _shuriken: IWeapon;
public constructor(
@inject(TYPE.IWeapon) @tagged("throwable", false) katana: IKatana,
@inject(TYPE.IWeapon) @tagged("throwable", true) shuriken: IShuriken
) {
this._katana = katana;
this._shuriken = shuriken;
}
public fight() { return this._katana.hit(); };
public sneak() { return this._shuriken.throw(); };
}
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.IWeapon, true)
class Katana implements IWeapon {
public hit() {
return "cut!";
}
}
@provideThrowable(TYPE.IWeapon, false)
class Shuriken implements IWeapon {
public hit() {
return "hit!";
}
}
Another example:
let provideSingleton = function(identifier) {
return provide(identifier)
.inSingletonScope()
.done();
};
@provideSingleton(TYPE.IWeapon)
class Shuriken implements IWeapon {
public hit() {
return "hit!";
}
}
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 kernel = new Kernel();
autoProvide(kernel, entites);
let warrior = kernel.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: Katana;
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: Katana
) {
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 75,255 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.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.