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

cruet

v0.2.1

Published

A **minimalist TypeScript Dependency Injection Container** written as a ES6 Module.

Downloads

17

Readme

cruet

A minimalist TypeScript Dependency Injection Container written as a ES6 Module.

Why cruet? Well, most importantly, it works anywhere modern JavaScript runs, is built in TypeScript and works in scenarios where other containers fail (like ts-syringe and npm workspaces ;)).

Targets Node 16+.

Contents

Installation

Install the cruet package from npm:

npm install cruet --save-dev

Set the experimentalDecorators flag to true in your tsconfig.json file.

{
    "compilerOptions": {
        ...
        "experimentalDecorators": true
    }
}

You can import cruet as an ES Module:

import { Container } from "cruet";

Type definitions are provided.

Basic Usage

First, you need to create a container instance.

import { Container } from "cruet";

const container = new Container();

Then, you can register your dependencies.

 // Register a class
container.register(TestClass);

// Register a class with a custom activation key
container.register("Key", TestClass);

// Register a class with a factory function
// Optionally, you can use the container during your factory function
container.register(SomeDepWhichNeedsAFactory, () => new SomeDepWhichNeedsAFactory("abc"));
container.register(SomeDepWhichNeedsAFactory, (current: Container) => new SomeDepWhichNeedsAFactory("abc"));

// Register an instance for a class
container.register(SomeDepWhichNeedsAFactory, new SomeDepWhichNeedsAFactory("abc"));

// Register a class with an explicit instance of IRegistration
container.register(SomeDepWhichNeedsAFactory, { using: () => new SomeDepWhichNeedsAFactory("abc") });

// Register a dependency by key
container.register("foo", () => "bar");

// Re-Register a depedency (replaces, rather than adds additional registrations)
container.reregister("foo", () => "baz");

// Clear registrations
container.clearRegistrations("foo");

Parameters that exist in constructors must be annotated so that the container understands them. To do this, you need to use the @Inject decorator.

import { Inject } from "cruet";

class TestClassWithDep {
    public foo: SomeDep;

    constructor(@Inject("SomeDep") someDep: SomeDep) {
        this.foo = someDep;
    }
}

By convention, use the class name when your dependencies are a class, and the parameter name when the dependencies are functions or values.

Once, annotated, you can resolve your dependencies from the container:

const container = new Container();
container.register(TestClass);

let result = container.get<TestClass>(TestClass);

// You can call get using a variety of different options
// with different degrees of type inference
result = container.get(TestClass);
result = container.get("TestClass");
result = container.get<TestClass>("TestClass");

expect(result).toBeInstanceOf(TestClass);

Your dependency graph will be resolved by the container, and the dependencies will be constructor injected.

Lifecycles

We support both singleton and transient life cycles. All activations are transient by default, but you can switch this default when you create your Container instance.

const container = new Container({
    defaultActivationLifecycle: "singleton" // or "transient"
})

You can also set the life cycle of a dependency manually.

// During the registration, you can also set the lifetime of the dependency
container.register(TestClass).asSingleton();
container.register(TestClass).asTransient();

This overrides the global default of the container.

Beware if you injects a transient object into a singleton, the singleton instance will be cached at runtime including any of it's created dependencies, regardless of how they were configured. The root object you request from the container will ultimately control the lifecycle of it's entire heirarchy downwards.

Transient objects injected into objects marked as singletons will always become singletons in their respective scopes.

Conditional Injection

You can add dependency filters to your registrations. These filters can be combined and chained to more tightly control dependency creation.

whenInjectedInto

whenInjectedInto allows you to control which implementation gets injected based on the dependency root.

container.register(WithDependencyOne);
container.register(WithDependencyTwo);
container.register(SomeDep, () => (new SomeDep("abc"))).whenInjectedInto(WithDependencyOne);
container.register(SomeDep, () => (new SomeDep("def"))).whenInjectedInto(WithDependencyTwo);

const result1 = container.get<WithDependencyOne>(WithDependencyOne); // SomeDep("abc")
const result2 = container.get<WithDependencyTwo>(WithDependencyTwo); // SomeDep("def")

inScope

inScope allows you to control which implementation gets injected based on a named dependency scope.

container.register(SomeDep).asSingleton(); // default scope
container.register(SomeDep).asTransient().inScope("special-scope");

const result1 = container.get<SomeDep>(SomeDep);
const result2 = container.get<SomeDep>(SomeDep);
const result3 = container.get<SomeDep>(SomeDep, "special-scope");
const result4 = container.get<SomeDep>(SomeDep, "special-scope");

expect(result1 === result2).toBe(true);  // true because they are registered as singletons
expect(result2 === result3).toBe(false); // false because they're requested from different scopes 
expect(result3 === result4).toBe(false); // false because the dependency in this scope is marked as transient

when

when allows you to control which implementation gets injected based on a condition.

container.register(SomeDep).asTransient().when(() => true); // Put whatever you like that returns a bool here.

Registration Modules

Registration modules allow you to capture your registrations in a class - this is useful when you want to ship some registrations in a package with some dependencies in a shared project.

You can define a Registration Module like this:

class TestModule implements IRegistrationModule {
    public registerComponents(container: Container): void {
        container.register(TestClass);
    }
}

Then register it with your container:

container.addModule(new TestModule());
const result = container.get<TestClass>(TestClass);

Credits

cruet is © David Whitney 2022.