Coder Social home page Coder Social logo

hapi-decorators's Introduction

hapi-decorators

Decorators for HapiJS routes. Heavily inspired and borrowed from https://github.com/stewartml/express-decorators

Great to mix with https://github.com/jayphelps/core-decorators.js

npm version Build Status Coverage Status

Usage

npm install --save hapi-decorators
import {
  get,
  controller
} from 'hapi-decorators'
import Hapi from 'hapi'

const server = new Hapi.Server()

server.connection({
  host: 'localhost',
  port: 3000
})

// Define your endpoint controller
@controller('/hello')
class TestController {
  constructor(target) {
    this.target = target
  }

  @get('/world')
  sayHello(request, reply) {
    reply({ message: `hello, ${this.target}` })
  }
}

// InitializeController
let test = new TestController('world')

// Add Test Controller routes to server
server.routes(test.routes())

// Start the server
server.start((err) => {
  if (err) throw err
  console.log(`Server running at: ${server.info.uri}`)
})

Setup Babel

Run the above script with the following command, after installing babel.

babel-node --optional es7.decorators,es7.objectRestSpread index.js

Note: Decorators are currently unsupported in Babel 6. To work around that issue use the transform-decorators-legacy plugin. See this post for detailed instructions.

Decorators

@controller(basePath)

REQUIRED This decorator is required at the class level, since it processes the other decorators, and adds the instance.routes() function, which returns the routes that can be used with Hapi, e.g. server.routes(users.routes()).

@route(method, path)

This decorator should be attached to a method of a class, e.g.

@controller('/users')
class Users {
  @route('post', '/')
  newUser(request, reply) {
    reply([])
  }
}

Helper Decorators

  • @get(path)
  • @post(path)
  • @put(path)
  • @patch(path)
  • @delete(path)
  • @all(path)

These are shortcuts for @route(method, path) where @get('/revoke') would be @route('get', '/revoke').

@config(config)

Overall configuration setting if none of the other decorators are sufficient.

@validate(validateConfig)

Add a validation object for the different types, except for the response. config is an object, with keys for the different types, e.g. payload.

@cache(cacheConfig)

Cache settings for the route config object.

hapi-decorators's People

Contributors

gausie avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.