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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@lovekicher/inject-it

v0.3.0

Published

Strong-typed dependency injection for both node.js and browser.

Downloads

3

Readme

inject-it

基于TypeScript强类型的轻量级依赖注入容器,支持node.js和浏览器环境

特性

  • 支持node.js和浏览器环境
  • 非常轻量,除了polyfill,没有其他依赖
  • 采用 reflect-metadata 通过装饰器为类型声明注入依赖
  • 如果tsconfig.json中设置emitDecoratorMetadata为true,则支持根据tsc 编译器生成的元数据自动获取依赖信息
  • 可以通过依赖的接口名称来注入依赖,支持非构造函数和类的依赖,例如普通对象实例
  • 支持构造函数注入,通过@service装饰器
  • 支持属性注入,通过@autowired装饰器
  • 支持工厂方法和单实例
  • 支持模块(插件化)注入,暴露一个安装方法,批量注册依赖
  • 依赖异步初始化

设计理念

inject-it注册的依赖默认采用Singleton模式,即重复获取同一个类型(接口名或者构造函数),始终返回一个实例。这和一些通常在web服务中使用的IoC容器的默认方式不一样,它们可能默认采用的是Transient或者Scoped模式。

这种设计是 刻意 的,目的是为了降低单用户/无用户体系的客户端和web应用使用的复杂度,并且更好地和vue等前端框架进行整合。通常在这些框架中,会有较多的在应用生命周期中始终存在的依赖,并且往往以全局状态的方式进行管理和使用,极少涉及作用域和资源释放。

inject-it希望在这些应用中,执行业务逻辑和获取依赖的代码,交由容器来完成,不再依赖全局对象,同时把UI交互和数据逻辑解耦,使得逻辑可以模块化和封装,提升可维护性和扩展性。

使用

⚠️ 同一个类,只支持构造函数注入和属性注入中的一种。 推荐使用构造函数注入,这样该类可以在容器以外的地方手动创建实例

⚠️ 注入的参数尽量避免包含值类型(Number, Boolean, BigInt), Array, Symbol和String,使用这些参数可能会带来无法预测的结果

构造函数注入

@service装饰器的参数为该类构造函数的参数对应的依赖类型的数组,可以是类构造函数或者接口名。数组元素的数量和顺序与构造函数参数一一对应,且参数不能是可选的

import { ServiceBuilder, service, autowired } from "@lovekicher/inject-it";


interface TimeoutConfig {
  timeout: number;
}

interface IDataService {
  getData(): Promise<number[]>;
}

// 声明依赖,可以是接口
@service(["TimeoutConfig"])
class DataService implements IDataService {

  private readonly config: TimeoutConfig;
  constructor(config: TimeoutConfig) {
    this.config = config;
  }

  async getData(): Promise<number[]> {
    await new Promise((resolve) => {
      setTimeout(() => {
        resolve();
      }, this.config.timeout);
    });
    return [1, 2, 3];
  }
}

属性注入

@autowired装饰器的参数为该属性对应的依赖类型的构造函数或者接口名

可以将属性声明为private readonly。由于js的限制,真正的私有属性(以#开头)无法通过外部写入,请不要注入这些属性。

⚠️ 属性注入要求该类有无参构造函数,且构造函数中没有需要清理的副作用代码


class DataService implements IDataService {

  @autowired("TimeoutConfig")
  private readonly config!: TimeoutConfig;

  async getData(): Promise<number[]> {
    await new Promise((resolve) => {
      setTimeout(() => {
        resolve();
      }, this.config.timeout);
    });
    return [1, 2, 3];
  }
}

使用容器


// 注册依赖
const container = new ServiceBuilder()
  .instance({ timeout: 1000 }, "TimeoutConfig")
  .constructorInject(DataService, "IDataService")
  .build();

// 使用容器
const service = container.resolve<IDataService>("IDataService");
await service.getData();