@yveskaufmann/inversify-koa-utils
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

inversify-koa-utils

Node.js CI

Use Inversify in your koa based applications, this modules is based on inversify-express-utils.

Installation

You can install @yveskaufmann/inversify-koa-utils using npm:

npm install inversify @yveskaufmann/inversify-koa-utils --save

The Basics

Step 1: Decorate your controllers

To use a class as a "controller" for your restify app, simply add the @Controller decorator to the class. Similarly, decorate methods of the class to serve as request handlers. The following example will declare a controller that responds to `GET /foo'.

import { inject } from 'inversify';
import { HttpController } from '@yveskaufmann/inversify-koa-utils';

@HttpController('/users')
class UserController {
  @inject('logService')
  private logService: LogService;

  @Get('/:id')
  getUserById(@HttpParam(PARAMETER_TYPE.PARAMS, 'Id') id: string) {
    this.logService.info(`Found user with userId=${id}`);
    return {
      id,
      name: `${user}-id`,
    };
  }
}

Step 2: Configure container and server

Configure the inversify container in your composition root as usual.

Then, pass the container to the InversifyKoaServer constructor. This will allow it to register all controllers and their dependencies from your container and attach them to the koa app. Then just call server.build() to prepare your app.

In order for the InversifyKoaServer to find your controllers, you must import them along the InversifyKoaServer.

import { Container } from 'inversify';
import { interfaces, InversifyKoaServer, TYPE } from '@yveskaufmann/inversify-koa-utils';

// set up container
let container = new Container();

container.bind<LogService>('logService').to(LogService);

// create server
let server = new InversifyRestifyServer(container);

// start listening on port 3000
let app = server.build();
app.listen(3000);

Readme

Keywords

Package Sidebar

Install

npm i @yveskaufmann/inversify-koa-utils

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

44.7 kB

Total Files

37

Last publish

Collaborators

  • yv35