Git Product home page Git Product logo

hapi-response-utilities's Introduction

hapi-response-utilities logo

hapi response decorations to conveniently compose responses


InstallationUsageMethods



Build Status Known Vulnerabilities hapi-response-utilities Version Monthly downloads

Follow @marcuspoehls for updates!


Development of this hapi plugin is supported by Future Studio University 馃殌
Join the Future Studio University and Skyrocket in Node.js


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 Release hapi.js version Node.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鈥檛 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

hapi-response-utilities's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar greenkeeper[bot] avatar marcuspoehls avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mastepanoski

hapi-response-utilities's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    馃枛 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 馃搳馃搱馃帀

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google 鉂わ笍 Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.