2.1.0 • Published 5 years ago

hapi-class-extension-points v2.1.0

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago


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 Releasehapi.js versionNode.js version
v2>=17 hapi>=12
v1>=17 hapi>=8

Installation

Add hapi-class-extension-points as a dependency to your project:

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:

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:

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

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


futurestud.io  ·  GitHub @futurestudio  ·  Twitter @futurestud_io

2.1.0

5 years ago

2.0.0

5 years ago

1.3.0

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.0

6 years ago