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

typescript-injections

v3.0.0-alpha.3

Published

TypeScript Injections is a library, which simplify dependency management in your project.

Downloads

247

Readme

TypeScript Injections is a library, which simplify dependency management in your project.

Introduction

TypeScript Injections is a library, which simplify dependency management in your project. Main features are:

  1. Library significantly simplify dependency management in your project. For example: injecting connection with a database in a few places not require from you give data connection every time - you do this one time in one place.

  2. In opposite to other tools to dependency management, you don't need to customize your all code to this library (e.g. with something like @injectable decorator). TypeScript Injections works like plug-in - you use that only in dependency definitions place and in creating main object.

  3. Singleton (e.g. connection with a database) is set only in dependency definitions place and in rest of code you use this like other dependency, which is not singleton.

  4. Allows writing code which is compatible with the SOLID rules.

Installation

You can get the latest release using npm:

$ npm install typescript-injections --save

Usage

Usage is included in 2 steps: define dependencies and resolving them.

Inject

Let's assume these code:

abstract class Application {
    public abstract run(): void;
}
class HelloWorldApplication implements Application {
    public run(): void {
        console.log(`Hello World!`);
    }
}
const definitions: Resolver[] = [
    new Inject([
        new InjectWithParams({
            type: Application,
            to: HelloWorldApplication,
        }),
    ]),
];

const injector = new Injector();
const { instance: application } = injector.resolve(Application, definitions);

application.run(); //prints 'Hello World!'

In the example above we have abstraction Application and concrete implementation - HelloWorldApplication. In resolve moment we just use abstraction, and injector, based on given definitions resolve that abstraction to HelloWorldApplication instance.

Note than Application abstraction is an abstract class, not interface. Unfortunately, interfaces in TypeScript doesn't exists at the runtime and we can't use them as value. For this reason, we use abstraction and implementations implements than abstraction, not extends.

Inject constructor params

We pass the constructor parameters as follows:

abstract class Application {
    public abstract run(): void;
}
class HelloWorldApplication implements Application {
    public constructor(private readonly name: string) {

    }

    public run(): void {
        console.log(`Hello, ${this.name}!`);
    }
}
const definitions: Resolver[] = [
    new Inject([
        new InjectWithParams({
            type: Application,
            to: HelloWorldApplication,
        }),
    ]),
    new InjectConstructorParams([
        new ConstructorWithParams({
            type: HelloWorldApplication,
            params: [
                () => 'John',
            ],
        }),
    ]),
];

const injector = new Injector();
const { instance: application } = injector.resolve(Application, definitions);

application.run(); //prints 'Hello, John!'

Note than parameters given in ConstructorWithParams are methods, which return target value. Thanks for that, every values are "execute" at the moment, when we create object, not at the dependency defining time. In many cases, constructor parameter is require not only a simple value, but object - we don't want to create instance of that dependency immediately.

The example below shows it well:

abstract class Application {
    public abstract run(): void;
}
abstract class Connection {
    public abstract ping(): void;
}
class HelloWorldApplication implements Application {
    public constructor(private readonly connection: Connection) {
        console.log(`HelloWorldApplication constructor`);
    }

    public run(): void {
        this.connection.ping();
        console.log(`Application run`);
    }
}
class MySQLConnection implements Connection {
    public constructor() {
        console.log(`MySQLConnection constructor`);
    }

    public ping(): void {
        //do something
    }
}
const definitions: Resolver[] = [
    new Inject([
        new InjectWithParams({
            type: Application,
            to: HelloWorldApplication,
        }),
        new InjectWithParams({
            type: Connection,
            to: MySQLConnection,
        }),
    ]),
    new InjectConstructorParams([
        new ConstructorWithParams({
            type: HelloWorldApplication,
            params: [
                ({resolve}) => resolve(Connection),
            ],
        }),
    ]),
];

console.log(`After definitions`);

const injector = new Injector();
const { instance: application } = injector.resolve(Application, definitions);

application.run();

Output will be:

After definitions
MySQLConnection constructor
HelloWorldApplication constructor
Application run

As you can see, MySQLConnection was created only at the moment, when HelloWorldApplication was require that to work.

Singletonize

In some cases we don't want to create instance in every injection. For example, we don't want to create new connection to database on every time, when some part of code needs connection to work - we want create only one connection and use than connection in all injections.

abstract class Application {
    public abstract run(): void;
}
abstract class Connection {
    public abstract ping(): void;
}
class HelloWorldApplication implements Application {
    public constructor(private readonly connection: Connection, private readonly otherConnection: Connection) {
        
    }

    public run(): void {
        console.log(this.connection === this.otherConnection);
    }
}
class MySQLConnection implements Connection {
    public ping(): void {
        //do something
    }
}
const definitions: Resolver[] = [
    new Inject([
        new InjectWithParams({
            type: Application,
            to: HelloWorldApplication,
        }),
        new InjectWithParams({
            type: Connection,
            to: MySQLConnection,
        }),
    ]),
    new Singletonize([
        new SingletonizeType({
            type: Connection,
        }),
    ]),
    new InjectConstructorParams([
        new ConstructorWithParams({
            type: HelloWorldApplication,
            params: [
                ({resolve}) => resolve(Connection),
                ({resolve}) => resolve(Connection),
            ],
        }),
    ]),
];

const injector = new Injector();
const { instance: application } = injector.resolve(Application, definitions);

application.run(); //prints 'true'

Instance of HelloWorldApplication in connection parameter and in otherConnection parameter has exactly the same instance of Connection.

License

MIT