@flexshopper/hapi-pres

3.1.0 • Public • Published

@flexshopper/hapi-pres

Build Status Dependencies Dev dependencies

Plugin to autoload pres based on patterns.

How to use:

  • Install hapi-pres npm package in your project our plugin. npm i @flexshopper/hapi-pres
  • Register plugin in your hapi server:

Registering

Kraken service style:

const server = new Hapi.Server();

server.connection();

server.register({
    register: require('hapi-routes'),
    options: {
        relativeTo: proccess.cwd() + '/pres',
        includes: ['path/to/**/*pres.js'],
        ignore: ['*.git'],
    }
}, (err) => {
  // continue application
});

Manifest style:

registrations: [
    ...
    {
        plugin: {
            register: 'hapi-pres',
            options: {
                relativeTo: proccess.cwd() + '/pres',
                includes: ['path/to/**/*pres.js'],
                ignore: ['*.git'],
            }
        }
    }
];

Options

includes

Required
Type: array

The glob pattern you would like to include

ignore

Type: array

The pattern or an array of patterns to exclude

relativeTo

Type: string

The current working directory in which to search (defaults to process.cwd())

Pre-requirement Signature(Version 2.x.x and before)

const internals = module.exports = {};

internals.assign = 'myAwesomePre';
internals.method = (request, reply) => {
    return reply(request.query);
};

// then in the routes
server.route({
    method: 'GET',
    path: '/ping',
    config: {
        pre: [
             server.pre.myAwesomePre
        ],
        handler: (request, reply) => {
             return reply(request.pre.example);
        }
    }   
});

Pre-requirement Signature(Version 3.x.x and after)

// in you pre-handler file
const internals = module.exports = {};

internals.myAwesomePre = (request, reply) => {

    return reply('myAwesomePre');
}

internals.mySecondAwesomePre = (request, reply) => {
    
    return reply('mySecondAwesomePre');
}

// assume your pre-handler file name is my-Pre.js
// then in the routes
server.route({
    method: 'GET',
    path: '/ping',
    config: {
        pre: [
            server.pre.myPre.myAwesomePre,
            server.pre.myPre.mySecondAwesomePre
        ],
        handler: (request, reply) => {
            return reply('handler');
        }
    }
});

// if you need to get result from pre handler
request.pre.myAwesomePre
request.pre.mySecondAwesomePre

Cautions

  • Please do not name two different pre-handlers with same name, even in different files. Otherwise they won't be accessed correctly.

Readme

Keywords

Package Sidebar

Install

npm i @flexshopper/hapi-pres

Weekly Downloads

13

Version

3.1.0

License

ISC

Last publish

Collaborators

  • mohanadsheikha
  • daniel.garavito
  • mcarballosa
  • fs-lstanley
  • danishkazmi
  • travis-fs