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

universal-common-dependencyinjection

v1.0.0

Published

A library containing classes to support dependency injection.

Downloads

4

Readme

universal-common-dependencyinjection

A lightweight dependency injection library for JavaScript applications.

Installation

npm install universal-common-dependencyinjection

Overview

This library provides a simple, flexible dependency injection system for JavaScript applications:

  • ServiceCollection - Container for registering services with different lifetimes
  • ServiceProvider - Resolves registered services when needed in your application

Usage

Importing

import { ServiceCollection, ServiceProvider } from "universal-common-dependencyinjection";

Basic Service Registration

class Logger {
  log(message) {
    console.log(`[LOG]: ${message}`);
  }
}

class UserService {
  #logger;
  
  constructor(logger) {
    this.#logger = logger;
  }
  
  getUser(id) {
    this.#logger.log(`Getting user with ID: ${id}`);
    return { id, name: "John Doe" };
  }
}

// Create a service collection
const services = new ServiceCollection();

// Register services
services.addSingleton(Logger, () => new Logger());
services.addTransient(UserService, (serviceProvider) => {
  const logger = serviceProvider.getRequiredService(Logger);
  return new UserService(logger);
});

// Build the service provider
const serviceProvider = services.buildServiceProvider();

// Resolve and use services
const userService = serviceProvider.getRequiredService(UserService);
const user = userService.getUser(123);
console.log(user); // { id: 123, name: "John Doe" }

Singleton Services

Singleton services are created once and the same instance is returned for each request:

class DatabaseConnection {
  constructor() {
    console.log("Opening database connection...");
  }
  
  query(sql) {
    console.log(`Executing query: ${sql}`);
  }
}

const services = new ServiceCollection();

services.addSingleton(DatabaseConnection, () => new DatabaseConnection());

const serviceProvider = services.buildServiceProvider();

const db1 = serviceProvider.getService(DatabaseConnection);
const db2 = serviceProvider.getService(DatabaseConnection);

console.log(db1 === db2); // true

Transient Services

Transient services are created each time they are requested:

class RequestHandler {
  #requestId;
  
  constructor() {
    this.#requestId = Math.random().toString(36).substr(2, 9);
    console.log(`Created request handler: ${this.#requestId}`);
  }
  
  get requestId() {
    return this.#requestId;
  }
}

const services = new ServiceCollection();

services.addTransient(RequestHandler, () => new RequestHandler());

const serviceProvider = services.buildServiceProvider();

const handler1 = serviceProvider.getService(RequestHandler);
const handler2 = serviceProvider.getService(RequestHandler);

console.log(handler1 === handler2);
console.log(handler1.requestId);
console.log(handler2.requestId);

Pre-Built Instances

You can register pre-built instances as singletons:

class Configuration {
  #settings;
  
  constructor(settings) {
    this.#settings = settings;
  }
  
  getSetting(key) {
    return this.#settings[key];
  }
}

const config = new Configuration({
  apiUrl: "https://api.example.com",
  timeout: 5000
});

const services = new ServiceCollection();

// Register the pre-built instance
services.addSingleton(Configuration, config);

const serviceProvider = services.buildServiceProvider();

// The pre-built instance is returned
const resolvedConfig = serviceProvider.getService(Configuration);
console.log(resolvedConfig.getSetting("apiUrl")); // https://api.example.com

Resolving Optional Services

You can handle optional dependencies using getService():

class AnalyticsService {
  #logger;
  
  constructor(logger) {
    this.#logger = logger || {
      log: () => {}
    };
  }
  
  trackEvent(name, data) {
    this.#logger.log(`Tracking event: ${name}`);
    // Implementation
  }
}

const services = new ServiceCollection();
services.addTransient(AnalyticsService, (provider) => {
  const logger = provider.getService(Logger);
  return new AnalyticsService(logger);
});

const serviceProvider = services.buildServiceProvider();
const analytics = serviceProvider.getRequiredService(AnalyticsService);
analytics.trackEvent("page_view", { path: "/home" });

Service Dependencies

Services can depend on other services, which will be resolved automatically:

class AuthService {
  authenticate(credentials) {
    return { userId: 1, role: "admin" };
  }
}

class UserRepository {
  getById(id) {
    return { id, name: "John Doe" };
  }
}

class UserController {
  #authService;
  #userRepository;
  
  constructor(authService, userRepository) {
    this.#authService = authService;
    this.#userRepository = userRepository;
  }
  
  login(credentials) {
    const authResult = this.#authService.authenticate(credentials);
    if (authResult) {
      const user = this.#userRepository.getById(authResult.userId);
      return {
        user,
        token: "jwt-token-here"
      };
    }
    return null;
  }
}

const services = new ServiceCollection();

services.addSingleton(AuthService, () => new AuthService());
services.addSingleton(UserRepository, () => new UserRepository());

// UserController depends on AuthService and UserRepository
services.addTransient(UserController, (provider) => {
  const authService = provider.getRequiredService(AuthService);
  const userRepository = provider.getRequiredService(UserRepository);
  return new UserController(authService, userRepository);
});

const serviceProvider = services.buildServiceProvider();
const userController = serviceProvider.getRequiredService(UserController);

const loginResult = userController.login({ username: "john", password: "secret" });
console.log(loginResult);