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

giver

v2.0.0

Published

Dependency injection with modern ECMAScript decorators

Downloads

15,453

Readme

"I'm really gonna giver when I head down this run" or "I don't have to worry about managing dependencies, I just giver and Giver injects my clients with what services they need"


Giver (styled "give'r") is a library that provides dependency injection through modern ECMAScript decorators. With Giver, you can easily achieve inversion of control in your code. Giver has no dependencies, is tiny (~2 KB), and is thoroughly tested.

[!NOTE]
Because the decorators proposal is in Stage 3, only some transpilers have released support for decorators syntax. Because of this, Giver can only be used in Typescript or any other compatible transpilation tool.

Usage

import { inject, injectable } from 'giver'

@injectable()
class LoggerService {
  log = (
    message: string
  ) => console.log(message);
}

class MyApplication {
  @inject() loggerService!: LoggerService

  start() {
    this.loggerService.log('Application started');
  }
}

const myApplication = new MyApplication();
myApplication.start();
// Emits "Application started" to the console.

Features

[!IMPORTANT] The current scope of Giver is completely implemented and tested today. That said, Giver is simple and not a full featured DI library, yet.

Giver supports constructor style injection of dependencies that are resolved with a registry.

  • Services can be injected by using the registry.
  • Services can be registered for injection.

Motivation

The existing dependency injection libraries on npm are great; However, at the time of writing, they all either:

  1. Use experimental decorators.
  2. Require dependencies.

So, the motivation for Giver is to be a lightweight, feature filled, tiny DI library.

Goals

  1. services can be injected like this
@injectable()
class MyDependency {
  log = (msg: string) => console.log(msg);
}

@injectable()
class MyOtherDependency {
  getContent = () => "content";
}

@injectable()
class MyService {
  @inject(MyDependency) myDependency!: MyDependency;
  @inject(MyOtherDependency) myOtherDependency!: MyOtherDependency;

  doWork = () => {
    this.myDependency.log(this.myOtherDependency.getContent());
  };
}

const myService = giver.getInstanceOfClass(MyService);
myService.doWork();
  1. clients can get instances of services like this:
const myService = giver.instanceOf(MyService);
  1. bootstrapping can be done manually:
const giver = new Giver();
giver.register(MyService);
giver.register(MyOtherService, Lifetime.SINGLETON);
  1. circular dependencies are detected and reported

  2. services can be registered by any token:

type Token = symbol | string | number | Class;
giver.register(MyService, MyServiceToken);
  1. clients can reference services that are not registered directly (e.g. they are interfaces)
class MyService {
  @inject(MyDependencyToken)
  private readonly myDependency!: IMyDependency;
  
  ctor(){}
}

Non-goals as of now

a) services define their lifetime: singleton or transient, like this:

@injectable(Lifetime.TRANSIENT | Lifetime.SINGLETON)
class MyService {
  doWork() {}
}

   ...

class MyService {...}
giver.register(MyService, Lifetime.TRANSIENT);

b) containers can be created with a parent/child relationship, child containers default to the parents services unless overridden. today, we just have one global container.

c) other registration patterns:

  • provide by class
  • provide by function (factory)
  • provide by value

Prior art

Giver is inspired by Guice and tsyringe.