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

@markdomkan/d-injector

v1.4.6

Published

D-injector is a really simple and tinny dependency injection library for Deno or Node.js.

Downloads

796

Readme

Introduction

D-injector is a really simple and tinny dependency injection library for Deno or Node.js.

Installation

Deno

import { Injector } from "https://deno.land/x/d_injector/mod.ts";

//...

Node.js

    npm install @markdomkan/d-injector

or

    yarn add @markdomkan/d-injector

Configuration

The D-injector is configured using the register method. This method receives an object with the following properties:

  • id: The id of the service. This id is used to get the service from the container.
  • serviceClass: The class of the service. This class will be instantiated by the container.
  • args: The arguments that the service needs to be instantiated. The arguments can be services, factories, or values.
  • tags: The tags of the service. The tags can be used to find the services by tag. The tags can be a string array or Record<string, string[]>. The "Record" mode is useful if you want to add some categories to your tags.

Arguments

The arguments can be services, factories, or values.

Service

The service argument is used to inject a service into another service. The service argument is an object with the following properties:

  • type: The type of the argument. In this case, the type is service.
  • id: The id of the service that you want to inject.

Factory

The factory argument is used to inject the result of a factory method. The factory argument is an object with the following properties:

  • type: The type of the argument. In this case, the type is service.
  • id: The id of the service that you want to inject.
  • method: The name of the method that you want to call.

Value

The value argument is used to inject a value into a service. The value argument is an object with the following properties:

  • type: The type of the argument. In this case, the type is value.
  • value: The value that you want to inject. This value can be a number, a string, a boolean, an array, an object, or a function.

Container

The container is the object that you will use to get the services from the container. The container has the following methods:

  • get: This method receives the id of the service that you want to get.
  • findByTag: This method receives the tag that you want to find. The method returns a map with the services that have the tag. The map has the id of the service as key and the service instance as value. if you use the "Record" mode, you can add the category of the tag as second parameter.
  • setNewService: This method allows you to set a new register into an already compiled container. Receive by parameter the service id, the instance, and tags object been the last optional.

The fisrt two methods return the InstancedService Object. This object has the following properties:

  • instance: The instance of the service.
  • tags: The tags of the service.

If you need more information or code examples, you can see the test files.

Usage

Simple example

Inject a service into a class and get it from the container using his id.

// Import the injector, if you are in Deno, you will have some like this:
//
// import { Injector } from "https://deno.land/x/[email protected]/mod.ts";
//
// if you are in Node.js, you will have something like this:
//
// import { Injector } from "@markdomkan/d-injector";

import { TestClass } from "./test-class.ts";
import { ServiceClass } from "./service-class.ts";

const injector = new D_Injector();
injector
  .register({
    id: "test.class",
    serviceClass: TestClass,
    tags: {
      "tag-category": ["tag1"],
      TagCategory: ["tag2", "tag3"],
    },
    args: [
      {
        type: "service",
        id: "service.class",
      },
    ],
  })
  .register({
    id: "service.class",
    serviceClass: ServiceClass,
  });

const container = injector.compile();
const myTestClassInstance = container.get<TestClass>("test.class");

myTestClassInstance.instance.someAwesomeMethod();

// You can also get the tags of the service. Usefull to get some extra information about the service.
myTestClassInstance.tags.tagCategory; // ["tag1"]

Adding some tags

You can add some tags to your services, and then you can find them by tag.

const injector = new D_Injector();
injector
  .register({
    id: "service.1",
    serviceClass: ServiceClassOne,
    tags: ["super-tag"],
  })

  .register({
    id: "service.2",
    serviceClass: ServiceClassTwo,
    tags: ["super-tag", "awesome-tag"],
  })

  // You can also use the "Record" mode to add some categories to your tags. In this case you can add custom typing into register method.
  .register<{
    specialCategoryTags: string[];
    otherCategoryTags: string[];
  }>({
    id: "service.3",
    serviceClass: ServiceClassTwo,
    tags: {
      specialCategoryTags: ["super-tag", "awesome-tag"],
      otherCategoryTags: ["simply-tag"],
    },
  })

  .register({
    id: "service.4",
    serviceClass: ServiceClassThree,
    tags: ["awesome-tag"],
  });

const container = injector.compile();

const allInjectedServicesWithSuperTagMap =
  container.findByTag<ServiceClass>("super-tag"); // service.1, service.2, service.3

const allInjectedServicesWithAwesomeTagMap =
  container.findByTag<ServiceClass>("awesome-tag"); // service.2, service.3, service.4

// If you use the "Record" mode, you can find the services by category and tag.
const only3InjectedService = container.findByTag<ServiceClass>(
  "awesome-tag",
  "specialCategoryTags"
); // service.3

Factory

You can use a factory classes to create your services. You only need to add the method name to the register method. The D-injector will instance the factory class with all arguments and then call the method. The result of the method is what the D-injector will inject.

const injector = new D_Injector();
injector
  .register({
    id: "test.class",
    serviceClass: TestClass,
    args: [
      {
        type: "service",
        id: "factory",
      },
    ],
  })
  .register({
    id: "factory",
    serviceClass: StringFactory,
    method: "create",
  });