2.0.0 • Published 4 years ago

hapi-response-utilities v2.0.0

Weekly downloads
17
License
MIT
Repository
github
Last release
4 years ago

Build Status Greenkeeper badge



Introduction

A hapi plugin that decorates the response toolkit h with methods to conveniently compose responses.

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-response-utilities as a dependency to your project:

npm i hapi-response-utilities

Usage

Register hapi-response-utilities to your hapi server and that's it :)

await server.register({
  plugin: require('hapi-response-utilities')
})

// went smooth like hot chocolate :)

Methods

An overview of available hapi toolkit decorations.

h.pdf(pdf-content, filename)

Creates a PDF response including the PDF related content type and HTTP headers allowing a custom filename.

The filename defaults to download.

handler: async (_, h) => {
  const content = await createPdfContent()
  return h.pdf(content, 'filename.pdf')
}

h.status(code)

Shortcut to respond with just an HTTP status code.

handler: (_, h) => {
  return h.status(204)
}

h.cookie(key, value, options)

Sets a cookie for the given key-value-pair. It's a convenience method for h.state.

handler: (_, h) => {
  return
    h.cookie('userId', '1')
     .cookie('username', 'Marcus')
     .continue
}

h.header(key, value, options)

Set a response header for the given key-value-pair. This method provides a unified interface to set response headers, no matter if the response is a hapi response object or a boom error instance:

handler: (_, h) => {
  return h
    .header('content-type', 'text/html')
    .header('api-key', 'secret-api-key')
    .continue
}

The accepted options are the same as provided by hapi itself.

h.headers()

Returns an object containing the response headers. This method provides a unified interface to set response headers, no matter if the response is a hapi response object or a boom error instance:

handler: (_, h) => {
  const responseHeaders = h.headers()

  return h.continue
}

h.isView()

Determines whether the response is a rendered view:

handler: (_, h) => {
  if (h.isView()) {
    // handle view response
  }

  return h.continue
}

Feature Requests

Do you miss a feature? Please don’t hesitate to create an issue with a short description of your desired addition to this plugin.

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.0.0

4 years ago

1.6.0

4 years ago

1.5.0

5 years ago

1.4.1

5 years ago

1.4.0

5 years ago

1.3.4

5 years ago

1.3.3

5 years ago

1.3.2

5 years ago

1.3.1

5 years ago

1.3.0

5 years ago

1.2.0

5 years ago

1.1.0

5 years ago

1.0.1

6 years ago

1.0.0

6 years ago