Skip to content

Instantly share code, notes, and snippets.

@brunoguerra
Created October 30, 2015 13:35
Show Gist options
  • Save brunoguerra/8b9e2533114bd60edaa1 to your computer and use it in GitHub Desktop.
Save brunoguerra/8b9e2533114bd60edaa1 to your computer and use it in GitHub Desktop.
simulate koa controller
// /modules/console/utils
import co from 'co';
import { returnAsPromise } from '../../lib/commons';
export default {
koaReq: (params, next) => {
console.log('koaReq: starting');
function* chain(next) {
this.params = params;
this.status = 200;
this.body = {nothing: true};
console.log('koaReq.chain: yielding');
try {
yield next.call(this, returnAsPromise(true));
} catch(e) {
console.log('koaReq.chain.error');
console.log(e);
console.log(e.stack);
}
console.log('koaReq.chain: after yield');
console.log(this.status);
return this.body;
}
console.log('koaReq: calling');
return co(chain.call({}, next));
}
}
// run on console
var k = require('./modules/console/utils').koaReq;
var c = require('./api/freight/freight.controller');
k({cep: "02209001" }, c.getCep).then(c => console.dir(c)).catch(e=> console.error(e));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment