Socket
Book a DemoInstallSign in
Socket

@wroud/di

Package Overview
Dependencies
Maintainers
1
Versions
60
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@wroud/di

A simple and lightweight dependency injection library for JavaScript and TypeScript

latest
Source
npmnpm
Version
0.19.3
Version published
Maintainers
1
Created
Source

@wroud/di

ESM-only package NPM version

@wroud/di is a lightweight dependency injection library for JavaScript, inspired by .NET's DI system. Written in TypeScript, it supports modern JavaScript features, including decorators, and provides robust dependency management capabilities.

Features

  • Modern JavaScript: Leverages ES modules, decorators, and asynchronous service loading for advanced performance optimizations.
  • TypeScript: Written in TypeScript for type safety.
  • Flexible DI: Supports singleton, transient, and scoped services.
  • Pure ESM package

Installation

Install via npm:

npm install @wroud/di

Install via yarn

yarn add @wroud/di

Documentation

For detailed usage and API reference, visit the documentation site.

Example

import {
  createService,
  injectable,
  ServiceContainerBuilder,
} from "@wroud/di";

interface ILogger {
  log: (message: string) => void;
}

interface IAuthService {
  authenticate(user: string, password: string): void;
}

const ILogger = createService<ILogger>("ILogger");
const IAuthService = createService<IAuthService>("IAuthService");

function loggerFactory(): ILogger {
  return {
    log: (message: string) => console.log(message),
  };
}

@injectable(() => [ILogger])
class AuthService implements IAuthService {
  constructor(private readonly logger: ILogger) {}

  authenticate(user: string, password: string): void {
    this.logger.log(`Authenticating user ${user}`);
    // Authentication logic here
  }
}

const provider = new ServiceContainerBuilder()
  .addSingleton(ILogger, loggerFactory)
  .addSingleton(IAuthService, AuthService)
  .build();

const authService = provider.getService(IAuthService);

await authService.authenticate("user1", "password");

Changelog

All notable changes to this project will be documented in the CHANGELOG file.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Keywords

dependency injection

FAQs

Package last updated on 28 Sep 2025

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts