Redi
ð A dependency injection library for TypeScript & JavaScript, along with a binding for React.
Install / Use
/learn @wzhudev/RediREADME
redi
<p className="flex h-6"> <img alt="stars" src="https://badgen.net/github/stars/wzhudev/redi" style={{ display: "inline-block", marginRight: "0.5rem" }} /> <img alt="downloads" src="https://badgen.net/npm/dw/@wendellhu/redi" style={{ display: "inline-block", marginRight: "0.5rem" }} /> <img alt="license" src="https://badgen.net/github/license/wzhudev/redi" style={{ display: "inline-block", marginRight: "0.5rem" }} /> <img alt="coverage" src="https://img.shields.io/codecov/c/github/wzhudev/redi.svg" style={{ display: "inline-block" }} /> </p>redi (pronounced 'ready') is a lightweight dependency injection library for TypeScript and JavaScript, with React bindings included.
Why redi?
| Feature | Description |
| ----------------------- | ----------------------------------------------- |
| ðŠķ Lightweight | Zero dependencies, small bundle size |
| ð§ esbuild friendly | No emitDecoratorMetadata required |
| ðĶ Feature-rich | Class, value, factory, async injection and more |
| âïļ React ready | Built-in hooks for React integration |
| â
Well tested | 100% code coverage |
Quick Start
npm install @wendellhu/redi
import { Inject, Injector } from '@wendellhu/redi';
class AuthService {
getCurrentUserInfo(): UserInfo {
/* ... */
}
}
class FileListService {
constructor(@Inject(AuthService) private authService: AuthService) {}
getUserFiles(): Promise<Files> {
const user = this.authService.getCurrentUserInfo();
// ...
}
}
const injector = new Injector([[AuthService], [FileListService]]);
const fileList = injector.get(FileListService);
Features
- Dependency Items: Class
{ useClass }, Value{ useValue }, Factory{ useFactory }, Async{ useAsync } - Interface Injection: Use
createIdentifierfor interface-based injection - Lazy Instantiation: Defer creation with
{ lazy: true } - Hierarchy Injection: Parent-child injectors with
@Self()and@SkipSelf() - Optional & Many:
@Optional()and@Many()decorators - React Integration:
useDependency,connectDependenciesand more hooks - RxJS Support:
useObservableanduseUpdateBinderfor reactive programming
Who's Using redi?
- Univer
- ByteDance
Links
License
MIT. Copyright 2021-present Wenzhao Hu.
