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 🙏

© 2026 – Pkg Stats / Ryan Hefner

chaq-di-ts

v2.0.0

Published

relatively lightweight dependency injection tool

Readme

chaq-di-ts

Relatively lightweight dependency injection tool

Demo showing functionality and auto-complete

Requirements

  • TypeScript ^5.9.3
  • ES2020 or later

Usage

Terminology

  • Interface - Collection of objects, denoted by a unique name and a type, that have some dependency on each other in order to be constructed
  • Members - Individual object in a interface, has a unique name and a type
  • Dependencies - For a given member, a list of other members which need to be constructed first, as this member requires them during its creation
  • Module - Instructions describing how to construct each member given its dependencies
  • Injector - Finished implementation of interface which constructs members based on the provided module

Basic usage

// Specify an interface
interface RightTriangle {
    a: number;
    b: number;
    c: number;

    digest: string;
}

// Create injector "factory"
const makeRightTriangleInjector = makeInjectorFactory<RightTriangle>();

// ^ This pattern is due to TypeScript requiring either all or no types to be passed into a template, there
// is no partial deduction.
// So you call `makeInjectorFactory<T>` and get back the function to create the injector
// This returned function accepts generic arguments based on `T`, which allows auto-complete to work
// for the dependencies, and subsequently it allows auto-complete to work for the module,
// based on the dependencies.

// Create an injector by specifying dependencies, and then the module based on dependencies
const RightTriangleInjector = makeRightTriangleInjector(
    {
        a: [],
        b: [],
        c: ['a', 'b'],
        digest: ['a', 'b', 'c'],
    },
    {
        a: () => 3,
        b: () => 4,
        c: ({ a, b }) => Math.sqrt(a * a + b * b),
        digest: ({ a, b, c }) => `${a}^2 + ${b}^2 = ${c}^2`,
    },
);

// Get member from newly created injector, all of its dependencies will be lazily constructed
console.log(RightTriangleInjector.digest);

// Log output:
// 3^2 + 4^2 = 5^2

Checking for cycles

By default, the dependencies passed into the injector factory will be checked for cycles. If a cycle is found, a CyclicDependencyError will be thrown.

interface FooBar {
    foo: Foo;
    bar: Bar;
};

// Skipping specifying module for example
const UNUSED = {} as any;

// Will throw `CyclicDependencyError`
const FooBarInjector = makeInjectorFactory<FooBar>()(
    {
        foo: ['bar'],
        bar: ['foo'],
    },
    UNUSED,
);

If you wish to skip the cycle check, it can be turned off by specifying in the options, during injector creation.

// Will not throw, but trying to use `foo` or `bar` from `FooBarInjector`
// results in an endless loop, and eventual stack overflow.
const FooBarInjector = makeInjectorFactory<FooBar>()(
    {
        foo: ['bar'],
        bar: ['foo'],
    },
    UNUSED,
    {
        checkForCycles: 'skip',
    }
);

If you wish to see more details on where the cycle is, you can specify detailed for the checkForCycles option.

interface ABCs {
    a: number;
    b: number;
    c: number;
    d: number;
    e: number;
}

try {
    const ABCsInjector = makeInjectorFactory<ABCs>()(
        {
            // Three-way cycle loop between `a`, `b`, and `c`
            a: ['b'],
            b: ['c'],
            c: ['a'],

            // Two-way cycle loop between `e` and `d`
            d: ['e', 'a'],
            e: ['d'],

            // ^ `e` also points to `a`, but since we can't go back
            // to `e` once within the `{a, b, c}` cycle, it's treated as a
            // separate cycle.
        },
        UNUSED,
        {
            checkForCycles: 'detailed',
        },
    );
} catch (e) {
    console.log(e);
}

// Log output:
// CyclicDependencyError: At least one cycle found in provided dependencies
//     ... stack trace here {
//   cycles: [ [ 'd', 'e' ], [ 'a', 'b', 'c' ] ]
// }

Logging

Using RightTriangle example from earlier:

const RightTriangleInjector = makeRightTriangleInjector(
    {
        a: [],
        b: [],
        c: ['a', 'b'],
        digest: ['a', 'b', 'c'],
    },
    {
        a: () => 3,
        b: () => 4,
        c: ({ a, b }) => Math.sqrt(a * a + b * b),
        digest: ({ a, b, c }) => `${a}^2 + ${b}^2 = ${c}^2`,
    },
    {
        event: ({ member, eventType }) =>
            console.log(`[RightTriangle] ${member} - ${eventType.toLowerCase().replace('_', ' ')}`),
    },
);

console.log(RightTriangleInjector.digest);

// Log output:
// [RightTriangle] digest - constructing
// [RightTriangle] a - constructing
// [RightTriangle] a - constructed
// [RightTriangle] b - constructing
// [RightTriangle] b - constructed
// [RightTriangle] c - constructing
// [RightTriangle] a - already constructed
// [RightTriangle] b - already constructed
// [RightTriangle] c - constructed
// [RightTriangle] digest - constructed
// 3^2 + 4^2 = 5^2

Using types ahead of making the injector

You can use DependenciesListRecord<I> and MemberProviderModule<I, D> to model the dependencies and module without needing to call makeInjectorFactory<I>. However, for the dependencies to provide the right auto-complete in the module, it's necessary to let the dependencies object type be as precise as possible, so you should use the satisfies keyword to enforce autocomplete, and verify the type fits.

interface FooBar {
    foo: string;
    bar: string;

    combined: string;
}

// DependenciesListRecord<T> lets you define dependencies for an interface T.
const fooBarDependencies = {
    foo: [],
    bar: [],
    combined: ['foo', 'bar'],
} satisfies DependenciesListRecord<FooBar>;

// MemberProviderModule<T, D> takes an interface T, and a dependencies list record D, which is a dependencies list record of T.
const fooBraModule: MemberProviderModule<FooBar, typeof fooBarDependencies> = {
    foo: () => 'foo',
    bar: () => 'bar',
    combined: ({ foo, bar }) => `${foo}${bar}`,
};

// Then to make the injector, you pass the interface T as a template type to `makeInjectorFactory`, and then pass
// the dependencies and corresponding module into the returned factory function.
const FooBarInjector = makeInjectorFactory<FooBar>()(fooBarDependencies, fooBraModule);

console.log(FooBaerInjector.combined);

// Log output:
// foobar

Changelogs

2.0.0

  • Replace log callback option with event callback option, which provides the same set of events but in a structued object for more flexible logging. Removing option, thus backwards incompatible, incrementing major version

1.1.0

  • Improved typing to disallow self-cycles in the type system instead of needing to check at runtime