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

@universal-di/core

v0.0.7

Published

Universal Dependency Injection for Typescript with Angular-like API

Downloads

7

Readme

Universal Dependency Injection

MIT License Build Status NPM version Coverage Status PRs welcome

Contents

Providing Dependencies

Imagine there is a class called ProductService that needs to act as a dependency in a state.

The first step is to add the @Injectable decorator to show that the class can be injected.

@Injectable()
class ProductState {
}

@Injectable()
class ProductService {
}

Second step is to make it part of your module

@Module({
    providers: [
        ProductState,
        ProductService
    ],
})
class ProductModule {
}

The last third step is to bootstrap a DIApplication with your module

const app = new DIApplication(ProductModule);

Once you register a provider, you will get singleton instance of this service every time you'd try to inject it.

Injecting a Dependency

Registered provider can be injected into a class from the same @Module

@Injectable()
class ProductState {
    constructor(private productService: ProductService) {
    }
}

or directly from the bootstrapped module

const productState = app.rootInjector.get(ProductState);

Besides being a singleton, class is instantiated only when injected, not before.

Ports

In order to make working with universal-di easier (e.g. injecting dependencies), you can take advantage of ports for particular libraries:

Advanced usage

Imagine you are tracking events differently depending on environment

interface AnalyticsService {
    track(event: string): void;
}

const ANALYTICS_SERVICE = new InjectionToken<AnalyticsService>('ANALYTICS_SERVICE')

After defining the abstraction, next step will be to define implementations

@Injectable()
class RealAnalyticsService implements AnalyticsService {
    constructor(
        @Inject(TRACKING_API_URL) private readonly _trackingApiUrl: string,
        private readonly _httpClient: HttpClient,
    ) {
    }

    track(event: string): void {
        this._httpClient.post<void>(this._trackingApiUrl);
    }
}

@Injectable()
class ConsoleAnalyticsService implements AnalyticsService {
    track(event: string): void {
        console.log('[tracking]', event);
    }
}

Put together in a @Module

const TRACKING_API_URL = new InjectionToken<string>('TRACKING_API_URL');

@Module({
    providers: [
        {
            provide: ANALYTICS_SERVICE,
            useClass: isDev() ? ConsoleAnalyticsService : RealAnalyticsService,
        },
        {
            provide: TRACKING_API_URL,
            useValue: '/api/track',
        }
    ],
})
class AnalyticsModule {
}

@Module({
    imports: [
        AnalyticsModule,
    ],
    providers: [
        HttpClient,
    ]
})
class AppModule {
}

And use

const application = new DIApplication(AppModule);

// AnalyticsService type is inferred here
const analyticsService = application.rootInjector.get(ANALYTICS_SERVICE);

analyticsService.track('application-started');

Authors

szymeo bartoszswitalski