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

routify-hapi-controllers

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

routify-hapi-controllers

Load your controllers as Hapi routes

  • 0.0.1-beta4
  • latest
  • npm
  • Socket score

Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

routify-hapi-controllers

A TypesScript or ES7 decorators library that generates Hapi routes for controllers based on decorators.

Dependencies

First and foremost your project needs Typescript or at least support to ES7 Decorators.

Main libraries you need for it to work:
Hapi 16
npm install hapi@16.1.0 --save

// For Typescript you also need
npm install @types/hapi --save-dev
Inversify

Needed for dependency injection.

npm install inversify --save

// For Typescript you also need
npm install @types/inversify --save-dev

It is actually possible to use the library without Inversify instantiating the controllers and their dependencies by hand and loading the routes for the controllers individually.

Reflect-Metadata

A polyfill for reflection necessary for both Inversify to work and for this library's decorators.

npm install reflect-metadata --save

// For Typescript you also need
npm install @types/reflect-metadata --save-dev

Getting Started

First install the library:
npm install routify-hapi-controllers --save

This library already has typings built-in.

Load Reflect Metadata

In your main TS or JS file load reflect-metadata, it has to be before every other dependencies.

app.ts (example)
import 'reflect-metadata';
import { Server } from 'hapi';

(...)
Setup your first Controller

For a controller you need to implement the IController interface (if in Typescript), load the Controller decorator as well as Inversify's injectable decorators and the method decorators.

import { injectable } from 'inversify';
import { Controller, Get, Post } from 'routify-hapi-controllers';

@injectable()
@Controller('/v1/lemmings'/* , { (Optional Hapi route config for all endpoints) } */)
export default class LemmingsController {
  @Get()
  public get(request: Request) {
    // Results in "GET /v1/lemmings"
  }

  @Get('/alive')
  public getAlive(request: Request) {
    // Results in "GET /v1/lemmings/alive"
  }

  @Post('/explode')
  public postExplode(request: Request) {
    // Results in "POST /v1/lemmings/explode"
  }
}

You can have the optional Hapi config in individual endpoints as well, they are merged with the controller's configs for that endpoint.

You can also have automatically resolved dependencies in your controllers, check out Inversify's documentation for that.

If you don't want to use inversify, you can get rid of the @injectable decorator.

Register your Controller (and dependencies)

You can do this anywhere, but it's probably better to have a file dedicated to that as your list of registrations might become big.

di.container.ts (or whatever)
import { Container } from 'inversify';
import LemmingsController from './controllers/lemmings.controller';
import { TYPES, registerController, IController } from 'routify-hapi-controllers';

const container = new Container();

// You can either register the controller using the inversify bind function:
container.bind<IController>(TYPES.CONTROLLER).to(LemmingsController);

// Or alternatively you can register with our helper function that makes things look nicer.
registerController(container, LemmingsController);
Load your routes
server.ts
import 'reflect-metadata'; // This was loaded in the first step.
import { container } from './di.container.ts';
import { retrieveAllRoutes } from 'routify-hapi-controllers';

// This will retrieve all routes for all of the registered Controllers
const routes = retrieveAllRoutes(container);

/*
Alternatively, if you didn't use Inversify, you can load the routes for specific Controllers.

const lemmingsController = new LemmingsController();
const potatoController = new PotatoController();

const routes = [
  ...getRoutesFromController(lemmingsController),
  ...getRoutesFromController(potatoController)
];
*/

FAQs

Package last updated on 20 Feb 2018

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