npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hapi-helpers

v0.1.8

Published

Define hapi.js routes with less code

Downloads

4

Readme

hapi.js helpers

npm install hapi-helpers Build Status Test Coverage Code Climate Known Vulnerabilities Downloads

https://github.com/rsp/node-hapi-helpers (readme)

Some helper functions for hapi.js to write less boilerplate code.

Currently it only simplifies the routes definitions. Feature requests welcome via issues.

It has no dependencies - only some devDependencies (it uses chai, mocha, istanbul and coveralls for testing and test coverage, and Travis for continuous integration, see: https://travis-ci.org/rsp/node-hapi-helpers)

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

Install to use in your project, updating the dependencies in package.json:

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; // ...

Or using the ES6 destructuring assignment:

var {get, post} = require('hapi-helpers');

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'

You can use either 'del' or 'delete' for the DELETE method.

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.