npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@soerenuhrbach/di

v1.2.3

Published

A lightweight dependency injection container for TypeScript/JavaScript for constructor injection. Based on [tsyringe](https://github.com/microsoft/tsyringe).

Downloads

6

Readme

@soerenuhrbach/di

A lightweight dependency injection container for TypeScript/JavaScript for constructor injection. Based on tsyringe.

Installation

Install by npm

npm install --save @soerenuhrbach/di

or install with yarn

yarn add @soerenuhrbach/di

Modify your tsconfig.json to include the following settings

{
  "compilerOptions": {
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true
  }
}

Add a polyfill for the Reflect API. You can use:

The Reflect polyfill import should only be added once, and before before DI is used:

// main.ts
import "reflect-metadata";

// Your code here...

API

The container performs Constructor Injection on the constructors of decorated classes.

Decorators

@Injectable()

Class decorator factory that allows the class' dependencies to be injected at runtime.

Usage

import { Injectable } from "@soerenuhrbach/di";

@Injectable()
class Foo {
  constructor(private database: Database) {}
}

// some other file
import "reflect-metadata";
import { container } from "@soerenuhrbach/di";
import { Foo } from "./foo";

const instance = container.resolve(Foo);

@Singleton()

Class decorator factory that registers the class as a singleton within the global container.

Usage

import { Singleton } from "@soerenuhrbach/di";

@Singleton()
class Foo {
  constructor() {}
}

// some other file
import "reflect-metadata";
import { container } from "@soerenuhrbach/di";
import { Foo } from "./foo";

const instance = container.resolve(Foo);

@AutoInjectable()

Class decorator factory that replaces the decorated class' constructor with a parameterless constructor that has dependencies auto-resolved.

Usage

import { AutoInjectable } from "@soerenuhrbach/di";

@AutoInjectable()
class Foo {
  constructor(private database?: Database) {}
}

// some other file
import { Foo } from "./foo";

const instance = new Foo();

Notice how in order to allow the use of the empty constructor new Foo(), we need to make the parameters optional, e.g. database?: Database.

@Inject()

Parameter decorator factory that allows for interface and other non-class information to be stored in the constructor's metadata.

Usage

import { Injectable, Inject } from "@soerenuhrbach/di";

interface Database {
  // ...
}

@Injectable()
class Foo {
  constructor(@Inject("Database") private database?: Database) {}
}

@InjectAll()

Parameter decorator for array parameters where the array contents will come from the container. It will inject an array using the specified injection token to resolve the values.

Usage

import { Injectable, InjectAll } from "@soerenuhrbach/di";

@Injectable()
class Foo {}

@Injectable()
class Bar {
  constructor(@InjectAll(Foo) fooArray: Foo[]) {
    // ...
  }
}

Providers

A provider is registered with the DI container and provides the container the information needed to resolve an instance for a given token.

ClassProvider

This provider is used to resolve classes by their constructor. When registering a class provider you can simply use the constructor itself, unless of course you're making an alias (a class provider where the token isn't the class itself).

Usage

import { container, ClassProvider } from '@soerenuhrbach/di';

class Foo {}
const provider = new ClassProvider(Foo);
container.register("Foo", provider);

ValueProvider

This provider is used to resolve a token to a given value. This is useful for registering constants, or things that have a already been instantiated in a particular way.

Usage

import { container, ValueProvider } from '@soerenuhrbach/di';

const provider = new ValueProvider("Bar");
container.register("Foo", provider);

TokenProvider

This provider can be thought of as a redirect or an alias, it simply states that given token x, resolve using token y.

Usage

import { container, TokenProvider } from '@soerenuhrbach/di';

class Foo {}
container.register("Foo", Foo);
const provider = new TokenProvider("Foo");
container.register("FooBar", provider);

FactoryProvider

This provider is used to resolve a token using a given factory. The factory has full access to the dependency container.

Usage

import { container, FactoryProvider, ContainerInterface } from '@soerenuhrbach/di';

class Foo { constructor(public bar: string) {} }
const FooFactory = (container: ContainerInterface) => new Foo("Bar");
const provider = new FactoryProvider(FooFactory);
container.register("Foo", provider);