Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

inversify-hooks

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

inversify-hooks

Under construction

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
520
decreased by-44.09%
Maintainers
1
Weekly downloads
 
Created
Source

Inversify Props

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.

GitHub last commit GitHub license GitHub forks GitHub contributors GitHub issues

Installation

npm install --save inversify-props

Usage

container.addSingleton<IService1>(Service1);
container.addSingleton<IService2>(Service2);

export default class extends Component {
  @Inject() service1: IService1;
  @Inject() service2: IService2;
}

Why we made this package

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:

  • You have experience using inversify and you don't need to simplify the process.
  • You want to use all the power of inversify, we are only injecting dependencies like services, helpers, utils...
  • You don't want to inject your dependencies as properties.

How register a dependency

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.

// iservice1.ts
export interface IService1 {
    method1(): string;
}

// service.ts
@injectable()
export class Service1 implements IService1 {
  method1(): string {
    return 'method 1';
  }
}

Note: Don't forget to decorate the class as @injectable() this will made your class candidate to be injectable inside other.

Now is time to register the service in the container, we usually do that in app.container.ts or app.ts.

 container.addSingleton<IService1>(Service1);

Other ways to register a class

As inversify accepts, we have configured three types of registration.

  • Singleton: The dependency will be created only once, one dependency - one object.
  • Transient: The dependency will be created each time is injected, one dependency - one object per injection.
  • Request: Special case of singleton, more info in official docs.

How to use in your components

Once your dependencies are registered in the container, is simple as create a property with the name and the interface.

export default class extends Component {
  @Inject() service1: IService1;
}

Note: Part of the magic is that the name of the property has to be the name of the interface in camelCase without the 'I', this is how we don't need to add the id.

How to configure uglify

If you want to use uglify to ofuscate the code, you will need to add this options to preserve the names of the clases (we need them to generate the ids magically 😉).

new UglifyJSPlugin({
  uglifyOptions: {
    keep_classnames: true,
    keep_fnames: true,
  }
});

Next steps

  • Investigate if can we remove @injectable

Keywords

FAQs

Package last updated on 10 Mar 2019

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc