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

rsdi

Package Overview
Dependencies
Maintainers
1
Versions
41
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rsdi

dependency injection container

  • 0.0.9
  • npm
  • Socket score

Version published
Weekly downloads
173
decreased by-77.91%
Maintainers
1
Weekly downloads
 
Created
Source

RSDI - Dependency Injection Container

Getting Started


import DIContainer, { object, get, value, factory, IDIContainer } from "rsdi";

const config = {
    "ENV": value("PRODUCTION"),               // define raw value
    "AuthStorage": object(AuthStorage).construct(
       get("Storage")                         // refer to another dependency       
    ),
    "Storage": object(CookieStorage),         // constructor without arguments    
    "dateFormatter": factory(dateFormatter),  // factory   
    "BrowserHistory": factory(configureHistory).singleton(), // factory singleton  
};
const container = new DIContainer();
container.addDefinitions(config);

function dateFormatter(date: Date) {
  return moment(date).format("ddd, DD MMM YYYY");
}
function configureHistory(container: IDIContainer): History {
    // this factory will be called only once
    const history = createBrowserHistory();
    const env = container.get("ENV");
    if (env === "production") {
        // do what you need
    }
    return history;
}

// in your code

const env = container.get<string>("ENV"); // PRODUCTION
const authStorage = container.get<AuthStorage>("AuthStorage");  // object of AuthStorage
const history = container.get<History>("BrowserHistory");  // History singleton will be returned



Motivation

Popular solution like inversify or tsyringe use reflect-metadata that allows to fetch argument types and based on that types and do autowiring. I like autowiring but the I don't like the means by which we achieve it. I don't like

  1. Those solutions in fact can deal only with typescript only. Since they rely on argument types that we don't have in JS.
  2. I have to update my tsconfig because of one package.
  3. Let my components know about injections
@injectable()
class Foo {  
}

Why component Foo should know that it's injectable?

More details thoughts in my blog article

Keywords

FAQs

Package last updated on 23 Jul 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