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

di-type

v2.0.0

Published

Dependency injection with typescript

Downloads

8

Readme

Introduction: di-type

This npm module support the ability of dependency injection and thereby also dependency inversion. It can be used to provide singletons to any class that might need a singleton of another class. The module is designed to be used as an decorator. This means experimentalDecorators: true should be set in your projects tsconfig.json.

Usage

To use this in your project and save it in the package.json file do: npm install di-type --save

Please be aware that we use semantic versioning. This means that you should be able to safely subscribe to updates on this module for versions 1.x.x or 2.x.x etc. Major versions for example from 1.x.x to 2.x.x is not safe as the module API might change.

Getting dependencies

In order to include a dependency it is important to understand WHEN it is needed. For applications there might be a runtime configuration phase. This is where a class might need acces to some providers. When this phase has ended there might be a need to acces the configured providers or directives as they are called here.

Example

An example is given here. Please be aware that the class SetConfig will run before SomeConsumer even tho it is denoted after the other class.

// import the Component and bootstrap from our libery make sure that it is installed first
import {Component, bootstrap}  from 'di-type';

// another class that is needed a singleton of
import {SomeConfigService} from './somepath';
import {SomeService}       from './someotherpath';

@Component({
    directives: [SomeConfigService],
    providers: []
})
class SomeConsumer {
    constructor(scs: SomeConfigService) {

        // here we can use the configured config service
    }
}

@Component({
    directives: [SomeService],
    providers: [SomeConfigService]
})
class SetConfig {
    constructor(scs: SomeConfigService, ss: SomeService) {

        // here we can change the content of scs using the ss
    }
}

class Application {
    constructor () {

        // doing something cool here
    }
}

// start up everthing
bootstrap(Application);

The bootstrap function takes one parameter. This parameter is the MAIN class of your application.

License

The MIT License (MIT)