npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

routify-hapi-controllers

v0.0.1-beta4

Published

Load your controllers as Hapi routes

Downloads

14

Readme

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 [email protected] --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)
];
*/