Gathering detailed insights and metrics for inversify-props
Gathering detailed insights and metrics for inversify-props
Gathering detailed insights and metrics for inversify-props
Gathering detailed insights and metrics for inversify-props
nmbg-inversify-props
Wrapper to use inversify as property decorator with TypeScript, useful for component libraries like Vue, React or LitElement
inversify-props-esm
This package is a wrapper of [Inversify (ES Module Support)](https://github.com/achmadk/inversify-esm) to simplify how inject your dependencies with property decorators in the components, made with TypeScript and compatible with Vue, React and other compo
inversify-hooks
Wrapper of inversify-props to inject your dependencies in the components, made with TypeScript using hooks.
@achmadk/inversify-props
Wrapper of Inversify to inject your dependencies in the components, made with TypeScript and compatible with Vue, React and other component libraries.
npm install inversify-props
Typescript
Module System
Node Version
NPM Version
82.1
Supply Chain
99.5
Quality
76.1
Maintenance
100
Vulnerability
100
License
TypeScript (96.11%)
JavaScript (3.89%)
Total Downloads
860,136
Last Day
59
Last Week
4,194
Last Month
18,052
Last Year
225,383
MIT License
85 Stars
125 Commits
9 Forks
2 Watchers
49 Branches
4 Contributors
Updated on May 10, 2024
Minified
Minified + Gzipped
Latest Version
2.2.6
Package Id
inversify-props@2.2.6
Unpacked Size
74.79 kB
Size
15.93 kB
File Count
52
NPM Version
6.14.5
Node Version
12.18.2
Cumulative downloads
Total Downloads
1
This package is a wrapper of Inversify to simplify how inject your dependencies with property decorators in the components, made with TypeScript and compatible with Vue, React and other component libraries.
Do you use Hooks? You can try the experimental package inversify-hooks
1$ npm install inversify-props reflect-metadata --save
The inversify-props type definitions are included in the inversify-props npm package.
1import 'reflect-metadata'; // Import only once 2import { container, inject } from 'inversify-props'; 3 4container.addSingleton<IService1>(Service1); 5container.addSingleton<IService2>(Service2); 6container.addSingleton(Service3); 7 8export default class extends Component { 9 @inject() service1: IService1; 10 @inject() _service2: IService2; 11 @inject() Service3: IService3; 12}
1import 'reflect-metadata'; // Import only once 2import { cid, container, inject } from 'inversify-props'; 3 4container.addSingleton<IService1>(Service1, 'MyService1'); 5 6// You can inject in other services as a Prop 7export class MyOtherService { 8 @inject() private service1: IService1; 9} 10 11// Also in the constructor as a param 12export class MyOtherService { 13 constructor(@inject() private exampleService: IExampleService) {} 14} 15 16// Or in any function as a variable 17export function myHelper() { 18 const service1 = container.get<IService1>(cid.IService1); 19} 20 21// camelCase, PascalCase and _ are allowed 22export class MyOtherService { 23 @inject() private service1: IService1; 24 @inject() private _service1: IService1; 25 @inject() private Service1: IService1; 26 @inject() private _Service1: IService1; 27}
1import 'reflect-metadata'; // Import only once 2import { container, inject } from 'inversify-props'; 3 4container.addSingleton<IService1>(Service1, 'MyService1'); 5 6export default class extends Component { 7 @inject('MyService1') service1: IService1; 8}
1import 'reflect-metadata'; // Import only once 2import { Container, inject, setContainer } from 'inversify-props'; 3 4setContainer(new Container()); 5container.addSingleton<IService1>(Service1, 'MyService1'); 6 7export default class extends Component { 8 @inject('MyService1') service1: IService1; 9}
:warning: Important! inversify-props requires TypeScript >= 2.0 and the
experimentalDecorators
,emitDecoratorMetadata
,types
andlib
compilation options in yourtsconfig.json
file.
1{ 2 "compilerOptions": { 3 "target": "es5", 4 "lib": ["es6"], 5 "types": ["reflect-metadata"], 6 "module": "commonjs", 7 "moduleResolution": "node", 8 "experimentalDecorators": true, 9 "emitDecoratorMetadata": true 10 } 11}
The idea is to add a simple wrapper that helps us to inject dependencies in components using property decorators
, we have also extend a little inversify
adding some methods that make our experience injecting dependencies easier.
You probably don't need this if:
Inversify needs an id to register our dependencies, this wrapper is going to do this for you 'magically' but if you want to uglify the code, keep reading the docs 🤓.
First of all create a class and an interface with the public methods of your class.
1// iservice1.ts 2export interface IService1 { 3 method1(): string; 4} 5 6// service.ts 7export class Service1 implements IService1 { 8 method1(): string { 9 return 'method 1'; 10 } 11}
Now is time to register the service in the container, we usually do that in app.container.ts
or app.ts
.
1container.addSingleton<IService1>(Service1);
There are some helper functions to test, the recommended way to test is beforeEach test:
1beforeEach(() => { 2 resetContainer(); 3 containerBuilder(); 4 mockSingleton<IHttpService>(cid.IHttpService, HttpServiceMock); 5});
As inversify accepts, we have configured three types of registration.
Once your dependencies are registered in the container, is simple as create a property with the name and the interface.
1export default class extends Component { 2 @inject() service1: IService1; 3}
Note: Part of the magic is that the name of the property has to be the name of the interface, this is how we don't need to add the
id
.
If you want to use Uglify or Terser to obfuscate the code, you will need to add this options to preserve the names of the classes (we need them to generate the ids magically
😉).
1new UglifyJSPlugin({ 2 uglifyOptions: { 3 keep_classnames: true, 4 keep_fnames: true 5 } 6});
1new TerserPlugin({ 2 terserOptions: { 3 keep_classnames: true, 4 keep_fnames: true 5 } 6});
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
0 commit(s) and 1 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
Reason
151 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-06-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 MoreLast Day
15.7%
59
Compared to previous day
Last Week
-13.6%
4,194
Compared to previous week
Last Month
-5.8%
18,052
Compared to previous month
Last Year
17.9%
225,383
Compared to previous year