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

@dmytromykhailiuk/dependency-injection-container

v1.0.5

Published

Solution for Dependency creation and management

Downloads

6

Readme

Dependency injection container

Solution for Dependency creation and management

(DI Container with inheritance similar to Angular and NestJS implementation)

Installation

npm i @dmytromykhailiuk/dependency-injection-container

Interesting packages

Basic example of usage


import { Container } from '@dmytromykhailiuk/dependency-injection-container';

class Logger {
  log(message: string) {
    console.log(message);
  }
}

class ApiService {
  static injectDependencies = [Logger];

  constructor(private logger: Logger) {}

  async makeCall(url: string) {
    this.logger.log(`makeCall to ${url}`);

    const result = await (await fetch(url)).json();

    this.logger.log(`makeCall to ${url} Success -> result: ${JSON.stringify(result)}`);

    return result;
  }
}

class UserService {
  static injectDependencies = [ApiService, USER_URL];

  constructor(private apiService: ApiService, private userUrl: string) {}

  getUser(id: string) {
    return this.apiService.makeCall(`${this.userUrl}/${id}`);
  }
}

class UserFacade {
  static injectDependencies = [UserService]; // need to define this property for DI

  currentUserId: string;

  constructor(private userService: UserService) {}

  getCurrentUser() {
    return this.userService.getUser(this.currentUserId);
  }
}

const container = new Container();

container.registerProviders([
  UserFacade,
  UserService,
  ApiService,
  Logger,
  {
    useValue: 'https://example-user/',
    provider: USER_URL,
  },
]);

const userFacade = container.inject<UserFacade>(UserFacade);

userFacade.getCurrentUser().then((user) => {
  console.log(user);
});

Ways to register provider:

using Class


container.registerProviders([SomeClass]); // instance of SomeClass will be created

using object with useValue proprty


const SECRET_KEY_TOKEN = 'SECRET_KEY_TOKEN';

container.registerProviders([
  {
    useValue: 'secret_key',
    provider: SECRET_KEY_TOKEN
  }
]); // secret_key value will be registered using SECRET_KEY_TOKEN injection token

using object with useClass proprty


class ClassABC {
  a() {  /* do something 1 */ }
  b() {  /* do something 2 */ }
  c() {  /* do something 3 */ }
}

abstract class ClassB {
  abstract b: () => void
}

container.registerProviders([
  {
    useClass: ClassABC,
    provider: ClassB
  }
]); // instance of class ClassABC will be created with ClassB type/token 

using object with useExisting proprty


container.registerProviders([
  {
    useValue: 1
    provider: 'Token 1'
  },
  {
    useExisting: 'Token 1',
    provider: 'Token 2'
  }
]); // register new provider with extisting data/object in container

using object with useFactory proprty with deps


container.registerProviders([
  {
    useValue: 1
    provider: 'Token 1'
  },
  {
    useValue: 2,
    provider: 'Token 2'
  }
  {
    useFactory: (dataFromFirstToken, dataFromSecondToken) => {
      // do something
      
      return newResultingProvider;
    }
    deps: ['Token 1', 'Token 2'],
    provider: 'Token 3'
  }
]); // call factory function to register provider

using multi true


container.registerProviders([
  {
    useValue: 1,
    multi: true,
    provider: 'Token'
  },
  {
    useValue: 2,
    multi: true,
    provider: 'Token'
  }
]);

const data = container.inject<number>('Token'); // [1, 2] // return values for "Token" provider