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

@spirex/di

v0.0.4

Published

Dependency injection library for JS/TS projects

Downloads

9

Readme

SpireX DI

Dependency injection library for JS/TS projects.

Why?

  • Easy and fast integration into a project of any complexity
  • Independence of the project infrastructure from the DI library
  • No global god-objects
  • Expandability

Features

  • Strong typing and secure container
  • Using instances and factories for injection
  • Instance lifecycle management
  • Injection by name
  • Conditional configuration
  • Delayed dependency injection (Provider / Lazy)
  • Splitting container configuration into modules
  • Auto-dispose scoped instances
  • Auto-detection of cyclic dependencies

Quick start

Step 1. Type definition.

Unfortunately JS/TS doesn't have runtime types, you have to define your own type "keys". For example, you can use string enum:

export enum AppType {
    AppManager = 'AppManager',
    HttpClient = 'HttpClient',
    ApiBaseUrl = 'ApiBaseUrl',
    Logger = 'Logger',
}

Step 2. Type map (TypeScript only).

In order for the DI container to understand what types it is dealing with, it needs to describe the type map.

It indicates the relationship between the key and a specific type:

import { IAppManager } from '../modules/app/abstract'
import { IHttpClient } from '../modules/network/abstract'
import { ILogger } from '../logger'

export type AppTypeMap = {
    [AppType.AppManager]: IAppManager;
    [AppType.HttpClient]: IHttpClient;
    [AppType.ApiBaseUrl]: string;
    [AppType.Logger]: ILogger;
}

Step 3. Container configuration.

Configuration of the container is carried out using a "builder". The builder requires TypeMap generic parameter to be set for the container typing. Container builder uses "bindings" to bind types with instances and factories.

The factory function takes "resolver" as a parameter. It can be used to provide required dependencies.

import { DIContainer } from 'spx-di'
import { AppManager } from '../modules/app'
import { ConsoleLogger, NetworkLogger } from '../logger'
import { HttpClient } from '../modules/network'

const container = DIContainer.builder<AppTypeMap>()
    // Binding a factory function to the `AppManager` type
    .bindFactory(
        AppType.AppManager,
        r => new AppManager(
            r.get(AppType.HttpClient),  // требует HttpClient
            r.get(AppType.Logger),      // требует Logger
        ),
    )

    // Binding a value as `ApiBaseUrl` type
    .bindInstance(
        AppType.ApiBaseUrl,
        "https://example.com/api", // This is the value that will be provided
    )
    
    // Conditional binding
    // The "when" statement only affects the next call of bindFactory/bindInstance
    .when(NODE_ENV !== 'production') // on dev environment
        .bindFactory(
            AppType.Logger,
            () => new ConsoleLogger(), // ConsoleLogger will be used
        )
    .when(NODE_ENV === 'production') // on production environment
        .bindFactory(
            AppType.Logger,
            r => new NetworkLogger(        // NetworkLogger will be used
                r.get(AppType.HttpClient), // it depends on HttpClient
            ),
        )
    .bindFactory(
        AppType.HttpClient,
        r => new HttpClient(
            r.get(AppType.ApiBaseUrl),
        ),
    )
    .build() // Commit configuration and build DIContainer

// Define type of container to refer to in the source code
export type AppContainer = typeof container

Final step 4. Using a dependency injection container.

Now the container is ready, you can request any service and use it:

function main(container: AppContainer) {
    // Resolve instance of "AppManager"
    const app = container.get(AppType.AppManager)
    
    // Use AppManager instance
    app.run()
}