https://github.com/knownasilya/hapi-decorators
Decorators for HapiJS routes
https://github.com/knownasilya/hapi-decorators
decorators hapi hapi-decorators nodejs
Last synced: 10 months ago
JSON representation
Decorators for HapiJS routes
- Host: GitHub
- URL: https://github.com/knownasilya/hapi-decorators
- Owner: knownasilya
- Created: 2015-08-29T01:28:06.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2023-01-27T04:30:45.000Z (about 3 years ago)
- Last Synced: 2025-03-30T21:41:33.809Z (12 months ago)
- Topics: decorators, hapi, hapi-decorators, nodejs
- Language: JavaScript
- Homepage: http://knownasilya.github.io/hapi-decorators
- Size: 1.64 MB
- Stars: 63
- Watchers: 6
- Forks: 9
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
# 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
[](http://badge.fury.io/js/hapi-decorators)
[](https://travis-ci.org/knownasilya/hapi-decorators)
[](https://coveralls.io/github/knownasilya/hapi-decorators?branch=master)
## Usage
Prerequisits:
- Hapi 18.4+ (Use 1.x for Hapi 17)
- Node 8+ (Use 1.x for Node 6)
```sh
npm install --save hapi-decorators
```
```js
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].
```no-highlight
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.
```js
@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.
[babel]: https://www.npmjs.com/package/babel
[transform-decorators-legacy]: https://www.npmjs.com/package/babel-plugin-transform-decorators-legacy
[issue]: https://phabricator.babeljs.io/T2645
[post]: http://technologyadvice.github.io/es7-decorators-babel6