JSPM

hapi-negotiator

2.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 2
  • Score
    100M100P100Q40408F
  • License MIT

Provides support for content negotiation in Hapi

Package Exports

  • hapi-negotiator

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (hapi-negotiator) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Hapi Content Negotiation

Build Status Dependency Status

Hapi Content Negotiation Plugin

This project enhances content negotiation capabilities of Hapi. It's a wrapper around Negotiator module.

Purpose

When you have the same route but wants it to respond json or a html view depending on the Accept header.

Options

The plugin supports the following options:

  • mediaTypes - (optional) a object containing a combination of global media types where the key is the media type name and the value is one of the following:
    • function (request, reply) - the function is called receiving the request and the reply
    • true - perform the default reply(). Effective no-op.
    • false - throws Boom.notAccepted() error
    • { method, args } - invoke the method on the reply method passing an args array. The args array is appended the default reply object.

Example

You have to configure the plugin for each route, so if you want a route to respond with a view named index when accept is text/html, notAccepted when image/jpeg and the regular json reply when application/vnd.project+json you should configure it as

server.route({
    method: 'GET',
    path: '/',
    config: {
        plugins: {
            'hapi-negotiator': {
                mediaTypes: {
                    'text/html': { method: 'view', args: ['index'] },
                    'image/jpeg': false,
                    'application/vnd.project+json': true,
                }
            }
        }
    }
});

Todo

  • Improve documentation
  • Include negotiation for Accept Language. Maybe combine with one of the localization modules?

Comments/Suggestions

Feel free to open an issue.