Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hapi-helpers

Package Overview
Dependencies
Maintainers
1
Versions
9
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hapi-helpers

Some helper functions for hapi.js

  • 0.1.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1
decreased by-88.89%
Maintainers
1
Weekly downloads
 
Created
Source

hapi.js helpers

Build Status Test Coverage

https://github.com/rsp/node-hapi-helpers

Some helper functions for hapi.js to write less boilerplate code. Currently simplify routes definitions.

Example

You can write:

server.addRoutes([
    get('/a/{id}', getA ),
    del('/a/{id}', delA ),
    route('get put post delete', '/b', handleB )
]);

Instead of:

server.addRoutes([
    {
        method: 'GET',
        path: '/a/{id}',
        handler: getA
    },
    {
        method: 'DELETE',
        path: '/a/{id}',
        handler: delA
    },
    {
        method: ['GET', 'PUT', 'POST', 'DELETE'],
        path: '/b',
        handler: handleB
    }
]);

Installation

npm install hapi-helpers --save

Usage

To use hh.get(), hh.post() etc:

var hh = require('hapi-helpers');

To use just get(), post() etc:

var hh = require('hapi-helpers'),
    get = hh.get, post = hh.post; // ...

Routes

To simplify the definition of routes in hapi.js you can use:

hh.route(method, path, handler, config)

where config is optional and method can be a string or a list of strings e.g.:

  • 'GET'
  • 'get'
  • ['get', 'post']
  • 'get post'

For example: hh.route('get post', path, handler, config)

It is a shortcut for:

{
    method: ['GET', 'POST'],
    path: path,
    handler: handler,
    config: config
}

hh.get(path, handler, config)

(config is optional) is a shortcut for:

{
    method: 'GET',
    path: path,
    handler: handler,
    config: config
}

hh.post(path, handler, config)

(config is optional) is a shortcut for:

{
    method: 'POST',
    path: path,
    handler: handler,
    config: config
}

hh.put(path, handler, config)

(config is optional) is a shortcut for:

{
    method: 'PUT',
    path: path,
    handler: handler,
    config: config
}

hh.patch(path, handler, config)

(config is optional) is a shortcut for:

{
    method: 'PATCH',
    path: path,
    handler: handler,
    config: config
}

hh.del(path, handler, config)

(config is optional) is a shortcut for:

{
    method: 'DELETE',
    path: path,
    handler: handler,
    config: config
}

Note: it is .del() and not .delete() because delete is a reserved word in JavaScript.

hh.options(path, handler, config)

(config is optional) is a shortcut for:

{
    method: 'OPTIONS',
    path: path,
    handler: handler,
    config: config
}

hh.all(path, handler, config)

(config is optional) is a shortcut for:

{
    method: '*',
    path: path,
    handler: handler,
    config: config
}

Real world example

Here is real world example, a route from hapi-example by Wyatt Preul:

var Types = require('hapi').types;

module.exports = [{
    method: 'GET',
    path: '/products',
    config: {
        handler: getProducts,
        validate: {
            query: {
                name: Types.String()
            }
        }
    }
}, {
    method: 'GET',
    path: '/products/{id}',
    config: {
        handler: getProduct
    }
}, {
    method: 'POST',
    path: '/products',
    config: {
        handler: addProduct,
        payload: 'parse',
        validate: {
            payload: {
                name: Types.String().required().min(3)
            }
        }
    }
}];

Here is the same using hapi-helpers, available on hapi-helpers-example:

var Types = require('hapi').types,
    hh = require('hapi-helpers'),
    get = hh.get,
    post = hh.post;

module.exports = [
    get('/products', getProducts, {
        validate: {
            query: {
                name: Types.String()
            }
        }
    }),
    get('/products/{id}', getProduct),
    post('/products', addProduct, {
        payload: 'parse',
        validate: {
            payload: {
                name: Types.String().required().min(3)
            }
        }
    })
];

The biggest difference is for simple routes like '/products/{id}' which is one simple line with hapi-helpers.

This is work in progress - more to come.

Issues

For any bug reports or feature requests please post an issue on GitHub.

Author

Rafał Pocztarski - https://github.com/rsp

License

MIT License (Expat). See LICENSE.md for details.

Keywords

FAQs

Package last updated on 11 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc