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

curli-di

v1.0.1

Published

Lightweight dependency injection container for JavaScript/TypeScript without using decorators

Downloads

16

Readme

Curli-DI

Build Status Coverage Status npm version Dependency Status

A Dependency Injection (DI) library in typescript without using decorators.

Installation

Install by npm

npm install --save curli-di

Basic Usage

import {DependencyInjection} from "curli-di";
import {Database} from "database";

class Foo {
  constructor(private database: Database) {}
}

const container = new DependencyInjection();

//register the dependencies for the class
container.registerService("dataBase", [], Database);
container.registerService("foo", ["dataBase"], Foo);

//creating the service
const foo = container.get("foo");

Registering external dependencies/properties:

Some time we need to inject properties or other kind of values into our services, for this we use the class ExternalDependencies like here:

import {DependencyInjection, ExternalDependencies} from "curli-di";
import {Database} from "database";

class Foo {
  constructor(private database: Database) {}
}

// Register previous dependencies already instantiated
const externalDependencies = new ExternalDependencies();
externalDependencies.add("dataBaseUser", "root");
externalDependencies.add("dataBasePass", "");

const container = new DependencyInjection(externalDependencies);

//register the dependencies for the class
container.registerService("dataBase", ["@dataBaseUser", "@dataBasePass"], Database);
container.registerService("foo", ["dataBase"], Foo);

//creating the service
const foo = container.get("foo");

As this example shows, to access to any external dependency or property we need to add the prefix @.

Also to inject or use a external dependency we can use the registerServiceBuilded method in the container after we initialized it:

import {DependencyInjection} from "curli-di";

const container = new DependencyInjection();
container.registerServiceBuilded("dataBaseUser", "root");

const dataBaseUser: string = container.get("@dataBaseUser");

Registering external dependencies/properties with an object in bulk mode:

import {DependencyInjection, ExternalDependencies} from "curli-di";
import {Database} from "database";
import {Oauth} from "oauth";
import {Lang} from "lang";

class Foo {
  constructor(private database: Database) {}
}

// Register previous dependencies already instantiated
const externalDependencies = new ExternalDependencies();
externalDependencies.bulk({
    "dataBaseUser": "root",
    "dataBasePass": "",
    "useOauth": false,
    "languagesSupported": ['en-GB','es-MX', 'zh-HK']
});

const container = new DependencyInjection(externalDependencies);

//register the dependencies for the class
container.registerService("dataBase", ["@dataBaseUser", "@dataBasePass"], Database);
container.registerService("foo", ["dataBase"], Foo);
container.registerService("lang", ["@languagesSupported"], Lang);
container.registerService("oauth", [], Oauth);

if (container.get("@useOauth")) {
    container.get("oauth").start();
}

//creating the service
const foo = container.get("foo");

Circular dependency:

To create a circular dependency between two services, one of the then will receive their dependencies with a different method than the constructor.

import {DependencyInjection} from "curli-di";

class Foo {
    constructor(private database: Database, private taa: Taa) {}
}


class Taa {
    private foo: Foo;

    public injectDependencies(foo: Foo) {
        this.foo = foo;
    }
}

const container = new DependencyInjection();

//registering of dependencies with the classes
container.registerService("foo", ["dataBase", "taa"], Foo);
container.registerService("taa", ["foo"], Taa, false, Taa.prototype.injectDependencies);

//creating the service
const foo = container.get("foo");

Registering services descriptions in different sides of the application:

If we want to split the responsibility of register services in different sides of the application we can perform it using the ExternalServicesRegister class:

import {DependencyInjection, ExternalServicesRegister} from "curli-di";
import {Database} from "database";

class Foo {
  constructor(private database: Database) {}
}

// Register previous dependencies already instantiated
const container = new DependencyInjection(),
      externalServicesRegister = new ExternalServicesRegister();


//register the dependencies for the class
externalServicesRegister.registerService("dataBase", ["@dataBaseUser", "@dataBasePass"], Database);
externalServicesRegister.registerService("foo", ["dataBase"], Foo);

container.registerExternalServiceRegister(externalServicesRegister);

//creating the service
const foo = container.get("foo");

Instantiate one or more services in a specific moment (like when we start the app) using autoInit option:

import {DependencyInjection} from "curli-di";
import {Database} from "database";
import {Oauth} from "oauth";
import {Lang} from "lang";
import {Foo} from "foo";

const container = new DependencyInjection();

//register the dependencies for the class
container.registerService("dataBase", [], Database, true);
container.registerService("foo", [], Foo);
container.registerService("lang", [], Lang);
container.registerService("oauth", [], Oauth, true);


//start dataBase service and oauth
container.callAllServicesWithAutoInit();