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

@inversifyjs/core

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@inversifyjs/core

InversifyJs core package

  • 1.3.2
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
146K
increased by13.29%
Maintainers
1
Weekly downloads
 
Created

What is @inversifyjs/core?

@inversifyjs/core is a powerful and flexible inversion of control (IoC) container for JavaScript and TypeScript applications. It helps in managing dependencies and promoting a clean architecture by using dependency injection.

What are @inversifyjs/core's main functionalities?

Dependency Injection

This feature allows you to define and inject dependencies into your classes. The code sample demonstrates how to use @inversifyjs/core to inject a Warrior instance into a Samurai class, promoting loose coupling and easier testing.

const { Container, injectable, inject } = require('inversify');

@injectable()
class Warrior {
  fight() {
    return 'Fight!';
  }
}

@injectable()
class Samurai {
  constructor(@inject(Warrior) warrior) {
    this.warrior = warrior;
  }
  battle() {
    return this.warrior.fight();
  }
}

const container = new Container();
container.bind(Warrior).toSelf();
container.bind(Samurai).toSelf();

const samurai = container.get(Samurai);
console.log(samurai.battle()); // Output: Fight!

Binding and Scoping

This feature allows you to define the lifecycle of your dependencies. The code sample shows how to bind a Ninja class in a singleton scope, ensuring that only one instance of Ninja is created and shared across the application.

const { Container, injectable } = require('inversify');

@injectable()
class Ninja {
  fight() {
    return 'Ninja fight!';
  }
}

const container = new Container();
container.bind(Ninja).toSelf().inSingletonScope();

const ninja1 = container.get(Ninja);
const ninja2 = container.get(Ninja);

console.log(ninja1 === ninja2); // Output: true

Middleware

Middleware in @inversifyjs/core allows you to intercept and modify the behavior of dependency resolution. The code sample demonstrates how to apply a middleware that logs messages before and after the execution of a dependency resolution.

const { Container, injectable, interfaces } = require('inversify');

@injectable()
class LoggerMiddleware {
  handler(next) {
    return (args) => {
      console.log('Before execution');
      const result = next(args);
      console.log('After execution');
      return result;
    };
  }
}

const container = new Container();
container.applyMiddleware((planAndResolve) => {
  return (args) => {
    console.log('Middleware applied');
    return planAndResolve(args);
  };
});

Other packages similar to @inversifyjs/core

Keywords

FAQs

Package last updated on 07 Nov 2024

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