Socket
Socket
Sign inDemoInstall

tsyringe

Package Overview
Dependencies
Maintainers
2
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tsyringe

Lightweight dependency injection container for JavaScript/TypeScript


Version published
Weekly downloads
354K
increased by1.36%
Maintainers
2
Weekly downloads
 
Created

What is tsyringe?

tsyringe is a lightweight dependency injection container for TypeScript and JavaScript applications. It helps manage class dependencies and promotes the use of the Inversion of Control (IoC) principle, making it easier to write modular, testable, and maintainable code.

What are tsyringe's main functionalities?

Dependency Injection

This feature allows you to inject dependencies into classes. In the example, `Foo` is injected into `Bar`, and `Bar` can use `Foo`'s methods.

const { container, injectable, inject } = require('tsyringe');

@injectable()
class Foo {
  log() {
    console.log('Foo');
  }
}

@injectable()
class Bar {
  constructor(@inject(Foo) foo) {
    this.foo = foo;
  }
  log() {
    this.foo.log();
    console.log('Bar');
  }
}

const bar = container.resolve(Bar);
bar.log();

Singleton Registration

This feature allows you to register a class as a singleton, ensuring that only one instance of the class is created and shared across the application.

const { container, singleton } = require('tsyringe');

@singleton()
class SingletonService {
  constructor() {
    this.id = Math.random();
  }
}

const instance1 = container.resolve(SingletonService);
const instance2 = container.resolve(SingletonService);
console.log(instance1.id === instance2.id); // true

Scoped Registration

This feature allows you to register a class with a scoped lifecycle, meaning a new instance is created for each container scope.

const { container, scoped, Lifecycle } = require('tsyringe');

@scoped(Lifecycle.ContainerScoped)
class ScopedService {
  constructor() {
    this.id = Math.random();
  }
}

const childContainer = container.createChildContainer();
const instance1 = childContainer.resolve(ScopedService);
const instance2 = childContainer.resolve(ScopedService);
console.log(instance1.id === instance2.id); // true

const instance3 = container.resolve(ScopedService);
console.log(instance1.id === instance3.id); // false

Custom Providers

This feature allows you to register custom providers for dependencies, giving you more control over how dependencies are resolved.

const { container, inject, injectable } = require('tsyringe');

class CustomProvider {
  get() {
    return 'Custom Value';
  }
}

container.register('CustomProvider', { useClass: CustomProvider });

@injectable()
class Consumer {
  constructor(@inject('CustomProvider') provider) {
    this.provider = provider;
  }
  log() {
    console.log(this.provider.get());
  }
}

const consumer = container.resolve(Consumer);
consumer.log(); // Custom Value

Other packages similar to tsyringe

Keywords

FAQs

Package last updated on 10 Sep 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