Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
hapi-decorators
Advanced tools
Changelog
2.0.0 (2019-11-12)
drop node v6 support
drop hapi < 18.4
Security fixes - non-breaking changes
Security vulnerabilities with force fix
Updating some packages
Updating to the new hapi libraries
<a name="1.0.0"></a>
Readme
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
Prerequisits:
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}`)
})
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.
@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.
FAQs
Decorators for HapiJS routes using ES6 classes
The npm package hapi-decorators receives a total of 8,082 weekly downloads. As such, hapi-decorators popularity was classified as popular.
We found that hapi-decorators demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.