This package has been deprecated

Author message:

Package no longer supported.

be-hapi
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

be-hapi

CircleCI npm version Dependencies devDependencies Known Vulnerabilities

NPM

Decorated class-based controllers for Hapi with IoC support (like InversifyJS).

Installation

You can get latest release with type definitions from NPM:

npm install be-hapi reflect-metadata --save

be-hapi requires TypeScript >= 2.0 and the experimentalDecorators, lib and types compilation option.

{
    "compilerOptions": {
        "target": "es5",
        "lib": ["es6"],
        "types": ["reflect-metadata"],
        "module": "commonjs",
        "moduleResolution": "node",
        "experimentalDecorators": true
    }
}

Usage

Very basic usage:

1. Create your first controller class

//controller.ts
import { controller, get, param } from 'be-hapi'
 
@controller()
class HelloController {
 
  @get('/{name?}')
  public index(
    @param('name', 'You') name: string
  ) {
    return `Hi, ${name}!`
  }
 
}

2. Register the plugin with Hapi

// server.ts
import { Server } from '@hapi/hapi' 
import beHapi from 'be-hapi'
 
// import previously created controller
import './controller'
 
(async () => {
    const server = new Server({ port: 3000 })
    await server.register(beHapi)
    await server.start()
})()

Now, compile, start the app and go to http://localhost:3000/.

More examples

API

Plugin options

Name Type Description
registerController (controller: ControllerConstructor) => void Function called on controller registration. Called once for each discovered controller.
controllerFactory (controller: ControllerConstructor) => object Function called that should return instance of controller. Called on each request to the controller. If not specified, returns new instance of controller class.

Available decorators

Name Target Description
@controller(basePath?, baseRouteSpec?) Class Decorates controller class. Can specify base path prefix and base route spec for all routes of the controller.
@route(routeSpec) Method, Class* Sets route spec.
@path(path) Method, Class* Sets path for the route.
@method(httpMethod) Method, Class* Sets HTTP method (eg. GET, POST...)
@get(path?) Method, Class* Sets HTTP method to GET. Optionally set path for the route.
@post(path?) Method, Class* Sets HTTP method to POST. Optionally set path for the route.
@put(path?) Method, Class* Sets HTTP method to PUT. Optionally set path for the route.
@patch(path?) Method, Class* Sets HTTP method to PATCH. Optionally set path for the route.
@del(path?) Method, Class* Sets HTTP method to DELETE. Optionally set path for the route.
@options(path?) Method, Class* Sets HTTP method to OPTIONS. Optionally set path for the route.
@all(path?) Method, Class* Sets HTTP method to *. Optionally set path for the route.
@vhost(hostOrHosts) Method, Class* Sets virtual host.
@rules(rules) Method, Class* Sets custom rules.
@routeOptions(options) Method, Class* Sets route options.
@routeOption(name, value) Method, Class* Sets single route option.
@cache(value) Method, Class* Sets cache option. See route option.
@cors(value) Method, Class* Sets cors option. See route option.
@description(value) Method, Class* Sets description option. See route option.
@notes(value) Method, Class* Sets notes option. See route option.
@pre(value) Method, Class* Sets pre option. See route option.
@response(value) Method, Class* Sets response option. See route option.
@security(value) Method, Class* Sets security option. See route option.
@tags(value) Method, Class* Sets tags option. See route option.
@validate(value) Method, Class* Sets validate option. See route option.
@plugin(name, options) Method, Class* Sets plugin options. See route option.
@param(name,defaultValue?) Method parameter Inject specified request param.
@queryParam(name,defaultValue?) Method parameter Inject specified request query param.
@cookie(name,defaultValue?) Method parameter Inject specified request cookie value.
@payload(name?,defaultValue?) Method parameter Inject request payload. If name would be specified, only requested property from payload will be injected.
@request(), @req() Method parameter Inject request object.
@responseToolkit(), @res() Method parameter Inject response toolkit object.

Footnotes:

Class* - you can decorate controller with method decorators to set base route specification for all routes in decorated controller.

Helpers

useInversify(container) - returns plugin options to easy integrate InversifyJS. See example.

Package Sidebar

Install

npm i be-hapi

Weekly Downloads

0

Version

1.1.0

License

MIT

Unpacked Size

40.1 kB

Total Files

27

Last publish

Collaborators

  • mckacz