https://github.com/futurestudio/hapi-class-extension-points
hapi plugin that registers lifecycle extension points from class methods
https://github.com/futurestudio/hapi-class-extension-points
Last synced: 10 months ago
JSON representation
hapi plugin that registers lifecycle extension points from class methods
- Host: GitHub
- URL: https://github.com/futurestudio/hapi-class-extension-points
- Owner: futurestudio
- License: mit
- Created: 2019-06-07T12:53:11.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2024-01-26T04:09:41.000Z (about 2 years ago)
- Last Synced: 2025-03-26T13:21:15.476Z (11 months ago)
- Language: JavaScript
- Homepage:
- Size: 216 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Use class methods to add lifecycle extension points to your hapi server.
Installation ·
Usage ·
Contribute
Follow @marcuspoehls for updates!
------
The Future Studio University supports development of this hapi plugin 🚀
Join the Future Studio University and Skyrocket in Node.js
------
## Introduction
This hapi plugin adds the `server.extClass(class)` decoration to register lifecycle extensions from class methods.
## Requirements
> **hapi v19 (or later)** and **Node.js v12 (or newer)**
This plugin requires **hapi v19** (or later) and **Node.js v12 or newer**.
### Compatibility
| Major Release | [hapi.js](https://github.com/hapijs/hapi) version | Node.js version |
| --- | --- | --- |
| `v2` | `>=17 hapi` | `>=12` |
| `v1` | `>=17 hapi` | `>=8` |
## Installation
Add `hapi-class-extension-points` as a dependency to your project:
```bash
npm i hapi-class-extension-points
```
## Usage
Register `hapi-class-extension-points` to your hapi server. This will decorate the hapi `server` with a `server.extClass()` method:
```js
await server.register({
plugin: require('hapi-class-extension-points')
})
// went smooth like chocolate :)
// now your hapi server supports 'server.extClass(class MyMiddleware {})'
```
Having the plugin registered, you can now write your lifecycle extension points as classes:
```js
class RateLimiting {
constructor (server) {
this.server = server
}
async onRequest (request, h) {
// rate limit the request
await this.handle(request)
return h.continue
}
async handle (request) {
// this is a private method that won't be registered as a lifecycle extension
}
onPreResponse (request, h) {
// add rate limiting headers
return h.continue
}
}
server.extClass(RateLimiting)
```
That's it! The constructor of your class receives the hapi server. You can then store it as a class property and use it when needed!
Enjoy!
## Links & Resources
- [hapi tutorial series](https://futurestud.io/tutorials/hapi-get-your-server-up-and-running) with 100+ tutorials
## Contributing
1. Create a fork
2. Create your feature branch: `git checkout -b my-feature`
3. Commit your changes: `git commit -am 'Add some feature'`
4. Push to the branch: `git push origin my-new-feature`
5. Submit a pull request 🚀
## License
MIT © [Future Studio](https://futurestud.io)
---
> [futurestud.io](https://futurestud.io) ·
> GitHub [@futurestudio](https://github.com/futurestudio/) ·
> Twitter [@futurestud_io](https://twitter.com/futurestud_io)