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

inject-it-mod-it

v0.0.6

Published

A module based dependency injection system.

Downloads

5

Readme

inject-it-mod-it

NPM Build Status Coverage Status

Description

This is a simple dependency injection library.

Usage

First create a module

@Module({})
export class AppModule {}

Then add some dependencies

@Service()
class NameGenerator {
  createName() {
    return 'John Doe';
  }
}

@Module({
  providers: [NameGenerator]
})
export class AppModule {}

Then add a controller class (a class that uses your dependencies)

@Service()
class NameGenerator {
  createName() {
    return 'John Doe';
  }
}

@Controller()
class Conversation {
  constructor(private readonly nameGenerator: NameGenerator) {}

  startTalking() {
    console.log(`Hello ${this.nameGenerator.createName()}! How's it going?`);
  }
}

@Module({
  providers: [NameGenerator],
  controllers: [Conversation]
})
class AppModule {}

ModuleLoader.getController(AppModule, Conversation).startTalking();

Modules can import other modules:

@Service()
class NameGenerator {
  createName() {
    return 'John Doe';
  }
}

@Module({
  providers: [NameGenerator],
  exports: [NameGenerator]
})
class NameModule {}

@Controller()
class Conversation {
  constructor(private readonly nameGenerator: NameGenerator) {}

  startTalking() {
    console.log(`Hello ${this.nameGenerator.createName()}! How's it going?`);
  }
}

@Module({
  imports: [NameModule]
})
export class AppModule {}

Note: modules cannot export controllers.

Example in context:

@Service()
class Foo {
  constructor() {}

  getName() {
  return this.personName();
 }
  private personName() {
  return 'John Doe';
 }}

@Service()
class CoolService {
 constructor(@Inject('TestToken') private readonly name, private readonly foo: Foo) {}
 getVal() { return `Hey ${this.name}, what's with ${this.foo.getName()}?`; }}

@Module({
 providers: [CoolService, Foo, {
   provide: 'TestToken',
   useValue: 'Kyle Pfromer'
 }],
 exports: [CoolService]
})
export class AppModule {}


@Controller()
class test {
 constructor(public coolService: CoolService) {
 }
}

@Module({
 imports: [AppModule], controllers: [test]
})
class TestMod {}

console.log(ModuleLoader.getController(TestMod, test).coolService.getVal().coolService.getVal());

Module take the following parameters:

@Module({
  imports: [/* Other Modules */],
  controllers: [/* Controllers */],
  providers: [/* services or token providers */],
  exports: [/* services or token providers */]
})

You can have custom token providers

All of the following providers are valid

class ToBeOverriden {}

class NewClass {}

const ValueProvider = {
  provide: 'NameToken',
  useValue: 'John Doe'
};
const ClassProvider = {
  provide: ToBeOverriden,
  useClass: NewClass
};
const FactoryProivder = {
  provide: 'Factory'
  useFactory: (name: string) => `Hello ${name}`
  deps: ['NameToken']
};

@Module({
  providers: [
    ValueProvider,
    ClassProvider,
    FactoryProivder
  ]
})
class ExampleModule {}

License

inject-it-mod-it is MIT licensed.