New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@brainstack/inject

Package Overview
Dependencies
Maintainers
3
Versions
145
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@brainstack/inject

Micro and ultra light Dependency Injection Container

  • 1.0.0
  • npm
  • Socket score

Version published
Weekly downloads
4
decreased by-75%
Maintainers
3
Weekly downloads
 
Created
Source

@brainstack/microinject


Banner 160 bytes 0 dependencies

Getting Started

Common design pattern to write loosely coupled code and being polite to others reading it.

Common use case
Reusability, scalability Avoid code duplication

Install

Run of one these command to install.

$ npm i @brainstack/microinject

or

$ yarn add @brainstack/microinject

Examples

To add, remove and consume a dependency.

Add

import { Container } from "@brainstack/microinject";

const inject = Container();
inject.add("dependency", (a)=>a*2)

Initialize

import { Container } from "@brainstack/microinject";

const inject = Container({
  test: (a)=>a*2
});

Remove

import { Container } from "@brainstack/microinject";

const inject = Container({
  test: (a)=>a*2
});

inject.remove("test")

Consume

import { Container } from "@brainstack/microinject";

const inject = Container({
  test: (a)=>a*2
});

const myTest = inject.getInstance("test")
console.log(myTest(2))

Learn

Let's create program to show weather. Everywhere in that app is required to have the weather.

We could write everywhere to fetch the weather directly.

What is going to happen if the weather url change? It's going to be required to walk trhough code and replace to another url. How are you going to test if url is different somewhere.

Using Dependency Injection, it's possible to replace the implementation without having impact somewhere else. In this case, changing url.

import { Container } from "@brainstack/microinject";
// Initialize
const inject = Container({
  getWeather: await fetchWeather("https://my.weather.api.com")
});


// Consumer where you need it
const weather = inject.getInstance("getWeather")
console.log(weather())

Powered 🚀 by Infinisoft World Inc.
Wanna code the future? Come with us [https://www.infinisoft.dev]

FAQs

Package last updated on 18 May 2023

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