Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/azz/ioc-ts
TypeScript Dependency Injection
https://github.com/azz/ioc-ts
Last synced: 2 days ago
JSON representation
TypeScript Dependency Injection
- Host: GitHub
- URL: https://github.com/azz/ioc-ts
- Owner: azz
- License: mit
- Created: 2017-01-30T08:58:08.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2017-01-30T11:48:51.000Z (almost 8 years ago)
- Last Synced: 2024-04-27T09:28:52.117Z (8 months ago)
- Language: TypeScript
- Size: 6.84 KB
- Stars: 2
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[![Build Status](https://travis-ci.org/DerFlatulator/ioc-ts.svg)][ci-server]
[![npm version](https://badge.fury.io/js/ioc-ts.svg)][npm-package]# ioc.ts
TypeScript Dependency Injection
## Features
* [x] Type safe
* [x] Tiny (614 bytes before minification or compression)
* [x] No classes required
* [x] Supports multiple containers## Install
```bash
npm i -S ioc-ts
```## API
Import:
```
import container from 'ioc-ts'
```### `container(deps?: { [key: string]: any }): Container`
Create a container, optionally specifying dependencies.
Dependencies should be an object. Returns the container.
```ts
const c = container({
x: 'foo',
y: 'bar',
})
```### `Container#define(deps?: { [key: string]: any }): Container`
Add additional dependencies to a container. Does not mutate underlying
container. Can be chained.```ts
const c = container({ x: 'foo' }).define({ y: 1 }).define({ z: true })
// c now has x, y and z.
```### `Container#combine(container: Container): Container`
Combine the dependencies of two containers. Does not mutate underlying
container. Can be chained.```ts
const c1 = container({ x: 'foo' })
const c2 = container({ y: 1 })
const c = container({ z: true }).combine(c1).combine(c2)
// c now has x, y and z.
```### `Container#inject(fn: Function): Function`
Inject a function with dependencies from a container.
Container must type-match the dependencies of the target function.
Target function must specify dependencies as the first argument.
```ts
function iNeedDependencies(deps: { x: string }, arg1: number) {
console.log(deps.x, arg1)
}const c = container({ x: 'foo' })
const iHasDependencies = c.inject(iNeedDependencies)
iHasDependencies(42) // logs 'foo', 42
```[npm-package]: https://www.npmjs.com/package/ioc-ts
[ci-server]: https://travis-ci.org/DerFlatulator/ioc-ts