Installations
npm install @ldlework/inversify-binding-decorators
Developer Guide
Typescript
No
Module System
CommonJS
Node Version
18.12.1
NPM Version
8.19.2
Score
73.2
Supply Chain
99.4
Quality
75.3
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
TypeScript (94.43%)
JavaScript (5.57%)
Developer
inversify
Download Statistics
Total Downloads
3,493
Last Day
1
Last Week
31
Last Month
112
Last Year
2,065
GitHub Statistics
173 Stars
341 Commits
21 Forks
9 Watching
63 Branches
19 Contributors
Package Meta Information
Latest Version
4.0.2
Package Id
@ldlework/inversify-binding-decorators@4.0.2
Unpacked Size
92.06 kB
Size
11.39 kB
File Count
56
NPM Version
8.19.2
Node Version
18.12.1
Total Downloads
Cumulative downloads
Total Downloads
3,493
Last day
0%
1
Compared to previous day
Last week
158.3%
31
Compared to previous week
Last month
-29.6%
112
Compared to previous month
Last year
65.3%
2,065
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
inversify-binding-decorators
An utility that allows developers to declare InversifyJS bindings using ES2016 decorators:
Installation
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 basics
The InversifyJS API allows us to declare 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
Using @provide multiple times
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}
Using classes, string literals & symbols as identifiers
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}
Fluent binding decorator
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:
1const provideSingleton = (identifier: any) => { 2 return fluentProvide(identifier) 3 .inSingletonScope() 4 .done(); 5}; 6 7@provideSingleton(TYPE.Weapon) 8class Shuriken implements Weapon { 9 public hit() { 10 return "hit!"; 11 } 12}
Using @fluentProvide multiple times
If you try to apply @fluentProvide
multiple times:
1let container = new Container(); 2 3const provideSingleton = (identifier: any) => { 4 return fluentProvide(identifier) 5 .inSingletonScope() 6 .done(); 7}; 8 9function shouldThrow() { 10 @provideSingleton("Ninja") 11 @provideSingleton("SilentNinja") 12 class Ninja {} 13 return Ninja; 14}
The library will throw an exception:
Cannot apply @fluentProvide 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 fluentProvide(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());
The auto provide utility
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 entities from "../entities";
2
3let container = new Container();
4autoProvide(container, entities);
5let warrior = container.get(entities.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 // @injectable decorator at runtime not compilation time 9 // in the future maybe this limitation will disappear 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;
Support
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.
Acknowledgements
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
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
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
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
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 30 are checked with a SAST tool
Score
3.7
/10
Last Scanned on 2024-12-16
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