hapiour-decorators
Typescript decorators for Hapi
Install
npm install -g typescriptnpm install --save hapinpm 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
@@@ implements IApp public server: Server public { thisserver = server } public : void console public : void // server decorated by the AwesomePlugin if thisserver'isBeerAwesome' console public : void console
Declare a module
src/beer.module.ts
@ private beerCount: number public { thisbeerCount = 0 } @ public { thisbeerCount++ } @ public { } @ public { thisbeerCount = 0 }
Declare and configure a plugin
src/health.plugin.ts
@ implements IPluginConfigurator public options: IPluginOptions public { thisoptions = path: '/health' tags: 'health' 'monitor' healthCheck: thishealthCheck } private : void
Create a plugin
src/awesome.plugin.ts
@Plugin name: 'Awesome' version: '0.1.0' implements IPlugin public { } public : void server
Bootstrap your app
src/main.ts
Or you can Bootstrap your app with options
src/main.ts
In this example apps are bootstraped with an external dependency injection library typedi
;
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.