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

@decouplejs/core

v2.3.3

Published

Dependency Injection Tool

Downloads

10

Readme

A dependency injection library based on IoC Container. Dependency Injection implementation for Node.js projects written with Typescript. Build a container and permit it for all dependencies. Get all injectable objects easily without use new keyword. It's light-weight library and easy-to-use.

Installation

npm i @decouplejs/core

And add below to compilerOptions in tsconfig.json

"experimentalDecorators": true

Usage

Lets think about you have two classes which are UserController and UserService.

UserController have a dependency to UserService which is declared with @inject decorator.

export class UserController {
  constructor(
    @inject(USER_SERVICE)
    private userService: UserService
  ) {}

  print() {
    console.log("UserController");
    this.userService.print();
  }
}
export class UserService {
  constructor() {}

  print() {
    console.log("UserService");
  }
}

When we used @inject decorator, USER_SERVICE binding key was declared. Lets define this binding keys.

Create a keys.ts file and define binding keys to be associated with above classes like below:

export const USER_CONTROLLER = BindingKey.create("user.controller");
export const USER_SERVICE = BindingKey.create("user.service");

Now, create a Container and introduce your classes to IoC Container via its .injectable() function with binding keys that are defined before.

const container = new Container();
container.injectable(USER_CONTROLLER, UserController);
container.injectable(USER_SERVICE, UserService);

Finally, if you want to create and use a UserController instance, call .get() of Container with USER_CONTROLLER key.

const controller = container.get(USER_CONTROLLER);
controller.print();

// output
// UserController
// UserService

@inject decorator

export class UserController {
  constructor(
    @inject(USER_SERVICE)
    private userService: UserService
  ) {}
  // ...
}

With @inject decorator, decouple.js understand which dependent classes instances will be initialize and assign during create a new base class instance.

You can also use `@inject' decorator as property decorator like constructor parameter decorator as below.

export class UserController {
  @inject(USER_SERVICE)
  private userService: UserService;

  constructor() {}
  // ...
}

Binding Scopes

Specifies how long the created objects will live and how many times they should be created during the application lifecycle. Scopes can be defined by two way:

  1. via .scope() function while class is introducing with.injectable()
container.injectable(USER_CONTROLLER, UserController).scope(BindingScope.SINGLETON);
  1. via @injectable decorator which is above class definition
@injectable(BindingScope.SINGLETON)
export class UserController {
  // ...
}

One of scope definition methods is enough for a injectable class.

If two methods are used at the same time, the scope of method 1 will be valid.

If no scope is specified, the TRANSIENT scope will be valid.

Decouple.js supports two types of scope for now:

SINGLETON

Only one instance will be created during the application lifecycle and this same instance will be used by all dependent classes too.

TRANSIENT

A new instance will be created for each need of the class instance.

Example Implementation

Next Features

  1. express.js middleware support.
  2. BindingScope.REQUEST support

License

Copyright © 2022 Abdulkadir Dede.

This project is licensed under the MIT License - see the LICENSE file for details.