Socket
Book a DemoInstallSign in
Socket

@ez.dev/di

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@ez.dev/di

A light DI container implementation.

1.0.0
latest
Source
npmnpm
Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

@ez.dev/di

A light DI container implementation.

Usage

Basic

You can registry Class, Factory, Value or `Alias.

import { Registry } from '@ez.dev/di'

class AuthService {}

const registry = new Registry()
const container = registry
  .addClass(AuthService)
  .addValue('foo', false)
  .addFactory(Symbol.for('GlobalOptions'), () => ({ bar: true }))
  .addAlias('baz', Symbol.for('GlobalOptions'))
  .build()

assert(container.get(AuthService) instanceof AuthService)
assert(container.get('foo') === false)
assert(container.get(Symbol.for('GlobalOptions')).bar === true)
assert(container.get('baz').bar === true)

Scopped Container

Create injection sanbox by scopes

const root = new Registry().addValue('foo', 1)
const scopped = root.scopped().addValue('bar', 2)

const rootContainer = root.build()
const scoppedContainer = scopped.build()

rootContainer.get('foo') // 1
scoppedContainer.get('foo') // 1

rootContainer.get('bar') // throws
rootContainer.tryGet('bar') // null
scoppedContainer.get('bar') // 2

Dependencies Injection

Class and Factory providers that requires dependencies will be auto resolved. And typechecking is available during registering.

class Foo {}
class Bar {}
class Baz {
  constructor(
    private foo: Foo,
    private bar: Bar,
  ) {}
}

const registry = new Registry()
registry
  .addClass(Foo)
  .addClass(Bar)
  .addClass(Baz, [Foo, Bar])
  //             ^ typechecked, type inferred from constructor parameters
  .addFactory('GlobalBaz', (foo: Foo, bar: Bar) => new Baz(foo, bar), [Foo, Bar])
  //                                                                  ^ typechecked, type inferred from factory parameters
  .build()

Keywords

di

FAQs

Package last updated on 14 Jul 2025

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

About

Packages

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc

U.S. Patent No. 12,346,443 & 12,314,394. Other pending.