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

yardic

v0.2.5

Published

Yet Another Dependecy Injection Container

Downloads

25

Readme

Yardic

Yet AnotheR Dependency Injection Container

Zero dep DI container for Javascript and Typescript

CI

Install

Yadic can be installed through the node package manager

$ npm install yardic

Usage

Javascript

Register all your Javascript classes into a container and use container.get(Class); anywhere in your code to get instance of a registered class. You can also use container.get(ClassName); where ClassName is the name of your class, like 'FooService', but it is recommended to use the Class itself. The reason behind this, is that if you require the class, the module you define the class in is guaranteed to be required, resulting in the module to be registered before requesting it.

const { container } = require('yardic');
class Bar {}
container.register(Bar);

class Fizz {}
container.register(Fizz, [], { type: "factory" });

class Foo {
  constructor(bar, fizz) {
    this.bar = bar;
    this.fizz = fizz;
  }
}
container.register(Foo, [Fizz, Bar]);

// somewhere in code
const fooInstance = container.get(Foo);
// it will create a singleton Bar, a new Fizz and a singleton Foo instance

// in another file
const fooInstanceAgain = container.get(Foo);
// it will return the already created Foo instance
const fizzInstance = container.get(Fizz);
// it will return a new Fizz

To set constant values, you can use container.setValue('name', { secretStuff: 'mysecretApiKey' }); This lets you register a non class object if needed.

const { container } = require('yardic');

class Foo {
  constructor(config) {
    this.someconfigvalue = config.foodata;
  }
}
container.register(Foo, ['config']);
container.setValue('config', {
  foodata: { key: 'asd', secret: 'asd' },
});

Testing

During testing, it might be useful to overwrite some of the services inside the container with mock objects. To be able to test isolated, it is recomended to modify a cloned version of your container.

Another useful feature during testing is to enforce a new instance, even for non factory (singleton) services. This lets you create new instances for every class, so your tests can be fully independent.

// foo.js
const { container } = require('yardic');
class Foo {
  constructor(bar) {
    this.bar = bar;
  }
}
container.register(Foo, [Bar]);

// foo.test.js
const { container } = require('yardic');

const testContaier = container.clone();
testContaier.setValue('Bar', mockBarObject);
// ...
  const fooUnderTest = testContainer.getNew('Foo');
// ...
  const anotherFooUnderTest = testContainer.getNew('Foo');
  // the instances will be different, even if Foo is defined as a singleton
// ...

If needed, you can also require the Container class, and create multiple instances.

const { Container } = require('yardic');
const container1 = new Container();
const container2 = new Container();
// ...

Typescript

When using yardic with typescript, it will work mostly the same as if you were using it with javascript. If you use .get with classes, as recommended. The return value will hold the correct type.

// foo.ts
import { container } from 'yardic';
class Foo {
  //...
}
container.register(Foo);

// anywhere in code:
//...
const foo = container.get(Foo); // foo will hold the type Foo
const foo2: Foo = container.get('Foo'); // when using name strings to get the service, you need to tell the type