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

loopus-injection

v3.0.1

Published

Dependency Injection for JS/TS

Readme

Dependency Injection library for TypeScript/JavaScript

Introduction

Dependency Injection is a code organization technique which, when used correctly, increases maintainability.

The intent behind dependency injection is to achieve separation of concerns of construction and use of objects. This can increase readability and code reuse.

Read more from Wikipedia.

Get started

Install the package

npm i loopus-injection

Register – Configure your services

import { service } from "loopus-injection";

class MyDependencyA {
  ...
}

class MyDependencyB {
  ...
}

class MyService {
  constructor(
    readonly depA: MyDependencyA,
    readonly depB: MyDependencyB) {}
  ...
}

// Configure injection
service(s => {
  s.scoped(); // 1 instance per scope
  return MyDependencyA;
});
service(s => {
  s.singleton(); // 1 shared instance
  return MyDependencyB;
});
service(s => {
  s.transient(); // many instances
  s.dependency(MyDependencyA);
  s.dependency(MyDependencyB);
  return MyService;
});

Begin a scope

  • Create a root scope with default registrations (ie registrations from your class decorators) – root scope will keep track of all your services
    const rootScope = new Scope();
  • Create a child scope (ex: let's say you develop an API – you would begin a child scope for each HTTP request)
    const childScope = rootScope.beginScope();

Resolve – Get instances of your services

const myScopedService = childScope.resolve(MyScopedService);

const mySingletonService = childScope.resolve(MySingletonService);

const myTransientService = childScope.resolve(MyTransientService);

Release – Cleanup your scopes

  • Let's say you develop an API – you would begin a child scope for each HTTP request. In that case, you would release the child scope when the HTTP request has been handled in order to cleanup all scoped services:
    childScope.release();

Advanced concepts

Custom registrations

For scenarios where you want to control how you service is constructed, use Registrations class.

Registrations.defaults.set({
  constructor: ..., // The constructor function to use as identifier
  factory: (scope: Scope) => ..., // A callback that will be used to create instances of the service
  lifetime: Lifetime.Transient // Or Scoped, Singleton
});

Composition root

In dependency injection world, composition root is a central place in your app where you put all your injection configuration code.

When using this library, you could decide to put all the configuration code in a central place, or configure each service right next to the service itself. That's up to you.

Factory Service

Let's say you want to send emails on your production environment, but when developing on your local machine you just want to log a message to the console.

The trick is to inject Scope class in order to resolve the proper service at runtime based on some configuration.

// Create an abstract class to declare a contract for our email service
abstract class EmailService {
  abstract sendEmail(to: string, body: string);
}

// We want this one to be injected in DEV
class DevEmailService extends EmailService {
  sendEmail(to: string, body: string) {
    console.log("Not sending email:", to, body);
  }
}

// We want this one to be injected in PROD
class ProdEmailService extends EmailService {
  sendEmail(to: string, body: string) {
    // TODO: Send the email for real
  }
}

// This factory service will be responsible for selecting the actual email service implementation based on some environment variable (NODE_ENV)
class EmailServiceSelector() {

  // Scope is a special service which allows you to resolve other services
  constructor(private scope: Scope) {

  }

  // Note the return type: our abstract class
  select(): EmailService {
    if (process.env.NODE_ENV === "production") {
      return this.scope.resolve(ProdEmailService);
    } else {
      return this.scope.resolve(DevEmailService);
    }
  }
}

// Configure injection
service(s => {
  s.singleton();
  return DevEmailService;
});

service(s => {
  s.scoped(); // Notice how lifetimes can be different for each implementation
  return ProdEmailService;
});

service(s => {
  s.dependency(Scope);
  s.transient(); // Important: the service factory should not have a 'higher' scope than the actual services. Here it could be Transient or Scoped, but never Singleton.
  return EmailServiceSelector;
});

// Use the service factory
const scope = new Scope();
const selector = scope.resolve(EmailServiceSelector);
const emailService = selector.select();
emailService.sendEmail("[email protected]", "hello!");

Use with Node

Should work out of the box.

Use with React

Should work out of the box.