Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/xeinebiu/ts_injector
Simple and lightweight injector for typescript projects.
https://github.com/xeinebiu/ts_injector
angular browser dependency dependency-graph dependency-injection dependency-manager html injection javascript node nodejs react reactjs simple typescript
Last synced: about 2 months ago
JSON representation
Simple and lightweight injector for typescript projects.
- Host: GitHub
- URL: https://github.com/xeinebiu/ts_injector
- Owner: xeinebiu
- License: mit
- Created: 2021-11-05T22:16:12.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2021-11-08T14:27:37.000Z (about 3 years ago)
- Last Synced: 2024-09-06T06:24:23.075Z (4 months ago)
- Topics: angular, browser, dependency, dependency-graph, dependency-injection, dependency-manager, html, injection, javascript, node, nodejs, react, reactjs, simple, typescript
- Language: TypeScript
- Homepage:
- Size: 63.5 KB
- Stars: 6
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
## Typescript Injector
![](https://img.shields.io/badge/minzipped%20size-7%20kb-green)
![](https://img.shields.io/badge/build-passing-green)
![](https://img.shields.io/badge/Typescript-100%25-green)
![](https://img.shields.io/badge/License-MIT-green)Simple and lightweight injector for typescript projects.
---
## Installation
``
npm install @xeinebiu/[email protected]
``## Modules
Module allows you to ``provide`` classes when they are requested via `injection`````typescript
createDiModule({
provides: [{
provide: FeedRepository
}]
})
````
On the example above, we are providing `FeedRepository` on any `scope` required.Now, to use it, simply annotate the property with ```@Inject()```
````typescript
class Demo {
@Inject()
private readonly feedRepository!: FeedRepository
}
````---
## Providers
### Singleton
To make the ``FeedRepository`` a singleton, simply add `singleton: true`
``FeedReposiotory`` class must not be `abstract`
````typescript
createDiModule({
provides: [{
provide: FeedRepository,
singleton: true
}]
})
````
### Use Class
Whether your project is built in on abstraction, the following example can be used.
```typescript
createDiModule({
provides: [{
singleton: true,
provide: FeedRepository, // can also be abstract when used with `useClass`
useClass: DemoFeedRepository
}]
})
```
```DemoFeedRepository``` will be provided for `FeedRepository`. Since we are setting `singleton: true`, a single instance is shared.### Use Value
Provide a class instance for requested provider.
```typescript
createDiModule({
provides: [{
provide: FeedRepository, // can also be abstract when used with `useValue`
useValue: new DemoFeedRepository(...arguments)
}]
})
```
When ``useValue`` is used, singleton is set by default to `true`---
## Scope
Using ```scope``` allows providing different classes on specific scope.
````typescript
createDiModule({
provides: [{
provide: FeedRepository,
useClass: RootFeedRepository,
provideIn: 'root'
}]
})
````
It is a must to annotate the class where the scope is needed.
````typescript
@Scope({
scope: 'root'
})
class RootDemo {@Inject()
private readonly feedRepository!: FeedRepository
}
````
``feedRepository`` on `RootDemo` is provided with `RootFeedRepository`