koajs / joi-router

Configurable, input and output validated routing for koa
MIT License
450 stars 96 forks source link

Is there plan to support async/await for handlers in this library? #5

Closed martinmicunda closed 7 years ago

aheckmann commented 8 years ago

Should work?? because it's designed for koa, which supports async/await via options/flags but I haven't tried it. On Oct 6, 2015 9:29 AM, "Martin Micunda" notifications@github.com wrote:

— Reply to this email directly or view it on GitHub https://github.com/pebble/koa-joi-router/issues/5.

martinmicunda commented 8 years ago

I have follow code:

import koa from 'koa';
import router from 'koa-joi-router';
import validator from './lib/validator';
import * as service from './lib/service';

const app = koa();
const api = router();

const routes = [
    {
        method: 'GET',
        path: '/partners/:id',
        validate: validator.findByID,
        handler: async() {
            const respond = await service.findByID(this.params.id);

            this.body = respond.data;
            this.status = respond.statusCode;
        }
    }
];

for(let route of routes) {
    api.route(route);
}
app.use(api.middleware());

export default app;

but when I am using async/await in handler then I am getting follow error:

  [ 'AssertionError: route handler must be a GeneratorFunction',
     '    at isGeneratorFunction (/home/vagrant/api/node_modules/koa-joi-router/joi-router.js:149:3)',
     '    at Array.forEach (native)',
     '    at checkHandler (/home/vagrant/api/node_modules/koa-joi-router/joi-router.js:141:23)',
     '    at Router.validateRouteSpec [as _validateRouteSpec] (/home/vagrant/api/node_modules/koa-joi-router/joi-router.js:127:3)',
     '    at Router.route (/home/vagrant/api/node_modules/koa-joi-router/joi-router.js:89:8)',
aheckmann commented 8 years ago

Ah, open a pull request with the fix + a test and we'll get that in for ya. On Oct 6, 2015 9:40 AM, "Martin Micunda" notifications@github.com wrote:

I have follow code:

import koa from 'koa';import router from 'koa-joi-router';import validator from './lib/validator';import * as service from './lib/service'; const app = koa();const api = router(); const routes = [ { method: 'GET', path: '/partners/:id', validate: validator.findByID, handler: async() { const respond = await service.findByID(this.params.id);

        this.body = respond.data;
        this.status = respond.statusCode;
    }
}

]; for(let route of routes) { api.route(route); } app.use(api.middleware()); export default app;

but when I am using async/await in handler then I am getting follow error:

[ 'AssertionError: route handler must be a GeneratorFunction', ' at isGeneratorFunction (/home/vagrant/api/node_modules/koa-joi-router/joi-router.js:149:3)', ' at Array.forEach (native)', ' at checkHandler (/home/vagrant/api/node_modules/koa-joi-router/joi-router.js:141:23)', ' at Router.validateRouteSpec as _validateRouteSpec', ' at Router.route (/home/vagrant/api/node_modules/koa-joi-router/joi-router.js:89:8)',

— Reply to this email directly or view it on GitHub https://github.com/pebble/koa-joi-router/issues/5#issuecomment-145922576 .

amcdnl commented 8 years ago

How about KOA2?

osenvosem commented 7 years ago

It seems the module do not support Koa2. Any plans to add it?

aheckmann commented 7 years ago

I've got a branch going and plan to finish it up before Node 7 is released this month.

aheckmann commented 7 years ago

5.0.0 is out with async/await support