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

ts-injecty

v0.0.22

Published

Dependency injection container without dependencies

Downloads

937

Readme

Dependency Injection Container for Typescript and Javascript 💉

Getting Started

Features 🏋️‍♂️

  • 👌 No dependencies
  • 🚀 Simple but powerful
  • 🎄 Does not requires decorators
  • 🏋️‍♂️ Works great with both javascript and typescript
  • 🏎️ Auto register dependencies class.
  • 🧩 Well typed for your dependencies

New version 🚀

Now we are supporting factories 🏭

// You must extends your custom factories from Factory, and we will inject automatically the resolver in your factory.

export class FactoryImplementation extends Factory {
    public create(type: "Bar" | "Buzz") {
        if (type === "Bar") {
            return this.resolver.resolve(Bar);
        }

        return this.resolver.resolve(Buzz);
    }
}

Container.register([
    register(Bar).build(),
    register(Buzz).build(),
    register(FactoryImplementation).build(),
]);

const resolved = Container.resolve(FactoryImplementation);

expect(resolved.create("Bar")).toBeInstanceOf(Bar);
expect(resolved.create("Buzz")).toBeInstanceOf(Buzz);

Version: 0.0.18 👇

Support the constructor parameters fully typed

// Now in your IDE you can see the withDependencies, this function is automatically generated only when your class has more than one argument.
class Root {
    constructor(public innerA: InnerRoot, public innerB: InnerRootB) {}
}
register(Root).withDependencies(InnerRoot, InnerRootB).build();

---
// If your class has just one parameter you can see this method
class InnerRoot {
    constructor(public inner: InnerDep) {}
}
register(InnerRootB).withDependency(InnerDep).build();

---
// If your class doesn't have any dependency you can't invoke the withDependency.
class NoDeps {}
register(NoDeps).build();

Motivation 🏃‍♀️

Popular solutions like inversify or tsyringe use reflect-metadata that allows to fetch argument types and based on those types and do autowiring. Autowiring is a nice feature but the trade-off is decorators. Disadvantages of other solutions

  1. Those solutions work with typescript only. Since they rely on argument types that we don't have in Javascript.
  2. I have to update my tsconfig because one package requires it.
  3. Let my components know about injections.
  4. They do not respect the principles of clean architecture.

Usage 🥁

Register Class with Class dependency

export class A {
    constructor(private b: B) {}

    public sum(x: number, y: number) {
        return this.b.sum(x, y);
    }
}

export class B {
    public sum(x: number, y: number) {
        return x + y;
    }
}

const dependencies = [register(A).withDependency(B).build()];

Container.register(dependencies);

const a = Container.resolve(A);

expect(a.sum(1, 2)).toBe(3);

Register interface with class implementation

export interface Interface {
    doSomething(): string;
}

export class InterfaceImplementation implements Interface {
    doSomething(): string {
        return "HI";
    }
}

export class ClassWithInterfaceDependency {
    constructor(private readonly dependency: Interface) {}
    doSomething(): string {
        return this.dependency.doSomething();
    }
}

Container.register([
    register("Interface").withImplementation(InterfaceImplementation).build(),
    register(ClassWithInterfaceDependency).withDependency("Interface").build(),
]);

const resolved = Container.resolve(ClassWithInterfaceDependency);

expect(resolved.doSomething()).toBe("HI");

Register objects or functions as reference without any manipulation from container

const FooFunction = () => "HI";

Container.register([
    register("Foo").withImplementation(FooFunction).build(),
]);

const resolved = Container.resolve<typeof FooFunction>("Foo");

expect(resolved).toBe(FooFunction);
expect(resolved()).toBe("HI")

NOTE: The difference for withImplementation or withDynamic is that the first one just save the function reference, but the last one is used for hooks or dynamic functions with reactivity or re rendering cases.

Register api

//if you want register interfaces 👇
//register interface with custom string, because typescript doesn't transpile interfaces.
register("Interface").withDynamic(parameter: Function).build();
register("Interface").withImplementation(implementation: Function | Class | object).build();

//if you want register classes 👇
// Singleton registration
register(YourClass).asASingleton().build();

register(YourClass).asASingleton().withDependency(dependency: string | Function | Class).build();

register(YourClass).asASingleton().withImplementation(implementation: Function | Class | object).build();

// Transient registration without dependencies
register(YourClass).build();

//The dependency can be a string because previously we can register interfaces.
register(YourClass).withDependency(dependency: string | Function | Class).build();

register(YourClass).withImplementation(implementation: Function | Class | object).build();