An open API service indexing awesome lists of open source software.

https://github.com/devtin/pleasure-di

a simple dependency injection module
https://github.com/devtin/pleasure-di

Last synced: about 1 year ago
JSON representation

a simple dependency injection module

Awesome Lists containing this project

README

          

pleasure-di


Version


a simple dependency injection module

## Installation

```sh
$ npm i pleasure-di --save
# or
$ yarn add pleasure-di
```

## Features

- [register containers](#register-containers)
- [injects dependencies](#injects-dependencies)
- [provides an interface to customize the container resolution](#provides-an-interface-to-customize-the-container-resolution)

## register containers

```js
const container = pleasureDi({
Gateway (name) {
if (name === 'NotFoundGateway') {
return
}

return () => {}
}
})

t.throws(() => container.SomeService, {
message: 'No suffixed container name registered matching SomeService'
})

t.throws(() => container.NotFoundGateway, {
message: 'Resource NotFoundGateway not found'
})
t.notThrows(() => container.SomeGateway)
```

## injects dependencies

```js
const container = pleasureDi({
Gateway (gatewayName) {
// holds the logic that resolves given gatewayName
// returns a function that will come with the container
return (/* container */) => {
return {
methodA (caller) {
return `methodA from ${gatewayName} called by ${caller}`
},
methodB (caller) {
return `methodB from ${gatewayName} called by ${caller}`
}
}
}
},
Service (serviceName) {
// holds the logic that resolves given serviceName
// returns a function that will come with the container
return ({ FreakingGateway }) => {
return {
methodC () {
return FreakingGateway.methodA(serviceName)
},
methodD () {
return FreakingGateway.methodB(serviceName)
}
}
}
}
})

const { PaymentService } = container

t.is(PaymentService.methodC(), 'methodA from FreakingGateway called by PaymentService')
t.is(PaymentService.methodD(), 'methodB from FreakingGateway called by PaymentService')
```

## provides an interface to customize the container resolution

```js
const resolveFixture = (...fixturePath) => {
return path.join(__dirname, '__tests__/fixtures', ...fixturePath)
}
const container = pleasureDi({
Gateway (gatewayName) {
return require(resolveFixture('gateways', gatewayName) + '.js')
},
Repository (respositoryName) {
return require(resolveFixture('repositories', respositoryName) + '.js')
},
Service (serviceName) {
return require(resolveFixture('services', serviceName) + '.js')
}
})

const { OrderService } = container

t.deepEqual(OrderService.findByUserId(123), {
id: 123
})
t.is(OrderService.pay(1111), 'amount 1111 paid')
t.throws(() => container.SomeService, {
code: 'MODULE_NOT_FOUND'
})
```


### pleasureDi(resolvers) ⇒ Object \| \*

| Param | Type | Description |
| --- | --- | --- |
| resolvers | Object | resolvers map |


### Resolved ⇒ Object

| Param | Type |
| --- | --- |
| container | Object |


### Resolver ⇒ [Resolved](#Resolved)

| Param | Type |
| --- | --- |
| containerName | String |

* * *

### License

[MIT](https://opensource.org/licenses/MIT)

© 2020-present Martin Rafael Gonzalez