hapi-generoutify
Wraps route's handlers into generator
Last updated 7 months ago by saaaaaaaaasha .
MIT · Repository · Bugs · Original npm · Tarball
$ cnpm install hapi-generoutify 
SYNC missed versions from official npm registry.

hapi-generoutify

Wraps route's handlers into generator

Travis Build Status Downloads per month Version License

Installation

npm install hapi-generoutify --save

Usage

You can use hapi-generoutify for generator router's handlers (and the yield keyword), and co today.

Registering the Plugin

const Hapi = require('hapi');
const hapiGeneroutify = require('hapi-generoutify');

const server = new Hapi.Server();
server.register([hapiGeneroutify], (error) => { ... });

Now all route's handler will be wrap to co.wrap.

function* getUserAction(request, reply) {
  const user = yield database.User.findOne({ email: request.payload.email });

  if (!user) {
    yield Promise.reject(Boom.notFound('USER_NOT_FOUND'));
  }
    
  reply(user.toObject());
}

server.route({
  method: 'GET',
  path: '/',
  handler: getUserAction
});

Also you can pass just simple function to handler and it will work as before.

By default your generator are catched by global errorHandler, which reply error. You can define custom handler as hapi server method like:


server.methods('errorHandler', (reply, err) => {
  if (err instanceof AwesomeHttpError) {
    reply(...).code(404);
  } else {
    // ...
  }
});

See full examples in examples folder.

Maintainers (1)
Downloads
Today 0
This Week 0
This Month 1
Last Day 0
Last Week 1
Last Month 0
Dependencies (2)
Dev Dependencies (2)
Dependents (0)
None

Copyright 2014 - 2017 © taobao.org |