Skip to content

Commit a4e108b

Browse files
authored
Pass action to container.get (#1)
* pass action through to IocAdapter * make class-validator a peer dependency * make package public * use correct github org * npm scripts for publish * fix readme example * improve type clarity * reuse type definition
1 parent 4a56d17 commit a4e108b

File tree

9 files changed

+1294
-1219
lines changed

9 files changed

+1294
-1219
lines changed

README.md

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ You can use routing-controllers with [express.js][1] or [koa.js][2].
7373

7474
1. Install module:
7575

76-
`npm install routing-controllers --save`
76+
`npm install routing-controllers class-validator --save`
7777

7878
2. `reflect-metadata` shim is required:
7979

@@ -1359,6 +1359,39 @@ export class UsersController {
13591359
}
13601360
```
13611361
1362+
For other IoC providers that don't expose a `get(xxx)` function, you can create an IoC adapter using `IocAdapter` like so:
1363+
1364+
```typescript
1365+
// inversify-adapter.ts
1366+
import { IoCAdapter } from 'routing-controllers'
1367+
import { Container } from 'inversify'
1368+
1369+
class InversifyAdapter implements IocAdapter {
1370+
constructor (
1371+
private readonly container: Container
1372+
) {
1373+
}
1374+
1375+
get<T> (someClass: ClassConstructor<T>, action?: Action): T {
1376+
const childContainer = this.container.createChild()
1377+
childContainer.bind(API_SYMBOLS.ClientIp).toConstantValue(action.context.ip)
1378+
return childContainer.resolve<T>(someClass)
1379+
}
1380+
}
1381+
```
1382+
1383+
And then tell Routing Controllers to use it:
1384+
```typescript
1385+
// Somewhere in your app startup
1386+
import { useContainer } from 'routing-controllers'
1387+
import { Container } from 'inversify'
1388+
import { InversifyAdapter } from './inversify-adapter.ts'
1389+
1390+
const container = new Container()
1391+
const inversifyAdapter = new InversifyAdapter(container)
1392+
useContainer(inversifyAdapter)
1393+
```
1394+
13621395
## Custom parameter decorators
13631396
13641397
You can create your own parameter decorators.

0 commit comments

Comments
 (0)