@decorators/di

中文文档
3.1.0发布于 2023/08/10MITTypeScriptCommonJS近 30 天下载 6987

node decorators - decorators for dependency injection

原始使用文档
文档目录4 节
  1. Installation
  2. Example
  3. API
  4. Multi-support

Node Decorators

Installation

npm install @decorators/di --save

Example

Fully working example can be found in example folder.

API

  • @Injectable() - Registers class as provider in the container
@Injectable()
class HttpService {}
  • @Inject(injectable: Injectable) - A parameter decorator that marks parameter as dependency.
@Injectable()
class HttpService {
  constructor(@Inject(API_URL) apiUrl: string) {}
}
  • @Optional() - A parameter decorator that marks parameter as optional dependency.
@Injectable()
class HttpService {
  constructor(@Optional() @Inject(API_URL) apiUrl: string) {}
}
  • InjectionToken - Creates a token that can be used in DI as Provider.
const API_URL = new InjectionToken('API_URL');
...
@Injectable()
class HttpService {
  constructor(@Inject(API_URL) apiUrl: string) {}
}
  • Container - Container interface
    • .setParent(container: Container) - set parent container
    • .provide(providers: Providers[]) - Registers an array of providers.
    • .get<T>(injectable: Injectable): Promise<T> - Retrieves a Promise with an instance of the injectable, throws:
      • MissingProviderError if dependency provider wasn't found
      • RecursiveProviderError in case of recursive dependency injection
const container = new Container();

container.provide([
  {
    provide: 'Message',
    async useFactory() {
      return delay('Async Provider');
    },
  },
  {
    provide: 'Hello World',
    useClass: Service,
  },
]);

Multi-support

It's possible to provide multiple things using one injection token via multi flag:

container.provide([
  {
    provide: GLOBAL_PIPE,
    useClass: ServerPipe,
    multi: true
  },
  {
    provide: GLOBAL_PIPE,
    useClass: ErrorPipe,
    multi: true
  },
]);

版本与发布记录

@decorators/di 共发布 8 个版本,首次发布于 2017/11/01;过去 12 个月发布了 0 个版本。

版本发布日期距今
3.1.0latest2023/08/103 年前
3.0.12023/08/043 年前
3.0.02023/07/283 年前
2.0.02023/01/303 年前
1.0.32021/04/095 年前
1.0.22018/03/018 年前

关于 @decorators/di 的常见问题

@decorators/di 是什么?

node decorators - decorators for dependency injection

如何安装 @decorators/di?

在项目目录运行 npm install @decorators/di,或使用 pnpm add / yarn add / bun add。

@decorators/di 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@decorators/di 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@decorators/di 可以商用吗?

@decorators/di 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@decorators/di 的下载量有多少?

根据 npm 官方下载统计,@decorators/di 在 2026/10/12 – 2026/11/10 这 30 天内被下载 6,987 次,日均约 233 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@decorators/di 还在维护吗?

最新版本 3.1.0 发布于 2023/08/10(3 年前);至今共发布 8 个版本,其中过去 12 个月发布了 0 个;首次发布于 2017/11/01。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。