loveboat-postreqs

2.0.0 • Public • Published

loveboat-postreqs

support hapi route post-requisites

(a transform written for loveboat)

Build Status Coverage Status

Usage

This loveboat transform allows you to add route post-requisites in your hapi route configuration, whereas hapi on its own only supports pre-requisites.

Post-requisites run right after the handler, so they are especially useful for cleanup and for modifying the response.

In the example below, a post-requisite is used to upper-case the response.

// Ever wish this worked?
server.loveboat({
    method: 'get',
    path: '/',
    config: {
        handler: function (request, reply) {
            return reply('loveboat');
        },
        post: [ // Now it does!
            function (request, reply) {
                // Upper-case the response and carry on
                request.response.source = request.response.source.toUpperCase();
                return reply.continue();
            }
        ]
    }
});

To use this transform,

  1. Make sure the loveboat hapi plugin is registered to your server.
  2. Tell loveboat that you'd like to use this transform by calling server.routeTransforms([require('loveboat-postreqs')]), possibly listing any other transforms you'd like to use.*
  3. Register your routes using server.loveboat() rather than server.route().

* There are other ways to tell loveboat which transforms to use too! Just check-out the readme.

const Hapi = require('hapi');
const Loveboat = require('loveboat');
 
const server = new Hapi.Server();
server.connection();
 
// 1. Register loveboat
server.register(Loveboat, (err) => {
 
    // 2. Tell loveboat to use this transform
    server.routeTransforms([
        require('loveboat-postreqs')
    ]);
 
    // 3. Configure your routes!
    server.loveboat({
        method: 'get',
        path: '/',
        config: {
            handler: function (request, reply) {
                return reply('loveboat');
            },
            post: [ // This works!
                function (request, reply) {
                    // Upper-case the response and carry on
                    request.response.source = request.response.source.toUpperCase();
                    return reply.continue();
                }
            ]
        }
    });
 
});

Limitations

The post-requisites are implemented as route-level onPostHandler request extensions. In order to place them as near to the handler as possible, they are specified to run before the request extensions of all plugins that have already been registered and before all other route-level request extensions (unless the tight option is false). This leaves two blind spots– request onPostHandler extensions that are created,

  1. in the same plugin that your route is registered and also before your route is registered.
  2. in a to-be-registered plugin and also opt to come before the extensions of the plugin in which your route is registered.

Basically, the post-requisites are run as near to the handler as possible using request extensions.

API

Options

The following options may be specified when the transform is registered,

  • tight - when false, the post-requisites wont be forced to run before onPostHandler extensions specified in other plugins. Defaults to true.

Route Definition

  • config.post - an item or array of items of the format,

    • a function with signature function(request, reply),
    • an object with,
      • method - a function with the signature described above.

    Each item is a post-requisite. Post-requisites run after the route's handler, serially and in the order specified.

Package Sidebar

Install

npm i loveboat-postreqs

Weekly Downloads

4

Version

2.0.0

License

MIT

Last publish

Collaborators

  • devinivy