Immutable request properties for hapi
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
CHANGELOG.md
CONTRIBUTING.md
LICENSE
README.md
package-lock.json
package.json

README.md

recourier

Immutable request properties for hapi.

NPM Version Build Status Coverage Status Dependencies Dev Dependencies

Table of Contents

Installation

Install via NPM.

$ npm install recourier

Usage

Register the package as a server plugin, providing an optional namespace where the properties will be available during the request lifecycle (in case you want to access them during that period) and the list of the hapi request properties you effectively want to make immutable.

The initial values of those properties (i.e. when they are parsed by the onPostAuth extension point) will be saved in that immutable application namespace, which will then be again available in the request handler itself.

Example 1

Avoid plugin registration ordering issues by registering Recourier in the last place.

const Hapi = require('hapi');
const Recourier = require('recourier');
const MyPlugin = require('my-plugin');

const plugins = [{
    plugin: MyPlugin
}, {
    // any additional plugins
}, {
    plugin: Recourier,
    options: {
        namespace: 'foo', // defaults to 'recourier'
        properties: ['payload'] // immutable request properties
    }
}];

try {
    const server = new Hapi.Server();

    await server.register(plugins);
    await server.start();
}
catch (err) {
    throw err;
}

Example 2

Avoid plugin registration ordering issues by using server.dependency().

const Hapi = require('hapi');
const Recourier = require('recourier');

const MyPlugin = {
    name: 'my-plugin',
    register: (server, options) => {

        server.dependency(Recourier.pkg.name, (app) => {
            // go nuts
        });
    }
};

const plugins = [{
    plugin: Recourier,
    options: {
        namespace: 'foo', // defaults to 'recourier'
        properties: ['payload'] // immutable request properties
    }
}, {
    plugin: MyPlugin
}, {
    // any additional plugins
}];

try {
    const server = new Hapi.Server();

    await server.register(plugins);
    await server.start();
}
catch (err) {
    throw err;
}