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

@nerisma/di

v1.0.14

Published

[![npm version](https://badge.fury.io/js/%40nerisma%2Fdi.svg)](https://badge.fury.io/js/%40nerisma%2Fdi)

Downloads

16

Readme

@nerisma/di

npm version

Installation

npm install @nerisma/di

Description

This is a simple dependency injection library for TypeScript. It allows you to resolve dependencies automatically.

To be able to use a dependency, just mark its class with the @Dependency decorator and use the Container.resolve method to get an instance of it.

Features

Resolve a dependency

To resolve a dependency, use the Container.resolve method.

Note: if the dependency does not yet exist in the container, it will be created and injected into to the container. If a dependency needs other dependencies, they will be resolved and injected as well.

import {Container, Dependency} from '@nerisma/di';

@Dependency() // This is needed to enable dependency resolution via the constructor
class A {
    constructor(public b: B) {}
}

@Dependency() // This is needed to be injected
class B {
    constructor() {}
}

const a = Container.resolve<A>(A);
console.log(a.b); // This will print an instance of B

Add a specific instance to the container

You may want to add a specific instance to the container manually, you can use the Container.inject method.

import {Container, Dependency} from '@nerisma/di';

@Dependency()
class A {
    constructor() {}
}

const a = new A();

// This will inject 'a' as the instance of A into the container
Container.inject(a);

Inject a class that is not decorated with @Dependency

Sometimes you might want to inject a class that is not decorated with @Dependency so its instance can be injected in a dependecy.

You can do this by using the Container.inject method with the force parameter inject to true.

import {Container, Dependency} from '@nerisma/di';

class A {
    constructor() {}
}

@Dependency()
class B {
    constructor(public a: A) {}
}

const a = new A();
Container.inject(a, true); // a is now injectable in any dependency

const b = Container.resolve<B>(B);
console.log(b.a); // This will print an instance of A)

Clearing the container

To clear the container of all dependencies, use the Container.clear method without any arguments.

You can also clear the container of specific types by providing those types as arguments.

import {Container, Dependency} from '@nerisma/di';

Container.clear(); // This will clear all dependencies
Container.clear(A); // This will clear instance of A

const dependencies = [A, B];
Container.clear(...dependencies); // This will clear instance of A and B
Container.clear(A, B); // This will also clear instance of A and B