All Projects → knownasilya → hapi-decorators

knownasilya / hapi-decorators

Licence: other
Decorators for HapiJS routes

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to hapi-decorators

resty
A Node.js framework
Stars: ✭ 20 (-69.23%)
Mutual labels:  decorators
tinsel
Use Decorators to Transform Functions
Stars: ✭ 18 (-72.31%)
Mutual labels:  decorators
next-api-decorators
Collection of decorators to create typed Next.js API routes, with easy request validation and transformation.
Stars: ✭ 187 (+187.69%)
Mutual labels:  decorators
hapi-react-fullstack-boilerplate
Hapi, Sequelize, React, etc.
Stars: ✭ 21 (-67.69%)
Mutual labels:  hapi
graphql-ts
Graphql implementation in Typescript using decorator
Stars: ✭ 63 (-3.08%)
Mutual labels:  decorators
api.pokedextracker.com
API for pokedextracker.com
Stars: ✭ 38 (-41.54%)
Mutual labels:  hapi
drape
Drape – Reincarnation of Draper for Rails 5
Stars: ✭ 57 (-12.31%)
Mutual labels:  decorators
hapi-moon
Hassle-free and production ready hapi.js Server boilerplate
Stars: ✭ 23 (-64.62%)
Mutual labels:  hapi
typesafe-hapi
Typechecking for HapiJS based on Joi schemas!
Stars: ✭ 21 (-67.69%)
Mutual labels:  hapi
organiser
An organic web framework for organized web servers.
Stars: ✭ 58 (-10.77%)
Mutual labels:  decorators
stupid-python-tricks
Stupid Python tricks.
Stars: ✭ 112 (+72.31%)
Mutual labels:  decorators
vue3-oop
使用类和依赖注入写vue组件
Stars: ✭ 90 (+38.46%)
Mutual labels:  decorators
my-blog
node.js vue.js nuxt.js hapi.js mysql 仿简书博客
Stars: ✭ 25 (-61.54%)
Mutual labels:  hapi
ta-json
Type-aware JSON serializer/parser
Stars: ✭ 67 (+3.08%)
Mutual labels:  decorators
hapi-auth-bearer-simple
Hapi authentication plugin for bearer token validation
Stars: ✭ 16 (-75.38%)
Mutual labels:  hapi
koa-smart
A framework base on Koajs2 with Decorator, Params checker and a base of modules (cors, bodyparser, compress, I18n, etc…) to let you develop smart api easily
Stars: ✭ 31 (-52.31%)
Mutual labels:  decorators
wily
Build Node.js APIs from the command line (Dead Project 😵)
Stars: ✭ 14 (-78.46%)
Mutual labels:  hapi
steerage
Hapi server configuration and composition using confidence, topo, and shortstop.
Stars: ✭ 15 (-76.92%)
Mutual labels:  hapi
capsid
💊 Declarative DOM programming library. Lightweight (1.79 kb).
Stars: ✭ 76 (+16.92%)
Mutual labels:  decorators
easy cache
Caching and invalidation have never been so easy
Stars: ✭ 28 (-56.92%)
Mutual labels:  decorators

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

Prerequisits:

  • Hapi 18.4+ (Use 1.x for Hapi 17)
  • Node 8+ (Use 1.x for Node 6)
npm install --save hapi-decorators
import {
  get,
  controller
} from 'hapi-decorators'
import Hapi from '@hapi/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.route(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)
  • @del(path)
  • @all(path)

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

@options(options)

Overall options 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.

@pre(preArray)

Set prerequisite middleware array for a given route. Expects an array, but if passed something else, it will put it into the pre array.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].