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

hapiour-decorators

v1.4.2

Published

Typescript decorators for Hapi

Downloads

23

Readme

Build Status npm version npm downloads dependencies npm devDependencies npm license

hapiour-decorators

Typescript decorators for Hapi

Install

npm install -g typescript
npm install --save hapi
npm install --save hapiour-decorators

Example

Files

src/
  -> main.ts
  -> app.ts
  -> beer.module.ts
  -> awesome.plugin.ts
  -> health.plugin.ts

Declare your app

src/app.ts

import { Server } from 'hapi'
import { App, IApp, Inject, Plugins } from 'hapiour-decorators'
import { Beer } from './beer.module'
import { AwesomePlugin } from './awesome.plugin'
import { HealthPluginConfigurator } from './health.plugin'

@App({
  port: 3000
})
@Inject([Beer])
@Plugins([AwesomePlugin, HealthPluginConfigurator])
export class MyApp implements IApp {

  public server: Server

  public constructor(server: Server) {
    this.server = server
  }

  public onInit(): void {
    console.log('Server init done')
  }

  public onRegister(): void {
    // server decorated by the AwesomePlugin
    if (this.server['isBeerAwesome']) {
      console.log('Beer is awesome')
    }
  }

  public onStart(): void {
    console.log('Server running at:', this.server.info.uri)
  }

}

Declare a module

src/beer.module.ts

import { Route, Module } from 'hapiour-decorators'
import { Request, ReplyNoContinue } from 'hapi'

@Module({
  basePath: '/beer'
})
export class Beer {

  private beerCount: number

  public constructor() {
    this.beerCount = 0
  }

  @Route({
    method: 'GET',
    path: '',
    config: {}
  })
  public getABeer(request: Request, reply: ReplyNoContinue) {
    this.beerCount++
    reply({
      'data': 'Hey! Take this beer !'
    })
  }

  @Route({
    method: 'GET',
    path: '/count',
    config: {}
  })
  public getCount(request: Request, reply: ReplyNoContinue) {
    reply({
      'data': this.beerCount
    })
  }

  @Route({
    method: 'DELETE',
    path: '/count',
    config: {}
  })
  public resetCount(request: Request, reply: ReplyNoContinue) {
    this.beerCount = 0
    reply({
      'data': 'Done'
    })
  }

}

Declare and configure a plugin

src/health.plugin.ts

import { PluginConfigurator, IPluginConfigurator, IPluginOptions } from 'hapiour-decorators'
import { Server } from 'hapi'
import * as alive from 'hapi-alive'

@PluginConfigurator(alive)
export class HealthPluginConfigurator implements IPluginConfigurator {

  public options: IPluginOptions

  public constructor() {
    this.options = {
      path: '/health',
      tags: ['health', 'monitor'],
      healthCheck: this.healthCheck
    }
  }

  private healthCheck(server: Server, callback: () => void): void {
    callback()
  }

}

Create a plugin

src/awesome.plugin.ts

import { Plugin, IPlugin, IPluginOptions } from 'hapiour-decorators'
import { Server } from 'hapi'

@Plugin({
  name: 'Awesome',
  version: '0.1.0'
})
export class AwesomePlugin implements IPlugin {

  public constructor() {
  }

  public register(server: Server, options: IPluginOptions, next: () => void): void {
    server.decorate('server', 'isBeerAwesome', () => {
      return true
    })
    next()
  }

}

Bootstrap your app

src/main.ts

import { bootstrap } from 'hapiour-decorators'
import { MyApp } from './app'

bootstrap(MyApp)

Or you can Bootstrap your app with options

src/main.ts

In this example apps are bootstraped with an external dependency injection library typedi

import { bootstrap } from 'hapiour-decorators'
import { MyApp } from './app'
import { Container } from 'typedi';

bootstrapWithOptions([MyApp], {
  injector: Container
})

API

Decorators

Class Decorators

  • @App(config: Hapi.IServerConnectionOptions) : Declare a new App (correspond to a new Hapi.Server instance).
  • @Module(config: IModuleConfig) : Declare a new Module (class containing routes).
  • @Inject(modules: Array<Module>) : Assign an array of modules to an App or a Module.
  • @Plugins(plugins: Array<IPlugin|IPluginConfigurator>) : Assign an array of plugins to an App.
  • @Plugin(attributes: {'name': String, 'version': String}) : Declare a new plugin.
  • @PluginConfigurator(plugin: IPlugin) : Declare and configure a plugin.

Method decorators

  • @Route(config: Hapi.IRouteConfiguration) : Declare a new Route inside a Module. The target method will become the route handler.

Interfaces

IApp

  • constructor(server: Hapi.Server) : App will be constructed with Hapi server instance as first argument.
  • onInit(): Method called when Hapi server initialization is done.
  • onRegister(): Method called when Hapi plugin registration is done.
  • onStart(): Method called when Hapi server is started.

IModuleConfig

  • basePath: string : Base path applied to all contained routes in the module.

IPlugin

  • register(server: Server, options: IPluginOptions, next: () => void): void : Plugin core function to be registered into Hapi.

IPluginConfigurator

  • options: IPluginOptions : Options used to configure a given plugin.

IBootstrapOptions

  • injector?: IInjector : Set a custom injector to your apps.

IInjector

  • get<T>(Dependency: IStatic<T>): T : Injector should at least have a get method to get dependencies.

Methods

  • bootstrap(...apps: Array<IApp>): Array<IApp> : Bootstrap your apps. Return an array of bootstraped apps.
  • bootstrapWithOptions(apps: Array<IApp>, options: IBootstrapOptions): Array<IApp> : Bootstrap your apps with options. Return an array of bootstraped apps.