koajs / convert Goto Github PK
View Code? Open in Web Editor NEWConvert koa generator-based middleware to promise-based middleware
License: MIT License
Convert koa generator-based middleware to promise-based middleware
License: MIT License
i've invited you to be part of the @koajs/koa-v2 team in @koajs. i'd like you to transfer your repo over. i'm not sure how transferring works when you're not an owner of the organization, though. let me know if you run into any issues.
I'm planning on upgrading to koa 2.x, however the notes seems to indicate that koa-router needs to be update first. I'm wondering if there is something special about koa-mount as well?
koa.use(convert(hbs.middleware({
extname: '.html',
viewPath: ['./www/admin', './www/home'],
layoutsPath: './www',
partialsPath: ['./www/admin', './www/home'],
disableCache: true
})));
koa.use(async(ctx, next) => {
const render = ctx.render;
ctx.render = async function _convertedRender() {
return co.call(ctx, render.apply(ctx, arguments))
}
await next();
});
await ctx.render('index_index');
###Question###
If I open the cache, view rendering not to come out, when I closed the cache, the view can render.
###I recorded a GIF###
http://www.wemallshop.com/hbs.gif
Actually return data, but this. The body has no output to the browser
First off, I do not know if this belongs here in this module, but I'll start here. I'm experimenting with the following code:
var convert = require('koa-convert');
var genbind = require('generator-bind');
function* myGen(i) {
console.log(this.x, i);
}
myGen = genbind({x:666}, myGen);
convert(myGen)(9); // <--- this outputs 666 undefined, was expecting 666 9
I can't seem to pass an argument to the generator function. If this isn't to do with koa-convert
then I'm guessing generator-bind
is at fault.
Hi,
I have a webapp that I built using the koa v2 framework. I use koa-convert, koa-generic-session and koa-redis to store user sessions in my redis instance. Since upgrading to node v6.5, my webapp fails with the following stacktrace:
TypeError: Cannot read property 'done' of undefined
at next (/Users/dev/work/node_modules/co/index.js:98:14)
at onFulfilled (/Users/dev/work/node_modules/co/index.js:69:7)
at /Users/Stephen/dev/work/node_modules/co/index.js:54:5
at Object.co (/Users/dev/work/node_modules/co/index.js:50:10)
at next (/Users/dev/work/node_modules/co/index.js:99:29)
at onFulfilled (/Users/dev/work/node_modules/co/index.js:69:7)
It worked fine with node v6.4. The stacktrace doesnt go past co, but through trial and error, I deleted a lot of third party libs and middleware and narrowed it down to this block of code:
app.use(convert(session({
key: 'session',
errorHandler(err, type, ctx) {
console.error(err, type)
},
store: redisStore({ url: config.redis.url })
})))
which can be reduced to app.use(convert(session()))
and still trigger the same bug.
I do not use the convert lib anywhere else in the app so I cannot quickly test to see if the issue is specifically in the convert or session lib.
I'd like to be able to call converted middleware without a next
param, similar to what happens in compose (https://github.com/koajs/compose/blob/master/index.js#L20). For example, using koa 2.x params middleware:
async function noop () {}
export async function organizationIdParam (id, ctx, next) {
await convert(rbac.allow('read:organization', { organization: id }))(ctx, noop);
ctx.organization = await Organization.findById(id);
await next();
}
It would be nice to instead be able to call it like this:
export async function organizationIdParam (id, ctx, next) {
await convert(rbac.allow('read:organization', { organization: id }))(ctx);
ctx.organization = await Organization.findById(id);
await next();
}
What do you think?
Currently I can't figure out an elegant way for passing arguments into v1.x middleware.
Is this something that could be useful to anyone?
I'm using the koa-ratelimiter library which currently doesn't have v2.x support. However I'd like to also pass in arguments.
The old version looked like this
app.use(ratelimit({
max: opts.ratelimit,
duration: opts.duration,
db: redis.createClient()
}));
What I'd like to do is pass in the options as a second parameter to convert
.
app.use(convert(ratelimit, {
max: opts.ratelimit,
duration: opts.duration,
db: redis.createClient()
}));
I am working on a Koa v1 app and would like to implement new routes with async/await instead of generator functions. I found this library has convert.back()
which is mostly working for me. However, the following example code will illustrate my problem:
router.get('/endpoint',
async function(next) {
const token = this.request.headers.token;
if (!token) {
this.response.status = 401;
return;
}
// I assume next is a Promise so this is not an issue?
await next;
},
async function(next) {
// This code still executes, even if there is no token defined on the headers.
// Do something important that shouldn't execute with invalid auth...
});
return convert.back(router.routes());
If there is no token defined, the endpoint does return a 401 and exits the first function, but the second function is still executed. If I set up the first function as a generator function and use yield next
, it works as would be expected.
Versions:
node: 6.13.1
npm: 3.10.10
├─┬ [email protected]
│ ├── [email protected]
│ ├── [email protected]
├─┬ [email protected]
│ └── [email protected]
├── [email protected]
├─┬ [email protected]
├─┬ [email protected]
│ ├── [email protected]
├─┬ [email protected]
│ └─┬ [email protected]
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.