Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@yveskaufmann/inversify-koa-utils

Package Overview
Dependencies
Maintainers
1
Versions
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@yveskaufmann/inversify-koa-utils

Use inversify in your koa based applications

  • 0.0.3
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
Maintainers
1
Weekly downloads
 
Created
Source

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);

Keywords

FAQs

Package last updated on 11 May 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc