Security News
Bun 1.2 Released with 90% Node.js Compatibility and Built-in S3 Object Support
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
@searchfe/inject-js
Advanced tools
一个极简的 TypeScript 依赖注入框架。https://searchfe.github.io/inject-js/
使用 npm 来安装:
npm install --save @searchfe/inject-js
inject-js 需要 Reflect Metadata 来在运行时决定依赖类型,你的 tsconfig.json
需要包含以下的设置:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true
}
}
另外因为该库依赖 Reflect 的使用,确保运行时存在 Reflect API 的 Polyfill,比如以下之一:
以下是一些使用案例,更多细节请参考 API 文档。
用 @injectable
来装饰一个 Service,这样 inject-js 就可以(借由 metadata)得知它的依赖并在运行时注入。
// car.ts
import { injectable } from 'inject-js';
import { Wheel } from './wheel';
@injectable
export class Car {
constructor(private wheel: Wheel) {}
}
// wheel.ts
import { injectable } from 'inject-js';
@injectable
export class Wheel {
constructor() {}
}
// index.ts 应用入口
import { Container } from 'inject-js';
import { Car } from './car';
import { Wheel } from './wheel;
const di = new Container();
di.addService(Car);
di.addService(Wheel);
const car = di.create(Car);
用 @inject(token)
来装饰一个依赖(构造参数),来指定被注入的类。token
即为当前容器内 Provider 的唯一标识。
用于没有 Service 声明的场景(只有 Provider),或者没有 Metadata API 支持的场景。
也就是说借此可以在 JavaScript 代码中使用 inject-js。
import { inject } from 'inject-js';
import { Wheel } from './wheel';
@injectable
export class Car {
constructor(@inject('Wheel') private wheel: Wheel) {}
}
// index.ts 应用入口
import { Container } from 'inject-js';
import { Car } from './car';
import { Wheel } from './wheel';
const di = new Container();
di.addService('Wheel', Wheel);
di.addService(Car);
const car = di.create(Car);
如果 Container 实例就在定义 Service 的上下文中,可以用 @service 装饰器来直接注册:
const container = new Container();
@service(container)
class FooService {}
// 相当于:
const container = new Container();
@injectable
class FooService {}
container.addService(FooService)
Container 会维护一个 Providers 集合,以及每个 Provider 对应的 Token。需要创建实例时,会根据 Token 查找对应的 Provider 并进行创建。 我们提供了如下几种注册 Provider 的方式:
.create(): Service
方法的 Provider 类(工厂类),其余注册方式都是用 addProvider 实现的。在 demo 下包含了一个使用 inject-js 的示例。可以按以下步骤执行该示例:
npm install && npm run build
。npm run demo
。FAQs
A Dependency Injection library
The npm package @searchfe/inject-js receives a total of 246 weekly downloads. As such, @searchfe/inject-js popularity was classified as not popular.
We found that @searchfe/inject-js demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 16 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Bun 1.2 enhances its JavaScript runtime with 90% Node.js compatibility, built-in S3 and Postgres support, HTML Imports, and faster, cloud-first performance.
Security News
Biden's executive order pushes for AI-driven cybersecurity, software supply chain transparency, and stronger protections for federal and open source systems.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.