peppermint-di
Advanced tools
Comparing version 2.0.0 to 2.0.1
# Change Log | ||
## [2.0.1 - 2019-08-25](https://github.com/alonrbar/peppermint-di/tree/v2.0.1) | ||
### Fixed | ||
- Tune `ResolveOptions.params` typings. | ||
## [2.0.0 - 2019-08-24](https://github.com/alonrbar/peppermint-di/tree/v2.0.0) | ||
@@ -4,0 +10,0 @@ |
@@ -118,3 +118,3 @@ | ||
*/ | ||
params?: Map<any, any>; | ||
params?: Map<ContainerKey<any>, any>; | ||
} | ||
@@ -121,0 +121,0 @@ |
{ | ||
"name": "peppermint-di", | ||
"version": "2.0.0", | ||
"version": "2.0.1", | ||
"description": "Dependency injection container for TypeScript and JavaScript", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
145
README.md
@@ -10,12 +10,2 @@ # peppermint-di | ||
This project was originally based on [this blog post](http://www.yusufaytas.com/dependency-injection-in-javascript/) by Yusuf Aytas as it appeared in [this StackOverflow question](https://stackoverflow.com/questions/20058391/javascript-SomeService-injection). | ||
It has since evolved to support: | ||
- TypeScript | ||
- Singleton registration | ||
- Interface registration | ||
- Instance initializers | ||
- Optional parameters | ||
- and more... | ||
## Installation | ||
@@ -33,5 +23,5 @@ | ||
## Short Example - TypeScript | ||
## The gist | ||
```javascript | ||
```typescript | ||
import { Container, injectable } from 'peppermint-di'; | ||
@@ -58,35 +48,65 @@ | ||
## Short Example - JavaScript | ||
## Table of Content | ||
```javascript | ||
import { Container } from 'peppermint-di'; | ||
- [Installation](#installation) | ||
- [The gist](#the-gist) | ||
- [Credits](#credits) | ||
- [Examples](#examples) | ||
- [TypeScript](#typescript) | ||
- [Custom parameters](#custom-parameters) | ||
- [Interface registration](#interface-registration) | ||
- [Instance initializers](#instance-initializers) | ||
- [JavaScript](#javascript) | ||
- [Simple example](#simple-example) | ||
- [Custom parameters](#custom-parameters-1) | ||
- [API](#api) | ||
- [The Container](#container) | ||
- [ResolveOptions](#resolveoptions) | ||
- [Changelog](#changelog) | ||
## Credits | ||
This project was originally based on [this blog post](http://www.yusufaytas.com/dependency-injection-in-javascript/) by Yusuf Aytas as it appeared in [this StackOverflow question](https://stackoverflow.com/questions/20058391/javascript-SomeService-injection). | ||
The API is somewhat inspired by the excellent [Simple Injector](https://simpleinjector.readthedocs.io/en/latest/quickstart.html#introducing-simple-injector) C# library. | ||
## Examples | ||
### TypeScript | ||
#### Custom parameters | ||
```typescript | ||
import { Container, injectable } from 'peppermint-di'; | ||
class SomeService { | ||
// ... | ||
public name = 'default name'; | ||
} | ||
class SomeClass { | ||
constructor(someService) { | ||
// ... | ||
@injectable | ||
class MyClass { | ||
public myService: SomeService; | ||
constructor(myService: SomeService) { | ||
this.myService = myService; | ||
} | ||
} | ||
const container = new Container(); | ||
container.registerSingle('someService', SomeService); | ||
const customDep = new SomeService(); | ||
customDep.name = 'custom name'; | ||
const myClass = container.get(SomeClass); | ||
const customParameters = new Map([ | ||
[SomeService, customDep] | ||
]); | ||
const myClass = container.get(MyClass, { params: customParameters }); | ||
expect(myClass.myService).to.be.instanceOf(SomeService); | ||
expect(myClass.myService.name).to.eql('custom name'); | ||
``` | ||
## More Examples | ||
#### Interface registration | ||
- [Interface registration](#interface-registration-typescript) | ||
- [Instance initializers](#instance-initializers-typescript) | ||
- [Custom parameters - TypeScript](#custom-parameters-typescript) | ||
- [Custom parameters - JavaScript](#custom-parameters-javascript) | ||
### Interface registration - TypeScript | ||
```javascript | ||
```typescript | ||
import { Container, i, injectable } from 'peppermint-di'; | ||
@@ -137,5 +157,5 @@ | ||
### Instance initializers - TypeScript | ||
#### Instance initializers | ||
```javascript | ||
```typescript | ||
import { Container, injectable } from 'peppermint-di'; | ||
@@ -158,36 +178,28 @@ | ||
### Custom parameters - TypeScript | ||
### JavaScript | ||
#### Simple example | ||
```javascript | ||
import { Container, injectable } from 'peppermint-di'; | ||
import { Container } from 'peppermint-di'; | ||
class SomeService { | ||
public name = 'default name'; | ||
// ... | ||
} | ||
@injectable | ||
class MyClass { | ||
public myService: SomeService; | ||
constructor(myService: SomeService) { | ||
this.myService = myService; | ||
class SomeClass { | ||
constructor(someService) { | ||
// ... | ||
} | ||
} | ||
const container = new Container(); | ||
const customDep = new SomeService(); | ||
customDep.name = 'custom name'; | ||
container.registerSingle('someService', SomeService); | ||
const customParameters = new Map([ | ||
[SomeService, customDep] | ||
]); | ||
const myClass = container.get(MyClass, { params: customParameters }); | ||
expect(myClass.myService).to.be.instanceOf(SomeService); | ||
expect(myClass.myService.name).to.eql('custom name'); | ||
const myClass = container.get(SomeClass); | ||
``` | ||
### Custom parameters - JavaScript | ||
#### Custom parameters | ||
@@ -198,10 +210,9 @@ ```javascript | ||
class SomeService { | ||
public name = 'default name'; | ||
constructor() { | ||
this.name = 'default name'; | ||
} | ||
} | ||
class MyClass { | ||
public myService: SomeService; | ||
constructor(myService: SomeService) { | ||
constructor(myService) { | ||
this.myService = myService; | ||
@@ -235,3 +246,3 @@ } | ||
```javascript | ||
```typescript | ||
type ContainerKey<T> = Constructor<T> | SimpleContainerKey; | ||
@@ -244,3 +255,3 @@ | ||
```javascript | ||
```typescript | ||
Container.register<T>(key: Constructor<T>, type?: Constructor<T>): void; | ||
@@ -255,3 +266,3 @@ | ||
```javascript | ||
```typescript | ||
Container.registerSingle<T>(key: Constructor<T>, valueOrType?: T | Constructor<T>): void; | ||
@@ -266,3 +277,3 @@ | ||
```javascript | ||
```typescript | ||
type Initializer<T> = (instance: T) => void; | ||
@@ -275,3 +286,3 @@ | ||
```javascript | ||
```typescript | ||
Container.get<T>(key: ContainerKey<T>, options?: ResolveOptions): T; | ||
@@ -282,3 +293,3 @@ ``` | ||
```javascript | ||
```typescript | ||
Container.call(foo: Function, thisArg?: any, options?: ResolveOptions): any; | ||
@@ -289,3 +300,3 @@ ``` | ||
```javascript | ||
```typescript | ||
class ResolveOptions { | ||
@@ -312,3 +323,3 @@ | ||
*/ | ||
params?: IDictionary<any>; | ||
params?: Map<ContainerKey<any>, any>; | ||
} | ||
@@ -319,2 +330,2 @@ ``` | ||
The change log can be found [here](https://github.com/alonrbar/peppermint-di/blob/master/CHANGELOG.md). | ||
The change log can be found [here](https://github.com/alonrbar/peppermint-di/blob/master/CHANGELOG.md). |
@@ -0,1 +1,2 @@ | ||
import { ContainerKey } from './types'; | ||
@@ -23,3 +24,3 @@ export class ResolveOptions { | ||
*/ | ||
public params?: Map<any, any>; | ||
public params?: Map<ContainerKey<any>, any>; | ||
} |
Sorry, the diff of this file is not supported yet
112325
1311
317