Gathering detailed insights and metrics for inversify-binding-decorators
Gathering detailed insights and metrics for inversify-binding-decorators
Gathering detailed insights and metrics for inversify-binding-decorators
Gathering detailed insights and metrics for inversify-binding-decorators
npm install inversify-binding-decorators
Typescript
Module System
Node Version
NPM Version
99.6
Supply Chain
100
Quality
80.8
Maintenance
100
Vulnerability
100
License
TypeScript (94.43%)
JavaScript (5.57%)
Total Downloads
6,764,500
Last Day
18,609
Last Week
136,599
Last Month
641,428
Last Year
2,954,368
173 Stars
341 Commits
21 Forks
9 Watching
63 Branches
19 Contributors
Latest Version
4.0.0
Package Id
inversify-binding-decorators@4.0.0
Size
11.46 kB
NPM Version
5.5.1
Node Version
8.9.3
Publised On
19 Apr 2018
Cumulative downloads
Total Downloads
Last day
-13.5%
18,609
Compared to previous day
Last week
-9%
136,599
Compared to previous week
Last month
32.5%
641,428
Compared to previous month
Last year
148.9%
2,954,368
Compared to previous year
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:
1import { injectable, Container } from "inversify"; 2import "reflect-metadata"; 3 4@injectable() 5class Katana implements Weapon { 6 public hit() { 7 return "cut!"; 8 } 9} 10 11@injectable() 12class Shuriken implements ThrowableWeapon { 13 public throw() { 14 return "hit!"; 15 } 16} 17 18var container = new Container(); 19container.bind<Katana>("Katana").to(Katana); 20container.bind<Shuriken>("Shuriken").to(Shuriken);
This small utility allows you to declare bindings using decorators:
1import { injectable, Container } from "inversify"; 2import { provide, buildProviderModule } from "inversify-binding-decorators"; 3import "reflect-metadata"; 4 5@provide(Katana) 6class Katana implements Weapon { 7 public hit() { 8 return "cut!"; 9 } 10} 11 12@provide(Shuriken) 13class Shuriken implements ThrowableWeapon { 14 public throw() { 15 return "hit!"; 16 } 17} 18 19var container = new Container(); 20// Reflects all decorators provided by this package and packages them into 21// a module to be loaded by the container 22container.load(buildProviderModule()); 23
If you try to apply @provide
multiple times:
1@provide("Ninja") 2@provide("SilentNinja") 3class Ninja { 4 // ... 5}
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
:
1@provide("Ninja", true) 2@provide("SilentNinja", true) 3class Ninja { 4 // ... 5}
When you invoke @provide
using classes:
1@provide(Katana) 2class Katana { 3 public hit() { 4 return "cut!"; 5 } 6} 7 8@provide(Ninja) 9class Ninja { 10 private _katana: Weapon; 11 public constructor( 12 katana: Weapon 13 ) { 14 this._katana = katana; 15 } 16 public fight() { return this._katana.hit(); }; 17}
A new binding is created under the hood:
1container.bind<Katana>(Katana).to(Katana); 2container.bind<Ninja>(Ninja).to(Ninja);
These bindings use classes as identidiers but you can also use string literals as identifiers:
1let TYPE = { 2 IKatana: "Katana", 3 INinja: "Ninja" 4}; 5 6@provide(TYPE.Katana) 7class Katana implements Weapon { 8 public hit() { 9 return "cut!"; 10 } 11} 12 13@provide(TYPE.Ninja) 14class Ninja implements Ninja { 15 16 private _katana: Weapon; 17 18 public constructor( 19 @inject(TYPE.Katana) katana: Weapon 20 ) { 21 this._katana = katana; 22 } 23 24 public fight() { return this._katana.hit(); }; 25 26}
You can also use symbols as identifiers:
1let TYPE = { 2 Katana: Symbol("Katana"), 3 Ninja: Symbol("Ninja") 4}; 5 6@provide(TYPE.Katana) 7class Katana implements Weapon { 8 public hit() { 9 return "cut!"; 10 } 11} 12 13@provide(TYPE.Ninja) 14class Ninja implements Ninja { 15 16 private _katana: Weapon; 17 18 public constructor( 19 @inject(TYPE.Katana) katana: Weapon 20 ) { 21 this._katana = katana; 22 } 23 24 public fight() { return this._katana.hit(); }; 25 26}
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:
1import { injectable, Container } from "inversify"; 2import { fluentProvide, buildProviderModule } from "inversify-binding-decorators"; 3 4let TYPE = { 5 Weapon : "Weapon", 6 Ninja: "Ninja" 7}; 8 9@fluentProvide(TYPE.Weapon).whenTargetTagged("throwable", true).done(); 10class Katana implements Weapon { 11 public hit() { 12 return "cut!"; 13 } 14} 15 16@fluentProvide(TYPE.Weapon).whenTargetTagged("throwable", false).done(); 17class Shuriken implements Weapon { 18 public hit() { 19 return "hit!"; 20 } 21} 22 23@fluentProvide(TYPE.Ninja).done(); 24class Ninja implements Ninja { 25 26 private _katana: Weapon; 27 private _shuriken: Weapon; 28 29 public constructor( 30 @inject(TYPE.Weapon) @tagged("throwable", false) katana: Weapon, 31 @inject(TYPE.Weapon) @tagged("throwable", true) shuriken: ThrowableWeapon 32 ) { 33 this._katana = katana; 34 this._shuriken = shuriken; 35 } 36 37 public fight() { return this._katana.hit(); }; 38 public sneak() { return this._shuriken.throw(); }; 39 40} 41 42var container = new Container(); 43container.load(buildProviderModule());
One of the best things about the fluent decorator is that you can create aliases to fit your needs:
1let provideThrowable = function(identifier, isThrowable) { 2 return provide(identifier) 3 .whenTargetTagged("throwable", isThrowable) 4 .done(); 5}; 6 7@provideThrowable(TYPE.Weapon, true) 8class Katana implements Weapon { 9 public hit() { 10 return "cut!"; 11 } 12} 13 14@provideThrowable(TYPE.Weapon, false) 15class Shuriken implements Weapon { 16 public hit() { 17 return "hit!"; 18 } 19}
Another example:
1let provideSingleton = function(identifier) { 2 return provide(identifier) 3 .inSingletonScope() 4 .done(); 5}; 6 7@provideSingleton(TYPE.Weapon) 8class Shuriken implements Weapon { 9 public hit() { 10 return "hit!"; 11 } 12}
If you try to apply @provideFluent
multiple times:
1let container = new Container(); 2let provideFluent = fluentProvide(container); 3 4const provideSingleton = (identifier: any) => { 5 return provideFluent(identifier) 6 .inSingletonScope() 7 .done(); 8}; 9 10function shouldThrow() { 11 @provideSingleton("Ninja") 12 @provideSingleton("SilentNinja") 13 class Ninja {} 14 return Ninja; 15}
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()
:
1 2const provideSingleton = (identifier: any) => { 3 return provideFluent(identifier) 4 .inSingletonScope() 5 .done(true); // IMPORTANT! 6}; 7 8function shouldThrow() { 9 @provideSingleton("Ninja") 10 @provideSingleton("SilentNinja") 11 class Ninja {} 12 return Ninja; 13} 14let container = new Container(); 15container.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:
1import * as entites from "../entities";
2
3let container = new Container();
4autoProvide(container, entites);
5let warrior = container.get(entites.Warrior);
6expect(warrior.fight()).eql("Using Katana...");
The contents of the entities.ts file are the following:
1export { default as Warrior } from "./warrior"; 2export { default as Katana } from "./katana";
The contents of the katana.ts file are the following:
1class Katana { 2 public use() { 3 return "Using Katana..."; 4 } 5} 6 7export default Katana;
The contents of the warrior.ts file are the following:
1import Katana from "./katana"; 2import { inject } from "inversify"; 3 4class Warrior { 5 private _weapon: Weapon; 6 public constructor( 7 // we need to declare binding because auto-provide uses 8 // @injectbale decorator at runtime not compilation time 9 // in the future maybe this limitation will desapear 10 // thanks to design-time decorators or some other TS feature 11 @inject(Katana) weapon: Weapon 12 ) { 13 this._weapon = weapon; 14 } 15 public fight() { 16 return this._weapon.use(); 17 } 18} 19 20export 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.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 3/6 approved changesets -- score normalized to 5
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-12-23
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More